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 15 matching lines...) Expand all Loading... |
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_.WaitForIncomingMethodCall()) | 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_.WaitForIncomingMethodCall()) { | 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_.WaitForIncomingMethodCall()) | 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_.WaitForIncomingMethodCall()) { | 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 |
103 if (position < 0) { | 103 if (position < 0) { |
104 // Service misbehaved. | 104 // Service misbehaved. |
105 MOJO_LOG(ERROR) << "Write() wrote more than requested"; | 105 MOJO_LOG(ERROR) << "Write() wrote more than requested"; |
106 // TODO(vtl): Is there a better error code for this? | 106 // TODO(vtl): Is there a better error code for this? |
(...skipping 27 matching lines...) Expand all Loading... |
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, mojo::files::WHENCE_FROM_CURRENT, |
143 Capture(&error, &bytes_read)); | 143 Capture(&error, &bytes_read)); |
144 if (!file_.WaitForIncomingMethodCall()) { | 144 if (!file_.WaitForIncomingResponse()) { |
145 errno_setter.Set(ESTALE); | 145 errno_setter.Set(ESTALE); |
146 return -1; | 146 return -1; |
147 } | 147 } |
148 if (!errno_setter.Set(ErrorToErrno(error))) | 148 if (!errno_setter.Set(ErrorToErrno(error))) |
149 return -1; | 149 return -1; |
150 if (bytes_read.size() > count) { | 150 if (bytes_read.size() > count) { |
151 // Service misbehaved. | 151 // Service misbehaved. |
152 MOJO_LOG(ERROR) << "Read() read more than requested"; | 152 MOJO_LOG(ERROR) << "Read() read more than requested"; |
153 // TODO(vtl): Is there a better error code for this? | 153 // TODO(vtl): Is there a better error code for this? |
154 errno_setter.Set(EIO); | 154 errno_setter.Set(EIO); |
(...skipping 26 matching lines...) Expand all Loading... |
181 | 181 |
182 // TODO(vtl): Is there a more natural (or efficient) way to do this? | 182 // TODO(vtl): Is there a more natural (or efficient) way to do this? |
183 mojo::Array<uint8_t> bytes_to_write(count); | 183 mojo::Array<uint8_t> bytes_to_write(count); |
184 if (count > 0) | 184 if (count > 0) |
185 memcpy(&bytes_to_write[0], buf, count); | 185 memcpy(&bytes_to_write[0], buf, count); |
186 | 186 |
187 mojo::files::Error error = mojo::files::ERROR_INTERNAL; | 187 mojo::files::Error error = mojo::files::ERROR_INTERNAL; |
188 uint32_t num_bytes_written = 0; | 188 uint32_t num_bytes_written = 0; |
189 file_->Write(bytes_to_write.Pass(), 0, mojo::files::WHENCE_FROM_CURRENT, | 189 file_->Write(bytes_to_write.Pass(), 0, mojo::files::WHENCE_FROM_CURRENT, |
190 Capture(&error, &num_bytes_written)); | 190 Capture(&error, &num_bytes_written)); |
191 if (!file_.WaitForIncomingMethodCall()) { | 191 if (!file_.WaitForIncomingResponse()) { |
192 errno_setter.Set(ESTALE); | 192 errno_setter.Set(ESTALE); |
193 return -1; | 193 return -1; |
194 } | 194 } |
195 if (!errno_setter.Set(ErrorToErrno(error))) | 195 if (!errno_setter.Set(ErrorToErrno(error))) |
196 return -1; | 196 return -1; |
197 | 197 |
198 if (num_bytes_written > count) { | 198 if (num_bytes_written > count) { |
199 // Service misbehaved. | 199 // Service misbehaved. |
200 MOJO_LOG(ERROR) << "Write() wrote than requested"; | 200 MOJO_LOG(ERROR) << "Write() wrote than requested"; |
201 // TODO(vtl): Is there a better error code for this? | 201 // TODO(vtl): Is there a better error code for this? |
202 errno_setter.Set(EIO); | 202 errno_setter.Set(EIO); |
203 return -1; | 203 return -1; |
204 } | 204 } |
205 | 205 |
206 return static_cast<mojio_ssize_t>(num_bytes_written); | 206 return static_cast<mojio_ssize_t>(num_bytes_written); |
207 } | 207 } |
208 | 208 |
209 bool FileFDImpl::Fstat(struct mojio_stat* buf) { | 209 bool FileFDImpl::Fstat(struct mojio_stat* buf) { |
210 ErrnoImpl::Setter errno_setter(errno_impl()); | 210 ErrnoImpl::Setter errno_setter(errno_impl()); |
211 MOJO_DCHECK(file_); | 211 MOJO_DCHECK(file_); |
212 | 212 |
213 if (!buf) { | 213 if (!buf) { |
214 errno_setter.Set(EFAULT); | 214 errno_setter.Set(EFAULT); |
215 return false; | 215 return false; |
216 } | 216 } |
217 | 217 |
218 mojo::files::FileInformationPtr file_info; | 218 mojo::files::FileInformationPtr file_info; |
219 mojo::files::Error error = mojo::files::ERROR_INTERNAL; | 219 mojo::files::Error error = mojo::files::ERROR_INTERNAL; |
220 file_->Stat(Capture(&error, &file_info)); | 220 file_->Stat(Capture(&error, &file_info)); |
221 if (!file_.WaitForIncomingMethodCall()) { | 221 if (!file_.WaitForIncomingResponse()) { |
222 errno_setter.Set(ESTALE); | 222 errno_setter.Set(ESTALE); |
223 return false; | 223 return false; |
224 } | 224 } |
225 if (!errno_setter.Set(ErrorToErrno(error))) | 225 if (!errno_setter.Set(ErrorToErrno(error))) |
226 return false; | 226 return false; |
227 | 227 |
228 if (!file_info) { | 228 if (!file_info) { |
229 // Service misbehaved. | 229 // Service misbehaved. |
230 MOJO_LOG(ERROR) << "Stat() didn't provide FileInformation"; | 230 MOJO_LOG(ERROR) << "Stat() didn't provide FileInformation"; |
231 // TODO(vtl): Is there a better error code for this? | 231 // TODO(vtl): Is there a better error code for this? |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 // Make up a value based on size. (Note: Despite the above "block size", this | 274 // Make up a value based on size. (Note: Despite the above "block size", this |
275 // block count is for 512-byte blocks!) | 275 // block count is for 512-byte blocks!) |
276 if (file_info->size > 0) | 276 if (file_info->size > 0) |
277 buf->st_blocks = (static_cast<mojio_blkcnt_t>(file_info->size) + 511) / 512; | 277 buf->st_blocks = (static_cast<mojio_blkcnt_t>(file_info->size) + 511) / 512; |
278 // Else leave |st_blocks| zero. | 278 // Else leave |st_blocks| zero. |
279 | 279 |
280 return true; | 280 return true; |
281 } | 281 } |
282 | 282 |
283 } // namespace mojio | 283 } // namespace mojio |
OLD | NEW |