Chromium Code Reviews| 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 = 4 * 1024 * 1024; | |
|
qsr
2015/03/03 11:56:44
Any reason you want to limit this? Performance rea
viettrungluu
2015/03/03 18:50:36
Performance among others (e.g., resource consumpti
| |
| 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 file_fd_.reset(); | |
| 44 callback.Run(ERROR_OK); | |
|
qsr
2015/03/03 11:56:44
Didn't you tell me that the reason we had close is
viettrungluu
2015/03/03 18:50:36
Done, though when you see the code you may still d
qsr
2015/03/04 15:10:19
The implementation is not as bad as you make it so
| |
| 45 } | |
| 46 | |
| 47 // TODO(vtl): Move the implementation to a thread pool. | |
| 48 void FileImpl::Read(uint32_t num_bytes_to_read, | |
| 49 int64_t offset, | |
| 50 Whence whence, | |
| 51 const Callback<void(Error, Array<uint8_t>)>& callback) { | |
| 52 if (!file_fd_.is_valid()) { | |
| 53 callback.Run(ERROR_CLOSED, Array<uint8_t>()); | |
| 54 return; | |
| 55 } | |
| 56 if (num_bytes_to_read > kMaxReadSize) { | |
| 57 callback.Run(ERROR_OUT_OF_RANGE, Array<uint8_t>()); | |
| 58 return; | |
| 59 } | |
| 60 if (Error error = IsOffsetValid(offset)) { | |
| 61 callback.Run(error, Array<uint8_t>()); | |
| 62 return; | |
| 63 } | |
| 64 if (Error error = IsWhenceValid(whence)) { | |
| 65 callback.Run(error, Array<uint8_t>()); | |
| 66 return; | |
| 67 } | |
| 68 | |
| 69 if (offset != 0 || whence != WHENCE_FROM_CURRENT) { | |
| 70 // TODO(vtl): Use |pread()| below in the |WHENCE_FROM_START| case. This | |
| 71 // implementation is obviously not atomic. (If someone seeks simultaneously, | |
| 72 // we'll end up writing somewhere else. Or, well, we would if we were | |
| 73 // multithreaded.) Maybe we should do an |ftell()| and always use |pread()|. | |
| 74 if (lseek(file_fd_.get(), static_cast<off_t>(offset), | |
| 75 WhenceToStandardWhence(whence)) < 0) { | |
|
qsr
2015/03/03 11:56:45
It is not clear from the documentation that the fi
viettrungluu
2015/03/03 18:50:36
I wonder if we should have a flag, or if we should
| |
| 76 callback.Run(ErrnoToError(errno), Array<uint8_t>()); | |
| 77 return; | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 Array<uint8_t> bytes_read(num_bytes_to_read); | |
| 82 ssize_t num_bytes_read = HANDLE_EINTR( | |
| 83 read(file_fd_.get(), &bytes_read.front(), num_bytes_to_read)); | |
| 84 if (num_bytes_read < 0) { | |
| 85 // TODO(vtl): Decode errno. | |
|
qsr
2015/03/03 11:56:44
Should you use ErrnoToError?
viettrungluu
2015/03/03 18:50:35
Done.
| |
| 86 callback.Run(ERROR_UNKNOWN, Array<uint8_t>()); | |
| 87 return; | |
| 88 } | |
| 89 | |
| 90 DCHECK_LE(static_cast<size_t>(num_bytes_read), num_bytes_to_read); | |
| 91 bytes_read.resize(static_cast<size_t>(num_bytes_read)); | |
| 92 callback.Run(ERROR_OK, bytes_read.Pass()); | |
| 93 } | |
| 94 | |
| 95 // TODO(vtl): Move the implementation to a thread pool. | |
| 96 void FileImpl::Write(Array<uint8_t> bytes_to_write, | |
| 97 int64_t offset, | |
| 98 Whence whence, | |
| 99 const Callback<void(Error, uint32_t)>& callback) { | |
| 100 DCHECK(!bytes_to_write.is_null()); | |
| 101 | |
| 102 if (!file_fd_.is_valid()) { | |
| 103 callback.Run(ERROR_CLOSED, 0); | |
| 104 return; | |
| 105 } | |
| 106 // Who knows what |write()| would return if the size is that big (and it | |
| 107 // actually wrote that much). | |
| 108 if (bytes_to_write.size() > | |
| 109 static_cast<size_t>(std::numeric_limits<ssize_t>::max())) { | |
| 110 callback.Run(ERROR_OUT_OF_RANGE, 0); | |
| 111 return; | |
| 112 } | |
| 113 if (Error error = IsOffsetValid(offset)) { | |
| 114 callback.Run(error, 0); | |
| 115 return; | |
| 116 } | |
| 117 if (Error error = IsWhenceValid(whence)) { | |
| 118 callback.Run(error, 0); | |
| 119 return; | |
| 120 } | |
| 121 | |
| 122 if (offset != 0 || whence != WHENCE_FROM_CURRENT) { | |
| 123 // TODO(vtl): Use |pwrite()| below in the |WHENCE_FROM_START| case. This | |
| 124 // implementation is obviously not atomic. (If someone seeks simultaneously, | |
| 125 // we'll end up writing somewhere else. Or, well, we would if we were | |
| 126 // multithreaded.) Maybe we should do an |ftell()| and always use | |
| 127 // |pwrite()|. | |
|
qsr
2015/03/03 11:56:45
"
viettrungluu
2015/03/03 18:50:36
Acknowledged.
| |
| 128 if (lseek(file_fd_.get(), static_cast<off_t>(offset), | |
| 129 WhenceToStandardWhence(whence)) < 0) { | |
| 130 callback.Run(ErrnoToError(errno), 0); | |
| 131 return; | |
| 132 } | |
| 133 } | |
| 134 | |
| 135 const void* buf = | |
| 136 (bytes_to_write.size() > 0) ? &bytes_to_write.front() : nullptr; | |
| 137 ssize_t num_bytes_written = | |
| 138 HANDLE_EINTR(write(file_fd_.get(), buf, bytes_to_write.size())); | |
| 139 if (num_bytes_written < 0) { | |
| 140 callback.Run(ErrnoToError(errno), 0); | |
| 141 return; | |
| 142 } | |
| 143 | |
| 144 DCHECK_LE(static_cast<size_t>(num_bytes_written), | |
| 145 std::numeric_limits<uint32_t>::max()); | |
| 146 callback.Run(ERROR_OK, static_cast<uint32_t>(num_bytes_written)); | |
| 147 } | |
| 148 | |
| 149 void FileImpl::ReadToStream(ScopedDataPipeProducerHandle source, | |
| 150 int64_t offset, | |
| 151 Whence whence, | |
| 152 int64_t num_bytes_to_read, | |
| 153 const Callback<void(Error)>& callback) { | |
| 154 if (!file_fd_.is_valid()) { | |
| 155 callback.Run(ERROR_CLOSED); | |
| 156 return; | |
| 157 } | |
| 158 if (Error error = IsOffsetValid(offset)) { | |
| 159 callback.Run(error); | |
| 160 return; | |
| 161 } | |
| 162 if (Error error = IsWhenceValid(whence)) { | |
| 163 callback.Run(error); | |
| 164 return; | |
| 165 } | |
| 166 | |
| 167 // TODO(vtl): FIXME soon | |
| 168 NOTIMPLEMENTED(); | |
| 169 callback.Run(ERROR_UNIMPLEMENTED); | |
| 170 } | |
| 171 | |
| 172 void FileImpl::WriteFromStream(ScopedDataPipeConsumerHandle sink, | |
| 173 int64_t offset, | |
| 174 Whence whence, | |
| 175 const Callback<void(Error)>& callback) { | |
| 176 if (!file_fd_.is_valid()) { | |
| 177 callback.Run(ERROR_CLOSED); | |
| 178 return; | |
| 179 } | |
| 180 if (Error error = IsOffsetValid(offset)) { | |
| 181 callback.Run(error); | |
| 182 return; | |
| 183 } | |
| 184 if (Error error = IsWhenceValid(whence)) { | |
| 185 callback.Run(error); | |
| 186 return; | |
| 187 } | |
| 188 | |
| 189 // TODO(vtl): FIXME soon | |
| 190 NOTIMPLEMENTED(); | |
| 191 callback.Run(ERROR_UNIMPLEMENTED); | |
| 192 } | |
| 193 | |
| 194 void FileImpl::Tell(const Callback<void(Error, int64_t)>& callback) { | |
| 195 Seek(0, WHENCE_FROM_CURRENT, callback); | |
| 196 } | |
| 197 | |
| 198 void FileImpl::Seek(int64_t offset, | |
| 199 Whence whence, | |
| 200 const Callback<void(Error, int64_t)>& callback) { | |
| 201 if (!file_fd_.is_valid()) { | |
| 202 callback.Run(ERROR_CLOSED, 0); | |
| 203 return; | |
| 204 } | |
| 205 if (Error error = IsOffsetValid(offset)) { | |
| 206 callback.Run(error, 0); | |
| 207 return; | |
| 208 } | |
| 209 if (Error error = IsWhenceValid(whence)) { | |
| 210 callback.Run(error, 0); | |
| 211 return; | |
| 212 } | |
| 213 | |
| 214 off_t position = lseek(file_fd_.get(), static_cast<off_t>(offset), | |
| 215 WhenceToStandardWhence(whence)); | |
| 216 if (position < 0) { | |
| 217 callback.Run(ErrnoToError(errno), 0); | |
| 218 return; | |
| 219 } | |
| 220 | |
| 221 callback.Run(ERROR_OK, static_cast<int64>(position)); | |
| 222 } | |
| 223 | |
| 224 void FileImpl::Stat(const Callback<void(Error, FileInformationPtr)>& callback) { | |
| 225 if (!file_fd_.is_valid()) { | |
| 226 callback.Run(ERROR_CLOSED, nullptr); | |
| 227 return; | |
| 228 } | |
| 229 | |
| 230 struct stat buf; | |
| 231 if (fstat(file_fd_.get(), &buf) != 0) { | |
| 232 callback.Run(ErrnoToError(errno), nullptr); | |
| 233 return; | |
| 234 } | |
| 235 | |
| 236 FileInformationPtr file_info(FileInformation::New()); | |
| 237 file_info->size = static_cast<int64_t>(buf.st_size); | |
| 238 file_info->atime = Timespec::New(); | |
| 239 file_info->mtime = Timespec::New(); | |
| 240 #if defined(OS_ANDROID) | |
| 241 file_info->atime->seconds = static_cast<int64_t>(buf.st_atime); | |
| 242 file_info->atime->nanoseconds = static_cast<int32_t>(buf.st_atime_nsec); | |
| 243 file_info->mtime->seconds = static_cast<int64_t>(buf.st_mtime); | |
| 244 file_info->mtime->nanoseconds = static_cast<int32_t>(buf.st_mtime_nsec); | |
| 245 #else | |
| 246 file_info->atime->seconds = static_cast<int64_t>(buf.st_atim.tv_sec); | |
| 247 file_info->atime->nanoseconds = static_cast<int32_t>(buf.st_atim.tv_nsec); | |
| 248 file_info->mtime->seconds = static_cast<int64_t>(buf.st_mtim.tv_sec); | |
| 249 file_info->mtime->nanoseconds = static_cast<int32_t>(buf.st_mtim.tv_nsec); | |
| 250 #endif | |
| 251 | |
| 252 callback.Run(ERROR_OK, file_info.Pass()); | |
| 253 } | |
| 254 | |
| 255 void FileImpl::Truncate(int64_t size, const Callback<void(Error)>& callback) { | |
| 256 if (!file_fd_.is_valid()) { | |
| 257 callback.Run(ERROR_CLOSED); | |
| 258 return; | |
| 259 } | |
| 260 if (size < 0) { | |
| 261 callback.Run(ERROR_INVALID_ARGUMENT); | |
| 262 return; | |
| 263 } | |
| 264 if (Error error = IsOffsetValid(size)) { | |
| 265 callback.Run(error); | |
| 266 return; | |
| 267 } | |
| 268 | |
| 269 if (ftruncate(file_fd_.get(), static_cast<off_t>(size)) != 0) { | |
| 270 callback.Run(ErrnoToError(errno)); | |
| 271 return; | |
| 272 } | |
| 273 | |
| 274 callback.Run(ERROR_OK); | |
| 275 } | |
| 276 | |
| 277 void FileImpl::Touch(TimespecOrNowPtr atime, | |
| 278 TimespecOrNowPtr mtime, | |
| 279 const Callback<void(Error)>& callback) { | |
| 280 if (!file_fd_.is_valid()) { | |
| 281 callback.Run(ERROR_CLOSED); | |
| 282 return; | |
| 283 } | |
| 284 | |
| 285 struct timespec times[2]; | |
| 286 if (Error error = TimespecOrNowToStandardTimespec(atime.get(), ×[0])) { | |
| 287 callback.Run(error); | |
| 288 return; | |
| 289 } | |
| 290 if (Error error = TimespecOrNowToStandardTimespec(mtime.get(), ×[1])) { | |
| 291 callback.Run(error); | |
| 292 return; | |
| 293 } | |
| 294 | |
| 295 if (futimens(file_fd_.get(), times) != 0) { | |
| 296 callback.Run(ErrnoToError(errno)); | |
| 297 return; | |
| 298 } | |
| 299 | |
| 300 callback.Run(ERROR_OK); | |
| 301 } | |
| 302 | |
| 303 void FileImpl::Dup(InterfaceRequest<File> file, | |
| 304 const Callback<void(Error)>& callback) { | |
| 305 if (!file_fd_.is_valid()) { | |
| 306 callback.Run(ERROR_CLOSED); | |
| 307 return; | |
| 308 } | |
| 309 | |
| 310 base::ScopedFD file_fd(dup(file_fd_.get())); | |
| 311 if (!file_fd.is_valid()) { | |
| 312 callback.Run(ErrnoToError(errno)); | |
| 313 return; | |
| 314 } | |
| 315 | |
| 316 new FileImpl(file.Pass(), file_fd.Pass()); | |
| 317 callback.Run(ERROR_OK); | |
| 318 } | |
| 319 | |
| 320 void FileImpl::Reopen(InterfaceRequest<File> file, | |
| 321 uint32_t open_flags, | |
| 322 const Callback<void(Error)>& callback) { | |
| 323 if (!file_fd_.is_valid()) { | |
| 324 callback.Run(ERROR_CLOSED); | |
| 325 return; | |
| 326 } | |
| 327 | |
| 328 // TODO(vtl): FIXME soon | |
| 329 NOTIMPLEMENTED(); | |
| 330 callback.Run(ERROR_UNIMPLEMENTED); | |
| 331 } | |
| 332 | |
| 333 void FileImpl::AsBuffer( | |
| 334 const Callback<void(Error, ScopedSharedBufferHandle)>& callback) { | |
| 335 if (!file_fd_.is_valid()) { | |
| 336 callback.Run(ERROR_CLOSED, ScopedSharedBufferHandle()); | |
| 337 return; | |
| 338 } | |
| 339 | |
| 340 // TODO(vtl): FIXME soon | |
| 341 NOTIMPLEMENTED(); | |
| 342 callback.Run(ERROR_UNIMPLEMENTED, ScopedSharedBufferHandle()); | |
| 343 } | |
| 344 | |
| 345 } // namespace files | |
| 346 } // namespace mojo | |
| OLD | NEW |