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
3 // found in the LICENSE file.
5 #include "base/files/file.h"
12 #include "base/files/file_path.h"
13 #include "base/files/file_posix_hooks_internal.h"
14 #include "base/logging.h"
15 #include "base/metrics/sparse_histogram.h"
16 #include "base/posix/eintr_wrapper.h"
17 #include "base/strings/utf_string_conversions.h"
18 #include "base/threading/thread_restrictions.h"
20 #if defined(OS_ANDROID)
21 #include "base/os_compat_android.h"
26 // Make sure our Whence mappings match the system headers.
27 COMPILE_ASSERT(File::FROM_BEGIN
== SEEK_SET
&&
28 File::FROM_CURRENT
== SEEK_CUR
&&
29 File::FROM_END
== SEEK_END
, whence_matches_system
);
33 #if defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL)
34 static int CallFstat(int fd
, stat_wrapper_t
*sb
) {
35 base::ThreadRestrictions::AssertIOAllowed();
39 static int CallFstat(int fd
, stat_wrapper_t
*sb
) {
40 base::ThreadRestrictions::AssertIOAllowed();
41 return fstat64(fd
, sb
);
45 // NaCl doesn't provide the following system calls, so either simulate them or
46 // wrap them in order to minimize the number of #ifdef's in this file.
48 static bool IsOpenAppend(PlatformFile file
) {
49 return (fcntl(file
, F_GETFL
) & O_APPEND
) != 0;
52 static int CallFtruncate(PlatformFile file
, int64 length
) {
53 return HANDLE_EINTR(ftruncate(file
, length
));
56 static int CallFutimes(PlatformFile file
, const struct timeval times
[2]) {
58 // futimens should be available, but futimes might not be
59 // http://pubs.opengroup.org/onlinepubs/9699919799/
62 ts_times
[0].tv_sec
= times
[0].tv_sec
;
63 ts_times
[0].tv_nsec
= times
[0].tv_usec
* 1000;
64 ts_times
[1].tv_sec
= times
[1].tv_sec
;
65 ts_times
[1].tv_nsec
= times
[1].tv_usec
* 1000;
67 return futimens(file
, ts_times
);
69 return futimes(file
, times
);
73 static File::Error
CallFctnlFlock(PlatformFile file
, bool do_lock
) {
75 lock
.l_type
= F_WRLCK
;
76 lock
.l_whence
= SEEK_SET
;
78 lock
.l_len
= 0; // Lock entire file.
79 if (HANDLE_EINTR(fcntl(file
, do_lock
? F_SETLK
: F_UNLCK
, &lock
)) == -1)
80 return File::OSErrorToFileError(errno
);
83 #else // defined(OS_NACL)
85 static bool IsOpenAppend(PlatformFile file
) {
86 // NaCl doesn't implement fcntl. Since NaCl's write conforms to the POSIX
87 // standard and always appends if the file is opened with O_APPEND, just
92 static int CallFtruncate(PlatformFile file
, int64 length
) {
93 NOTIMPLEMENTED(); // NaCl doesn't implement ftruncate.
97 static int CallFutimes(PlatformFile file
, const struct timeval times
[2]) {
98 NOTIMPLEMENTED(); // NaCl doesn't implement futimes.
102 static File::Error
CallFctnlFlock(PlatformFile file
, bool do_lock
) {
103 NOTIMPLEMENTED(); // NaCl doesn't implement flock struct.
104 return File::FILE_ERROR_INVALID_OPERATION
;
106 #endif // defined(OS_NACL)
110 void File::Info::FromStat(const stat_wrapper_t
& stat_info
) {
111 is_directory
= S_ISDIR(stat_info
.st_mode
);
112 is_symbolic_link
= S_ISLNK(stat_info
.st_mode
);
113 size
= stat_info
.st_size
;
115 #if defined(OS_LINUX)
116 time_t last_modified_sec
= stat_info
.st_mtim
.tv_sec
;
117 int64 last_modified_nsec
= stat_info
.st_mtim
.tv_nsec
;
118 time_t last_accessed_sec
= stat_info
.st_atim
.tv_sec
;
119 int64 last_accessed_nsec
= stat_info
.st_atim
.tv_nsec
;
120 time_t creation_time_sec
= stat_info
.st_ctim
.tv_sec
;
121 int64 creation_time_nsec
= stat_info
.st_ctim
.tv_nsec
;
122 #elif defined(OS_ANDROID)
123 time_t last_modified_sec
= stat_info
.st_mtime
;
124 int64 last_modified_nsec
= stat_info
.st_mtime_nsec
;
125 time_t last_accessed_sec
= stat_info
.st_atime
;
126 int64 last_accessed_nsec
= stat_info
.st_atime_nsec
;
127 time_t creation_time_sec
= stat_info
.st_ctime
;
128 int64 creation_time_nsec
= stat_info
.st_ctime_nsec
;
129 #elif defined(OS_MACOSX) || defined(OS_IOS) || defined(OS_BSD)
130 time_t last_modified_sec
= stat_info
.st_mtimespec
.tv_sec
;
131 int64 last_modified_nsec
= stat_info
.st_mtimespec
.tv_nsec
;
132 time_t last_accessed_sec
= stat_info
.st_atimespec
.tv_sec
;
133 int64 last_accessed_nsec
= stat_info
.st_atimespec
.tv_nsec
;
134 time_t creation_time_sec
= stat_info
.st_ctimespec
.tv_sec
;
135 int64 creation_time_nsec
= stat_info
.st_ctimespec
.tv_nsec
;
137 time_t last_modified_sec
= stat_info
.st_mtime
;
138 int64 last_modified_nsec
= 0;
139 time_t last_accessed_sec
= stat_info
.st_atime
;
140 int64 last_accessed_nsec
= 0;
141 time_t creation_time_sec
= stat_info
.st_ctime
;
142 int64 creation_time_nsec
= 0;
146 Time::FromTimeT(last_modified_sec
) +
147 TimeDelta::FromMicroseconds(last_modified_nsec
/
148 Time::kNanosecondsPerMicrosecond
);
151 Time::FromTimeT(last_accessed_sec
) +
152 TimeDelta::FromMicroseconds(last_accessed_nsec
/
153 Time::kNanosecondsPerMicrosecond
);
156 Time::FromTimeT(creation_time_sec
) +
157 TimeDelta::FromMicroseconds(creation_time_nsec
/
158 Time::kNanosecondsPerMicrosecond
);
161 // Default implementations of Protect/Unprotect hooks defined as weak symbols
163 void ProtectFileDescriptor(int fd
) {
166 void UnprotectFileDescriptor(int fd
) {
169 // NaCl doesn't implement system calls to open files directly.
170 #if !defined(OS_NACL)
171 // TODO(erikkay): does it make sense to support FLAG_EXCLUSIVE_* here?
172 void File::InitializeUnsafe(const FilePath
& name
, uint32 flags
) {
173 base::ThreadRestrictions::AssertIOAllowed();
177 if (flags
& FLAG_CREATE
)
178 open_flags
= O_CREAT
| O_EXCL
;
182 if (flags
& FLAG_CREATE_ALWAYS
) {
184 DCHECK(flags
& FLAG_WRITE
);
185 open_flags
= O_CREAT
| O_TRUNC
;
188 if (flags
& FLAG_OPEN_TRUNCATED
) {
190 DCHECK(flags
& FLAG_WRITE
);
191 open_flags
= O_TRUNC
;
194 if (!open_flags
&& !(flags
& FLAG_OPEN
) && !(flags
& FLAG_OPEN_ALWAYS
)) {
197 error_details_
= FILE_ERROR_FAILED
;
201 if (flags
& FLAG_WRITE
&& flags
& FLAG_READ
) {
202 open_flags
|= O_RDWR
;
203 } else if (flags
& FLAG_WRITE
) {
204 open_flags
|= O_WRONLY
;
205 } else if (!(flags
& FLAG_READ
) &&
206 !(flags
& FLAG_WRITE_ATTRIBUTES
) &&
207 !(flags
& FLAG_APPEND
) &&
208 !(flags
& FLAG_OPEN_ALWAYS
)) {
212 if (flags
& FLAG_TERMINAL_DEVICE
)
213 open_flags
|= O_NOCTTY
| O_NDELAY
;
215 if (flags
& FLAG_APPEND
&& flags
& FLAG_READ
)
216 open_flags
|= O_APPEND
| O_RDWR
;
217 else if (flags
& FLAG_APPEND
)
218 open_flags
|= O_APPEND
| O_WRONLY
;
220 COMPILE_ASSERT(O_RDONLY
== 0, O_RDONLY_must_equal_zero
);
222 int mode
= S_IRUSR
| S_IWUSR
;
223 #if defined(OS_CHROMEOS)
224 mode
|= S_IRGRP
| S_IROTH
;
227 int descriptor
= HANDLE_EINTR(open(name
.value().c_str(), open_flags
, mode
));
229 if (flags
& FLAG_OPEN_ALWAYS
) {
230 if (descriptor
< 0) {
231 open_flags
|= O_CREAT
;
232 if (flags
& FLAG_EXCLUSIVE_READ
|| flags
& FLAG_EXCLUSIVE_WRITE
)
233 open_flags
|= O_EXCL
; // together with O_CREAT implies O_NOFOLLOW
235 descriptor
= HANDLE_EINTR(open(name
.value().c_str(), open_flags
, mode
));
241 if (descriptor
< 0) {
242 error_details_
= File::OSErrorToFileError(errno
);
246 if (flags
& (FLAG_CREATE_ALWAYS
| FLAG_CREATE
))
249 if (flags
& FLAG_DELETE_ON_CLOSE
)
250 unlink(name
.value().c_str());
252 async_
= ((flags
& FLAG_ASYNC
) == FLAG_ASYNC
);
253 error_details_
= FILE_OK
;
254 file_
.reset(descriptor
);
255 ProtectFileDescriptor(descriptor
);
257 #endif // !defined(OS_NACL)
259 bool File::IsValid() const {
260 return file_
.is_valid();
263 PlatformFile
File::GetPlatformFile() const {
267 PlatformFile
File::TakePlatformFile() {
269 UnprotectFileDescriptor(GetPlatformFile());
270 return file_
.release();
277 base::ThreadRestrictions::AssertIOAllowed();
278 UnprotectFileDescriptor(GetPlatformFile());
282 int64
File::Seek(Whence whence
, int64 offset
) {
283 base::ThreadRestrictions::AssertIOAllowed();
286 #if defined(OS_ANDROID)
287 COMPILE_ASSERT(sizeof(int64
) == sizeof(off64_t
), off64_t_64_bit
);
288 return lseek64(file_
.get(), static_cast<off64_t
>(offset
),
289 static_cast<int>(whence
));
291 COMPILE_ASSERT(sizeof(int64
) == sizeof(off_t
), off_t_64_bit
);
292 return lseek(file_
.get(), static_cast<off_t
>(offset
),
293 static_cast<int>(whence
));
297 int File::Read(int64 offset
, char* data
, int size
) {
298 base::ThreadRestrictions::AssertIOAllowed();
306 rv
= HANDLE_EINTR(pread(file_
.get(), data
+ bytes_read
,
307 size
- bytes_read
, offset
+ bytes_read
));
312 } while (bytes_read
< size
);
314 return bytes_read
? bytes_read
: rv
;
317 int File::ReadAtCurrentPos(char* data
, int size
) {
318 base::ThreadRestrictions::AssertIOAllowed();
326 rv
= HANDLE_EINTR(read(file_
.get(), data
+ bytes_read
, size
- bytes_read
));
331 } while (bytes_read
< size
);
333 return bytes_read
? bytes_read
: rv
;
336 int File::ReadNoBestEffort(int64 offset
, char* data
, int size
) {
337 base::ThreadRestrictions::AssertIOAllowed();
340 return HANDLE_EINTR(pread(file_
.get(), data
, size
, offset
));
343 int File::ReadAtCurrentPosNoBestEffort(char* data
, int size
) {
344 base::ThreadRestrictions::AssertIOAllowed();
349 return HANDLE_EINTR(read(file_
.get(), data
, size
));
352 int File::Write(int64 offset
, const char* data
, int size
) {
353 base::ThreadRestrictions::AssertIOAllowed();
355 if (IsOpenAppend(file_
.get()))
356 return WriteAtCurrentPos(data
, size
);
362 int bytes_written
= 0;
365 rv
= HANDLE_EINTR(pwrite(file_
.get(), data
+ bytes_written
,
366 size
- bytes_written
, offset
+ bytes_written
));
371 } while (bytes_written
< size
);
373 return bytes_written
? bytes_written
: rv
;
376 int File::WriteAtCurrentPos(const char* data
, int size
) {
377 base::ThreadRestrictions::AssertIOAllowed();
382 int bytes_written
= 0;
385 rv
= HANDLE_EINTR(write(file_
.get(), data
+ bytes_written
,
386 size
- bytes_written
));
391 } while (bytes_written
< size
);
393 return bytes_written
? bytes_written
: rv
;
396 int File::WriteAtCurrentPosNoBestEffort(const char* data
, int size
) {
397 base::ThreadRestrictions::AssertIOAllowed();
402 return HANDLE_EINTR(write(file_
.get(), data
, size
));
405 int64
File::GetLength() {
408 stat_wrapper_t file_info
;
409 if (CallFstat(file_
.get(), &file_info
))
412 return file_info
.st_size
;
415 bool File::SetLength(int64 length
) {
416 base::ThreadRestrictions::AssertIOAllowed();
418 return !CallFtruncate(file_
.get(), length
);
422 base::ThreadRestrictions::AssertIOAllowed();
425 NOTIMPLEMENTED(); // NaCl doesn't implement fsync.
427 #elif defined(OS_LINUX) || defined(OS_ANDROID)
428 return !HANDLE_EINTR(fdatasync(file_
.get()));
430 return !HANDLE_EINTR(fsync(file_
.get()));
434 bool File::SetTimes(Time last_access_time
, Time last_modified_time
) {
435 base::ThreadRestrictions::AssertIOAllowed();
439 times
[0] = last_access_time
.ToTimeVal();
440 times
[1] = last_modified_time
.ToTimeVal();
442 return !CallFutimes(file_
.get(), times
);
445 bool File::GetInfo(Info
* info
) {
448 stat_wrapper_t file_info
;
449 if (CallFstat(file_
.get(), &file_info
))
452 info
->FromStat(file_info
);
456 File::Error
File::Lock() {
457 return CallFctnlFlock(file_
.get(), true);
460 File::Error
File::Unlock() {
461 return CallFctnlFlock(file_
.get(), false);
464 File
File::Duplicate() {
468 PlatformFile other_fd
= dup(GetPlatformFile());
470 return File(OSErrorToFileError(errno
));
472 File
other(other_fd
);
479 File::Error
File::OSErrorToFileError(int saved_errno
) {
480 switch (saved_errno
) {
485 return FILE_ERROR_ACCESS_DENIED
;
487 #if !defined(OS_NACL) // ETXTBSY not defined by NaCl.
490 return FILE_ERROR_IN_USE
;
492 return FILE_ERROR_EXISTS
;
494 return FILE_ERROR_IO
;
496 return FILE_ERROR_NOT_FOUND
;
498 return FILE_ERROR_TOO_MANY_OPENED
;
500 return FILE_ERROR_NO_MEMORY
;
502 return FILE_ERROR_NO_SPACE
;
504 return FILE_ERROR_NOT_A_DIRECTORY
;
506 #if !defined(OS_NACL) // NaCl build has no metrics code.
507 UMA_HISTOGRAM_SPARSE_SLOWLY("PlatformFile.UnknownErrors.Posix",
510 return FILE_ERROR_FAILED
;
514 File::MemoryCheckingScopedFD::MemoryCheckingScopedFD() {
518 File::MemoryCheckingScopedFD::MemoryCheckingScopedFD(int fd
) : file_(fd
) {
522 File::MemoryCheckingScopedFD::~MemoryCheckingScopedFD() {}
525 void File::MemoryCheckingScopedFD::ComputeMemoryChecksum(
526 unsigned int* out_checksum
) const {
527 // Use a single iteration of a linear congruentional generator (lcg) to
528 // provide a cheap checksum unlikely to be accidentally matched by a random
529 // memory corruption.
531 // By choosing constants that satisfy the Hull-Duebell Theorem on lcg cycle
532 // length, we insure that each distinct fd value maps to a distinct checksum,
533 // which maximises the utility of our checksum.
535 // This code uses "unsigned int" throughout for its defined modular semantics,
536 // which implicitly gives us a divisor that is a power of two.
538 const unsigned int kMultiplier
= 13035 * 4 + 1;
539 COMPILE_ASSERT(((kMultiplier
- 1) & 3) == 0, pred_must_be_multiple_of_four
);
540 const unsigned int kIncrement
= 1595649551;
541 COMPILE_ASSERT(kIncrement
& 1, must_be_coprime_to_powers_of_two
);
544 static_cast<unsigned int>(file_
.get()) * kMultiplier
+ kIncrement
;
547 void File::MemoryCheckingScopedFD::Check() const {
548 unsigned int computed_checksum
;
549 ComputeMemoryChecksum(&computed_checksum
);
550 CHECK_EQ(file_memory_checksum_
, computed_checksum
) << "corrupted fd memory";
553 void File::MemoryCheckingScopedFD::UpdateChecksum() {
554 ComputeMemoryChecksum(&file_memory_checksum_
);
557 void File::SetPlatformFile(PlatformFile file
) {
558 CHECK(!file_
.is_valid());
560 if (file_
.is_valid())
561 ProtectFileDescriptor(GetPlatformFile());