diff options
author | rvargas@chromium.org <rvargas@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-06-24 05:40:09 +0000 |
---|---|---|
committer | rvargas@chromium.org <rvargas@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-06-24 05:40:09 +0000 |
commit | aefeb011b33eeded5c59bb6e2e08b7612a2666d7 (patch) | |
tree | 64f2c77f6d49ae042829dec39dcd2713e71efa72 | |
parent | ba7d5f92ff6354fb0061764120c113e72681961c (diff) | |
download | chromium_src-aefeb011b33eeded5c59bb6e2e08b7612a2666d7.zip chromium_src-aefeb011b33eeded5c59bb6e2e08b7612a2666d7.tar.gz chromium_src-aefeb011b33eeded5c59bb6e2e08b7612a2666d7.tar.bz2 |
Remove base/platform_file.*
Fix remaining users of platform_file.h
BUG=322664
R=brettw@chromium.org
TBR=thestig@chromium.org, satorux@chromium.org, jochen@chromium.org, rockot@chromium.org, viettrungluu@chromium.org
Review URL: https://codereview.chromium.org/326383005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@279330 0039d316-1c4b-4281-b951-d872f2087c98
25 files changed, 12 insertions, 1408 deletions
diff --git a/base/BUILD.gn b/base/BUILD.gn index 53084d5..700ea33 100644 --- a/base/BUILD.gn +++ b/base/BUILD.gn @@ -410,10 +410,6 @@ component("base") { "pending_task.h", "pickle.cc", "pickle.h", - "platform_file.cc", - "platform_file.h", - "platform_file_posix.cc", - "platform_file_win.cc", "port.h", "posix/eintr_wrapper.h", "posix/file_descriptor_shuffle.cc", @@ -1139,7 +1135,6 @@ test("base_unittests") { "os_compat_android_unittest.cc", "path_service_unittest.cc", "pickle_unittest.cc", - "platform_file_unittest.cc", "posix/file_descriptor_shuffle_unittest.cc", "posix/unix_domain_socket_linux_unittest.cc", "power_monitor/power_monitor_unittest.cc", diff --git a/base/base.gyp b/base/base.gyp index 9f7b993..9f60e25 100644 --- a/base/base.gyp +++ b/base/base.gyp @@ -532,7 +532,6 @@ 'os_compat_android_unittest.cc', 'path_service_unittest.cc', 'pickle_unittest.cc', - 'platform_file_unittest.cc', 'posix/file_descriptor_shuffle_unittest.cc', 'posix/unix_domain_socket_linux_unittest.cc', 'power_monitor/power_monitor_unittest.cc', diff --git a/base/base.gypi b/base/base.gypi index d876921..971c66a 100644 --- a/base/base.gypi +++ b/base/base.gypi @@ -222,8 +222,6 @@ 'files/memory_mapped_file_win.cc', 'files/scoped_file.cc', 'files/scoped_file.h', - 'files/scoped_platform_file_closer.cc', - 'files/scoped_platform_file_closer.h', 'files/scoped_temp_dir.cc', 'files/scoped_temp_dir.h', 'float_util.h', @@ -417,10 +415,6 @@ 'pending_task.h', 'pickle.cc', 'pickle.h', - 'platform_file.cc', - 'platform_file.h', - 'platform_file_posix.cc', - 'platform_file_win.cc', 'port.h', 'posix/eintr_wrapper.h', 'posix/global_descriptors.cc', diff --git a/base/files/file.cc b/base/files/file.cc index bfe87bd..ea8dbf2 100644 --- a/base/files/file.cc +++ b/base/files/file.cc @@ -3,9 +3,7 @@ // found in the LICENSE file. #include "base/files/file.h" - -// TODO(rvargas): remove this (needed for kInvalidPlatformFileValue). -#include "base/platform_file.h" +#include "base/files/file_path.h" namespace base { @@ -19,16 +17,14 @@ File::Info::~Info() { } File::File() - : file_(kInvalidPlatformFileValue), - error_details_(FILE_ERROR_FAILED), + : error_details_(FILE_ERROR_FAILED), created_(false), async_(false) { } #if !defined(OS_NACL) File::File(const FilePath& name, uint32 flags) - : file_(kInvalidPlatformFileValue), - error_details_(FILE_OK), + : error_details_(FILE_OK), created_(false), async_(false) { Initialize(name, flags); @@ -46,8 +42,7 @@ File::File(PlatformFile platform_file) } File::File(Error error_details) - : file_(kInvalidPlatformFileValue), - error_details_(error_details), + : error_details_(error_details), created_(false), async_(false) { } diff --git a/base/files/file_posix.cc b/base/files/file_posix.cc index a51aaa81..43684b5 100644 --- a/base/files/file_posix.cc +++ b/base/files/file_posix.cc @@ -12,8 +12,6 @@ #include "base/files/file_path.h" #include "base/logging.h" #include "base/metrics/sparse_histogram.h" -// TODO(rvargas): remove this (needed for kInvalidPlatformFileValue). -#include "base/platform_file.h" #include "base/posix/eintr_wrapper.h" #include "base/strings/utf_string_conversions.h" #include "base/threading/thread_restrictions.h" diff --git a/base/files/scoped_platform_file_closer.cc b/base/files/scoped_platform_file_closer.cc deleted file mode 100644 index 44f6752..0000000 --- a/base/files/scoped_platform_file_closer.cc +++ /dev/null @@ -1,16 +0,0 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "base/files/scoped_platform_file_closer.h" - -namespace base { -namespace internal { - -void PlatformFileCloser::operator()(PlatformFile* file) const { - if (file && *file != kInvalidPlatformFileValue) - ClosePlatformFile(*file); -} - -} // namespace internal -} // namespace base diff --git a/base/files/scoped_platform_file_closer.h b/base/files/scoped_platform_file_closer.h deleted file mode 100644 index 8fe4a28..0000000 --- a/base/files/scoped_platform_file_closer.h +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef BASE_FILES_SCOPED_PLATFORM_FILE_CLOSER_H_ -#define BASE_FILES_SCOPED_PLATFORM_FILE_CLOSER_H_ - -#include "base/memory/scoped_ptr.h" -#include "base/platform_file.h" - -namespace base { - -namespace internal { - -struct BASE_EXPORT PlatformFileCloser { - void operator()(PlatformFile* file) const; -}; - -} // namespace internal - -typedef scoped_ptr<PlatformFile, internal::PlatformFileCloser> - ScopedPlatformFileCloser; - -} // namespace base - -#endif // BASE_FILES_SCOPED_PLATFORM_FILE_CLOSER_H_ diff --git a/base/platform_file.cc b/base/platform_file.cc deleted file mode 100644 index 70700fc..0000000 --- a/base/platform_file.cc +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "base/platform_file.h" - -namespace base { - -PlatformFileInfo::PlatformFileInfo() - : size(0), - is_directory(false), - is_symbolic_link(false) { -} - -PlatformFileInfo::~PlatformFileInfo() {} - -} // namespace base diff --git a/base/platform_file.h b/base/platform_file.h deleted file mode 100644 index 27686d1..0000000 --- a/base/platform_file.h +++ /dev/null @@ -1,268 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef BASE_PLATFORM_FILE_H_ -#define BASE_PLATFORM_FILE_H_ - -#include "build/build_config.h" -#if defined(OS_WIN) -#include <windows.h> -#endif - -#include <string> - -#include "base/base_export.h" -#include "base/basictypes.h" -#include "base/files/file_path.h" -#include "base/time/time.h" - -namespace base { - -// *************************************************************************** -// ***** Don't use anything from this file anymore. It is being removed! -// ***** Use base/files/file.h instead -// *************************************************************************** - -// PLATFORM_FILE_(OPEN|CREATE).* are mutually exclusive. You should specify -// exactly one of the five (possibly combining with other flags) when opening -// or creating a file. -// PLATFORM_FILE_(WRITE|APPEND) are mutually exclusive. This is so that APPEND -// behavior will be consistent with O_APPEND on POSIX. -// PLATFORM_FILE_EXCLUSIVE_(READ|WRITE) only grant exclusive access to the file -// on creation on POSIX; for existing files, consider using LockPlatformFile(). -enum PlatformFileFlags { - PLATFORM_FILE_OPEN = 1 << 0, // Opens a file, only if it exists. - PLATFORM_FILE_CREATE = 1 << 1, // Creates a new file, only if it - // does not already exist. - PLATFORM_FILE_OPEN_ALWAYS = 1 << 2, // May create a new file. - PLATFORM_FILE_CREATE_ALWAYS = 1 << 3, // May overwrite an old file. - PLATFORM_FILE_OPEN_TRUNCATED = 1 << 4, // Opens a file and truncates it, - // only if it exists. - PLATFORM_FILE_READ = 1 << 5, - PLATFORM_FILE_WRITE = 1 << 6, - PLATFORM_FILE_APPEND = 1 << 7, - PLATFORM_FILE_EXCLUSIVE_READ = 1 << 8, // EXCLUSIVE is opposite of Windows - // SHARE - PLATFORM_FILE_EXCLUSIVE_WRITE = 1 << 9, - PLATFORM_FILE_ASYNC = 1 << 10, - PLATFORM_FILE_TEMPORARY = 1 << 11, // Used on Windows only - PLATFORM_FILE_HIDDEN = 1 << 12, // Used on Windows only - PLATFORM_FILE_DELETE_ON_CLOSE = 1 << 13, - - PLATFORM_FILE_WRITE_ATTRIBUTES = 1 << 14, // Used on Windows only - - PLATFORM_FILE_SHARE_DELETE = 1 << 15, // Used on Windows only - - PLATFORM_FILE_TERMINAL_DEVICE = 1 << 16, // Serial port flags - PLATFORM_FILE_BACKUP_SEMANTICS = 1 << 17, // Used on Windows only - - PLATFORM_FILE_EXECUTE = 1 << 18, // Used on Windows only -}; - -// This enum has been recorded in multiple histograms. If the order of the -// fields needs to change, please ensure that those histograms are obsolete or -// have been moved to a different enum. -// -// PLATFORM_FILE_ERROR_ACCESS_DENIED is returned when a call fails because of -// a filesystem restriction. PLATFORM_FILE_ERROR_SECURITY is returned when a -// browser policy doesn't allow the operation to be executed. -enum PlatformFileError { - PLATFORM_FILE_OK = 0, - PLATFORM_FILE_ERROR_FAILED = -1, - PLATFORM_FILE_ERROR_IN_USE = -2, - PLATFORM_FILE_ERROR_EXISTS = -3, - PLATFORM_FILE_ERROR_NOT_FOUND = -4, - PLATFORM_FILE_ERROR_ACCESS_DENIED = -5, - PLATFORM_FILE_ERROR_TOO_MANY_OPENED = -6, - PLATFORM_FILE_ERROR_NO_MEMORY = -7, - PLATFORM_FILE_ERROR_NO_SPACE = -8, - PLATFORM_FILE_ERROR_NOT_A_DIRECTORY = -9, - PLATFORM_FILE_ERROR_INVALID_OPERATION = -10, - PLATFORM_FILE_ERROR_SECURITY = -11, - PLATFORM_FILE_ERROR_ABORT = -12, - PLATFORM_FILE_ERROR_NOT_A_FILE = -13, - PLATFORM_FILE_ERROR_NOT_EMPTY = -14, - PLATFORM_FILE_ERROR_INVALID_URL = -15, - PLATFORM_FILE_ERROR_IO = -16, - // Put new entries here and increment PLATFORM_FILE_ERROR_MAX. - PLATFORM_FILE_ERROR_MAX = -17 -}; - -// This explicit mapping matches both FILE_ on Windows and SEEK_ on Linux. -enum PlatformFileWhence { - PLATFORM_FILE_FROM_BEGIN = 0, - PLATFORM_FILE_FROM_CURRENT = 1, - PLATFORM_FILE_FROM_END = 2 -}; - -// Used to hold information about a given file. -// If you add more fields to this structure (platform-specific fields are OK), -// make sure to update all functions that use it in file_util_{win|posix}.cc -// too, and the ParamTraits<base::PlatformFileInfo> implementation in -// chrome/common/common_param_traits.cc. -struct BASE_EXPORT PlatformFileInfo { - PlatformFileInfo(); - ~PlatformFileInfo(); - - // The size of the file in bytes. Undefined when is_directory is true. - int64 size; - - // True if the file corresponds to a directory. - bool is_directory; - - // True if the file corresponds to a symbolic link. - bool is_symbolic_link; - - // The last modified time of a file. - base::Time last_modified; - - // The last accessed time of a file. - base::Time last_accessed; - - // The creation time of a file. - base::Time creation_time; -}; - -#if defined(OS_WIN) -typedef HANDLE PlatformFile; -const PlatformFile kInvalidPlatformFileValue = INVALID_HANDLE_VALUE; -BASE_EXPORT PlatformFileError LastErrorToPlatformFileError(DWORD last_error); -#elif defined(OS_POSIX) -typedef int PlatformFile; -const PlatformFile kInvalidPlatformFileValue = -1; -BASE_EXPORT PlatformFileError ErrnoToPlatformFileError(int saved_errno); -#endif - -// Closes a file handle. Returns |true| on success and |false| otherwise. -BASE_EXPORT bool ClosePlatformFile(PlatformFile file); - -// Changes current position in the file to an |offset| relative to an origin -// defined by |whence|. Returns the resultant current position in the file -// (relative to the start) or -1 in case of error. -BASE_EXPORT int64 SeekPlatformFile(PlatformFile file, - PlatformFileWhence whence, - int64 offset); - -// Reads the given number of bytes (or until EOF is reached) starting with the -// given offset. Returns the number of bytes read, or -1 on error. Note that -// this function makes a best effort to read all data on all platforms, so it is -// not intended for stream oriented files but instead for cases when the normal -// expectation is that actually |size| bytes are read unless there is an error. -BASE_EXPORT int ReadPlatformFile(PlatformFile file, int64 offset, - char* data, int size); - -// Same as above but without seek. -BASE_EXPORT int ReadPlatformFileAtCurrentPos(PlatformFile file, - char* data, int size); - -// Reads the given number of bytes (or until EOF is reached) starting with the -// given offset, but does not make any effort to read all data on all platforms. -// Returns the number of bytes read, or -1 on error. -BASE_EXPORT int ReadPlatformFileNoBestEffort(PlatformFile file, int64 offset, - char* data, int size); - -// Same as above but without seek. -BASE_EXPORT int ReadPlatformFileCurPosNoBestEffort(PlatformFile file, - char* data, int size); - -// Writes the given buffer into the file at the given offset, overwritting any -// data that was previously there. Returns the number of bytes written, or -1 -// on error. Note that this function makes a best effort to write all data on -// all platforms. -// Ignores the offset and writes to the end of the file if the file was opened -// with PLATFORM_FILE_APPEND. -BASE_EXPORT int WritePlatformFile(PlatformFile file, int64 offset, - const char* data, int size); - -// Save as above but without seek. -BASE_EXPORT int WritePlatformFileAtCurrentPos(PlatformFile file, - const char* data, int size); - -// Save as above but does not make any effort to write all data on all -// platforms. Returns the number of bytes written, or -1 on error. -BASE_EXPORT int WritePlatformFileCurPosNoBestEffort(PlatformFile file, - const char* data, int size); - -// Truncates the given file to the given length. If |length| is greater than -// the current size of the file, the file is extended with zeros. If the file -// doesn't exist, |false| is returned. -BASE_EXPORT bool TruncatePlatformFile(PlatformFile file, int64 length); - -// Flushes the buffers of the given file. -BASE_EXPORT bool FlushPlatformFile(PlatformFile file); - -// Touches the given file. -BASE_EXPORT bool TouchPlatformFile(PlatformFile file, - const Time& last_access_time, - const Time& last_modified_time); - -// Returns some information for the given file. -BASE_EXPORT bool GetPlatformFileInfo(PlatformFile file, PlatformFileInfo* info); - -// Attempts to take an exclusive write lock on the file. Returns immediately -// (i.e. does not wait for another process to unlock the file). If the lock -// was obtained, the result will be PLATFORM_FILE_OK. A lock only guarantees -// that other processes may not also take a lock on the same file with the -// same API - it may still be opened, renamed, unlinked, etc. -// -// Common semantics: -// * Locks are held by processes, but not inherited by child processes. -// * Locks are released by the OS on file handle close or process termination. -// * Locks are reliable only on local filesystems. -// * Duplicated file handles may also write to locked files. -// Windows-specific semantics: -// * Locks are mandatory for read/write APIs, advisory for mapping APIs. -// * Within a process, locking the same file (by the same or new handle) -// will fail. -// POSIX-specific semantics: -// * Locks are advisory only. -// * Within a process, locking the same file (by the same or new handle) -// will succeed. -// * Closing any descriptor on a given file releases the lock. -BASE_EXPORT PlatformFileError LockPlatformFile(PlatformFile file); - -// Unlock a file previously locked with LockPlatformFile. -BASE_EXPORT PlatformFileError UnlockPlatformFile(PlatformFile file); - -// Use this class to pass ownership of a PlatformFile to a receiver that may or -// may not want to accept it. This class does not own the storage for the -// PlatformFile. -// -// EXAMPLE: -// -// void MaybeProcessFile(PassPlatformFile pass_file) { -// if (...) { -// PlatformFile file = pass_file.ReleaseValue(); -// // Now, we are responsible for closing |file|. -// } -// } -// -// void OpenAndMaybeProcessFile(const FilePath& path) { -// PlatformFile file = CreatePlatformFile(path, ...); -// MaybeProcessFile(PassPlatformFile(&file)); -// if (file != kInvalidPlatformFileValue) -// ClosePlatformFile(file); -// } -// -class BASE_EXPORT PassPlatformFile { - public: - explicit PassPlatformFile(PlatformFile* value) : value_(value) { - } - - // Called to retrieve the PlatformFile stored in this object. The caller - // gains ownership of the PlatformFile and is now responsible for closing it. - // Any subsequent calls to this method will return an invalid PlatformFile. - PlatformFile ReleaseValue() { - PlatformFile temp = *value_; - *value_ = kInvalidPlatformFileValue; - return temp; - } - - private: - PlatformFile* value_; -}; - -} // namespace base - -#endif // BASE_PLATFORM_FILE_H_ diff --git a/base/platform_file_posix.cc b/base/platform_file_posix.cc deleted file mode 100644 index cee1f61..0000000 --- a/base/platform_file_posix.cc +++ /dev/null @@ -1,363 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "base/platform_file.h" - -#include <fcntl.h> -#include <errno.h> -#include <sys/stat.h> -#include <unistd.h> - -#include "base/files/file_path.h" -#include "base/logging.h" -#include "base/metrics/sparse_histogram.h" -#include "base/posix/eintr_wrapper.h" -#include "base/strings/utf_string_conversions.h" -#include "base/threading/thread_restrictions.h" - -#if defined(OS_ANDROID) -#include "base/os_compat_android.h" -#endif - -namespace base { - -// Make sure our Whence mappings match the system headers. -COMPILE_ASSERT(PLATFORM_FILE_FROM_BEGIN == SEEK_SET && - PLATFORM_FILE_FROM_CURRENT == SEEK_CUR && - PLATFORM_FILE_FROM_END == SEEK_END, whence_matches_system); - -namespace { - -#if defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL) -typedef struct stat stat_wrapper_t; -static int CallFstat(int fd, stat_wrapper_t *sb) { - base::ThreadRestrictions::AssertIOAllowed(); - return fstat(fd, sb); -} -#else -typedef struct stat64 stat_wrapper_t; -static int CallFstat(int fd, stat_wrapper_t *sb) { - base::ThreadRestrictions::AssertIOAllowed(); - return fstat64(fd, sb); -} -#endif - -// NaCl doesn't provide the following system calls, so either simulate them or -// wrap them in order to minimize the number of #ifdef's in this file. -#if !defined(OS_NACL) -static bool IsOpenAppend(PlatformFile file) { - return (fcntl(file, F_GETFL) & O_APPEND) != 0; -} - -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 - // http://pubs.opengroup.org/onlinepubs/9699919799/ - - timespec ts_times[2]; - ts_times[0].tv_sec = times[0].tv_sec; - ts_times[0].tv_nsec = times[0].tv_usec * 1000; - ts_times[1].tv_sec = times[1].tv_sec; - ts_times[1].tv_nsec = times[1].tv_usec * 1000; - - return futimens(file, ts_times); -#else - return futimes(file, times); -#endif -} - -static PlatformFileError CallFctnlFlock(PlatformFile file, bool do_lock) { - struct flock lock; - lock.l_type = F_WRLCK; - lock.l_whence = SEEK_SET; - lock.l_start = 0; - lock.l_len = 0; // Lock entire file. - if (HANDLE_EINTR(fcntl(file, do_lock ? F_SETLK : F_UNLCK, &lock)) == -1) - return ErrnoToPlatformFileError(errno); - return PLATFORM_FILE_OK; -} -#else // defined(OS_NACL) - -static bool IsOpenAppend(PlatformFile file) { - // NaCl doesn't implement fcntl. Since NaCl's write conforms to the POSIX - // standard and always appends if the file is opened with O_APPEND, just - // return false here. - return false; -} - -static int CallFtruncate(PlatformFile file, int64 length) { - NOTIMPLEMENTED(); // NaCl doesn't implement ftruncate. - 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; -} - -static PlatformFileError CallFctnlFlock(PlatformFile file, bool do_lock) { - NOTIMPLEMENTED(); // NaCl doesn't implement flock struct. - return PLATFORM_FILE_ERROR_INVALID_OPERATION; -} -#endif // defined(OS_NACL) - -} // namespace - -bool ClosePlatformFile(PlatformFile file) { - base::ThreadRestrictions::AssertIOAllowed(); - return !IGNORE_EINTR(close(file)); -} - -int64 SeekPlatformFile(PlatformFile file, - PlatformFileWhence whence, - int64 offset) { - base::ThreadRestrictions::AssertIOAllowed(); - if (file < 0 || offset < 0) - return -1; - - return lseek(file, static_cast<off_t>(offset), static_cast<int>(whence)); -} - -int ReadPlatformFile(PlatformFile file, int64 offset, char* data, int size) { - base::ThreadRestrictions::AssertIOAllowed(); - if (file < 0 || size < 0) - return -1; - - int bytes_read = 0; - int rv; - do { - rv = HANDLE_EINTR(pread(file, data + bytes_read, - size - bytes_read, offset + bytes_read)); - if (rv <= 0) - break; - - bytes_read += rv; - } while (bytes_read < size); - - return bytes_read ? bytes_read : rv; -} - -int ReadPlatformFileAtCurrentPos(PlatformFile file, char* data, int size) { - base::ThreadRestrictions::AssertIOAllowed(); - if (file < 0 || size < 0) - return -1; - - int bytes_read = 0; - int rv; - do { - rv = HANDLE_EINTR(read(file, data + bytes_read, size - bytes_read)); - if (rv <= 0) - break; - - bytes_read += rv; - } while (bytes_read < size); - - return bytes_read ? bytes_read : rv; -} - -int ReadPlatformFileNoBestEffort(PlatformFile file, int64 offset, - char* data, int size) { - base::ThreadRestrictions::AssertIOAllowed(); - if (file < 0) - return -1; - - return HANDLE_EINTR(pread(file, data, size, offset)); -} - -int ReadPlatformFileCurPosNoBestEffort(PlatformFile file, - char* data, int size) { - base::ThreadRestrictions::AssertIOAllowed(); - if (file < 0 || size < 0) - return -1; - - return HANDLE_EINTR(read(file, data, size)); -} - -int WritePlatformFile(PlatformFile file, int64 offset, - const char* data, int size) { - base::ThreadRestrictions::AssertIOAllowed(); - - if (IsOpenAppend(file)) - return WritePlatformFileAtCurrentPos(file, data, size); - - if (file < 0 || size < 0) - return -1; - - int bytes_written = 0; - int rv; - do { - rv = HANDLE_EINTR(pwrite(file, data + bytes_written, - size - bytes_written, offset + bytes_written)); - if (rv <= 0) - break; - - bytes_written += rv; - } while (bytes_written < size); - - return bytes_written ? bytes_written : rv; -} - -int WritePlatformFileAtCurrentPos(PlatformFile file, - const char* data, int size) { - base::ThreadRestrictions::AssertIOAllowed(); - if (file < 0 || size < 0) - return -1; - - int bytes_written = 0; - int rv; - do { - rv = HANDLE_EINTR(write(file, data + bytes_written, size - bytes_written)); - if (rv <= 0) - break; - - bytes_written += rv; - } while (bytes_written < size); - - return bytes_written ? bytes_written : rv; -} - -int WritePlatformFileCurPosNoBestEffort(PlatformFile file, - const char* data, int size) { - base::ThreadRestrictions::AssertIOAllowed(); - if (file < 0 || size < 0) - return -1; - - return HANDLE_EINTR(write(file, data, size)); -} - -bool TruncatePlatformFile(PlatformFile file, int64 length) { - base::ThreadRestrictions::AssertIOAllowed(); - return ((file >= 0) && !CallFtruncate(file, length)); -} - -bool FlushPlatformFile(PlatformFile file) { - base::ThreadRestrictions::AssertIOAllowed(); - return !CallFsync(file); -} - -bool TouchPlatformFile(PlatformFile file, const base::Time& last_access_time, - const base::Time& last_modified_time) { - base::ThreadRestrictions::AssertIOAllowed(); - if (file < 0) - return false; - - timeval times[2]; - times[0] = last_access_time.ToTimeVal(); - times[1] = last_modified_time.ToTimeVal(); - - return !CallFutimes(file, times); -} - -bool GetPlatformFileInfo(PlatformFile file, PlatformFileInfo* info) { - if (!info) - return false; - - stat_wrapper_t file_info; - if (CallFstat(file, &file_info)) - return false; - - info->is_directory = S_ISDIR(file_info.st_mode); - info->is_symbolic_link = S_ISLNK(file_info.st_mode); - info->size = file_info.st_size; - -#if defined(OS_LINUX) - const time_t last_modified_sec = file_info.st_mtim.tv_sec; - const int64 last_modified_nsec = file_info.st_mtim.tv_nsec; - const time_t last_accessed_sec = file_info.st_atim.tv_sec; - const int64 last_accessed_nsec = file_info.st_atim.tv_nsec; - const time_t creation_time_sec = file_info.st_ctim.tv_sec; - const int64 creation_time_nsec = file_info.st_ctim.tv_nsec; -#elif defined(OS_ANDROID) - const time_t last_modified_sec = file_info.st_mtime; - const int64 last_modified_nsec = file_info.st_mtime_nsec; - const time_t last_accessed_sec = file_info.st_atime; - const int64 last_accessed_nsec = file_info.st_atime_nsec; - const time_t creation_time_sec = file_info.st_ctime; - const int64 creation_time_nsec = file_info.st_ctime_nsec; -#elif defined(OS_MACOSX) || defined(OS_IOS) || defined(OS_BSD) - const time_t last_modified_sec = file_info.st_mtimespec.tv_sec; - const int64 last_modified_nsec = file_info.st_mtimespec.tv_nsec; - const time_t last_accessed_sec = file_info.st_atimespec.tv_sec; - const int64 last_accessed_nsec = file_info.st_atimespec.tv_nsec; - const time_t creation_time_sec = file_info.st_ctimespec.tv_sec; - const int64 creation_time_nsec = file_info.st_ctimespec.tv_nsec; -#else - // TODO(gavinp): Investigate a good high resolution option for OS_NACL. - const time_t last_modified_sec = file_info.st_mtime; - const int64 last_modified_nsec = 0; - const time_t last_accessed_sec = file_info.st_atime; - const int64 last_accessed_nsec = 0; - const time_t creation_time_sec = file_info.st_ctime; - const int64 creation_time_nsec = 0; -#endif - - info->last_modified = - base::Time::FromTimeT(last_modified_sec) + - base::TimeDelta::FromMicroseconds(last_modified_nsec / - base::Time::kNanosecondsPerMicrosecond); - info->last_accessed = - base::Time::FromTimeT(last_accessed_sec) + - base::TimeDelta::FromMicroseconds(last_accessed_nsec / - base::Time::kNanosecondsPerMicrosecond); - info->creation_time = - base::Time::FromTimeT(creation_time_sec) + - base::TimeDelta::FromMicroseconds(creation_time_nsec / - base::Time::kNanosecondsPerMicrosecond); - return true; -} - -PlatformFileError LockPlatformFile(PlatformFile file) { - return CallFctnlFlock(file, true); -} - -PlatformFileError UnlockPlatformFile(PlatformFile file) { - return CallFctnlFlock(file, false); -} - -PlatformFileError ErrnoToPlatformFileError(int saved_errno) { - switch (saved_errno) { - case EACCES: - case EISDIR: - case EROFS: - case EPERM: - return PLATFORM_FILE_ERROR_ACCESS_DENIED; -#if !defined(OS_NACL) // ETXTBSY not defined by NaCl. - case ETXTBSY: - return PLATFORM_FILE_ERROR_IN_USE; -#endif - case EEXIST: - return PLATFORM_FILE_ERROR_EXISTS; - case ENOENT: - return PLATFORM_FILE_ERROR_NOT_FOUND; - case EMFILE: - return PLATFORM_FILE_ERROR_TOO_MANY_OPENED; - case ENOMEM: - return PLATFORM_FILE_ERROR_NO_MEMORY; - case ENOSPC: - return PLATFORM_FILE_ERROR_NO_SPACE; - case ENOTDIR: - return PLATFORM_FILE_ERROR_NOT_A_DIRECTORY; - default: -#if !defined(OS_NACL) // NaCl build has no metrics code. - UMA_HISTOGRAM_SPARSE_SLOWLY("PlatformFile.UnknownErrors.Posix", - saved_errno); -#endif - return PLATFORM_FILE_ERROR_FAILED; - } -} - -} // namespace base diff --git a/base/platform_file_unittest.cc b/base/platform_file_unittest.cc deleted file mode 100644 index 70058ea..0000000 --- a/base/platform_file_unittest.cc +++ /dev/null @@ -1,454 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "base/file_util.h" -#include "base/files/scoped_temp_dir.h" -#include "base/platform_file.h" -#include "base/time/time.h" -#include "testing/gtest/include/gtest/gtest.h" - -namespace base { - -namespace { - -// Reads from a file the given number of bytes, or until EOF is reached. -// Returns the number of bytes read. -int ReadFully(PlatformFile file, int64 offset, char* data, int size) { - return ReadPlatformFile(file, offset, data, size); -} - -// Writes the given number of bytes to a file. -// Returns the number of bytes written. -int WriteFully(PlatformFile file, int64 offset, - const char* data, int size) { - return WritePlatformFile(file, offset, data, size); -} - -PlatformFile CreatePlatformFile(const FilePath& path, - int flags, - bool* created, - PlatformFileError* error) { - File file(path, flags); - if (!file.IsValid()) { - if (error) - *error = static_cast<PlatformFileError>(file.error_details()); - return kInvalidPlatformFileValue; - } - - if (created) - *created = file.created(); - - if (error) - *error = PLATFORM_FILE_OK; - - return file.TakePlatformFile(); -} - -} // namespace - -TEST(PlatformFile, CreatePlatformFile) { - ScopedTempDir temp_dir; - ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); - FilePath file_path = temp_dir.path().AppendASCII("create_file_1"); - - // Open a file that doesn't exist. - PlatformFileError error_code = PLATFORM_FILE_OK; - PlatformFile file = CreatePlatformFile( - file_path, - PLATFORM_FILE_OPEN | PLATFORM_FILE_READ, - NULL, - &error_code); - EXPECT_EQ(kInvalidPlatformFileValue, file); - EXPECT_EQ(PLATFORM_FILE_ERROR_NOT_FOUND, error_code); - - // Open or create a file. - bool created = false; - error_code = PLATFORM_FILE_OK; - file = CreatePlatformFile( - file_path, - PLATFORM_FILE_OPEN_ALWAYS | PLATFORM_FILE_READ, - &created, - &error_code); - EXPECT_NE(kInvalidPlatformFileValue, file); - EXPECT_TRUE(created); - EXPECT_EQ(PLATFORM_FILE_OK, error_code); - ClosePlatformFile(file); - - // Open an existing file. - created = false; - file = CreatePlatformFile( - file_path, - PLATFORM_FILE_OPEN | PLATFORM_FILE_READ, - &created, - &error_code); - EXPECT_NE(kInvalidPlatformFileValue, file); - EXPECT_FALSE(created); - EXPECT_EQ(PLATFORM_FILE_OK, error_code); - ClosePlatformFile(file); - - // Create a file that exists. - file = CreatePlatformFile( - file_path, - PLATFORM_FILE_CREATE | PLATFORM_FILE_READ, - &created, - &error_code); - EXPECT_EQ(kInvalidPlatformFileValue, file); - EXPECT_FALSE(created); - EXPECT_EQ(PLATFORM_FILE_ERROR_EXISTS, error_code); - - // Create or overwrite a file. - error_code = PLATFORM_FILE_OK; - file = CreatePlatformFile( - file_path, - PLATFORM_FILE_CREATE_ALWAYS | PLATFORM_FILE_WRITE, - &created, - &error_code); - EXPECT_NE(kInvalidPlatformFileValue, file); - EXPECT_TRUE(created); - EXPECT_EQ(PLATFORM_FILE_OK, error_code); - ClosePlatformFile(file); - - // Create a delete-on-close file. - created = false; - file_path = temp_dir.path().AppendASCII("create_file_2"); - file = CreatePlatformFile( - file_path, - PLATFORM_FILE_OPEN_ALWAYS | PLATFORM_FILE_DELETE_ON_CLOSE | - PLATFORM_FILE_READ, - &created, - &error_code); - EXPECT_NE(kInvalidPlatformFileValue, file); - EXPECT_TRUE(created); - EXPECT_EQ(PLATFORM_FILE_OK, error_code); - - EXPECT_TRUE(ClosePlatformFile(file)); - EXPECT_FALSE(PathExists(file_path)); -} - -TEST(PlatformFile, DeleteOpenFile) { - ScopedTempDir temp_dir; - ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); - FilePath file_path = temp_dir.path().AppendASCII("create_file_1"); - - // Create a file. - bool created = false; - PlatformFileError error_code = PLATFORM_FILE_OK; - PlatformFile file = CreatePlatformFile( - file_path, - PLATFORM_FILE_OPEN_ALWAYS | PLATFORM_FILE_READ | - PLATFORM_FILE_SHARE_DELETE, - &created, - &error_code); - EXPECT_NE(kInvalidPlatformFileValue, file); - EXPECT_TRUE(created); - EXPECT_EQ(PLATFORM_FILE_OK, error_code); - - // Open an existing file and mark it as delete on close. - created = false; - PlatformFile same_file = CreatePlatformFile( - file_path, - PLATFORM_FILE_OPEN | PLATFORM_FILE_DELETE_ON_CLOSE | - PLATFORM_FILE_READ, - &created, - &error_code); - EXPECT_NE(kInvalidPlatformFileValue, file); - EXPECT_FALSE(created); - EXPECT_EQ(PLATFORM_FILE_OK, error_code); - - // Close both handles and check that the file is gone. - ClosePlatformFile(file); - ClosePlatformFile(same_file); - EXPECT_FALSE(PathExists(file_path)); -} - -TEST(PlatformFile, ReadWritePlatformFile) { - ScopedTempDir temp_dir; - ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); - FilePath file_path = temp_dir.path().AppendASCII("read_write_file"); - PlatformFile file = CreatePlatformFile( - file_path, - PLATFORM_FILE_CREATE | PLATFORM_FILE_READ | - PLATFORM_FILE_WRITE, - NULL, - NULL); - EXPECT_NE(kInvalidPlatformFileValue, file); - - char data_to_write[] = "test"; - const int kTestDataSize = 4; - - // Write 0 bytes to the file. - int bytes_written = WriteFully(file, 0, data_to_write, 0); - EXPECT_EQ(0, bytes_written); - - // Write "test" to the file. - bytes_written = WriteFully(file, 0, data_to_write, kTestDataSize); - EXPECT_EQ(kTestDataSize, bytes_written); - - // Read from EOF. - char data_read_1[32]; - int bytes_read = ReadFully(file, kTestDataSize, data_read_1, kTestDataSize); - EXPECT_EQ(0, bytes_read); - - // Read from somewhere in the middle of the file. - const int kPartialReadOffset = 1; - bytes_read = ReadFully(file, kPartialReadOffset, data_read_1, kTestDataSize); - EXPECT_EQ(kTestDataSize - kPartialReadOffset, bytes_read); - for (int i = 0; i < bytes_read; i++) - EXPECT_EQ(data_to_write[i + kPartialReadOffset], data_read_1[i]); - - // Read 0 bytes. - bytes_read = ReadFully(file, 0, data_read_1, 0); - EXPECT_EQ(0, bytes_read); - - // Read the entire file. - bytes_read = ReadFully(file, 0, data_read_1, kTestDataSize); - EXPECT_EQ(kTestDataSize, bytes_read); - for (int i = 0; i < bytes_read; i++) - EXPECT_EQ(data_to_write[i], data_read_1[i]); - - // Read again, but using the trivial native wrapper. - bytes_read = ReadPlatformFileNoBestEffort(file, 0, data_read_1, - kTestDataSize); - EXPECT_LE(bytes_read, kTestDataSize); - for (int i = 0; i < bytes_read; i++) - EXPECT_EQ(data_to_write[i], data_read_1[i]); - - // Write past the end of the file. - const int kOffsetBeyondEndOfFile = 10; - const int kPartialWriteLength = 2; - bytes_written = WriteFully(file, kOffsetBeyondEndOfFile, - data_to_write, kPartialWriteLength); - EXPECT_EQ(kPartialWriteLength, bytes_written); - - // Make sure the file was extended. - int64 file_size = 0; - EXPECT_TRUE(GetFileSize(file_path, &file_size)); - EXPECT_EQ(kOffsetBeyondEndOfFile + kPartialWriteLength, file_size); - - // Make sure the file was zero-padded. - char data_read_2[32]; - bytes_read = ReadFully(file, 0, data_read_2, static_cast<int>(file_size)); - EXPECT_EQ(file_size, bytes_read); - for (int i = 0; i < kTestDataSize; i++) - EXPECT_EQ(data_to_write[i], data_read_2[i]); - for (int i = kTestDataSize; i < kOffsetBeyondEndOfFile; i++) - EXPECT_EQ(0, data_read_2[i]); - for (int i = kOffsetBeyondEndOfFile; i < file_size; i++) - EXPECT_EQ(data_to_write[i - kOffsetBeyondEndOfFile], data_read_2[i]); - - // Close the file handle to allow the temp directory to be deleted. - ClosePlatformFile(file); -} - -TEST(PlatformFile, AppendPlatformFile) { - ScopedTempDir temp_dir; - ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); - FilePath file_path = temp_dir.path().AppendASCII("append_file"); - PlatformFile file = CreatePlatformFile( - file_path, - PLATFORM_FILE_CREATE | PLATFORM_FILE_APPEND, - NULL, - NULL); - EXPECT_NE(kInvalidPlatformFileValue, file); - - char data_to_write[] = "test"; - const int kTestDataSize = 4; - - // Write 0 bytes to the file. - int bytes_written = WriteFully(file, 0, data_to_write, 0); - EXPECT_EQ(0, bytes_written); - - // Write "test" to the file. - bytes_written = WriteFully(file, 0, data_to_write, kTestDataSize); - EXPECT_EQ(kTestDataSize, bytes_written); - - ClosePlatformFile(file); - file = CreatePlatformFile( - file_path, - PLATFORM_FILE_OPEN | PLATFORM_FILE_READ | - PLATFORM_FILE_APPEND, - NULL, - NULL); - EXPECT_NE(kInvalidPlatformFileValue, file); - - char append_data_to_write[] = "78"; - const int kAppendDataSize = 2; - - // Append "78" to the file. - bytes_written = WriteFully(file, 0, append_data_to_write, kAppendDataSize); - EXPECT_EQ(kAppendDataSize, bytes_written); - - // Read the entire file. - char data_read_1[32]; - int bytes_read = ReadFully(file, 0, data_read_1, - kTestDataSize + kAppendDataSize); - EXPECT_EQ(kTestDataSize + kAppendDataSize, bytes_read); - for (int i = 0; i < kTestDataSize; i++) - EXPECT_EQ(data_to_write[i], data_read_1[i]); - for (int i = 0; i < kAppendDataSize; i++) - EXPECT_EQ(append_data_to_write[i], data_read_1[kTestDataSize + i]); - - // Close the file handle to allow the temp directory to be deleted. - ClosePlatformFile(file); -} - - -TEST(PlatformFile, TruncatePlatformFile) { - ScopedTempDir temp_dir; - ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); - FilePath file_path = temp_dir.path().AppendASCII("truncate_file"); - PlatformFile file = CreatePlatformFile( - file_path, - PLATFORM_FILE_CREATE | PLATFORM_FILE_READ | - PLATFORM_FILE_WRITE, - NULL, - NULL); - EXPECT_NE(kInvalidPlatformFileValue, file); - - // Write "test" to the file. - char data_to_write[] = "test"; - int kTestDataSize = 4; - int bytes_written = WriteFully(file, 0, data_to_write, kTestDataSize); - EXPECT_EQ(kTestDataSize, bytes_written); - - // Extend the file. - const int kExtendedFileLength = 10; - int64 file_size = 0; - EXPECT_TRUE(TruncatePlatformFile(file, kExtendedFileLength)); - EXPECT_TRUE(GetFileSize(file_path, &file_size)); - EXPECT_EQ(kExtendedFileLength, file_size); - - // Make sure the file was zero-padded. - char data_read[32]; - int bytes_read = ReadFully(file, 0, data_read, static_cast<int>(file_size)); - EXPECT_EQ(file_size, bytes_read); - for (int i = 0; i < kTestDataSize; i++) - EXPECT_EQ(data_to_write[i], data_read[i]); - for (int i = kTestDataSize; i < file_size; i++) - EXPECT_EQ(0, data_read[i]); - - // Truncate the file. - const int kTruncatedFileLength = 2; - EXPECT_TRUE(TruncatePlatformFile(file, kTruncatedFileLength)); - EXPECT_TRUE(GetFileSize(file_path, &file_size)); - EXPECT_EQ(kTruncatedFileLength, file_size); - - // Make sure the file was truncated. - bytes_read = ReadFully(file, 0, data_read, kTestDataSize); - EXPECT_EQ(file_size, bytes_read); - for (int i = 0; i < file_size; i++) - EXPECT_EQ(data_to_write[i], data_read[i]); - - // Close the file handle to allow the temp directory to be deleted. - ClosePlatformFile(file); -} - -// Flakily fails: http://crbug.com/86494 -#if defined(OS_ANDROID) -TEST(PlatformFile, TouchGetInfoPlatformFile) { -#else -TEST(PlatformFile, DISABLED_TouchGetInfoPlatformFile) { -#endif - ScopedTempDir temp_dir; - ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); - PlatformFile file = CreatePlatformFile( - temp_dir.path().AppendASCII("touch_get_info_file"), - PLATFORM_FILE_CREATE | PLATFORM_FILE_WRITE | - PLATFORM_FILE_WRITE_ATTRIBUTES, - NULL, - NULL); - EXPECT_NE(kInvalidPlatformFileValue, file); - - // Get info for a newly created file. - PlatformFileInfo info; - EXPECT_TRUE(GetPlatformFileInfo(file, &info)); - - // Add 2 seconds to account for possible rounding errors on - // filesystems that use a 1s or 2s timestamp granularity. - Time now = Time::Now() + TimeDelta::FromSeconds(2); - EXPECT_EQ(0, info.size); - EXPECT_FALSE(info.is_directory); - EXPECT_FALSE(info.is_symbolic_link); - EXPECT_LE(info.last_accessed.ToInternalValue(), now.ToInternalValue()); - EXPECT_LE(info.last_modified.ToInternalValue(), now.ToInternalValue()); - EXPECT_LE(info.creation_time.ToInternalValue(), now.ToInternalValue()); - Time creation_time = info.creation_time; - - // Write "test" to the file. - char data[] = "test"; - const int kTestDataSize = 4; - int bytes_written = WriteFully(file, 0, data, kTestDataSize); - EXPECT_EQ(kTestDataSize, bytes_written); - - // Change the last_accessed and last_modified dates. - // It's best to add values that are multiples of 2 (in seconds) - // to the current last_accessed and last_modified times, because - // FATxx uses a 2s timestamp granularity. - Time new_last_accessed = - info.last_accessed + TimeDelta::FromSeconds(234); - Time new_last_modified = - info.last_modified + TimeDelta::FromMinutes(567); - - EXPECT_TRUE(TouchPlatformFile(file, new_last_accessed, new_last_modified)); - - // Make sure the file info was updated accordingly. - EXPECT_TRUE(GetPlatformFileInfo(file, &info)); - EXPECT_EQ(info.size, kTestDataSize); - EXPECT_FALSE(info.is_directory); - EXPECT_FALSE(info.is_symbolic_link); - - // ext2/ext3 and HPS/HPS+ seem to have a timestamp granularity of 1s. -#if defined(OS_POSIX) - EXPECT_EQ(info.last_accessed.ToTimeVal().tv_sec, - new_last_accessed.ToTimeVal().tv_sec); - EXPECT_EQ(info.last_modified.ToTimeVal().tv_sec, - new_last_modified.ToTimeVal().tv_sec); -#else - EXPECT_EQ(info.last_accessed.ToInternalValue(), - new_last_accessed.ToInternalValue()); - EXPECT_EQ(info.last_modified.ToInternalValue(), - new_last_modified.ToInternalValue()); -#endif - - EXPECT_EQ(info.creation_time.ToInternalValue(), - creation_time.ToInternalValue()); - - // Close the file handle to allow the temp directory to be deleted. - ClosePlatformFile(file); -} - -TEST(PlatformFile, ReadFileAtCurrentPosition) { - ScopedTempDir temp_dir; - ASSERT_TRUE(temp_dir.CreateUniqueTempDir()); - FilePath file_path = - temp_dir.path().AppendASCII("read_file_at_current_position"); - PlatformFile file = CreatePlatformFile( - file_path, - PLATFORM_FILE_CREATE | PLATFORM_FILE_READ | - PLATFORM_FILE_WRITE, - NULL, NULL); - EXPECT_NE(kInvalidPlatformFileValue, file); - - const char kData[] = "test"; - const int kDataSize = arraysize(kData) - 1; - EXPECT_EQ(kDataSize, WriteFully(file, 0, kData, kDataSize)); - - EXPECT_EQ(0, SeekPlatformFile(file, PLATFORM_FILE_FROM_BEGIN, 0)); - - char buffer[kDataSize]; - int first_chunk_size = kDataSize / 2; - EXPECT_EQ(first_chunk_size, - ReadPlatformFileAtCurrentPos( - file, buffer, first_chunk_size)); - EXPECT_EQ(kDataSize - first_chunk_size, - ReadPlatformFileAtCurrentPos( - file, buffer + first_chunk_size, - kDataSize - first_chunk_size)); - EXPECT_EQ(std::string(buffer, buffer + kDataSize), - std::string(kData)); - - ClosePlatformFile(file); -} - -} // namespace base diff --git a/base/platform_file_win.cc b/base/platform_file_win.cc deleted file mode 100644 index af786ab..0000000 --- a/base/platform_file_win.cc +++ /dev/null @@ -1,226 +0,0 @@ -// Copyright (c) 2012 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "base/platform_file.h" - -#include <io.h> - -#include "base/files/file_path.h" -#include "base/logging.h" -#include "base/metrics/sparse_histogram.h" -#include "base/threading/thread_restrictions.h" - -namespace base { - -bool ClosePlatformFile(PlatformFile file) { - base::ThreadRestrictions::AssertIOAllowed(); - return (CloseHandle(file) != 0); -} - -int64 SeekPlatformFile(PlatformFile file, - PlatformFileWhence whence, - int64 offset) { - base::ThreadRestrictions::AssertIOAllowed(); - if (file == kInvalidPlatformFileValue || offset < 0) - return -1; - - LARGE_INTEGER distance, res; - distance.QuadPart = offset; - DWORD move_method = static_cast<DWORD>(whence); - if (!SetFilePointerEx(file, distance, &res, move_method)) - return -1; - return res.QuadPart; -} - -int ReadPlatformFile(PlatformFile file, int64 offset, char* data, int size) { - base::ThreadRestrictions::AssertIOAllowed(); - if (file == kInvalidPlatformFileValue || size < 0) - return -1; - - LARGE_INTEGER offset_li; - offset_li.QuadPart = offset; - - OVERLAPPED overlapped = {0}; - overlapped.Offset = offset_li.LowPart; - overlapped.OffsetHigh = offset_li.HighPart; - - DWORD bytes_read; - if (::ReadFile(file, data, size, &bytes_read, &overlapped) != 0) - return bytes_read; - if (ERROR_HANDLE_EOF == GetLastError()) - return 0; - - return -1; -} - -int ReadPlatformFileAtCurrentPos(PlatformFile file, char* data, int size) { - base::ThreadRestrictions::AssertIOAllowed(); - if (file == kInvalidPlatformFileValue || size < 0) - return -1; - - DWORD bytes_read; - if (::ReadFile(file, data, size, &bytes_read, NULL) != 0) - return bytes_read; - if (ERROR_HANDLE_EOF == GetLastError()) - return 0; - - return -1; -} - -int ReadPlatformFileNoBestEffort(PlatformFile file, int64 offset, char* data, - int size) { - return ReadPlatformFile(file, offset, data, size); -} - -int ReadPlatformFileCurPosNoBestEffort(PlatformFile file, - char* data, int size) { - return ReadPlatformFileAtCurrentPos(file, data, size); -} - -int WritePlatformFile(PlatformFile file, int64 offset, - const char* data, int size) { - base::ThreadRestrictions::AssertIOAllowed(); - if (file == kInvalidPlatformFileValue) - return -1; - - LARGE_INTEGER offset_li; - offset_li.QuadPart = offset; - - OVERLAPPED overlapped = {0}; - overlapped.Offset = offset_li.LowPart; - overlapped.OffsetHigh = offset_li.HighPart; - - DWORD bytes_written; - if (::WriteFile(file, data, size, &bytes_written, &overlapped) != 0) - return bytes_written; - - return -1; -} - -int WritePlatformFileAtCurrentPos(PlatformFile file, const char* data, - int size) { - return WritePlatformFile(file, 0, data, size); -} - -int WritePlatformFileCurPosNoBestEffort(PlatformFile file, - const char* data, int size) { - return WritePlatformFile(file, 0, data, size); -} - -bool TruncatePlatformFile(PlatformFile file, int64 length) { - base::ThreadRestrictions::AssertIOAllowed(); - if (file == kInvalidPlatformFileValue) - return false; - - // Get the current file pointer. - LARGE_INTEGER file_pointer; - LARGE_INTEGER zero; - zero.QuadPart = 0; - if (::SetFilePointerEx(file, zero, &file_pointer, FILE_CURRENT) == 0) - return false; - - LARGE_INTEGER length_li; - length_li.QuadPart = length; - // If length > file size, SetFilePointerEx() should extend the file - // with zeroes on all Windows standard file systems (NTFS, FATxx). - if (!::SetFilePointerEx(file, length_li, NULL, FILE_BEGIN)) - return false; - - // Set the new file length and move the file pointer to its old position. - // This is consistent with ftruncate()'s behavior, even when the file - // pointer points to a location beyond the end of the file. - return ((::SetEndOfFile(file) != 0) && - (::SetFilePointerEx(file, file_pointer, NULL, FILE_BEGIN) != 0)); -} - -bool FlushPlatformFile(PlatformFile file) { - base::ThreadRestrictions::AssertIOAllowed(); - return ((file != kInvalidPlatformFileValue) && ::FlushFileBuffers(file)); -} - -bool TouchPlatformFile(PlatformFile file, const base::Time& last_access_time, - const base::Time& last_modified_time) { - base::ThreadRestrictions::AssertIOAllowed(); - if (file == kInvalidPlatformFileValue) - return false; - - FILETIME last_access_filetime = last_access_time.ToFileTime(); - FILETIME last_modified_filetime = last_modified_time.ToFileTime(); - return (::SetFileTime(file, NULL, &last_access_filetime, - &last_modified_filetime) != 0); -} - -bool GetPlatformFileInfo(PlatformFile file, PlatformFileInfo* info) { - base::ThreadRestrictions::AssertIOAllowed(); - if (!info) - return false; - - BY_HANDLE_FILE_INFORMATION file_info; - if (GetFileInformationByHandle(file, &file_info) == 0) - return false; - - LARGE_INTEGER size; - size.HighPart = file_info.nFileSizeHigh; - size.LowPart = file_info.nFileSizeLow; - info->size = size.QuadPart; - info->is_directory = - (file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0; - info->is_symbolic_link = false; // Windows doesn't have symbolic links. - info->last_modified = base::Time::FromFileTime(file_info.ftLastWriteTime); - info->last_accessed = base::Time::FromFileTime(file_info.ftLastAccessTime); - info->creation_time = base::Time::FromFileTime(file_info.ftCreationTime); - return true; -} - -PlatformFileError LockPlatformFile(PlatformFile file) { - BOOL result = LockFile(file, 0, 0, MAXDWORD, MAXDWORD); - if (!result) - return LastErrorToPlatformFileError(GetLastError()); - return PLATFORM_FILE_OK; -} - -PlatformFileError UnlockPlatformFile(PlatformFile file) { - BOOL result = UnlockFile(file, 0, 0, MAXDWORD, MAXDWORD); - if (!result) - return LastErrorToPlatformFileError(GetLastError()); - return PLATFORM_FILE_OK; -} - -PlatformFileError LastErrorToPlatformFileError(DWORD last_error) { - switch (last_error) { - case ERROR_SHARING_VIOLATION: - return PLATFORM_FILE_ERROR_IN_USE; - case ERROR_FILE_EXISTS: - return PLATFORM_FILE_ERROR_EXISTS; - case ERROR_FILE_NOT_FOUND: - case ERROR_PATH_NOT_FOUND: - return PLATFORM_FILE_ERROR_NOT_FOUND; - case ERROR_ACCESS_DENIED: - return PLATFORM_FILE_ERROR_ACCESS_DENIED; - case ERROR_TOO_MANY_OPEN_FILES: - return PLATFORM_FILE_ERROR_TOO_MANY_OPENED; - case ERROR_OUTOFMEMORY: - case ERROR_NOT_ENOUGH_MEMORY: - return PLATFORM_FILE_ERROR_NO_MEMORY; - case ERROR_HANDLE_DISK_FULL: - case ERROR_DISK_FULL: - case ERROR_DISK_RESOURCES_EXHAUSTED: - return PLATFORM_FILE_ERROR_NO_SPACE; - case ERROR_USER_MAPPED_FILE: - return PLATFORM_FILE_ERROR_INVALID_OPERATION; - case ERROR_NOT_READY: - case ERROR_SECTOR_NOT_FOUND: - case ERROR_DEV_NOT_EXIST: - case ERROR_IO_DEVICE: - case ERROR_FILE_CORRUPT: - case ERROR_DISK_CORRUPT: - return PLATFORM_FILE_ERROR_IO; - default: - UMA_HISTOGRAM_SPARSE_SLOWLY("PlatformFile.UnknownErrors.Windows", - last_error); - return PLATFORM_FILE_ERROR_FAILED; - } -} - -} // namespace base diff --git a/chrome/browser/chromeos/file_system_provider/fileapi/file_stream_reader_unittest.cc b/chrome/browser/chromeos/file_system_provider/fileapi/file_stream_reader_unittest.cc index ac81b54..5c9be74 100644 --- a/chrome/browser/chromeos/file_system_provider/fileapi/file_stream_reader_unittest.cc +++ b/chrome/browser/chromeos/file_system_provider/fileapi/file_stream_reader_unittest.cc @@ -10,7 +10,6 @@ #include "base/files/scoped_temp_dir.h" #include "base/memory/scoped_ptr.h" #include "base/memory/weak_ptr.h" -#include "base/platform_file.h" #include "base/run_loop.h" #include "chrome/browser/chromeos/file_system_provider/fake_provided_file_system.h" #include "chrome/browser/chromeos/file_system_provider/fileapi/file_stream_reader.h" diff --git a/chrome/browser/component_updater/cld_component_installer.cc b/chrome/browser/component_updater/cld_component_installer.cc index 3ec1f59..5028c5b 100644 --- a/chrome/browser/component_updater/cld_component_installer.cc +++ b/chrome/browser/component_updater/cld_component_installer.cc @@ -13,7 +13,6 @@ #include "base/lazy_instance.h" #include "base/logging.h" #include "base/path_service.h" -#include "base/platform_file.h" #include "chrome/browser/profiles/profile.h" #include "chrome/common/chrome_constants.h" #include "chrome/common/chrome_paths.h" diff --git a/chrome/browser/extensions/api/serial/serial_io_handler.h b/chrome/browser/extensions/api/serial/serial_io_handler.h index a98f520..b913e14 100644 --- a/chrome/browser/extensions/api/serial/serial_io_handler.h +++ b/chrome/browser/extensions/api/serial/serial_io_handler.h @@ -6,8 +6,8 @@ #define CHROME_BROWSER_EXTENSIONS_API_SERIAL_SERIAL_IO_HANDLER_H_ #include "base/callback.h" +#include "base/files/file.h" #include "base/memory/ref_counted.h" -#include "base/platform_file.h" #include "base/threading/non_thread_safe.h" #include "chrome/common/extensions/api/serial.h" #include "net/base/io_buffer.h" diff --git a/chrome/browser/translate/translate_browser_test_utils.cc b/chrome/browser/translate/translate_browser_test_utils.cc index aefd8b9..68d611c 100644 --- a/chrome/browser/translate/translate_browser_test_utils.cc +++ b/chrome/browser/translate/translate_browser_test_utils.cc @@ -8,7 +8,6 @@ #include "base/file_util.h" #include "base/logging.h" #include "base/path_service.h" -#include "base/platform_file.h" #include "base/synchronization/lock.h" #include "chrome/browser/component_updater/cld_component_installer.h" #include "chrome/browser/translate/chrome_translate_client.h" diff --git a/chrome/common/chrome_utility_messages.h b/chrome/common/chrome_utility_messages.h index 66e19b4..d8e40c6 100644 --- a/chrome/common/chrome_utility_messages.h +++ b/chrome/common/chrome_utility_messages.h @@ -9,7 +9,6 @@ #include "base/basictypes.h" #include "base/files/file_path.h" -#include "base/platform_file.h" #include "base/strings/string16.h" #include "base/tuple.h" #include "base/values.h" diff --git a/chromeos/dbus/lorgnette_manager_client.cc b/chromeos/dbus/lorgnette_manager_client.cc index 629a60a..eeebc10 100644 --- a/chromeos/dbus/lorgnette_manager_client.cc +++ b/chromeos/dbus/lorgnette_manager_client.cc @@ -11,7 +11,6 @@ #include "base/callback.h" #include "base/location.h" #include "base/memory/ref_counted_memory.h" -#include "base/platform_file.h" #include "base/threading/worker_pool.h" #include "dbus/bus.h" #include "dbus/message.h" diff --git a/chromeos/dbus/lorgnette_manager_client.h b/chromeos/dbus/lorgnette_manager_client.h index 2f3103e..d5ecd4a 100644 --- a/chromeos/dbus/lorgnette_manager_client.h +++ b/chromeos/dbus/lorgnette_manager_client.h @@ -8,8 +8,8 @@ #include <map> #include "base/callback.h" +#include "base/files/file.h" #include "base/memory/ref_counted_memory.h" -#include "base/platform_file.h" #include "chromeos/chromeos_export.h" #include "chromeos/dbus/dbus_client.h" diff --git a/components/breakpad/app/breakpad_linux.cc b/components/breakpad/app/breakpad_linux.cc index e80d753..fe9388c 100644 --- a/components/breakpad/app/breakpad_linux.cc +++ b/components/breakpad/app/breakpad_linux.cc @@ -29,7 +29,6 @@ #include "base/files/file_path.h" #include "base/linux_util.h" #include "base/path_service.h" -#include "base/platform_file.h" #include "base/posix/eintr_wrapper.h" #include "base/posix/global_descriptors.h" #include "base/process/memory.h" diff --git a/components/nacl/renderer/ppb_nacl_private_impl.cc b/components/nacl/renderer/ppb_nacl_private_impl.cc index 5e2f9fe..ece621d 100644 --- a/components/nacl/renderer/ppb_nacl_private_impl.cc +++ b/components/nacl/renderer/ppb_nacl_private_impl.cc @@ -333,7 +333,7 @@ void LaunchSelLdr(PP_Instance instance, routing_id = GetRoutingID(instance); if (!routing_id) { if (nexe_file_info->handle != PP_kInvalidFileHandle) { - base::ClosePlatformFile(nexe_file_info->handle); + base::File closer(nexe_file_info->handle); } ppapi::PpapiGlobals::Get()->GetMainThreadMessageLoop()->PostTask( FROM_HERE, diff --git a/content/browser/renderer_host/pepper/pepper_file_io_host.h b/content/browser/renderer_host/pepper/pepper_file_io_host.h index abef036..d201bf5 100644 --- a/content/browser/renderer_host/pepper/pepper_file_io_host.h +++ b/content/browser/renderer_host/pepper/pepper_file_io_host.h @@ -12,7 +12,6 @@ #include "base/files/file.h" #include "base/files/file_proxy.h" #include "base/memory/weak_ptr.h" -#include "base/platform_file.h" #include "content/browser/renderer_host/pepper/browser_ppapi_host_impl.h" #include "ipc/ipc_listener.h" #include "ipc/ipc_platform_file.h" diff --git a/content/public/renderer/renderer_ppapi_host.h b/content/public/renderer/renderer_ppapi_host.h index 6c843dc..e13c313 100644 --- a/content/public/renderer/renderer_ppapi_host.h +++ b/content/public/renderer/renderer_ppapi_host.h @@ -8,8 +8,8 @@ #include <vector> #include "base/callback_forward.h" +#include "base/files/file.h" #include "base/memory/ref_counted.h" -#include "base/platform_file.h" #include "base/process/process.h" #include "content/common/content_export.h" #include "ipc/ipc_platform_file.h" diff --git a/device/hid/hid_service_linux.cc b/device/hid/hid_service_linux.cc index 7033501..d7ab558 100644 --- a/device/hid/hid_service_linux.cc +++ b/device/hid/hid_service_linux.cc @@ -2,17 +2,18 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "device/hid/hid_service_linux.h" + #include <linux/hidraw.h> #include <sys/ioctl.h> - #include <stdint.h> #include <string> #include "base/bind.h" +#include "base/files/file.h" #include "base/files/file_path.h" #include "base/logging.h" -#include "base/platform_file.h" #include "base/stl_util.h" #include "base/strings/string_number_conversions.h" #include "base/strings/string_piece.h" @@ -20,7 +21,6 @@ #include "device/hid/hid_connection_linux.h" #include "device/hid/hid_device_info.h" #include "device/hid/hid_report_descriptor.h" -#include "device/hid/hid_service_linux.h" #include "device/udev_linux/udev.h" namespace device { diff --git a/mojo/embedder/channel_init.h b/mojo/embedder/channel_init.h index 002bd86..0a5189c 100644 --- a/mojo/embedder/channel_init.h +++ b/mojo/embedder/channel_init.h @@ -5,9 +5,9 @@ #ifndef MOJO_EMBEDDER_CHANNEL_INIT_H_ #define MOJO_EMBEDDER_CHANNEL_INIT_H_ +#include "base/files/file.h" #include "base/memory/ref_counted.h" #include "base/memory/weak_ptr.h" -#include "base/platform_file.h" #include "mojo/public/cpp/system/core.h" #include "mojo/system/system_impl_export.h" |