Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1051)

Side by Side Diff: services/files/file_impl.cc

Issue 963093004: Files: Add basic implementation of Directory and some basic tests. (Closed) Base URL: https://github.com/domokit/mojo.git@file_man
Patch Set: oops Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « services/files/directory_impl.cc ('k') | services/files/files_apptest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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>
11 #include <time.h>
12 #include <unistd.h> 11 #include <unistd.h>
13 12
14 #include <limits> 13 #include <limits>
15 14
16 #include "base/files/scoped_file.h" 15 #include "base/files/scoped_file.h"
17 #include "base/logging.h" 16 #include "base/logging.h"
18 #include "base/posix/eintr_wrapper.h" 17 #include "base/posix/eintr_wrapper.h"
19 #include "services/files/futimens.h" 18 #include "services/files/shared_impl.h"
20 #include "services/files/util.h" 19 #include "services/files/util.h"
21 20
22 static_assert(sizeof(off_t) <= sizeof(int64_t), "off_t too big"); 21 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"); 22 static_assert(sizeof(size_t) >= sizeof(uint32_t), "size_t too small");
24 23
25 namespace mojo { 24 namespace mojo {
26 namespace files { 25 namespace files {
27 26
28 const size_t kMaxReadSize = 1 * 1024 * 1024; // 1 MB. 27 const size_t kMaxReadSize = 1 * 1024 * 1024; // 1 MB.
29 28
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 } 236 }
238 237
239 callback.Run(ERROR_OK, static_cast<int64>(position)); 238 callback.Run(ERROR_OK, static_cast<int64>(position));
240 } 239 }
241 240
242 void FileImpl::Stat(const Callback<void(Error, FileInformationPtr)>& callback) { 241 void FileImpl::Stat(const Callback<void(Error, FileInformationPtr)>& callback) {
243 if (!file_fd_.is_valid()) { 242 if (!file_fd_.is_valid()) {
244 callback.Run(ERROR_CLOSED, nullptr); 243 callback.Run(ERROR_CLOSED, nullptr);
245 return; 244 return;
246 } 245 }
247 246 StatFD(file_fd_.get(), callback);
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 } 247 }
272 248
273 void FileImpl::Truncate(int64_t size, const Callback<void(Error)>& callback) { 249 void FileImpl::Truncate(int64_t size, const Callback<void(Error)>& callback) {
274 if (!file_fd_.is_valid()) { 250 if (!file_fd_.is_valid()) {
275 callback.Run(ERROR_CLOSED); 251 callback.Run(ERROR_CLOSED);
276 return; 252 return;
277 } 253 }
278 if (size < 0) { 254 if (size < 0) {
279 callback.Run(ERROR_INVALID_ARGUMENT); 255 callback.Run(ERROR_INVALID_ARGUMENT);
280 return; 256 return;
(...skipping 11 matching lines...) Expand all
292 callback.Run(ERROR_OK); 268 callback.Run(ERROR_OK);
293 } 269 }
294 270
295 void FileImpl::Touch(TimespecOrNowPtr atime, 271 void FileImpl::Touch(TimespecOrNowPtr atime,
296 TimespecOrNowPtr mtime, 272 TimespecOrNowPtr mtime,
297 const Callback<void(Error)>& callback) { 273 const Callback<void(Error)>& callback) {
298 if (!file_fd_.is_valid()) { 274 if (!file_fd_.is_valid()) {
299 callback.Run(ERROR_CLOSED); 275 callback.Run(ERROR_CLOSED);
300 return; 276 return;
301 } 277 }
302 278 TouchFD(file_fd_.get(), atime.Pass(), mtime.Pass(), callback);
303 struct timespec times[2];
304 if (Error error = TimespecOrNowToStandardTimespec(atime.get(), &times[0])) {
305 callback.Run(error);
306 return;
307 }
308 if (Error error = TimespecOrNowToStandardTimespec(mtime.get(), &times[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 } 279 }
320 280
321 void FileImpl::Dup(InterfaceRequest<File> file, 281 void FileImpl::Dup(InterfaceRequest<File> file,
322 const Callback<void(Error)>& callback) { 282 const Callback<void(Error)>& callback) {
323 if (!file_fd_.is_valid()) { 283 if (!file_fd_.is_valid()) {
324 callback.Run(ERROR_CLOSED); 284 callback.Run(ERROR_CLOSED);
325 return; 285 return;
326 } 286 }
327 287
328 base::ScopedFD file_fd(dup(file_fd_.get())); 288 base::ScopedFD file_fd(dup(file_fd_.get()));
(...skipping 26 matching lines...) Expand all
355 return; 315 return;
356 } 316 }
357 317
358 // TODO(vtl): FIXME soon 318 // TODO(vtl): FIXME soon
359 NOTIMPLEMENTED(); 319 NOTIMPLEMENTED();
360 callback.Run(ERROR_UNIMPLEMENTED, ScopedSharedBufferHandle()); 320 callback.Run(ERROR_UNIMPLEMENTED, ScopedSharedBufferHandle());
361 } 321 }
362 322
363 } // namespace files 323 } // namespace files
364 } // namespace mojo 324 } // namespace mojo
OLDNEW
« no previous file with comments | « services/files/directory_impl.cc ('k') | services/files/files_apptest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698