OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/files/file.h" | 5 #include "base/files/file.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 <unistd.h> | 10 #include <unistd.h> |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 // wrap them in order to minimize the number of #ifdef's in this file. | 46 // wrap them in order to minimize the number of #ifdef's in this file. |
47 #if !defined(OS_NACL) | 47 #if !defined(OS_NACL) |
48 static bool IsOpenAppend(PlatformFile file) { | 48 static bool IsOpenAppend(PlatformFile file) { |
49 return (fcntl(file, F_GETFL) & O_APPEND) != 0; | 49 return (fcntl(file, F_GETFL) & O_APPEND) != 0; |
50 } | 50 } |
51 | 51 |
52 static int CallFtruncate(PlatformFile file, int64 length) { | 52 static int CallFtruncate(PlatformFile file, int64 length) { |
53 return HANDLE_EINTR(ftruncate(file, length)); | 53 return HANDLE_EINTR(ftruncate(file, length)); |
54 } | 54 } |
55 | 55 |
56 static int CallFsync(PlatformFile file) { | |
57 return HANDLE_EINTR(fsync(file)); | |
58 } | |
59 | |
60 static int CallFutimes(PlatformFile file, const struct timeval times[2]) { | 56 static int CallFutimes(PlatformFile file, const struct timeval times[2]) { |
61 #ifdef __USE_XOPEN2K8 | 57 #ifdef __USE_XOPEN2K8 |
62 // futimens should be available, but futimes might not be | 58 // futimens should be available, but futimes might not be |
63 // http://pubs.opengroup.org/onlinepubs/9699919799/ | 59 // http://pubs.opengroup.org/onlinepubs/9699919799/ |
64 | 60 |
65 timespec ts_times[2]; | 61 timespec ts_times[2]; |
66 ts_times[0].tv_sec = times[0].tv_sec; | 62 ts_times[0].tv_sec = times[0].tv_sec; |
67 ts_times[0].tv_nsec = times[0].tv_usec * 1000; | 63 ts_times[0].tv_nsec = times[0].tv_usec * 1000; |
68 ts_times[1].tv_sec = times[1].tv_sec; | 64 ts_times[1].tv_sec = times[1].tv_sec; |
69 ts_times[1].tv_nsec = times[1].tv_usec * 1000; | 65 ts_times[1].tv_nsec = times[1].tv_usec * 1000; |
(...skipping 21 matching lines...) Expand all Loading... |
91 // standard and always appends if the file is opened with O_APPEND, just | 87 // standard and always appends if the file is opened with O_APPEND, just |
92 // return false here. | 88 // return false here. |
93 return false; | 89 return false; |
94 } | 90 } |
95 | 91 |
96 static int CallFtruncate(PlatformFile file, int64 length) { | 92 static int CallFtruncate(PlatformFile file, int64 length) { |
97 NOTIMPLEMENTED(); // NaCl doesn't implement ftruncate. | 93 NOTIMPLEMENTED(); // NaCl doesn't implement ftruncate. |
98 return 0; | 94 return 0; |
99 } | 95 } |
100 | 96 |
101 static int CallFsync(PlatformFile file) { | |
102 NOTIMPLEMENTED(); // NaCl doesn't implement fsync. | |
103 return 0; | |
104 } | |
105 | |
106 static int CallFutimes(PlatformFile file, const struct timeval times[2]) { | 97 static int CallFutimes(PlatformFile file, const struct timeval times[2]) { |
107 NOTIMPLEMENTED(); // NaCl doesn't implement futimes. | 98 NOTIMPLEMENTED(); // NaCl doesn't implement futimes. |
108 return 0; | 99 return 0; |
109 } | 100 } |
110 | 101 |
111 static File::Error CallFctnlFlock(PlatformFile file, bool do_lock) { | 102 static File::Error CallFctnlFlock(PlatformFile file, bool do_lock) { |
112 NOTIMPLEMENTED(); // NaCl doesn't implement flock struct. | 103 NOTIMPLEMENTED(); // NaCl doesn't implement flock struct. |
113 return File::FILE_ERROR_INVALID_OPERATION; | 104 return File::FILE_ERROR_INVALID_OPERATION; |
114 } | 105 } |
115 #endif // defined(OS_NACL) | 106 #endif // defined(OS_NACL) |
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
423 | 414 |
424 bool File::SetLength(int64 length) { | 415 bool File::SetLength(int64 length) { |
425 base::ThreadRestrictions::AssertIOAllowed(); | 416 base::ThreadRestrictions::AssertIOAllowed(); |
426 DCHECK(IsValid()); | 417 DCHECK(IsValid()); |
427 return !CallFtruncate(file_.get(), length); | 418 return !CallFtruncate(file_.get(), length); |
428 } | 419 } |
429 | 420 |
430 bool File::Flush() { | 421 bool File::Flush() { |
431 base::ThreadRestrictions::AssertIOAllowed(); | 422 base::ThreadRestrictions::AssertIOAllowed(); |
432 DCHECK(IsValid()); | 423 DCHECK(IsValid()); |
433 return !CallFsync(file_.get()); | 424 #if defined(OS_NACL) |
| 425 NOTIMPLEMENTED(); // NaCl doesn't implement fsync. |
| 426 return true; |
| 427 #elif defined(OS_LINUX) || defined(OS_ANDROID) |
| 428 return !HANDLE_EINTR(fdatasync(file_.get())); |
| 429 #else |
| 430 return !HANDLE_EINTR(fsync(file_.get())); |
| 431 #endif |
434 } | 432 } |
435 | 433 |
436 bool File::SetTimes(Time last_access_time, Time last_modified_time) { | 434 bool File::SetTimes(Time last_access_time, Time last_modified_time) { |
437 base::ThreadRestrictions::AssertIOAllowed(); | 435 base::ThreadRestrictions::AssertIOAllowed(); |
438 DCHECK(IsValid()); | 436 DCHECK(IsValid()); |
439 | 437 |
440 timeval times[2]; | 438 timeval times[2]; |
441 times[0] = last_access_time.ToTimeVal(); | 439 times[0] = last_access_time.ToTimeVal(); |
442 times[1] = last_modified_time.ToTimeVal(); | 440 times[1] = last_modified_time.ToTimeVal(); |
443 | 441 |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
557 } | 555 } |
558 | 556 |
559 void File::SetPlatformFile(PlatformFile file) { | 557 void File::SetPlatformFile(PlatformFile file) { |
560 CHECK(!file_.is_valid()); | 558 CHECK(!file_.is_valid()); |
561 file_.reset(file); | 559 file_.reset(file); |
562 if (file_.is_valid()) | 560 if (file_.is_valid()) |
563 ProtectFileDescriptor(GetPlatformFile()); | 561 ProtectFileDescriptor(GetPlatformFile()); |
564 } | 562 } |
565 | 563 |
566 } // namespace base | 564 } // namespace base |
OLD | NEW |