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 11 matching lines...) Expand all Loading... |
22 namespace base { | 22 namespace base { |
23 | 23 |
24 // Make sure our Whence mappings match the system headers. | 24 // Make sure our Whence mappings match the system headers. |
25 static_assert(File::FROM_BEGIN == SEEK_SET && File::FROM_CURRENT == SEEK_CUR && | 25 static_assert(File::FROM_BEGIN == SEEK_SET && File::FROM_CURRENT == SEEK_CUR && |
26 File::FROM_END == SEEK_END, | 26 File::FROM_END == SEEK_END, |
27 "whence mapping must match the system headers"); | 27 "whence mapping must match the system headers"); |
28 | 28 |
29 namespace { | 29 namespace { |
30 | 30 |
31 #if defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL) | 31 #if defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL) |
32 static int CallFstat(int fd, stat_wrapper_t *sb) { | 32 int CallFstat(int fd, stat_wrapper_t *sb) { |
33 ThreadRestrictions::AssertIOAllowed(); | 33 ThreadRestrictions::AssertIOAllowed(); |
34 return fstat(fd, sb); | 34 return fstat(fd, sb); |
35 } | 35 } |
36 #else | 36 #else |
37 static int CallFstat(int fd, stat_wrapper_t *sb) { | 37 int CallFstat(int fd, stat_wrapper_t *sb) { |
38 ThreadRestrictions::AssertIOAllowed(); | 38 ThreadRestrictions::AssertIOAllowed(); |
39 return fstat64(fd, sb); | 39 return fstat64(fd, sb); |
40 } | 40 } |
41 #endif | 41 #endif |
42 | 42 |
43 // NaCl doesn't provide the following system calls, so either simulate them or | 43 // NaCl doesn't provide the following system calls, so either simulate them or |
44 // wrap them in order to minimize the number of #ifdef's in this file. | 44 // wrap them in order to minimize the number of #ifdef's in this file. |
45 #if !defined(OS_NACL) | 45 #if !defined(OS_NACL) |
46 static bool IsOpenAppend(PlatformFile file) { | 46 bool IsOpenAppend(PlatformFile file) { |
47 return (fcntl(file, F_GETFL) & O_APPEND) != 0; | 47 return (fcntl(file, F_GETFL) & O_APPEND) != 0; |
48 } | 48 } |
49 | 49 |
50 static int CallFtruncate(PlatformFile file, int64 length) { | 50 int CallFtruncate(PlatformFile file, int64 length) { |
51 return HANDLE_EINTR(ftruncate(file, length)); | 51 return HANDLE_EINTR(ftruncate(file, length)); |
52 } | 52 } |
53 | 53 |
54 static int CallFutimes(PlatformFile file, const struct timeval times[2]) { | 54 int CallFutimes(PlatformFile file, const struct timeval times[2]) { |
55 #ifdef __USE_XOPEN2K8 | 55 #ifdef __USE_XOPEN2K8 |
56 // futimens should be available, but futimes might not be | 56 // futimens should be available, but futimes might not be |
57 // http://pubs.opengroup.org/onlinepubs/9699919799/ | 57 // http://pubs.opengroup.org/onlinepubs/9699919799/ |
58 | 58 |
59 timespec ts_times[2]; | 59 timespec ts_times[2]; |
60 ts_times[0].tv_sec = times[0].tv_sec; | 60 ts_times[0].tv_sec = times[0].tv_sec; |
61 ts_times[0].tv_nsec = times[0].tv_usec * 1000; | 61 ts_times[0].tv_nsec = times[0].tv_usec * 1000; |
62 ts_times[1].tv_sec = times[1].tv_sec; | 62 ts_times[1].tv_sec = times[1].tv_sec; |
63 ts_times[1].tv_nsec = times[1].tv_usec * 1000; | 63 ts_times[1].tv_nsec = times[1].tv_usec * 1000; |
64 | 64 |
65 return futimens(file, ts_times); | 65 return futimens(file, ts_times); |
66 #else | 66 #else |
67 return futimes(file, times); | 67 return futimes(file, times); |
68 #endif | 68 #endif |
69 } | 69 } |
70 | 70 |
71 static File::Error CallFctnlFlock(PlatformFile file, bool do_lock) { | 71 File::Error CallFcntlFlock(PlatformFile file, bool do_lock) { |
72 struct flock lock; | 72 struct flock lock; |
73 lock.l_type = F_WRLCK; | 73 lock.l_type = F_WRLCK; |
74 lock.l_whence = SEEK_SET; | 74 lock.l_whence = SEEK_SET; |
75 lock.l_start = 0; | 75 lock.l_start = 0; |
76 lock.l_len = 0; // Lock entire file. | 76 lock.l_len = 0; // Lock entire file. |
77 if (HANDLE_EINTR(fcntl(file, do_lock ? F_SETLK : F_UNLCK, &lock)) == -1) | 77 if (HANDLE_EINTR(fcntl(file, do_lock ? F_SETLK : F_UNLCK, &lock)) == -1) |
78 return File::OSErrorToFileError(errno); | 78 return File::OSErrorToFileError(errno); |
79 return File::FILE_OK; | 79 return File::FILE_OK; |
80 } | 80 } |
81 #else // defined(OS_NACL) | 81 #else // defined(OS_NACL) |
82 | 82 |
83 static bool IsOpenAppend(PlatformFile file) { | 83 bool IsOpenAppend(PlatformFile file) { |
84 // NaCl doesn't implement fcntl. Since NaCl's write conforms to the POSIX | 84 // NaCl doesn't implement fcntl. Since NaCl's write conforms to the POSIX |
85 // standard and always appends if the file is opened with O_APPEND, just | 85 // standard and always appends if the file is opened with O_APPEND, just |
86 // return false here. | 86 // return false here. |
87 return false; | 87 return false; |
88 } | 88 } |
89 | 89 |
90 static int CallFtruncate(PlatformFile file, int64 length) { | 90 int CallFtruncate(PlatformFile file, int64 length) { |
91 NOTIMPLEMENTED(); // NaCl doesn't implement ftruncate. | 91 NOTIMPLEMENTED(); // NaCl doesn't implement ftruncate. |
92 return 0; | 92 return 0; |
93 } | 93 } |
94 | 94 |
95 static int CallFutimes(PlatformFile file, const struct timeval times[2]) { | 95 int CallFutimes(PlatformFile file, const struct timeval times[2]) { |
96 NOTIMPLEMENTED(); // NaCl doesn't implement futimes. | 96 NOTIMPLEMENTED(); // NaCl doesn't implement futimes. |
97 return 0; | 97 return 0; |
98 } | 98 } |
99 | 99 |
100 static File::Error CallFctnlFlock(PlatformFile file, bool do_lock) { | 100 File::Error CallFcntlFlock(PlatformFile file, bool do_lock) { |
101 NOTIMPLEMENTED(); // NaCl doesn't implement flock struct. | 101 NOTIMPLEMENTED(); // NaCl doesn't implement flock struct. |
102 return File::FILE_ERROR_INVALID_OPERATION; | 102 return File::FILE_ERROR_INVALID_OPERATION; |
103 } | 103 } |
104 #endif // defined(OS_NACL) | 104 #endif // defined(OS_NACL) |
105 | 105 |
106 } // namespace | 106 } // namespace |
107 | 107 |
108 void File::Info::FromStat(const stat_wrapper_t& stat_info) { | 108 void File::Info::FromStat(const stat_wrapper_t& stat_info) { |
109 is_directory = S_ISDIR(stat_info.st_mode); | 109 is_directory = S_ISDIR(stat_info.st_mode); |
110 is_symbolic_link = S_ISLNK(stat_info.st_mode); | 110 is_symbolic_link = S_ISLNK(stat_info.st_mode); |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
353 stat_wrapper_t file_info; | 353 stat_wrapper_t file_info; |
354 if (CallFstat(file_.get(), &file_info)) | 354 if (CallFstat(file_.get(), &file_info)) |
355 return false; | 355 return false; |
356 | 356 |
357 info->FromStat(file_info); | 357 info->FromStat(file_info); |
358 return true; | 358 return true; |
359 } | 359 } |
360 | 360 |
361 File::Error File::Lock() { | 361 File::Error File::Lock() { |
362 SCOPED_FILE_TRACE("Lock"); | 362 SCOPED_FILE_TRACE("Lock"); |
363 return CallFctnlFlock(file_.get(), true); | 363 return CallFcntlFlock(file_.get(), true); |
364 } | 364 } |
365 | 365 |
366 File::Error File::Unlock() { | 366 File::Error File::Unlock() { |
367 SCOPED_FILE_TRACE("Unlock"); | 367 SCOPED_FILE_TRACE("Unlock"); |
368 return CallFctnlFlock(file_.get(), false); | 368 return CallFcntlFlock(file_.get(), false); |
369 } | 369 } |
370 | 370 |
371 File File::Duplicate() { | 371 File File::Duplicate() { |
372 if (!IsValid()) | 372 if (!IsValid()) |
373 return File(); | 373 return File(); |
374 | 374 |
375 SCOPED_FILE_TRACE("Duplicate"); | 375 SCOPED_FILE_TRACE("Duplicate"); |
376 | 376 |
377 PlatformFile other_fd = dup(GetPlatformFile()); | 377 PlatformFile other_fd = dup(GetPlatformFile()); |
378 if (other_fd == -1) | 378 if (other_fd == -1) |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
522 return !HANDLE_EINTR(fsync(file_.get())); | 522 return !HANDLE_EINTR(fsync(file_.get())); |
523 #endif | 523 #endif |
524 } | 524 } |
525 | 525 |
526 void File::SetPlatformFile(PlatformFile file) { | 526 void File::SetPlatformFile(PlatformFile file) { |
527 DCHECK(!file_.is_valid()); | 527 DCHECK(!file_.is_valid()); |
528 file_.reset(file); | 528 file_.reset(file); |
529 } | 529 } |
530 | 530 |
531 } // namespace base | 531 } // namespace base |
OLD | NEW |