OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "services/files/file_impl.h" | 5 #include "services/files/file_impl.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
10 #include <sys/types.h> | 10 #include <sys/types.h> |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 | 57 |
58 callback.Run(Error::OK); | 58 callback.Run(Error::OK); |
59 } | 59 } |
60 | 60 |
61 // TODO(vtl): Move the implementation to a thread pool. | 61 // TODO(vtl): Move the implementation to a thread pool. |
62 void FileImpl::Read(uint32_t num_bytes_to_read, | 62 void FileImpl::Read(uint32_t num_bytes_to_read, |
63 int64_t offset, | 63 int64_t offset, |
64 Whence whence, | 64 Whence whence, |
65 const ReadCallback& callback) { | 65 const ReadCallback& callback) { |
66 if (!file_fd_.is_valid()) { | 66 if (!file_fd_.is_valid()) { |
67 callback.Run(Error::CLOSED, Array<uint8_t>()); | 67 callback.Run(Error::CLOSED, nullptr); |
68 return; | 68 return; |
69 } | 69 } |
70 if (num_bytes_to_read > kMaxReadSize) { | 70 if (num_bytes_to_read > kMaxReadSize) { |
71 callback.Run(Error::OUT_OF_RANGE, Array<uint8_t>()); | 71 callback.Run(Error::OUT_OF_RANGE, nullptr); |
72 return; | 72 return; |
73 } | 73 } |
74 | 74 |
75 Error error = IsOffsetValid(offset); | 75 Error error = IsOffsetValid(offset); |
76 if (error != Error::OK) { | 76 if (error != Error::OK) { |
77 callback.Run(error, Array<uint8_t>()); | 77 callback.Run(error, nullptr); |
78 return; | 78 return; |
79 } | 79 } |
80 | 80 |
81 error = IsWhenceValid(whence); | 81 error = IsWhenceValid(whence); |
82 if (error != Error::OK) { | 82 if (error != Error::OK) { |
83 callback.Run(error, Array<uint8_t>()); | 83 callback.Run(error, nullptr); |
84 return; | 84 return; |
85 } | 85 } |
86 | 86 |
87 if (offset != 0 || whence != Whence::FROM_CURRENT) { | 87 if (offset != 0 || whence != Whence::FROM_CURRENT) { |
88 // TODO(vtl): Use |pread()| below in the |Whence::FROM_START| case. This | 88 // TODO(vtl): Use |pread()| below in the |Whence::FROM_START| case. This |
89 // implementation is obviously not atomic. (If someone seeks simultaneously, | 89 // implementation is obviously not atomic. (If someone seeks simultaneously, |
90 // we'll end up writing somewhere else. Or, well, we would if we were | 90 // we'll end up writing somewhere else. Or, well, we would if we were |
91 // multithreaded.) Maybe we should do an |ftell()| and always use |pread()|. | 91 // multithreaded.) Maybe we should do an |ftell()| and always use |pread()|. |
92 // TODO(vtl): Possibly, at least sometimes we should not change the file | 92 // TODO(vtl): Possibly, at least sometimes we should not change the file |
93 // position. See TODO in file.mojom. | 93 // position. See TODO in file.mojom. |
94 if (lseek(file_fd_.get(), static_cast<off_t>(offset), | 94 if (lseek(file_fd_.get(), static_cast<off_t>(offset), |
95 WhenceToStandardWhence(whence)) < 0) { | 95 WhenceToStandardWhence(whence)) < 0) { |
96 callback.Run(ErrnoToError(errno), Array<uint8_t>()); | 96 callback.Run(ErrnoToError(errno), nullptr); |
97 return; | 97 return; |
98 } | 98 } |
99 } | 99 } |
100 | 100 |
101 auto bytes_read = Array<uint8_t>::New(num_bytes_to_read); | 101 auto bytes_read = Array<uint8_t>::New(num_bytes_to_read); |
102 ssize_t num_bytes_read = HANDLE_EINTR( | 102 ssize_t num_bytes_read = HANDLE_EINTR( |
103 read(file_fd_.get(), &bytes_read.front(), num_bytes_to_read)); | 103 read(file_fd_.get(), &bytes_read.front(), num_bytes_to_read)); |
104 if (num_bytes_read < 0) { | 104 if (num_bytes_read < 0) { |
105 callback.Run(ErrnoToError(errno), Array<uint8_t>()); | 105 callback.Run(ErrnoToError(errno), nullptr); |
106 return; | 106 return; |
107 } | 107 } |
108 | 108 |
109 DCHECK_LE(static_cast<size_t>(num_bytes_read), num_bytes_to_read); | 109 DCHECK_LE(static_cast<size_t>(num_bytes_read), num_bytes_to_read); |
110 bytes_read.resize(static_cast<size_t>(num_bytes_read)); | 110 bytes_read.resize(static_cast<size_t>(num_bytes_read)); |
111 callback.Run(Error::OK, bytes_read.Pass()); | 111 callback.Run(Error::OK, bytes_read.Pass()); |
112 } | 112 } |
113 | 113 |
114 // TODO(vtl): Move the implementation to a thread pool. | 114 // TODO(vtl): Move the implementation to a thread pool. |
115 void FileImpl::Write(Array<uint8_t> bytes_to_write, | 115 void FileImpl::Write(Array<uint8_t> bytes_to_write, |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
337 | 337 |
338 // TODO(vtl): FIXME soon | 338 // TODO(vtl): FIXME soon |
339 NOTIMPLEMENTED(); | 339 NOTIMPLEMENTED(); |
340 callback.Run(Error::UNIMPLEMENTED, ScopedSharedBufferHandle()); | 340 callback.Run(Error::UNIMPLEMENTED, ScopedSharedBufferHandle()); |
341 } | 341 } |
342 | 342 |
343 void FileImpl::Ioctl(uint32_t request, | 343 void FileImpl::Ioctl(uint32_t request, |
344 Array<uint32_t> in_values, | 344 Array<uint32_t> in_values, |
345 const IoctlCallback& callback) { | 345 const IoctlCallback& callback) { |
346 if (!file_fd_.is_valid()) { | 346 if (!file_fd_.is_valid()) { |
347 callback.Run(Error::CLOSED, Array<uint32_t>()); | 347 callback.Run(Error::CLOSED, nullptr); |
348 return; | 348 return; |
349 } | 349 } |
350 | 350 |
351 // TODO(vtl): The "correct" error code should be one that can be translated to | 351 // TODO(vtl): The "correct" error code should be one that can be translated to |
352 // ENOTTY! | 352 // ENOTTY! |
353 callback.Run(Error::UNAVAILABLE, Array<uint32_t>()); | 353 callback.Run(Error::UNAVAILABLE, nullptr); |
354 } | 354 } |
355 | 355 |
356 } // namespace files | 356 } // namespace files |
357 } // namespace mojo | 357 } // namespace mojo |
OLD | NEW |