OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "services/file_manager/file_impl.h" |
| 6 |
| 7 #include <errno.h> |
| 8 #include <sys/types.h> |
| 9 #include <unistd.h> |
| 10 |
| 11 #include <algorithm> |
| 12 #include <limits> |
| 13 |
| 14 #include "base/files/scoped_file.h" |
| 15 #include "base/logging.h" |
| 16 #include "base/posix/eintr_wrapper.h" |
| 17 #include "services/file_manager/util.h" |
| 18 |
| 19 static_assert(sizeof(off_t) <= sizeof(int64_t), "off_t too big"); |
| 20 static_assert(sizeof(size_t) >= sizeof(uint32_t), "size_t too small"); |
| 21 |
| 22 namespace mojo { |
| 23 namespace files { |
| 24 |
| 25 const size_t kMaxReadSize = 4 * 1024 * 1024; |
| 26 |
| 27 FileImpl::FileImpl(InterfaceRequest<File> request, base::ScopedFD file_fd) |
| 28 : binding_(this, request.Pass()), file_fd_(file_fd.Pass()) { |
| 29 DCHECK(file_fd_.is_valid()); |
| 30 } |
| 31 |
| 32 FileImpl::~FileImpl() { |
| 33 } |
| 34 |
| 35 void FileImpl::Close(const Callback<void(Error)>& callback) { |
| 36 if (!file_fd_.is_valid()) { |
| 37 callback.Run(ERROR_CLOSED); |
| 38 return; |
| 39 } |
| 40 file_fd_.reset(); |
| 41 callback.Run(ERROR_OK); |
| 42 } |
| 43 |
| 44 // TODO(vtl): Move the implementation to a thread pool. |
| 45 void FileImpl::Read(uint32_t num_bytes_to_read, |
| 46 int64_t offset, |
| 47 Whence whence, |
| 48 const Callback<void(Error, Array<uint8_t>)>& callback) { |
| 49 if (!file_fd_.is_valid()) { |
| 50 callback.Run(ERROR_CLOSED, Array<uint8_t>()); |
| 51 return; |
| 52 } |
| 53 if (num_bytes_to_read > kMaxReadSize) { |
| 54 callback.Run(ERROR_OUT_OF_RANGE, Array<uint8_t>()); |
| 55 return; |
| 56 } |
| 57 if (Error error = IsOffsetValid(offset)) { |
| 58 callback.Run(error, Array<uint8_t>()); |
| 59 return; |
| 60 } |
| 61 if (Error error = IsWhenceValid(whence)) { |
| 62 callback.Run(error, Array<uint8_t>()); |
| 63 return; |
| 64 } |
| 65 |
| 66 if (offset != 0 || whence != WHENCE_FROM_CURRENT) { |
| 67 // TODO(vtl): Use |pread()| below in the |WHENCE_FROM_START| case. This |
| 68 // implementation is obviously not atomic. (If someone seeks simultaneously, |
| 69 // we'll end up writing somewhere else. Or, well, we would if we were |
| 70 // multithreaded.) Maybe we should do an |ftell()| and always use |pread()|. |
| 71 if (lseek(file_fd_.get(), static_cast<off_t>(offset), |
| 72 WhenceToStandardWhence(whence)) < 0) { |
| 73 callback.Run(ErrnoToError(errno), Array<uint8_t>()); |
| 74 return; |
| 75 } |
| 76 } |
| 77 |
| 78 Array<uint8_t> bytes_read(num_bytes_to_read); |
| 79 ssize_t num_bytes_read = HANDLE_EINTR( |
| 80 read(file_fd_.get(), &bytes_read.front(), num_bytes_to_read)); |
| 81 if (num_bytes_read < 0) { |
| 82 // TODO(vtl): Decode errno. |
| 83 callback.Run(ERROR_UNKNOWN, Array<uint8_t>()); |
| 84 return; |
| 85 } |
| 86 |
| 87 DCHECK_LE(static_cast<size_t>(num_bytes_read), num_bytes_to_read); |
| 88 bytes_read.resize(static_cast<size_t>(num_bytes_read)); |
| 89 callback.Run(ERROR_OK, bytes_read.Pass()); |
| 90 } |
| 91 |
| 92 // TODO(vtl): Move the implementation to a thread pool. |
| 93 void FileImpl::Write(Array<uint8_t> bytes_to_write, |
| 94 int64_t offset, |
| 95 Whence whence, |
| 96 const Callback<void(Error, uint32_t)>& callback) { |
| 97 DCHECK(!bytes_to_write.is_null()); |
| 98 |
| 99 if (!file_fd_.is_valid()) { |
| 100 callback.Run(ERROR_CLOSED, 0); |
| 101 return; |
| 102 } |
| 103 // Who knows what |write()| would return if the size is that big (and it |
| 104 // actually wrote that much). |
| 105 if (bytes_to_write.size() > |
| 106 static_cast<size_t>(std::numeric_limits<ssize_t>::max())) { |
| 107 callback.Run(ERROR_OUT_OF_RANGE, 0); |
| 108 return; |
| 109 } |
| 110 if (Error error = IsOffsetValid(offset)) { |
| 111 callback.Run(error, 0); |
| 112 return; |
| 113 } |
| 114 if (Error error = IsWhenceValid(whence)) { |
| 115 callback.Run(error, 0); |
| 116 return; |
| 117 } |
| 118 |
| 119 if (offset != 0 || whence != WHENCE_FROM_CURRENT) { |
| 120 // TODO(vtl): Use |pwrite()| below in the |WHENCE_FROM_START| case. This |
| 121 // implementation is obviously not atomic. (If someone seeks simultaneously, |
| 122 // we'll end up writing somewhere else. Or, well, we would if we were |
| 123 // multithreaded.) Maybe we should do an |ftell()| and always use |
| 124 // |pwrite()|. |
| 125 if (lseek(file_fd_.get(), static_cast<off_t>(offset), |
| 126 WhenceToStandardWhence(whence)) < 0) { |
| 127 callback.Run(ErrnoToError(errno), 0); |
| 128 return; |
| 129 } |
| 130 } |
| 131 |
| 132 const void* buf = |
| 133 (bytes_to_write.size() > 0) ? &bytes_to_write.front() : nullptr; |
| 134 ssize_t num_bytes_written = |
| 135 HANDLE_EINTR(write(file_fd_.get(), buf, bytes_to_write.size())); |
| 136 if (num_bytes_written < 0) { |
| 137 callback.Run(ErrnoToError(errno), 0); |
| 138 return; |
| 139 } |
| 140 |
| 141 DCHECK_LE(static_cast<size_t>(num_bytes_written), |
| 142 std::numeric_limits<uint32_t>::max()); |
| 143 callback.Run(ERROR_OK, static_cast<uint32_t>(num_bytes_written)); |
| 144 } |
| 145 |
| 146 void FileImpl::ReadStream(ScopedDataPipeProducerHandle source, |
| 147 int64_t offset, |
| 148 Whence whence, |
| 149 const Callback<void(Error)>& callback) { |
| 150 if (!file_fd_.is_valid()) { |
| 151 callback.Run(ERROR_CLOSED); |
| 152 return; |
| 153 } |
| 154 if (Error error = IsOffsetValid(offset)) { |
| 155 callback.Run(error); |
| 156 return; |
| 157 } |
| 158 if (Error error = IsWhenceValid(whence)) { |
| 159 callback.Run(error); |
| 160 return; |
| 161 } |
| 162 |
| 163 // TODO(vtl): FIXME soon |
| 164 NOTIMPLEMENTED(); |
| 165 callback.Run(ERROR_UNIMPLEMENTED); |
| 166 } |
| 167 |
| 168 void FileImpl::WriteStream(ScopedDataPipeConsumerHandle sink, |
| 169 int64_t offset, |
| 170 Whence whence, |
| 171 const Callback<void(Error)>& callback) { |
| 172 if (!file_fd_.is_valid()) { |
| 173 callback.Run(ERROR_CLOSED); |
| 174 return; |
| 175 } |
| 176 if (Error error = IsOffsetValid(offset)) { |
| 177 callback.Run(error); |
| 178 return; |
| 179 } |
| 180 if (Error error = IsWhenceValid(whence)) { |
| 181 callback.Run(error); |
| 182 return; |
| 183 } |
| 184 |
| 185 // TODO(vtl): FIXME soon |
| 186 NOTIMPLEMENTED(); |
| 187 callback.Run(ERROR_UNIMPLEMENTED); |
| 188 } |
| 189 |
| 190 void FileImpl::Tell(const Callback<void(Error, int64_t)>& callback) { |
| 191 Seek(0, WHENCE_FROM_CURRENT, callback); |
| 192 } |
| 193 |
| 194 void FileImpl::Seek(int64_t offset, |
| 195 Whence whence, |
| 196 const Callback<void(Error, int64_t)>& callback) { |
| 197 if (!file_fd_.is_valid()) { |
| 198 callback.Run(ERROR_CLOSED, 0); |
| 199 return; |
| 200 } |
| 201 if (Error error = IsOffsetValid(offset)) { |
| 202 callback.Run(error, 0); |
| 203 return; |
| 204 } |
| 205 if (Error error = IsWhenceValid(whence)) { |
| 206 callback.Run(error, 0); |
| 207 return; |
| 208 } |
| 209 |
| 210 off_t position = lseek(file_fd_.get(), static_cast<off_t>(offset), |
| 211 WhenceToStandardWhence(whence)); |
| 212 if (position < 0) { |
| 213 callback.Run(ErrnoToError(errno), 0); |
| 214 return; |
| 215 } |
| 216 |
| 217 callback.Run(ERROR_OK, static_cast<int64>(position)); |
| 218 } |
| 219 |
| 220 void FileImpl::Stat(const Callback<void(Error, FileInformationPtr)>& callback) { |
| 221 if (!file_fd_.is_valid()) { |
| 222 callback.Run(ERROR_CLOSED, nullptr); |
| 223 return; |
| 224 } |
| 225 |
| 226 // TODO(vtl): FIXME soon |
| 227 NOTIMPLEMENTED(); |
| 228 callback.Run(ERROR_UNIMPLEMENTED, nullptr); |
| 229 } |
| 230 |
| 231 void FileImpl::Truncate(int64_t size, const Callback<void(Error)>& callback) { |
| 232 if (!file_fd_.is_valid()) { |
| 233 callback.Run(ERROR_CLOSED); |
| 234 return; |
| 235 } |
| 236 if (size < 0) { |
| 237 callback.Run(ERROR_INVALID_ARGUMENT); |
| 238 return; |
| 239 } |
| 240 if (Error error = IsOffsetValid(size)) { |
| 241 callback.Run(error); |
| 242 return; |
| 243 } |
| 244 |
| 245 if (ftruncate(file_fd_.get(), static_cast<off_t>(size)) != 0) { |
| 246 callback.Run(ErrnoToError(errno)); |
| 247 return; |
| 248 } |
| 249 |
| 250 callback.Run(ERROR_OK); |
| 251 } |
| 252 |
| 253 void FileImpl::Touch(FileTimesPtr times, |
| 254 const Callback<void(Error)>& callback) { |
| 255 if (!file_fd_.is_valid()) { |
| 256 callback.Run(ERROR_CLOSED); |
| 257 return; |
| 258 } |
| 259 |
| 260 // TODO(vtl): FIXME soon |
| 261 NOTIMPLEMENTED(); |
| 262 callback.Run(ERROR_UNIMPLEMENTED); |
| 263 } |
| 264 |
| 265 void FileImpl::Dup(InterfaceRequest<File> file, |
| 266 const Callback<void(Error)>& callback) { |
| 267 if (!file_fd_.is_valid()) { |
| 268 callback.Run(ERROR_CLOSED); |
| 269 return; |
| 270 } |
| 271 |
| 272 base::ScopedFD file_fd(dup(file_fd_.get())); |
| 273 if (!file_fd.is_valid()) { |
| 274 callback.Run(ErrnoToError(errno)); |
| 275 return; |
| 276 } |
| 277 |
| 278 new FileImpl(file.Pass(), file_fd.Pass()); |
| 279 callback.Run(ERROR_OK); |
| 280 } |
| 281 |
| 282 void FileImpl::Reopen(InterfaceRequest<File> file, |
| 283 uint32_t access_flags, |
| 284 uint32_t open_flags, |
| 285 const Callback<void(Error)>& callback) { |
| 286 if (!file_fd_.is_valid()) { |
| 287 callback.Run(ERROR_CLOSED); |
| 288 return; |
| 289 } |
| 290 |
| 291 // TODO(vtl): FIXME soon |
| 292 NOTIMPLEMENTED(); |
| 293 callback.Run(ERROR_UNIMPLEMENTED); |
| 294 } |
| 295 |
| 296 void FileImpl::AsBuffer( |
| 297 const Callback<void(Error, ScopedSharedBufferHandle)>& callback) { |
| 298 if (!file_fd_.is_valid()) { |
| 299 callback.Run(ERROR_CLOSED, ScopedSharedBufferHandle()); |
| 300 return; |
| 301 } |
| 302 |
| 303 // TODO(vtl): FIXME soon |
| 304 NOTIMPLEMENTED(); |
| 305 callback.Run(ERROR_UNIMPLEMENTED, ScopedSharedBufferHandle()); |
| 306 } |
| 307 |
| 308 } // namespace files |
| 309 } // namespace mojo |
OLD | NEW |