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/files/file_impl.h" |
| 6 |
| 7 #include <errno.h> |
| 8 #include <fcntl.h> |
| 9 #include <sys/stat.h> |
| 10 #include <sys/types.h> |
| 11 #include <time.h> |
| 12 #include <unistd.h> |
| 13 |
| 14 #include <limits> |
| 15 |
| 16 #include "base/files/scoped_file.h" |
| 17 #include "base/logging.h" |
| 18 #include "base/posix/eintr_wrapper.h" |
| 19 #include "services/files/futimens.h" |
| 20 #include "services/files/util.h" |
| 21 |
| 22 static_assert(sizeof(off_t) <= sizeof(int64_t), "off_t too big"); |
| 23 static_assert(sizeof(size_t) >= sizeof(uint32_t), "size_t too small"); |
| 24 |
| 25 namespace mojo { |
| 26 namespace files { |
| 27 |
| 28 const size_t kMaxReadSize = 1 * 1024 * 1024; // 1 MB. |
| 29 |
| 30 FileImpl::FileImpl(InterfaceRequest<File> request, base::ScopedFD file_fd) |
| 31 : binding_(this, request.Pass()), file_fd_(file_fd.Pass()) { |
| 32 DCHECK(file_fd_.is_valid()); |
| 33 } |
| 34 |
| 35 FileImpl::~FileImpl() { |
| 36 } |
| 37 |
| 38 void FileImpl::Close(const Callback<void(Error)>& callback) { |
| 39 if (!file_fd_.is_valid()) { |
| 40 callback.Run(ERROR_CLOSED); |
| 41 return; |
| 42 } |
| 43 int fd_to_try_to_close = file_fd_.release(); |
| 44 // POSIX.1 (2013) leaves the validity of the FD undefined on EINTR and EIO. On |
| 45 // Linux, the FD is always invalidated, so we'll pretend that the close |
| 46 // succeeded. (On other Unixes, the situation may be different and possibly |
| 47 // totally broken; see crbug.com/269623.) |
| 48 if (IGNORE_EINTR(close(fd_to_try_to_close)) != 0) { |
| 49 // Save errno, since we do a few things and we don't want it trampled. |
| 50 int error = errno; |
| 51 CHECK_NE(error, EBADF); // This should never happen. |
| 52 DCHECK_NE(error, EINTR); // We already ignored EINTR. |
| 53 // I don't know what Linux does on EIO (or any other errors) -- POSIX leaves |
| 54 // it undefined -- so report the error and hope that the FD was invalidated. |
| 55 callback.Run(ErrnoToError(error)); |
| 56 return; |
| 57 } |
| 58 |
| 59 callback.Run(ERROR_OK); |
| 60 } |
| 61 |
| 62 // TODO(vtl): Move the implementation to a thread pool. |
| 63 void FileImpl::Read(uint32_t num_bytes_to_read, |
| 64 int64_t offset, |
| 65 Whence whence, |
| 66 const Callback<void(Error, Array<uint8_t>)>& callback) { |
| 67 if (!file_fd_.is_valid()) { |
| 68 callback.Run(ERROR_CLOSED, Array<uint8_t>()); |
| 69 return; |
| 70 } |
| 71 if (num_bytes_to_read > kMaxReadSize) { |
| 72 callback.Run(ERROR_OUT_OF_RANGE, Array<uint8_t>()); |
| 73 return; |
| 74 } |
| 75 if (Error error = IsOffsetValid(offset)) { |
| 76 callback.Run(error, Array<uint8_t>()); |
| 77 return; |
| 78 } |
| 79 if (Error error = IsWhenceValid(whence)) { |
| 80 callback.Run(error, Array<uint8_t>()); |
| 81 return; |
| 82 } |
| 83 |
| 84 if (offset != 0 || whence != WHENCE_FROM_CURRENT) { |
| 85 // TODO(vtl): Use |pread()| below in the |WHENCE_FROM_START| case. This |
| 86 // implementation is obviously not atomic. (If someone seeks simultaneously, |
| 87 // we'll end up writing somewhere else. Or, well, we would if we were |
| 88 // multithreaded.) Maybe we should do an |ftell()| and always use |pread()|. |
| 89 // TODO(vtl): Possibly, at least sometimes we should not change the file |
| 90 // position. See TODO in file.mojom. |
| 91 if (lseek(file_fd_.get(), static_cast<off_t>(offset), |
| 92 WhenceToStandardWhence(whence)) < 0) { |
| 93 callback.Run(ErrnoToError(errno), Array<uint8_t>()); |
| 94 return; |
| 95 } |
| 96 } |
| 97 |
| 98 Array<uint8_t> bytes_read(num_bytes_to_read); |
| 99 ssize_t num_bytes_read = HANDLE_EINTR( |
| 100 read(file_fd_.get(), &bytes_read.front(), num_bytes_to_read)); |
| 101 if (num_bytes_read < 0) { |
| 102 callback.Run(ErrnoToError(errno), Array<uint8_t>()); |
| 103 return; |
| 104 } |
| 105 |
| 106 DCHECK_LE(static_cast<size_t>(num_bytes_read), num_bytes_to_read); |
| 107 bytes_read.resize(static_cast<size_t>(num_bytes_read)); |
| 108 callback.Run(ERROR_OK, bytes_read.Pass()); |
| 109 } |
| 110 |
| 111 // TODO(vtl): Move the implementation to a thread pool. |
| 112 void FileImpl::Write(Array<uint8_t> bytes_to_write, |
| 113 int64_t offset, |
| 114 Whence whence, |
| 115 const Callback<void(Error, uint32_t)>& callback) { |
| 116 DCHECK(!bytes_to_write.is_null()); |
| 117 |
| 118 if (!file_fd_.is_valid()) { |
| 119 callback.Run(ERROR_CLOSED, 0); |
| 120 return; |
| 121 } |
| 122 // Who knows what |write()| would return if the size is that big (and it |
| 123 // actually wrote that much). |
| 124 if (bytes_to_write.size() > |
| 125 static_cast<size_t>(std::numeric_limits<ssize_t>::max())) { |
| 126 callback.Run(ERROR_OUT_OF_RANGE, 0); |
| 127 return; |
| 128 } |
| 129 if (Error error = IsOffsetValid(offset)) { |
| 130 callback.Run(error, 0); |
| 131 return; |
| 132 } |
| 133 if (Error error = IsWhenceValid(whence)) { |
| 134 callback.Run(error, 0); |
| 135 return; |
| 136 } |
| 137 |
| 138 if (offset != 0 || whence != WHENCE_FROM_CURRENT) { |
| 139 // TODO(vtl): Use |pwrite()| below in the |WHENCE_FROM_START| case. This |
| 140 // implementation is obviously not atomic. (If someone seeks simultaneously, |
| 141 // we'll end up writing somewhere else. Or, well, we would if we were |
| 142 // multithreaded.) Maybe we should do an |ftell()| and always use |
| 143 // |pwrite()|. |
| 144 // TODO(vtl): Possibly, at least sometimes we should not change the file |
| 145 // position. See TODO in file.mojom. |
| 146 if (lseek(file_fd_.get(), static_cast<off_t>(offset), |
| 147 WhenceToStandardWhence(whence)) < 0) { |
| 148 callback.Run(ErrnoToError(errno), 0); |
| 149 return; |
| 150 } |
| 151 } |
| 152 |
| 153 const void* buf = |
| 154 (bytes_to_write.size() > 0) ? &bytes_to_write.front() : nullptr; |
| 155 ssize_t num_bytes_written = |
| 156 HANDLE_EINTR(write(file_fd_.get(), buf, bytes_to_write.size())); |
| 157 if (num_bytes_written < 0) { |
| 158 callback.Run(ErrnoToError(errno), 0); |
| 159 return; |
| 160 } |
| 161 |
| 162 DCHECK_LE(static_cast<size_t>(num_bytes_written), |
| 163 std::numeric_limits<uint32_t>::max()); |
| 164 callback.Run(ERROR_OK, static_cast<uint32_t>(num_bytes_written)); |
| 165 } |
| 166 |
| 167 void FileImpl::ReadToStream(ScopedDataPipeProducerHandle source, |
| 168 int64_t offset, |
| 169 Whence whence, |
| 170 int64_t num_bytes_to_read, |
| 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::WriteFromStream(ScopedDataPipeConsumerHandle sink, |
| 191 int64_t offset, |
| 192 Whence whence, |
| 193 const Callback<void(Error)>& callback) { |
| 194 if (!file_fd_.is_valid()) { |
| 195 callback.Run(ERROR_CLOSED); |
| 196 return; |
| 197 } |
| 198 if (Error error = IsOffsetValid(offset)) { |
| 199 callback.Run(error); |
| 200 return; |
| 201 } |
| 202 if (Error error = IsWhenceValid(whence)) { |
| 203 callback.Run(error); |
| 204 return; |
| 205 } |
| 206 |
| 207 // TODO(vtl): FIXME soon |
| 208 NOTIMPLEMENTED(); |
| 209 callback.Run(ERROR_UNIMPLEMENTED); |
| 210 } |
| 211 |
| 212 void FileImpl::Tell(const Callback<void(Error, int64_t)>& callback) { |
| 213 Seek(0, WHENCE_FROM_CURRENT, callback); |
| 214 } |
| 215 |
| 216 void FileImpl::Seek(int64_t offset, |
| 217 Whence whence, |
| 218 const Callback<void(Error, int64_t)>& callback) { |
| 219 if (!file_fd_.is_valid()) { |
| 220 callback.Run(ERROR_CLOSED, 0); |
| 221 return; |
| 222 } |
| 223 if (Error error = IsOffsetValid(offset)) { |
| 224 callback.Run(error, 0); |
| 225 return; |
| 226 } |
| 227 if (Error error = IsWhenceValid(whence)) { |
| 228 callback.Run(error, 0); |
| 229 return; |
| 230 } |
| 231 |
| 232 off_t position = lseek(file_fd_.get(), static_cast<off_t>(offset), |
| 233 WhenceToStandardWhence(whence)); |
| 234 if (position < 0) { |
| 235 callback.Run(ErrnoToError(errno), 0); |
| 236 return; |
| 237 } |
| 238 |
| 239 callback.Run(ERROR_OK, static_cast<int64>(position)); |
| 240 } |
| 241 |
| 242 void FileImpl::Stat(const Callback<void(Error, FileInformationPtr)>& callback) { |
| 243 if (!file_fd_.is_valid()) { |
| 244 callback.Run(ERROR_CLOSED, nullptr); |
| 245 return; |
| 246 } |
| 247 |
| 248 struct stat buf; |
| 249 if (fstat(file_fd_.get(), &buf) != 0) { |
| 250 callback.Run(ErrnoToError(errno), nullptr); |
| 251 return; |
| 252 } |
| 253 |
| 254 FileInformationPtr file_info(FileInformation::New()); |
| 255 file_info->size = static_cast<int64_t>(buf.st_size); |
| 256 file_info->atime = Timespec::New(); |
| 257 file_info->mtime = Timespec::New(); |
| 258 #if defined(OS_ANDROID) |
| 259 file_info->atime->seconds = static_cast<int64_t>(buf.st_atime); |
| 260 file_info->atime->nanoseconds = static_cast<int32_t>(buf.st_atime_nsec); |
| 261 file_info->mtime->seconds = static_cast<int64_t>(buf.st_mtime); |
| 262 file_info->mtime->nanoseconds = static_cast<int32_t>(buf.st_mtime_nsec); |
| 263 #else |
| 264 file_info->atime->seconds = static_cast<int64_t>(buf.st_atim.tv_sec); |
| 265 file_info->atime->nanoseconds = static_cast<int32_t>(buf.st_atim.tv_nsec); |
| 266 file_info->mtime->seconds = static_cast<int64_t>(buf.st_mtim.tv_sec); |
| 267 file_info->mtime->nanoseconds = static_cast<int32_t>(buf.st_mtim.tv_nsec); |
| 268 #endif |
| 269 |
| 270 callback.Run(ERROR_OK, file_info.Pass()); |
| 271 } |
| 272 |
| 273 void FileImpl::Truncate(int64_t size, const Callback<void(Error)>& callback) { |
| 274 if (!file_fd_.is_valid()) { |
| 275 callback.Run(ERROR_CLOSED); |
| 276 return; |
| 277 } |
| 278 if (size < 0) { |
| 279 callback.Run(ERROR_INVALID_ARGUMENT); |
| 280 return; |
| 281 } |
| 282 if (Error error = IsOffsetValid(size)) { |
| 283 callback.Run(error); |
| 284 return; |
| 285 } |
| 286 |
| 287 if (ftruncate(file_fd_.get(), static_cast<off_t>(size)) != 0) { |
| 288 callback.Run(ErrnoToError(errno)); |
| 289 return; |
| 290 } |
| 291 |
| 292 callback.Run(ERROR_OK); |
| 293 } |
| 294 |
| 295 void FileImpl::Touch(TimespecOrNowPtr atime, |
| 296 TimespecOrNowPtr mtime, |
| 297 const Callback<void(Error)>& callback) { |
| 298 if (!file_fd_.is_valid()) { |
| 299 callback.Run(ERROR_CLOSED); |
| 300 return; |
| 301 } |
| 302 |
| 303 struct timespec times[2]; |
| 304 if (Error error = TimespecOrNowToStandardTimespec(atime.get(), ×[0])) { |
| 305 callback.Run(error); |
| 306 return; |
| 307 } |
| 308 if (Error error = TimespecOrNowToStandardTimespec(mtime.get(), ×[1])) { |
| 309 callback.Run(error); |
| 310 return; |
| 311 } |
| 312 |
| 313 if (futimens(file_fd_.get(), times) != 0) { |
| 314 callback.Run(ErrnoToError(errno)); |
| 315 return; |
| 316 } |
| 317 |
| 318 callback.Run(ERROR_OK); |
| 319 } |
| 320 |
| 321 void FileImpl::Dup(InterfaceRequest<File> file, |
| 322 const Callback<void(Error)>& callback) { |
| 323 if (!file_fd_.is_valid()) { |
| 324 callback.Run(ERROR_CLOSED); |
| 325 return; |
| 326 } |
| 327 |
| 328 base::ScopedFD file_fd(dup(file_fd_.get())); |
| 329 if (!file_fd.is_valid()) { |
| 330 callback.Run(ErrnoToError(errno)); |
| 331 return; |
| 332 } |
| 333 |
| 334 new FileImpl(file.Pass(), file_fd.Pass()); |
| 335 callback.Run(ERROR_OK); |
| 336 } |
| 337 |
| 338 void FileImpl::Reopen(InterfaceRequest<File> file, |
| 339 uint32_t open_flags, |
| 340 const Callback<void(Error)>& callback) { |
| 341 if (!file_fd_.is_valid()) { |
| 342 callback.Run(ERROR_CLOSED); |
| 343 return; |
| 344 } |
| 345 |
| 346 // TODO(vtl): FIXME soon |
| 347 NOTIMPLEMENTED(); |
| 348 callback.Run(ERROR_UNIMPLEMENTED); |
| 349 } |
| 350 |
| 351 void FileImpl::AsBuffer( |
| 352 const Callback<void(Error, ScopedSharedBufferHandle)>& callback) { |
| 353 if (!file_fd_.is_valid()) { |
| 354 callback.Run(ERROR_CLOSED, ScopedSharedBufferHandle()); |
| 355 return; |
| 356 } |
| 357 |
| 358 // TODO(vtl): FIXME soon |
| 359 NOTIMPLEMENTED(); |
| 360 callback.Run(ERROR_UNIMPLEMENTED, ScopedSharedBufferHandle()); |
| 361 } |
| 362 |
| 363 } // namespace files |
| 364 } // namespace mojo |
OLD | NEW |