summaryrefslogtreecommitdiffstats
path: root/net/base/file_stream_posix.cc
blob: 535891663b5dced22094b1bbedf83c09d914004c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright (c) 2008 The Chromium Authors. All rights reserved.  Use of this
// source code is governed by a BSD-style license that can be found in the
// LICENSE file.

// For 64-bit file access (off_t = off64_t, lseek64, etc).
#define _FILE_OFFSET_BITS 64

#include "net/base/file_stream.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#include "base/basictypes.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "base/string_util.h"
#include "net/base/net_errors.h"

// We cast back and forth, so make sure it's the size we're expecting.
COMPILE_ASSERT(sizeof(int64) == sizeof(off_t), off_t_64_bit);

// Make sure our Whence mappings match the system headers.
COMPILE_ASSERT(net::FROM_BEGIN   == SEEK_SET &&
               net::FROM_CURRENT == SEEK_CUR &&
               net::FROM_END     == SEEK_END, whence_matches_system);

namespace net {

// FileStream::AsyncContext ----------------------------------------------

// TODO(deanm): Figure out how to best do async IO.
class FileStream::AsyncContext {
 public:

  CompletionCallback* callback() const { return NULL; }
 private:

  DISALLOW_COPY_AND_ASSIGN(AsyncContext);
};

// FileStream ------------------------------------------------------------

FileStream::FileStream() : file_(base::kInvalidPlatformFileValue) {
  DCHECK(!IsOpen());
}

FileStream::~FileStream() {
  Close();
}

void FileStream::Close() {
  if (file_ != base::kInvalidPlatformFileValue) {
    if (close(file_) != 0) {
      NOTREACHED();
    }
    file_ = base::kInvalidPlatformFileValue;
  }
  async_context_.reset();
}

// Map from errno to net error codes.
static int64 MapErrorCode(int err) {
  switch(err) {
    case ENOENT:
      return ERR_FILE_NOT_FOUND;
    case EACCES:
      return ERR_ACCESS_DENIED;
    default:
      LOG(WARNING) << "Unknown error " << err << " mapped to net::ERR_FAILED";
      return ERR_FAILED;
  }
}

int FileStream::Open(const std::wstring& path, int open_flags) {
  if (IsOpen()) {
    DLOG(FATAL) << "File is already open!";
    return ERR_UNEXPECTED;
  }

  open_flags_ = open_flags;
  file_ = base::CreatePlatformFile(path, open_flags_, NULL);
  if (file_ == base::kInvalidPlatformFileValue) {
    LOG(WARNING) << "Failed to open file: " << errno;
    return MapErrorCode(errno);
  }

  return OK;
}

bool FileStream::IsOpen() const {
  return file_ != base::kInvalidPlatformFileValue;
}

int64 FileStream::Seek(Whence whence, int64 offset) {
  if (!IsOpen())
    return ERR_UNEXPECTED;

  // If we're in async, make sure we don't have a request in flight.
  DCHECK(!async_context_.get() || !async_context_->callback());
  
  off_t res = lseek(file_, static_cast<off_t>(offset),
                    static_cast<int>(whence));
  if (res == static_cast<off_t>(-1))
    return MapErrorCode(errno);

  return res;
}

int64 FileStream::Available() {
  if (!IsOpen())
    return ERR_UNEXPECTED;

  int64 cur_pos = Seek(FROM_CURRENT, 0);
  if (cur_pos < 0)
    return cur_pos;

  struct stat info;
  if (fstat(file_, &info) != 0)
    return MapErrorCode(errno);

  int64 size = static_cast<int64>(info.st_size);
  DCHECK(size >= cur_pos);

  return size - cur_pos;
}

// TODO(deanm): async.
int FileStream::Read(
    char* buf, int buf_len, CompletionCallback* callback) {
  // read(..., 0) will return 0, which indicates end-of-file.
  DCHECK(buf_len > 0 && buf_len <= SSIZE_MAX);

  if (!IsOpen())
    return ERR_UNEXPECTED;

  // Loop in the case of getting interrupted by a signal.
  for (;;) {
    ssize_t res = read(file_, buf, static_cast<size_t>(buf_len));
    if (res == static_cast<ssize_t>(-1)) {
      if (errno == EINTR)
        continue;
      return MapErrorCode(errno);
    }
    return static_cast<int>(res);
  }
}

// TODO(deanm): async.
int FileStream::Write(
    const char* buf, int buf_len, CompletionCallback* callback) {

  // read(..., 0) will return 0, which indicates end-of-file.
  DCHECK(buf_len > 0 && buf_len <= SSIZE_MAX);

  if (!IsOpen())
    return ERR_UNEXPECTED;

  int total_bytes_written = 0;
  size_t len = static_cast<size_t>(buf_len);
  while (total_bytes_written < buf_len) {
    ssize_t res = write(file_, buf, len);
    if (res == static_cast<ssize_t>(-1)) {
      if (errno == EINTR)
        continue;
      return MapErrorCode(errno);
    }
    total_bytes_written += res;
    buf += res;
    len -= res;
  }
  return total_bytes_written;
}

}  // namespace net