| Index: base/files/file_posix.cc
|
| diff --git a/base/files/file_posix.cc b/base/files/file_posix.cc
|
| index 517390f8eacf71135f9e62aff00de4a2d2b7f337..4c790571e8d6aeb732c6abfa038e51a7191edaa1 100644
|
| --- a/base/files/file_posix.cc
|
| +++ b/base/files/file_posix.cc
|
| @@ -10,7 +10,6 @@
|
| #include <unistd.h>
|
|
|
| #include "base/files/file_path.h"
|
| -#include "base/files/file_posix_hooks_internal.h"
|
| #include "base/logging.h"
|
| #include "base/metrics/sparse_histogram.h"
|
| #include "base/posix/eintr_wrapper.h"
|
| @@ -32,12 +31,12 @@ namespace {
|
|
|
| #if defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL)
|
| static int CallFstat(int fd, stat_wrapper_t *sb) {
|
| - base::ThreadRestrictions::AssertIOAllowed();
|
| + ThreadRestrictions::AssertIOAllowed();
|
| return fstat(fd, sb);
|
| }
|
| #else
|
| static int CallFstat(int fd, stat_wrapper_t *sb) {
|
| - base::ThreadRestrictions::AssertIOAllowed();
|
| + ThreadRestrictions::AssertIOAllowed();
|
| return fstat64(fd, sb);
|
| }
|
| #endif
|
| @@ -53,10 +52,6 @@ static int CallFtruncate(PlatformFile file, int64 length) {
|
| return HANDLE_EINTR(ftruncate(file, length));
|
| }
|
|
|
| -static int CallFsync(PlatformFile file) {
|
| - return HANDLE_EINTR(fsync(file));
|
| -}
|
| -
|
| static int CallFutimes(PlatformFile file, const struct timeval times[2]) {
|
| #ifdef __USE_XOPEN2K8
|
| // futimens should be available, but futimes might not be
|
| @@ -98,11 +93,6 @@ static int CallFtruncate(PlatformFile file, int64 length) {
|
| return 0;
|
| }
|
|
|
| -static int CallFsync(PlatformFile file) {
|
| - NOTIMPLEMENTED(); // NaCl doesn't implement fsync.
|
| - return 0;
|
| -}
|
| -
|
| static int CallFutimes(PlatformFile file, const struct timeval times[2]) {
|
| NOTIMPLEMENTED(); // NaCl doesn't implement futimes.
|
| return 0;
|
| @@ -167,104 +157,6 @@ void File::Info::FromStat(const stat_wrapper_t& stat_info) {
|
| Time::kNanosecondsPerMicrosecond);
|
| }
|
|
|
| -// Default implementations of Protect/Unprotect hooks defined as weak symbols
|
| -// where possible.
|
| -void ProtectFileDescriptor(int fd) {
|
| -}
|
| -
|
| -void UnprotectFileDescriptor(int fd) {
|
| -}
|
| -
|
| -// NaCl doesn't implement system calls to open files directly.
|
| -#if !defined(OS_NACL)
|
| -// TODO(erikkay): does it make sense to support FLAG_EXCLUSIVE_* here?
|
| -void File::InitializeUnsafe(const FilePath& name, uint32 flags) {
|
| - base::ThreadRestrictions::AssertIOAllowed();
|
| - DCHECK(!IsValid());
|
| -
|
| - int open_flags = 0;
|
| - if (flags & FLAG_CREATE)
|
| - open_flags = O_CREAT | O_EXCL;
|
| -
|
| - created_ = false;
|
| -
|
| - if (flags & FLAG_CREATE_ALWAYS) {
|
| - DCHECK(!open_flags);
|
| - DCHECK(flags & FLAG_WRITE);
|
| - open_flags = O_CREAT | O_TRUNC;
|
| - }
|
| -
|
| - if (flags & FLAG_OPEN_TRUNCATED) {
|
| - DCHECK(!open_flags);
|
| - DCHECK(flags & FLAG_WRITE);
|
| - open_flags = O_TRUNC;
|
| - }
|
| -
|
| - if (!open_flags && !(flags & FLAG_OPEN) && !(flags & FLAG_OPEN_ALWAYS)) {
|
| - NOTREACHED();
|
| - errno = EOPNOTSUPP;
|
| - error_details_ = FILE_ERROR_FAILED;
|
| - return;
|
| - }
|
| -
|
| - if (flags & FLAG_WRITE && flags & FLAG_READ) {
|
| - open_flags |= O_RDWR;
|
| - } else if (flags & FLAG_WRITE) {
|
| - open_flags |= O_WRONLY;
|
| - } else if (!(flags & FLAG_READ) &&
|
| - !(flags & FLAG_WRITE_ATTRIBUTES) &&
|
| - !(flags & FLAG_APPEND) &&
|
| - !(flags & FLAG_OPEN_ALWAYS)) {
|
| - NOTREACHED();
|
| - }
|
| -
|
| - if (flags & FLAG_TERMINAL_DEVICE)
|
| - open_flags |= O_NOCTTY | O_NDELAY;
|
| -
|
| - if (flags & FLAG_APPEND && flags & FLAG_READ)
|
| - open_flags |= O_APPEND | O_RDWR;
|
| - else if (flags & FLAG_APPEND)
|
| - open_flags |= O_APPEND | O_WRONLY;
|
| -
|
| - COMPILE_ASSERT(O_RDONLY == 0, O_RDONLY_must_equal_zero);
|
| -
|
| - int mode = S_IRUSR | S_IWUSR;
|
| -#if defined(OS_CHROMEOS)
|
| - mode |= S_IRGRP | S_IROTH;
|
| -#endif
|
| -
|
| - int descriptor = HANDLE_EINTR(open(name.value().c_str(), open_flags, mode));
|
| -
|
| - if (flags & FLAG_OPEN_ALWAYS) {
|
| - if (descriptor < 0) {
|
| - open_flags |= O_CREAT;
|
| - if (flags & FLAG_EXCLUSIVE_READ || flags & FLAG_EXCLUSIVE_WRITE)
|
| - open_flags |= O_EXCL; // together with O_CREAT implies O_NOFOLLOW
|
| -
|
| - descriptor = HANDLE_EINTR(open(name.value().c_str(), open_flags, mode));
|
| - if (descriptor >= 0)
|
| - created_ = true;
|
| - }
|
| - }
|
| -
|
| - if (descriptor < 0) {
|
| - error_details_ = File::OSErrorToFileError(errno);
|
| - return;
|
| - }
|
| -
|
| - if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE))
|
| - created_ = true;
|
| -
|
| - if (flags & FLAG_DELETE_ON_CLOSE)
|
| - unlink(name.value().c_str());
|
| -
|
| - async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC);
|
| - error_details_ = FILE_OK;
|
| - file_.reset(descriptor);
|
| - ProtectFileDescriptor(descriptor);
|
| -}
|
| -#endif // !defined(OS_NACL)
|
| -
|
| bool File::IsValid() const {
|
| return file_.is_valid();
|
| }
|
| @@ -274,8 +166,6 @@ PlatformFile File::GetPlatformFile() const {
|
| }
|
|
|
| PlatformFile File::TakePlatformFile() {
|
| - if (IsValid())
|
| - UnprotectFileDescriptor(GetPlatformFile());
|
| return file_.release();
|
| }
|
|
|
| @@ -283,13 +173,12 @@ void File::Close() {
|
| if (!IsValid())
|
| return;
|
|
|
| - base::ThreadRestrictions::AssertIOAllowed();
|
| - UnprotectFileDescriptor(GetPlatformFile());
|
| + ThreadRestrictions::AssertIOAllowed();
|
| file_.reset();
|
| }
|
|
|
| int64 File::Seek(Whence whence, int64 offset) {
|
| - base::ThreadRestrictions::AssertIOAllowed();
|
| + ThreadRestrictions::AssertIOAllowed();
|
| DCHECK(IsValid());
|
|
|
| #if defined(OS_ANDROID)
|
| @@ -304,7 +193,7 @@ int64 File::Seek(Whence whence, int64 offset) {
|
| }
|
|
|
| int File::Read(int64 offset, char* data, int size) {
|
| - base::ThreadRestrictions::AssertIOAllowed();
|
| + ThreadRestrictions::AssertIOAllowed();
|
| DCHECK(IsValid());
|
| if (size < 0)
|
| return -1;
|
| @@ -324,7 +213,7 @@ int File::Read(int64 offset, char* data, int size) {
|
| }
|
|
|
| int File::ReadAtCurrentPos(char* data, int size) {
|
| - base::ThreadRestrictions::AssertIOAllowed();
|
| + ThreadRestrictions::AssertIOAllowed();
|
| DCHECK(IsValid());
|
| if (size < 0)
|
| return -1;
|
| @@ -343,14 +232,14 @@ int File::ReadAtCurrentPos(char* data, int size) {
|
| }
|
|
|
| int File::ReadNoBestEffort(int64 offset, char* data, int size) {
|
| - base::ThreadRestrictions::AssertIOAllowed();
|
| + ThreadRestrictions::AssertIOAllowed();
|
| DCHECK(IsValid());
|
|
|
| return HANDLE_EINTR(pread(file_.get(), data, size, offset));
|
| }
|
|
|
| int File::ReadAtCurrentPosNoBestEffort(char* data, int size) {
|
| - base::ThreadRestrictions::AssertIOAllowed();
|
| + ThreadRestrictions::AssertIOAllowed();
|
| DCHECK(IsValid());
|
| if (size < 0)
|
| return -1;
|
| @@ -359,7 +248,7 @@ int File::ReadAtCurrentPosNoBestEffort(char* data, int size) {
|
| }
|
|
|
| int File::Write(int64 offset, const char* data, int size) {
|
| - base::ThreadRestrictions::AssertIOAllowed();
|
| + ThreadRestrictions::AssertIOAllowed();
|
|
|
| if (IsOpenAppend(file_.get()))
|
| return WriteAtCurrentPos(data, size);
|
| @@ -383,7 +272,7 @@ int File::Write(int64 offset, const char* data, int size) {
|
| }
|
|
|
| int File::WriteAtCurrentPos(const char* data, int size) {
|
| - base::ThreadRestrictions::AssertIOAllowed();
|
| + ThreadRestrictions::AssertIOAllowed();
|
| DCHECK(IsValid());
|
| if (size < 0)
|
| return -1;
|
| @@ -403,7 +292,7 @@ int File::WriteAtCurrentPos(const char* data, int size) {
|
| }
|
|
|
| int File::WriteAtCurrentPosNoBestEffort(const char* data, int size) {
|
| - base::ThreadRestrictions::AssertIOAllowed();
|
| + ThreadRestrictions::AssertIOAllowed();
|
| DCHECK(IsValid());
|
| if (size < 0)
|
| return -1;
|
| @@ -422,19 +311,13 @@ int64 File::GetLength() {
|
| }
|
|
|
| bool File::SetLength(int64 length) {
|
| - base::ThreadRestrictions::AssertIOAllowed();
|
| + ThreadRestrictions::AssertIOAllowed();
|
| DCHECK(IsValid());
|
| return !CallFtruncate(file_.get(), length);
|
| }
|
|
|
| -bool File::Flush() {
|
| - base::ThreadRestrictions::AssertIOAllowed();
|
| - DCHECK(IsValid());
|
| - return !CallFsync(file_.get());
|
| -}
|
| -
|
| bool File::SetTimes(Time last_access_time, Time last_modified_time) {
|
| - base::ThreadRestrictions::AssertIOAllowed();
|
| + ThreadRestrictions::AssertIOAllowed();
|
| DCHECK(IsValid());
|
|
|
| timeval times[2];
|
| @@ -556,11 +439,111 @@ void File::MemoryCheckingScopedFD::UpdateChecksum() {
|
| ComputeMemoryChecksum(&file_memory_checksum_);
|
| }
|
|
|
| +// NaCl doesn't implement system calls to open files directly.
|
| +#if !defined(OS_NACL)
|
| +// TODO(erikkay): does it make sense to support FLAG_EXCLUSIVE_* here?
|
| +void File::DoInitialize(const FilePath& name, uint32 flags) {
|
| + ThreadRestrictions::AssertIOAllowed();
|
| + DCHECK(!IsValid());
|
| +
|
| + int open_flags = 0;
|
| + if (flags & FLAG_CREATE)
|
| + open_flags = O_CREAT | O_EXCL;
|
| +
|
| + created_ = false;
|
| +
|
| + if (flags & FLAG_CREATE_ALWAYS) {
|
| + DCHECK(!open_flags);
|
| + DCHECK(flags & FLAG_WRITE);
|
| + open_flags = O_CREAT | O_TRUNC;
|
| + }
|
| +
|
| + if (flags & FLAG_OPEN_TRUNCATED) {
|
| + DCHECK(!open_flags);
|
| + DCHECK(flags & FLAG_WRITE);
|
| + open_flags = O_TRUNC;
|
| + }
|
| +
|
| + if (!open_flags && !(flags & FLAG_OPEN) && !(flags & FLAG_OPEN_ALWAYS)) {
|
| + NOTREACHED();
|
| + errno = EOPNOTSUPP;
|
| + error_details_ = FILE_ERROR_FAILED;
|
| + return;
|
| + }
|
| +
|
| + if (flags & FLAG_WRITE && flags & FLAG_READ) {
|
| + open_flags |= O_RDWR;
|
| + } else if (flags & FLAG_WRITE) {
|
| + open_flags |= O_WRONLY;
|
| + } else if (!(flags & FLAG_READ) &&
|
| + !(flags & FLAG_WRITE_ATTRIBUTES) &&
|
| + !(flags & FLAG_APPEND) &&
|
| + !(flags & FLAG_OPEN_ALWAYS)) {
|
| + NOTREACHED();
|
| + }
|
| +
|
| + if (flags & FLAG_TERMINAL_DEVICE)
|
| + open_flags |= O_NOCTTY | O_NDELAY;
|
| +
|
| + if (flags & FLAG_APPEND && flags & FLAG_READ)
|
| + open_flags |= O_APPEND | O_RDWR;
|
| + else if (flags & FLAG_APPEND)
|
| + open_flags |= O_APPEND | O_WRONLY;
|
| +
|
| + COMPILE_ASSERT(O_RDONLY == 0, O_RDONLY_must_equal_zero);
|
| +
|
| + int mode = S_IRUSR | S_IWUSR;
|
| +#if defined(OS_CHROMEOS)
|
| + mode |= S_IRGRP | S_IROTH;
|
| +#endif
|
| +
|
| + int descriptor = HANDLE_EINTR(open(name.value().c_str(), open_flags, mode));
|
| +
|
| + if (flags & FLAG_OPEN_ALWAYS) {
|
| + if (descriptor < 0) {
|
| + open_flags |= O_CREAT;
|
| + if (flags & FLAG_EXCLUSIVE_READ || flags & FLAG_EXCLUSIVE_WRITE)
|
| + open_flags |= O_EXCL; // together with O_CREAT implies O_NOFOLLOW
|
| +
|
| + descriptor = HANDLE_EINTR(open(name.value().c_str(), open_flags, mode));
|
| + if (descriptor >= 0)
|
| + created_ = true;
|
| + }
|
| + }
|
| +
|
| + if (descriptor < 0) {
|
| + error_details_ = File::OSErrorToFileError(errno);
|
| + return;
|
| + }
|
| +
|
| + if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE))
|
| + created_ = true;
|
| +
|
| + if (flags & FLAG_DELETE_ON_CLOSE)
|
| + unlink(name.value().c_str());
|
| +
|
| + async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC);
|
| + error_details_ = FILE_OK;
|
| + file_.reset(descriptor);
|
| +}
|
| +#endif // !defined(OS_NACL)
|
| +
|
| +bool File::DoFlush() {
|
| + ThreadRestrictions::AssertIOAllowed();
|
| + DCHECK(IsValid());
|
| +#if defined(OS_NACL)
|
| + NOTIMPLEMENTED(); // NaCl doesn't implement fsync.
|
| + return true;
|
| +#elif defined(OS_LINUX) || defined(OS_ANDROID)
|
| + return !HANDLE_EINTR(fdatasync(file_.get()));
|
| +#else
|
| + return !HANDLE_EINTR(fsync(file_.get()));
|
| +#endif
|
| +}
|
| +
|
| void File::SetPlatformFile(PlatformFile file) {
|
| - CHECK(!file_.is_valid());
|
| + DCHECK(!file_.is_valid());
|
| file_.reset(file);
|
| - if (file_.is_valid())
|
| - ProtectFileDescriptor(GetPlatformFile());
|
| }
|
|
|
| } // namespace base
|
|
|