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 "files/public/c/lib/file_fd_impl.h" | 5 #include "files/public/c/lib/file_fd_impl.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
10 #include <limits> | 10 #include <limits> |
(...skipping 13 matching lines...) Expand all Loading... |
24 MOJO_DCHECK(file_); | 24 MOJO_DCHECK(file_); |
25 } | 25 } |
26 | 26 |
27 FileFDImpl::~FileFDImpl() { | 27 FileFDImpl::~FileFDImpl() { |
28 } | 28 } |
29 | 29 |
30 bool FileFDImpl::Close() { | 30 bool FileFDImpl::Close() { |
31 ErrnoImpl::Setter errno_setter(errno_impl()); | 31 ErrnoImpl::Setter errno_setter(errno_impl()); |
32 MOJO_DCHECK(file_); | 32 MOJO_DCHECK(file_); |
33 | 33 |
34 mojo::files::Error error = mojo::files::ERROR_INTERNAL; | 34 mojo::files::Error error = mojo::files::Error::INTERNAL; |
35 file_->Close(Capture(&error)); | 35 file_->Close(Capture(&error)); |
36 if (!file_.WaitForIncomingResponse()) | 36 if (!file_.WaitForIncomingResponse()) |
37 return errno_setter.Set(ESTALE); | 37 return errno_setter.Set(ESTALE); |
38 return errno_setter.Set(ErrorToErrno(error)); | 38 return errno_setter.Set(ErrorToErrno(error)); |
39 } | 39 } |
40 | 40 |
41 std::unique_ptr<FDImpl> FileFDImpl::Dup() { | 41 std::unique_ptr<FDImpl> FileFDImpl::Dup() { |
42 ErrnoImpl::Setter errno_setter(errno_impl()); | 42 ErrnoImpl::Setter errno_setter(errno_impl()); |
43 MOJO_DCHECK(file_); | 43 MOJO_DCHECK(file_); |
44 | 44 |
45 mojo::files::FilePtr new_file; | 45 mojo::files::FilePtr new_file; |
46 mojo::files::Error error = mojo::files::ERROR_INTERNAL; | 46 mojo::files::Error error = mojo::files::Error::INTERNAL; |
47 file_->Dup(mojo::GetProxy(&new_file), Capture(&error)); | 47 file_->Dup(mojo::GetProxy(&new_file), Capture(&error)); |
48 if (!file_.WaitForIncomingResponse()) { | 48 if (!file_.WaitForIncomingResponse()) { |
49 errno_setter.Set(ESTALE); | 49 errno_setter.Set(ESTALE); |
50 return nullptr; | 50 return nullptr; |
51 } | 51 } |
52 if (!errno_setter.Set(ErrorToErrno(error))) | 52 if (!errno_setter.Set(ErrorToErrno(error))) |
53 return nullptr; | 53 return nullptr; |
54 // C++11, why don't you have make_unique? | 54 // C++11, why don't you have make_unique? |
55 return std::unique_ptr<FDImpl>(new FileFDImpl(errno_impl(), new_file.Pass())); | 55 return std::unique_ptr<FDImpl>(new FileFDImpl(errno_impl(), new_file.Pass())); |
56 } | 56 } |
57 | 57 |
58 bool FileFDImpl::Ftruncate(mojio_off_t length) { | 58 bool FileFDImpl::Ftruncate(mojio_off_t length) { |
59 ErrnoImpl::Setter errno_setter(errno_impl()); | 59 ErrnoImpl::Setter errno_setter(errno_impl()); |
60 MOJO_DCHECK(file_); | 60 MOJO_DCHECK(file_); |
61 | 61 |
62 if (length < 0) | 62 if (length < 0) |
63 return errno_setter.Set(EINVAL); | 63 return errno_setter.Set(EINVAL); |
64 | 64 |
65 mojo::files::Error error = mojo::files::ERROR_INTERNAL; | 65 mojo::files::Error error = mojo::files::Error::INTERNAL; |
66 file_->Truncate(static_cast<int64_t>(length), Capture(&error)); | 66 file_->Truncate(static_cast<int64_t>(length), Capture(&error)); |
67 if (!file_.WaitForIncomingResponse()) | 67 if (!file_.WaitForIncomingResponse()) |
68 return errno_setter.Set(ESTALE); | 68 return errno_setter.Set(ESTALE); |
69 return errno_setter.Set(ErrorToErrno(error)); | 69 return errno_setter.Set(ErrorToErrno(error)); |
70 } | 70 } |
71 | 71 |
72 mojio_off_t FileFDImpl::Lseek(mojio_off_t offset, int whence) { | 72 mojio_off_t FileFDImpl::Lseek(mojio_off_t offset, int whence) { |
73 ErrnoImpl::Setter errno_setter(errno_impl()); | 73 ErrnoImpl::Setter errno_setter(errno_impl()); |
74 MOJO_DCHECK(file_); | 74 MOJO_DCHECK(file_); |
75 | 75 |
76 mojo::files::Whence mojo_whence; | 76 mojo::files::Whence mojo_whence; |
77 switch (whence) { | 77 switch (whence) { |
78 case MOJIO_SEEK_SET: | 78 case MOJIO_SEEK_SET: |
79 mojo_whence = mojo::files::WHENCE_FROM_START; | 79 mojo_whence = mojo::files::Whence::FROM_START; |
80 break; | 80 break; |
81 case MOJIO_SEEK_CUR: | 81 case MOJIO_SEEK_CUR: |
82 mojo_whence = mojo::files::WHENCE_FROM_CURRENT; | 82 mojo_whence = mojo::files::Whence::FROM_CURRENT; |
83 break; | 83 break; |
84 case MOJIO_SEEK_END: | 84 case MOJIO_SEEK_END: |
85 mojo_whence = mojo::files::WHENCE_FROM_END; | 85 mojo_whence = mojo::files::Whence::FROM_END; |
86 break; | 86 break; |
87 default: | 87 default: |
88 errno_setter.Set(EINVAL); | 88 errno_setter.Set(EINVAL); |
89 return -1; | 89 return -1; |
90 } | 90 } |
91 | 91 |
92 mojo::files::Error error = mojo::files::ERROR_INTERNAL; | 92 mojo::files::Error error = mojo::files::Error::INTERNAL; |
93 int64_t position = -1; | 93 int64_t position = -1; |
94 file_->Seek(static_cast<int64_t>(offset), mojo_whence, | 94 file_->Seek(static_cast<int64_t>(offset), mojo_whence, |
95 Capture(&error, &position)); | 95 Capture(&error, &position)); |
96 if (!file_.WaitForIncomingResponse()) { | 96 if (!file_.WaitForIncomingResponse()) { |
97 errno_setter.Set(ESTALE); | 97 errno_setter.Set(ESTALE); |
98 return -1; | 98 return -1; |
99 } | 99 } |
100 if (!errno_setter.Set(ErrorToErrno(error))) | 100 if (!errno_setter.Set(ErrorToErrno(error))) |
101 return -1; | 101 return -1; |
102 | 102 |
(...skipping 27 matching lines...) Expand all Loading... |
130 // mathematical functions. ERANGE is for return values.) | 130 // mathematical functions. ERANGE is for return values.) |
131 errno_setter.Set(EINVAL); | 131 errno_setter.Set(EINVAL); |
132 return -1; | 132 return -1; |
133 } | 133 } |
134 | 134 |
135 if (!buf && count > 0) { | 135 if (!buf && count > 0) { |
136 errno_setter.Set(EFAULT); | 136 errno_setter.Set(EFAULT); |
137 return -1; | 137 return -1; |
138 } | 138 } |
139 | 139 |
140 mojo::files::Error error = mojo::files::ERROR_INTERNAL; | 140 mojo::files::Error error = mojo::files::Error::INTERNAL; |
141 mojo::Array<uint8_t> bytes_read; | 141 mojo::Array<uint8_t> bytes_read; |
142 file_->Read(static_cast<uint32_t>(count), 0, mojo::files::WHENCE_FROM_CURRENT, | 142 file_->Read(static_cast<uint32_t>(count), 0, |
| 143 mojo::files::Whence::FROM_CURRENT, |
143 Capture(&error, &bytes_read)); | 144 Capture(&error, &bytes_read)); |
144 if (!file_.WaitForIncomingResponse()) { | 145 if (!file_.WaitForIncomingResponse()) { |
145 errno_setter.Set(ESTALE); | 146 errno_setter.Set(ESTALE); |
146 return -1; | 147 return -1; |
147 } | 148 } |
148 if (!errno_setter.Set(ErrorToErrno(error))) | 149 if (!errno_setter.Set(ErrorToErrno(error))) |
149 return -1; | 150 return -1; |
150 if (bytes_read.size() > count) { | 151 if (bytes_read.size() > count) { |
151 // Service misbehaved. | 152 // Service misbehaved. |
152 MOJO_LOG(ERROR) << "Read() read more than requested"; | 153 MOJO_LOG(ERROR) << "Read() read more than requested"; |
(...skipping 24 matching lines...) Expand all Loading... |
177 if (!buf && count > 0) { | 178 if (!buf && count > 0) { |
178 errno_setter.Set(EFAULT); | 179 errno_setter.Set(EFAULT); |
179 return -1; | 180 return -1; |
180 } | 181 } |
181 | 182 |
182 // TODO(vtl): Is there a more natural (or efficient) way to do this? | 183 // TODO(vtl): Is there a more natural (or efficient) way to do this? |
183 mojo::Array<uint8_t> bytes_to_write(count); | 184 mojo::Array<uint8_t> bytes_to_write(count); |
184 if (count > 0) | 185 if (count > 0) |
185 memcpy(&bytes_to_write[0], buf, count); | 186 memcpy(&bytes_to_write[0], buf, count); |
186 | 187 |
187 mojo::files::Error error = mojo::files::ERROR_INTERNAL; | 188 mojo::files::Error error = mojo::files::Error::INTERNAL; |
188 uint32_t num_bytes_written = 0; | 189 uint32_t num_bytes_written = 0; |
189 file_->Write(bytes_to_write.Pass(), 0, mojo::files::WHENCE_FROM_CURRENT, | 190 file_->Write(bytes_to_write.Pass(), 0, mojo::files::Whence::FROM_CURRENT, |
190 Capture(&error, &num_bytes_written)); | 191 Capture(&error, &num_bytes_written)); |
191 if (!file_.WaitForIncomingResponse()) { | 192 if (!file_.WaitForIncomingResponse()) { |
192 errno_setter.Set(ESTALE); | 193 errno_setter.Set(ESTALE); |
193 return -1; | 194 return -1; |
194 } | 195 } |
195 if (!errno_setter.Set(ErrorToErrno(error))) | 196 if (!errno_setter.Set(ErrorToErrno(error))) |
196 return -1; | 197 return -1; |
197 | 198 |
198 if (num_bytes_written > count) { | 199 if (num_bytes_written > count) { |
199 // Service misbehaved. | 200 // Service misbehaved. |
200 MOJO_LOG(ERROR) << "Write() wrote than requested"; | 201 MOJO_LOG(ERROR) << "Write() wrote than requested"; |
201 // TODO(vtl): Is there a better error code for this? | 202 // TODO(vtl): Is there a better error code for this? |
202 errno_setter.Set(EIO); | 203 errno_setter.Set(EIO); |
203 return -1; | 204 return -1; |
204 } | 205 } |
205 | 206 |
206 return static_cast<mojio_ssize_t>(num_bytes_written); | 207 return static_cast<mojio_ssize_t>(num_bytes_written); |
207 } | 208 } |
208 | 209 |
209 bool FileFDImpl::Fstat(struct mojio_stat* buf) { | 210 bool FileFDImpl::Fstat(struct mojio_stat* buf) { |
210 ErrnoImpl::Setter errno_setter(errno_impl()); | 211 ErrnoImpl::Setter errno_setter(errno_impl()); |
211 MOJO_DCHECK(file_); | 212 MOJO_DCHECK(file_); |
212 | 213 |
213 if (!buf) { | 214 if (!buf) { |
214 errno_setter.Set(EFAULT); | 215 errno_setter.Set(EFAULT); |
215 return false; | 216 return false; |
216 } | 217 } |
217 | 218 |
218 mojo::files::FileInformationPtr file_info; | 219 mojo::files::FileInformationPtr file_info; |
219 mojo::files::Error error = mojo::files::ERROR_INTERNAL; | 220 mojo::files::Error error = mojo::files::Error::INTERNAL; |
220 file_->Stat(Capture(&error, &file_info)); | 221 file_->Stat(Capture(&error, &file_info)); |
221 if (!file_.WaitForIncomingResponse()) { | 222 if (!file_.WaitForIncomingResponse()) { |
222 errno_setter.Set(ESTALE); | 223 errno_setter.Set(ESTALE); |
223 return false; | 224 return false; |
224 } | 225 } |
225 if (!errno_setter.Set(ErrorToErrno(error))) | 226 if (!errno_setter.Set(ErrorToErrno(error))) |
226 return false; | 227 return false; |
227 | 228 |
228 if (!file_info) { | 229 if (!file_info) { |
229 // Service misbehaved. | 230 // Service misbehaved. |
230 MOJO_LOG(ERROR) << "Stat() didn't provide FileInformation"; | 231 MOJO_LOG(ERROR) << "Stat() didn't provide FileInformation"; |
231 // TODO(vtl): Is there a better error code for this? | 232 // TODO(vtl): Is there a better error code for this? |
232 errno_setter.Set(EIO); | 233 errno_setter.Set(EIO); |
233 return false; | 234 return false; |
234 } | 235 } |
235 | 236 |
236 // Zero everything first. | 237 // Zero everything first. |
237 memset(buf, 0, sizeof(*buf)); | 238 memset(buf, 0, sizeof(*buf)); |
238 // Leave |st_dev| zero. | 239 // Leave |st_dev| zero. |
239 // Leave |st_ino| zero. | 240 // Leave |st_ino| zero. |
240 buf->st_mode = MOJIO_S_IRWXU; | 241 buf->st_mode = MOJIO_S_IRWXU; |
241 switch (file_info->type) { | 242 switch (file_info->type) { |
242 case mojo::files::FILE_TYPE_UNKNOWN: | 243 case mojo::files::FileType::UNKNOWN: |
243 break; | 244 break; |
244 case mojo::files::FILE_TYPE_REGULAR_FILE: | 245 case mojo::files::FileType::REGULAR_FILE: |
245 buf->st_mode |= MOJIO_S_IFREG; | 246 buf->st_mode |= MOJIO_S_IFREG; |
246 break; | 247 break; |
247 case mojo::files::FILE_TYPE_DIRECTORY: | 248 case mojo::files::FileType::DIRECTORY: |
248 buf->st_mode |= MOJIO_S_IFDIR; | 249 buf->st_mode |= MOJIO_S_IFDIR; |
249 break; | 250 break; |
250 default: | 251 default: |
251 MOJO_LOG(WARNING) << "Unknown FileType: " << file_info->type; | 252 MOJO_LOG(WARNING) << "Unknown FileType: " << file_info->type; |
252 break; | 253 break; |
253 } | 254 } |
254 // The most likely value (it'll be wrong if |file_| has been deleted). | 255 // The most likely value (it'll be wrong if |file_| has been deleted). |
255 // TODO(vtl): Ponder this. Maybe |FileInformation| should have nlink? | 256 // TODO(vtl): Ponder this. Maybe |FileInformation| should have nlink? |
256 buf->st_nlink = 1; | 257 buf->st_nlink = 1; |
257 // Leave |st_uid| zero. | 258 // Leave |st_uid| zero. |
(...skipping 16 matching lines...) Expand all Loading... |
274 // Make up a value based on size. (Note: Despite the above "block size", this | 275 // Make up a value based on size. (Note: Despite the above "block size", this |
275 // block count is for 512-byte blocks!) | 276 // block count is for 512-byte blocks!) |
276 if (file_info->size > 0) | 277 if (file_info->size > 0) |
277 buf->st_blocks = (static_cast<mojio_blkcnt_t>(file_info->size) + 511) / 512; | 278 buf->st_blocks = (static_cast<mojio_blkcnt_t>(file_info->size) + 511) / 512; |
278 // Else leave |st_blocks| zero. | 279 // Else leave |st_blocks| zero. |
279 | 280 |
280 return true; | 281 return true; |
281 } | 282 } |
282 | 283 |
283 } // namespace mojio | 284 } // namespace mojio |
OLD | NEW |