diff options
author | dbeam <dbeam@chromium.org> | 2015-04-23 18:27:53 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-04-24 01:27:59 +0000 |
commit | 492cd47d8927756efa57d58b005da3c7636aeb1b (patch) | |
tree | d60dec0961bc2354bf29576c18fbd4a7df3bcb83 /base | |
parent | 4f49c2c56dad1ce2a2ca2a7df3610814ccc6d80b (diff) | |
download | chromium_src-492cd47d8927756efa57d58b005da3c7636aeb1b.zip chromium_src-492cd47d8927756efa57d58b005da3c7636aeb1b.tar.gz chromium_src-492cd47d8927756efa57d58b005da3c7636aeb1b.tar.bz2 |
Make unused InitializeUnsafe private and rename to DoInitialize.
R=thestig@chromium.org
BUG=none
TEST=no unsafe way to use base::File
Review URL: https://codereview.chromium.org/1103733002
Cr-Commit-Position: refs/heads/master@{#326714}
Diffstat (limited to 'base')
-rw-r--r-- | base/files/file.cc | 2 | ||||
-rw-r--r-- | base/files/file.h | 8 | ||||
-rw-r--r-- | base/files/file_posix.cc | 180 | ||||
-rw-r--r-- | base/files/file_win.cc | 168 |
4 files changed, 179 insertions, 179 deletions
diff --git a/base/files/file.cc b/base/files/file.cc index 86fdbc9..4c0e5de 100644 --- a/base/files/file.cc +++ b/base/files/file.cc @@ -88,7 +88,7 @@ void File::Initialize(const FilePath& name, uint32 flags) { error_details_ = FILE_ERROR_ACCESS_DENIED; return; } - InitializeUnsafe(name, flags); + DoInitialize(name, flags); } #endif diff --git a/base/files/file.h b/base/files/file.h index f50a8b1..89077b4 100644 --- a/base/files/file.h +++ b/base/files/file.h @@ -182,10 +182,6 @@ class BASE_EXPORT File { // Creates or opens the given file. void Initialize(const FilePath& name, uint32 flags); - // Creates or opens the given file, allowing paths with traversal ('..') - // components. Use only with extreme care. - void InitializeUnsafe(const FilePath& name, uint32 flags); - bool IsValid() const; // Returns true if a new file was created (or an old one truncated to zero @@ -354,6 +350,10 @@ class BASE_EXPORT File { }; #endif + // Creates or opens the given file. Only called if |name| has no traversal + // ('..') components. + void DoInitialize(const FilePath& name, uint32 flags); + // TODO(tnagel): Reintegrate into Flush() once histogram isn't needed anymore, // cf. issue 473337. bool DoFlush(); diff --git a/base/files/file_posix.cc b/base/files/file_posix.cc index 41d6304..dab9cc2 100644 --- a/base/files/file_posix.cc +++ b/base/files/file_posix.cc @@ -166,96 +166,6 @@ 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) { - 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(); } @@ -541,6 +451,96 @@ 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); + ProtectFileDescriptor(descriptor); +} +#endif // !defined(OS_NACL) + bool File::DoFlush() { ThreadRestrictions::AssertIOAllowed(); DCHECK(IsValid()); diff --git a/base/files/file_win.cc b/base/files/file_win.cc index 85f17e4..200ea29 100644 --- a/base/files/file_win.cc +++ b/base/files/file_win.cc @@ -18,90 +18,6 @@ COMPILE_ASSERT(File::FROM_BEGIN == FILE_BEGIN && File::FROM_CURRENT == FILE_CURRENT && File::FROM_END == FILE_END, whence_matches_system); -void File::InitializeUnsafe(const FilePath& name, uint32 flags) { - ThreadRestrictions::AssertIOAllowed(); - DCHECK(!IsValid()); - - DWORD disposition = 0; - - if (flags & FLAG_OPEN) - disposition = OPEN_EXISTING; - - if (flags & FLAG_CREATE) { - DCHECK(!disposition); - disposition = CREATE_NEW; - } - - if (flags & FLAG_OPEN_ALWAYS) { - DCHECK(!disposition); - disposition = OPEN_ALWAYS; - } - - if (flags & FLAG_CREATE_ALWAYS) { - DCHECK(!disposition); - DCHECK(flags & FLAG_WRITE); - disposition = CREATE_ALWAYS; - } - - if (flags & FLAG_OPEN_TRUNCATED) { - DCHECK(!disposition); - DCHECK(flags & FLAG_WRITE); - disposition = TRUNCATE_EXISTING; - } - - if (!disposition) { - NOTREACHED(); - return; - } - - DWORD access = 0; - if (flags & FLAG_WRITE) - access = GENERIC_WRITE; - if (flags & FLAG_APPEND) { - DCHECK(!access); - access = FILE_APPEND_DATA; - } - if (flags & FLAG_READ) - access |= GENERIC_READ; - if (flags & FLAG_WRITE_ATTRIBUTES) - access |= FILE_WRITE_ATTRIBUTES; - if (flags & FLAG_EXECUTE) - access |= GENERIC_EXECUTE; - - DWORD sharing = (flags & FLAG_EXCLUSIVE_READ) ? 0 : FILE_SHARE_READ; - if (!(flags & FLAG_EXCLUSIVE_WRITE)) - sharing |= FILE_SHARE_WRITE; - if (flags & FLAG_SHARE_DELETE) - sharing |= FILE_SHARE_DELETE; - - DWORD create_flags = 0; - if (flags & FLAG_ASYNC) - create_flags |= FILE_FLAG_OVERLAPPED; - if (flags & FLAG_TEMPORARY) - create_flags |= FILE_ATTRIBUTE_TEMPORARY; - if (flags & FLAG_HIDDEN) - create_flags |= FILE_ATTRIBUTE_HIDDEN; - if (flags & FLAG_DELETE_ON_CLOSE) - create_flags |= FILE_FLAG_DELETE_ON_CLOSE; - if (flags & FLAG_BACKUP_SEMANTICS) - create_flags |= FILE_FLAG_BACKUP_SEMANTICS; - - file_.Set(CreateFile(name.value().c_str(), access, sharing, NULL, - disposition, create_flags, NULL)); - - if (file_.IsValid()) { - error_details_ = FILE_OK; - async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC); - - if (flags & (FLAG_OPEN_ALWAYS)) - created_ = (ERROR_ALREADY_EXISTS != GetLastError()); - else if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE)) - created_ = true; - } else { - error_details_ = OSErrorToFileError(GetLastError()); - } -} - bool File::IsValid() const { return file_.IsValid(); } @@ -362,6 +278,90 @@ File::Error File::OSErrorToFileError(DWORD last_error) { } } +void File::DoInitialize(const FilePath& name, uint32 flags) { + ThreadRestrictions::AssertIOAllowed(); + DCHECK(!IsValid()); + + DWORD disposition = 0; + + if (flags & FLAG_OPEN) + disposition = OPEN_EXISTING; + + if (flags & FLAG_CREATE) { + DCHECK(!disposition); + disposition = CREATE_NEW; + } + + if (flags & FLAG_OPEN_ALWAYS) { + DCHECK(!disposition); + disposition = OPEN_ALWAYS; + } + + if (flags & FLAG_CREATE_ALWAYS) { + DCHECK(!disposition); + DCHECK(flags & FLAG_WRITE); + disposition = CREATE_ALWAYS; + } + + if (flags & FLAG_OPEN_TRUNCATED) { + DCHECK(!disposition); + DCHECK(flags & FLAG_WRITE); + disposition = TRUNCATE_EXISTING; + } + + if (!disposition) { + NOTREACHED(); + return; + } + + DWORD access = 0; + if (flags & FLAG_WRITE) + access = GENERIC_WRITE; + if (flags & FLAG_APPEND) { + DCHECK(!access); + access = FILE_APPEND_DATA; + } + if (flags & FLAG_READ) + access |= GENERIC_READ; + if (flags & FLAG_WRITE_ATTRIBUTES) + access |= FILE_WRITE_ATTRIBUTES; + if (flags & FLAG_EXECUTE) + access |= GENERIC_EXECUTE; + + DWORD sharing = (flags & FLAG_EXCLUSIVE_READ) ? 0 : FILE_SHARE_READ; + if (!(flags & FLAG_EXCLUSIVE_WRITE)) + sharing |= FILE_SHARE_WRITE; + if (flags & FLAG_SHARE_DELETE) + sharing |= FILE_SHARE_DELETE; + + DWORD create_flags = 0; + if (flags & FLAG_ASYNC) + create_flags |= FILE_FLAG_OVERLAPPED; + if (flags & FLAG_TEMPORARY) + create_flags |= FILE_ATTRIBUTE_TEMPORARY; + if (flags & FLAG_HIDDEN) + create_flags |= FILE_ATTRIBUTE_HIDDEN; + if (flags & FLAG_DELETE_ON_CLOSE) + create_flags |= FILE_FLAG_DELETE_ON_CLOSE; + if (flags & FLAG_BACKUP_SEMANTICS) + create_flags |= FILE_FLAG_BACKUP_SEMANTICS; + + file_.Set(CreateFile(name.value().c_str(), access, sharing, NULL, + disposition, create_flags, NULL)); + + if (file_.IsValid()) { + error_details_ = FILE_OK; + async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC); + + if (flags & (FLAG_OPEN_ALWAYS)) + created_ = (ERROR_ALREADY_EXISTS != GetLastError()); + else if (flags & (FLAG_CREATE_ALWAYS | FLAG_CREATE)) + created_ = true; + } else { + error_details_ = OSErrorToFileError(GetLastError()); + } +} + bool File::DoFlush() { ThreadRestrictions::AssertIOAllowed(); DCHECK(IsValid()); |