summaryrefslogtreecommitdiff
path: root/base/files
diff options
context:
space:
mode:
authorAlex Vakulenko <avakulenko@google.com>2016-01-15 13:02:14 -0800
committerAlex Vakulenko <avakulenko@google.com>2016-01-20 14:42:17 -0800
commit0d205d712abd16eeed2f5d5b1052a367d23a223f (patch)
treeb41d6d907d8b307da68edf5d905d2660d1f3a19c /base/files
parentb7972fa941a92a43e7ed703ab262afeb7bcc2a5a (diff)
downloadlibchrome-0d205d712abd16eeed2f5d5b1052a367d23a223f.tar.gz
libchrome: Uprev the library to r369476 from Chromium
Pulled the latest and greatest version of libchrome from Chromium. The merge was done against r369476 which corresponds to git commit 0471d0e2e2ef4a544a63481a389e1df33ea7c00a of Jan 14, 2016 Notable changes are: - base::scoped_ptr<T> is now almost identical to std::unique_ptr<T> No Pass() method, now std::move() is used on scoped pointers - basictypes.h is removed and custom int types such as int32 are now replaced with the standard int32_t and similar from <stdint.h> - String utility functions are cleaned up/refactored. Now all are in base:: namespace, many now return values rather than take pointers for results, ambiguous Booleans are replaced with enums, such as: base::StartsWithASCII(current_url, "https://", false); now is: base::StartsWith(current_url, "https://", base::CompareCase::INSENSITIVE_ASCII); - COMPILE_ASSERT() is now replaced with standard static_assert() - Numeric range constants such as kuint64max are removed in favor of standard <limits> constructs such as std::numeric_limits<uint64_t>::max() - base::Value and derived classes use scoped_ptr<> more and support for raw pointers to base::Value is deprecated and/or removed in many places. - base::MessageLoopProxy is completely removed (was marked deprecated before) - base::MessageLoop::Quit() and QuitClosure are renamed to QuitWhenIdle and QuitWhenIdleClosure for more semantic clarity. Change-Id: I1f5436d253a0a32b2299160a76993752d818736f
Diffstat (limited to 'base/files')
-rw-r--r--base/files/OWNERS3
-rw-r--r--base/files/dir_reader_fallback.h2
-rw-r--r--base/files/dir_reader_linux.h7
-rw-r--r--base/files/dir_reader_posix_unittest.cc1
-rw-r--r--base/files/file.cc53
-rw-r--r--base/files/file.h109
-rw-r--r--base/files/file_enumerator.h7
-rw-r--r--base/files/file_enumerator_posix.cc4
-rw-r--r--base/files/file_path.cc132
-rw-r--r--base/files/file_path.h50
-rw-r--r--base/files/file_path_constants.cc3
-rw-r--r--base/files/file_path_unittest.cc7
-rw-r--r--base/files/file_path_watcher.cc1
-rw-r--r--base/files/file_path_watcher.h7
-rw-r--r--base/files/file_path_watcher_fsevents.cc1
-rw-r--r--base/files/file_path_watcher_fsevents.h2
-rw-r--r--base/files/file_path_watcher_kqueue.cc1
-rw-r--r--base/files/file_path_watcher_kqueue.h1
-rw-r--r--base/files/file_path_watcher_linux.cc33
-rw-r--r--base/files/file_path_watcher_mac.cc1
-rw-r--r--base/files/file_path_watcher_unittest.cc3
-rw-r--r--base/files/file_posix.cc131
-rw-r--r--base/files/file_tracing.cc20
-rw-r--r--base/files/file_tracing.h19
-rw-r--r--base/files/file_unittest.cc85
-rw-r--r--base/files/file_util.cc7
-rw-r--r--base/files/file_util.h29
-rw-r--r--base/files/file_util_mac.mm1
-rw-r--r--base/files/file_util_posix.cc37
-rw-r--r--base/files/important_file_writer.cc65
-rw-r--r--base/files/important_file_writer.h28
-rw-r--r--base/files/important_file_writer_unittest.cc11
-rw-r--r--base/files/memory_mapped_file.h30
-rw-r--r--base/files/scoped_file.cc1
-rw-r--r--base/files/scoped_temp_dir.h1
-rw-r--r--base/files/scoped_temp_dir_unittest.cc1
36 files changed, 401 insertions, 493 deletions
diff --git a/base/files/OWNERS b/base/files/OWNERS
deleted file mode 100644
index b99e8a2fc7..0000000000
--- a/base/files/OWNERS
+++ /dev/null
@@ -1,3 +0,0 @@
-rvargas@chromium.org
-
-per-file file_path_watcher*=mnissler@chromium.org
diff --git a/base/files/dir_reader_fallback.h b/base/files/dir_reader_fallback.h
index d2bf252a6e..4bc199a922 100644
--- a/base/files/dir_reader_fallback.h
+++ b/base/files/dir_reader_fallback.h
@@ -21,7 +21,7 @@ class DirReaderFallback {
bool Next() { return false; }
// Return the name of the current directory entry.
- const char* name() { return 0;}
+ const char* name() { return nullptr;}
// Return the file descriptor which is being used.
int fd() const { return -1; }
diff --git a/base/files/dir_reader_linux.h b/base/files/dir_reader_linux.h
index abf259530b..4ce0c34245 100644
--- a/base/files/dir_reader_linux.h
+++ b/base/files/dir_reader_linux.h
@@ -7,11 +7,13 @@
#include <errno.h>
#include <fcntl.h>
+#include <stddef.h>
#include <stdint.h>
#include <sys/syscall.h>
#include <unistd.h>
#include "base/logging.h"
+#include "base/macros.h"
#include "base/posix/eintr_wrapper.h"
// See the comments in dir_reader_posix.h about this.
@@ -70,7 +72,7 @@ class DirReaderLinux {
const char* name() const {
if (!size_)
- return NULL;
+ return nullptr;
const linux_dirent* dirent =
reinterpret_cast<const linux_dirent*>(&buf_[offset_]);
@@ -88,7 +90,8 @@ class DirReaderLinux {
private:
const int fd_;
unsigned char buf_[512];
- size_t offset_, size_;
+ size_t offset_;
+ size_t size_;
DISALLOW_COPY_AND_ASSIGN(DirReaderLinux);
};
diff --git a/base/files/dir_reader_posix_unittest.cc b/base/files/dir_reader_posix_unittest.cc
index 2e181b3d85..a75858feeb 100644
--- a/base/files/dir_reader_posix_unittest.cc
+++ b/base/files/dir_reader_posix_unittest.cc
@@ -12,6 +12,7 @@
#include "base/files/scoped_temp_dir.h"
#include "base/logging.h"
+#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_ANDROID)
diff --git a/base/files/file.cc b/base/files/file.cc
index 58f80c5232..ab05630062 100644
--- a/base/files/file.cc
+++ b/base/files/file.cc
@@ -7,6 +7,7 @@
#include "base/files/file_tracing.h"
#include "base/metrics/histogram.h"
#include "base/timer/elapsed_timer.h"
+#include "build/build_config.h"
namespace base {
@@ -26,10 +27,8 @@ File::File()
}
#if !defined(OS_NACL)
-File::File(const FilePath& path, uint32 flags)
- : error_details_(FILE_OK),
- created_(false),
- async_(false) {
+File::File(const FilePath& path, uint32_t flags)
+ : error_details_(FILE_OK), created_(false), async_(false) {
Initialize(path, flags);
}
#endif
@@ -50,40 +49,48 @@ File::File(Error error_details)
async_(false) {
}
-File::File(RValue other)
- : file_(other.object->TakePlatformFile()),
- path_(other.object->path_),
- error_details_(other.object->error_details()),
- created_(other.object->created()),
- async_(other.object->async_) {
-}
+File::File(File&& other)
+ : file_(other.TakePlatformFile()),
+ tracing_path_(other.tracing_path_),
+ error_details_(other.error_details()),
+ created_(other.created()),
+ async_(other.async_) {}
File::~File() {
// Go through the AssertIOAllowed logic.
Close();
}
-File& File::operator=(RValue other) {
- if (this != other.object) {
- Close();
- SetPlatformFile(other.object->TakePlatformFile());
- path_ = other.object->path_;
- error_details_ = other.object->error_details();
- created_ = other.object->created();
- async_ = other.object->async_;
- }
+// static
+File File::CreateForAsyncHandle(PlatformFile platform_file) {
+ File file(platform_file);
+ // It would be nice if we could validate that |platform_file| was opened with
+ // FILE_FLAG_OVERLAPPED on Windows but this doesn't appear to be possible.
+ file.async_ = true;
+ return file;
+}
+
+File& File::operator=(File&& other) {
+ DCHECK_NE(this, &other);
+ Close();
+ SetPlatformFile(other.TakePlatformFile());
+ tracing_path_ = other.tracing_path_;
+ error_details_ = other.error_details();
+ created_ = other.created();
+ async_ = other.async_;
return *this;
}
#if !defined(OS_NACL)
-void File::Initialize(const FilePath& path, uint32 flags) {
+void File::Initialize(const FilePath& path, uint32_t flags) {
if (path.ReferencesParent()) {
error_details_ = FILE_ERROR_ACCESS_DENIED;
return;
}
- path_ = path;
+ if (FileTracing::IsCategoryEnabled())
+ tracing_path_ = path;
SCOPED_FILE_TRACE("Initialize");
- DoInitialize(flags);
+ DoInitialize(path, flags);
}
#endif
diff --git a/base/files/file.h b/base/files/file.h
index b21b15972b..7ab5ca5859 100644
--- a/base/files/file.h
+++ b/base/files/file.h
@@ -5,31 +5,26 @@
#ifndef BASE_FILES_FILE_H_
#define BASE_FILES_FILE_H_
-#include "build/build_config.h"
-#if defined(OS_WIN)
-#include <windows.h>
-#endif
-
-#if defined(OS_POSIX)
-#include <sys/stat.h>
-#endif
+#include <stdint.h>
#include <string>
#include "base/base_export.h"
-#include "base/basictypes.h"
#include "base/files/file_path.h"
#include "base/files/file_tracing.h"
#include "base/files/scoped_file.h"
-#include "base/gtest_prod_util.h"
#include "base/move.h"
#include "base/time/time.h"
+#include "build/build_config.h"
#if defined(OS_WIN)
+#include <windows.h>
#include "base/win/scoped_handle.h"
#endif
-FORWARD_DECLARE_TEST(FileTest, MemoryCorruption);
+#if defined(OS_POSIX)
+#include <sys/stat.h>
+#endif
namespace base {
@@ -56,7 +51,7 @@ typedef struct stat64 stat_wrapper_t;
// to the OS is not considered const, even if there is no apparent change to
// member variables.
class BASE_EXPORT File {
- MOVE_ONLY_TYPE_FOR_CPP_03(File, RValue)
+ MOVE_ONLY_TYPE_FOR_CPP_03(File)
public:
// FLAG_(OPEN|CREATE).* are mutually exclusive. You should specify exactly one
@@ -88,6 +83,7 @@ class BASE_EXPORT File {
FLAG_TERMINAL_DEVICE = 1 << 16, // Serial port flags.
FLAG_BACKUP_SEMANTICS = 1 << 17, // Used on Windows only.
FLAG_EXECUTE = 1 << 18, // Used on Windows only.
+ FLAG_SEQUENTIAL_SCAN = 1 << 19, // Used on Windows only.
};
// This enum has been recorded in multiple histograms. If the order of the
@@ -140,7 +136,7 @@ class BASE_EXPORT File {
#endif
// The size of the file in bytes. Undefined when is_directory is true.
- int64 size;
+ int64_t size;
// True if the file corresponds to a directory.
bool is_directory;
@@ -163,7 +159,7 @@ class BASE_EXPORT File {
// Creates or opens the given file. This will fail with 'access denied' if the
// |path| contains path traversal ('..') components.
- File(const FilePath& path, uint32 flags);
+ File(const FilePath& path, uint32_t flags);
// Takes ownership of |platform_file|.
explicit File(PlatformFile platform_file);
@@ -171,17 +167,21 @@ class BASE_EXPORT File {
// Creates an object with a specific error_details code.
explicit File(Error error_details);
- // Move constructor for C++03 move emulation of this type.
- File(RValue other);
+ File(File&& other);
~File();
- // Move operator= for C++03 move emulation of this type.
- File& operator=(RValue other);
+ // Takes ownership of |platform_file|.
+ static File CreateForAsyncHandle(PlatformFile platform_file);
+
+ File& operator=(File&& other);
// Creates or opens the given file.
- void Initialize(const FilePath& path, uint32 flags);
+ void Initialize(const FilePath& path, uint32_t flags);
+ // Returns |true| if the handle / fd wrapped by this object is valid. This
+ // method doesn't interact with the file system (and is safe to be called from
+ // ThreadRestrictions::SetIOAllowed(false) threads).
bool IsValid() const;
// Returns true if a new file was created (or an old one truncated to zero
@@ -205,7 +205,7 @@ class BASE_EXPORT 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.
- int64 Seek(Whence whence, int64 offset);
+ int64_t Seek(Whence whence, int64_t 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
@@ -213,7 +213,7 @@ class BASE_EXPORT File {
// 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.
- int Read(int64 offset, char* data, int size);
+ int Read(int64_t offset, char* data, int size);
// Same as above but without seek.
int ReadAtCurrentPos(char* data, int size);
@@ -221,7 +221,7 @@ class BASE_EXPORT File {
// 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.
- int ReadNoBestEffort(int64 offset, char* data, int size);
+ int ReadNoBestEffort(int64_t offset, char* data, int size);
// Same as above but without seek.
int ReadAtCurrentPosNoBestEffort(char* data, int size);
@@ -232,7 +232,7 @@ class BASE_EXPORT File {
// all platforms.
// Ignores the offset and writes to the end of the file if the file was opened
// with FLAG_APPEND.
- int Write(int64 offset, const char* data, int size);
+ int Write(int64_t offset, const char* data, int size);
// Save as above but without seek.
int WriteAtCurrentPos(const char* data, int size);
@@ -242,12 +242,12 @@ class BASE_EXPORT File {
int WriteAtCurrentPosNoBestEffort(const char* data, int size);
// Returns the current size of this file, or a negative number on failure.
- int64 GetLength();
+ int64_t GetLength();
// Truncates the 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.
- bool SetLength(int64 length);
+ bool SetLength(int64_t length);
// Instructs the filesystem to flush the file to disk. (POSIX: fsync, Windows:
// FlushFileBuffers).
@@ -303,58 +303,11 @@ class BASE_EXPORT File {
static std::string ErrorToString(Error error);
private:
- FRIEND_TEST_ALL_PREFIXES(::FileTest, MemoryCorruption);
-
friend class FileTracing::ScopedTrace;
-#if defined(OS_POSIX)
- // Encloses a single ScopedFD, saving a cheap tamper resistent memory checksum
- // alongside it. This checksum is validated at every access, allowing early
- // detection of memory corruption.
-
- // TODO(gavinp): This is in place temporarily to help us debug
- // https://crbug.com/424562 , which can't be reproduced in valgrind. Remove
- // this code after we have fixed this issue.
- class MemoryCheckingScopedFD {
- public:
- MemoryCheckingScopedFD();
- MemoryCheckingScopedFD(int fd);
- ~MemoryCheckingScopedFD();
-
- bool is_valid() const { Check(); return file_.is_valid(); }
- int get() const { Check(); return file_.get(); }
-
- void reset() { Check(); file_.reset(); UpdateChecksum(); }
- void reset(int fd) { Check(); file_.reset(fd); UpdateChecksum(); }
- int release() {
- Check();
- int fd = file_.release();
- UpdateChecksum();
- return fd;
- }
-
- private:
- FRIEND_TEST_ALL_PREFIXES(::FileTest, MemoryCorruption);
-
- // Computes the checksum for the current value of |file_|. Returns via an
- // out parameter to guard against implicit conversions of unsigned integral
- // types.
- void ComputeMemoryChecksum(unsigned int* out_checksum) const;
-
- // Confirms that the current |file_| and |file_memory_checksum_| agree,
- // failing a CHECK if they do not.
- void Check() const;
-
- void UpdateChecksum();
-
- ScopedFD file_;
- unsigned int file_memory_checksum_;
- };
-#endif
-
- // Creates or opens the given file. Only called if |path_| has no
+ // Creates or opens the given file. Only called if |path| has no
// traversal ('..') components.
- void DoInitialize(uint32 flags);
+ void DoInitialize(const FilePath& path, uint32_t flags);
// TODO(tnagel): Reintegrate into Flush() once histogram isn't needed anymore,
// cf. issue 473337.
@@ -365,11 +318,12 @@ class BASE_EXPORT File {
#if defined(OS_WIN)
win::ScopedHandle file_;
#elif defined(OS_POSIX)
- MemoryCheckingScopedFD file_;
+ ScopedFD file_;
#endif
- // Path that |Initialize()| was called with. Only set if safe (i.e. no '..').
- FilePath path_;
+ // A path to use for tracing purposes. Set if file tracing is enabled during
+ // |Initialize()|.
+ FilePath tracing_path_;
// Object tied to the lifetime of |this| that enables/disables tracing.
FileTracing::ScopedEnabler trace_enabler_;
@@ -382,3 +336,4 @@ class BASE_EXPORT File {
} // namespace base
#endif // BASE_FILES_FILE_H_
+
diff --git a/base/files/file_enumerator.h b/base/files/file_enumerator.h
index 38bb8337c7..7cac8dd9d4 100644
--- a/base/files/file_enumerator.h
+++ b/base/files/file_enumerator.h
@@ -5,12 +5,15 @@
#ifndef BASE_FILES_FILE_ENUMERATOR_H_
#define BASE_FILES_FILE_ENUMERATOR_H_
+#include <stddef.h>
+#include <stdint.h>
+
#include <stack>
#include <vector>
#include "base/base_export.h"
-#include "base/basictypes.h"
#include "base/files/file_path.h"
+#include "base/macros.h"
#include "base/time/time.h"
#include "build/build_config.h"
@@ -49,7 +52,7 @@ class BASE_EXPORT FileEnumerator {
// includes the |root_path| passed into the FileEnumerator constructor.
FilePath GetName() const;
- int64 GetSize() const;
+ int64_t GetSize() const;
Time GetLastModifiedTime() const;
#if defined(OS_WIN)
diff --git a/base/files/file_enumerator_posix.cc b/base/files/file_enumerator_posix.cc
index 7533a24c35..fb4010aadc 100644
--- a/base/files/file_enumerator_posix.cc
+++ b/base/files/file_enumerator_posix.cc
@@ -7,9 +7,11 @@
#include <dirent.h>
#include <errno.h>
#include <fnmatch.h>
+#include <stdint.h>
#include "base/logging.h"
#include "base/threading/thread_restrictions.h"
+#include "build/build_config.h"
namespace base {
@@ -27,7 +29,7 @@ FilePath FileEnumerator::FileInfo::GetName() const {
return filename_;
}
-int64 FileEnumerator::FileInfo::GetSize() const {
+int64_t FileEnumerator::FileInfo::GetSize() const {
return stat_.st_size;
}
diff --git a/base/files/file_path.cc b/base/files/file_path.cc
index b04a01df26..2677258128 100644
--- a/base/files/file_path.cc
+++ b/base/files/file_path.cc
@@ -7,16 +7,14 @@
#include <string.h>
#include <algorithm>
-#include "base/basictypes.h"
#include "base/logging.h"
+#include "base/macros.h"
#include "base/pickle.h"
-
-// These includes are just for the *Hack functions, and should be removed
-// when those functions are removed.
#include "base/strings/string_piece.h"
#include "base/strings/string_util.h"
#include "base/strings/sys_string_conversions.h"
#include "base/strings/utf_string_conversions.h"
+#include "build/build_config.h"
#if defined(OS_MACOSX)
#include "base/mac/scoped_cftyperef.h"
@@ -31,7 +29,8 @@
namespace base {
-typedef FilePath::StringType StringType;
+using StringType = FilePath::StringType;
+using StringPieceType = FilePath::StringPieceType;
namespace {
@@ -45,7 +44,7 @@ const FilePath::CharType kStringTerminator = FILE_PATH_LITERAL('\0');
// otherwise returns npos. This can only be true on Windows, when a pathname
// begins with a letter followed by a colon. On other platforms, this always
// returns npos.
-StringType::size_type FindDriveLetter(const StringType& /* path */) {
+StringPieceType::size_type FindDriveLetter(StringPieceType /* path */) {
#if defined(FILE_PATH_USES_DRIVE_LETTERS)
// This is dependent on an ASCII-based character set, but that's a
// reasonable assumption. iswalpha can be too inclusive here.
@@ -59,26 +58,25 @@ StringType::size_type FindDriveLetter(const StringType& /* path */) {
}
#if defined(FILE_PATH_USES_DRIVE_LETTERS)
-bool EqualDriveLetterCaseInsensitive(const StringType& a,
- const StringType& b) {
+bool EqualDriveLetterCaseInsensitive(StringPieceType a, StringPieceType b) {
size_t a_letter_pos = FindDriveLetter(a);
size_t b_letter_pos = FindDriveLetter(b);
if (a_letter_pos == StringType::npos || b_letter_pos == StringType::npos)
return a == b;
- StringType a_letter(a.substr(0, a_letter_pos + 1));
- StringType b_letter(b.substr(0, b_letter_pos + 1));
- if (!StartsWith(a_letter, b_letter, false))
+ StringPieceType a_letter(a.substr(0, a_letter_pos + 1));
+ StringPieceType b_letter(b.substr(0, b_letter_pos + 1));
+ if (!StartsWith(a_letter, b_letter, CompareCase::INSENSITIVE_ASCII))
return false;
- StringType a_rest(a.substr(a_letter_pos + 1));
- StringType b_rest(b.substr(b_letter_pos + 1));
+ StringPieceType a_rest(a.substr(a_letter_pos + 1));
+ StringPieceType b_rest(b.substr(b_letter_pos + 1));
return a_rest == b_rest;
}
#endif // defined(FILE_PATH_USES_DRIVE_LETTERS)
-bool IsPathAbsolute(const StringType& path) {
+bool IsPathAbsolute(StringPieceType path) {
#if defined(FILE_PATH_USES_DRIVE_LETTERS)
StringType::size_type letter = FindDriveLetter(path);
if (letter != StringType::npos) {
@@ -177,7 +175,8 @@ FilePath::FilePath() {
FilePath::FilePath(const FilePath& that) : path_(that.path_) {
}
-FilePath::FilePath(const StringType& path) : path_(path) {
+FilePath::FilePath(StringPieceType path) {
+ path.CopyToString(&path_);
StringType::size_type nul_pos = path_.find(kStringTerminator);
if (nul_pos != StringType::npos)
path_.erase(nul_pos, StringType::npos);
@@ -279,7 +278,7 @@ bool FilePath::AppendRelativePath(const FilePath& child,
// never case sensitive.
if ((FindDriveLetter(*parent_comp) != StringType::npos) &&
(FindDriveLetter(*child_comp) != StringType::npos)) {
- if (!StartsWith(*parent_comp, *child_comp, false))
+ if (!StartsWith(*parent_comp, *child_comp, CompareCase::INSENSITIVE_ASCII))
return false;
++parent_comp;
++child_comp;
@@ -404,7 +403,7 @@ FilePath FilePath::RemoveFinalExtension() const {
return FilePath(path_.substr(0, dot));
}
-FilePath FilePath::InsertBeforeExtension(const StringType& suffix) const {
+FilePath FilePath::InsertBeforeExtension(StringPieceType suffix) const {
if (suffix.empty())
return FilePath(path_);
@@ -413,27 +412,28 @@ FilePath FilePath::InsertBeforeExtension(const StringType& suffix) const {
StringType ext = Extension();
StringType ret = RemoveExtension().value();
- ret.append(suffix);
+ suffix.AppendToString(&ret);
ret.append(ext);
return FilePath(ret);
}
-FilePath FilePath::InsertBeforeExtensionASCII(const StringPiece& suffix)
+FilePath FilePath::InsertBeforeExtensionASCII(StringPiece suffix)
const {
DCHECK(IsStringASCII(suffix));
#if defined(OS_WIN)
- return InsertBeforeExtension(ASCIIToUTF16(suffix.as_string()));
+ return InsertBeforeExtension(ASCIIToUTF16(suffix));
#elif defined(OS_POSIX)
- return InsertBeforeExtension(suffix.as_string());
+ return InsertBeforeExtension(suffix);
#endif
}
-FilePath FilePath::AddExtension(const StringType& extension) const {
+FilePath FilePath::AddExtension(StringPieceType extension) const {
if (IsEmptyOrSpecialCase(BaseName().value()))
return FilePath();
// If the new extension is "" or ".", then just return the current FilePath.
- if (extension.empty() || extension == StringType(1, kExtensionSeparator))
+ if (extension.empty() ||
+ (extension.size() == 1 && extension[0] == kExtensionSeparator))
return *this;
StringType str = path_;
@@ -441,27 +441,28 @@ FilePath FilePath::AddExtension(const StringType& extension) const {
*(str.end() - 1) != kExtensionSeparator) {
str.append(1, kExtensionSeparator);
}
- str.append(extension);
+ extension.AppendToString(&str);
return FilePath(str);
}
-FilePath FilePath::ReplaceExtension(const StringType& extension) const {
+FilePath FilePath::ReplaceExtension(StringPieceType extension) const {
if (IsEmptyOrSpecialCase(BaseName().value()))
return FilePath();
FilePath no_ext = RemoveExtension();
// If the new extension is "" or ".", then just remove the current extension.
- if (extension.empty() || extension == StringType(1, kExtensionSeparator))
+ if (extension.empty() ||
+ (extension.size() == 1 && extension[0] == kExtensionSeparator))
return no_ext;
StringType str = no_ext.value();
if (extension[0] != kExtensionSeparator)
str.append(1, kExtensionSeparator);
- str.append(extension);
+ extension.AppendToString(&str);
return FilePath(str);
}
-bool FilePath::MatchesExtension(const StringType& extension) const {
+bool FilePath::MatchesExtension(StringPieceType extension) const {
DCHECK(extension.empty() || extension[0] == kExtensionSeparator);
StringType current_extension = Extension();
@@ -472,17 +473,17 @@ bool FilePath::MatchesExtension(const StringType& extension) const {
return FilePath::CompareEqualIgnoreCase(extension, current_extension);
}
-FilePath FilePath::Append(const StringType& component) const {
- const StringType* appended = &component;
+FilePath FilePath::Append(StringPieceType component) const {
+ StringPieceType appended = component;
StringType without_nuls;
StringType::size_type nul_pos = component.find(kStringTerminator);
- if (nul_pos != StringType::npos) {
- without_nuls = component.substr(0, nul_pos);
- appended = &without_nuls;
+ if (nul_pos != StringPieceType::npos) {
+ component.substr(0, nul_pos).CopyToString(&without_nuls);
+ appended = StringPieceType(without_nuls);
}
- DCHECK(!IsPathAbsolute(*appended));
+ DCHECK(!IsPathAbsolute(appended));
if (path_.compare(kCurrentDirectory) == 0) {
// Append normally doesn't do any normalization, but as a special case,
@@ -492,7 +493,7 @@ FilePath FilePath::Append(const StringType& component) const {
// it's likely in practice to wind up with FilePath objects containing
// only kCurrentDirectory when calling DirName on a single relative path
// component.
- return FilePath(*appended);
+ return FilePath(appended);
}
FilePath new_path(path_);
@@ -501,7 +502,7 @@ FilePath FilePath::Append(const StringType& component) const {
// Don't append a separator if the path is empty (indicating the current
// directory) or if the path component is empty (indicating nothing to
// append).
- if (appended->length() > 0 && new_path.path_.length() > 0) {
+ if (appended.length() > 0 && new_path.path_.length() > 0) {
// Don't append a separator if the path still ends with a trailing
// separator after stripping (indicating the root directory).
if (!IsSeparator(new_path.path_[new_path.path_.length() - 1])) {
@@ -512,7 +513,7 @@ FilePath FilePath::Append(const StringType& component) const {
}
}
- new_path.path_.append(*appended);
+ appended.AppendToString(&new_path.path_);
return new_path;
}
@@ -520,12 +521,12 @@ FilePath FilePath::Append(const FilePath& component) const {
return Append(component.value());
}
-FilePath FilePath::AppendASCII(const StringPiece& component) const {
+FilePath FilePath::AppendASCII(StringPiece component) const {
DCHECK(base::IsStringASCII(component));
#if defined(OS_WIN)
- return Append(ASCIIToUTF16(component.as_string()));
+ return Append(ASCIIToUTF16(component));
#elif defined(OS_POSIX)
- return Append(component.as_string());
+ return Append(component);
#endif
}
@@ -680,17 +681,17 @@ bool FilePath::ReadFromPickle(PickleIterator* iter) {
}
#if defined(OS_WIN)
-// Windows specific implementation of file string comparisons
+// Windows specific implementation of file string comparisons.
-int FilePath::CompareIgnoreCase(const StringType& string1,
- const StringType& string2) {
+int FilePath::CompareIgnoreCase(StringPieceType string1,
+ StringPieceType string2) {
// Perform character-wise upper case comparison rather than using the
// fully Unicode-aware CompareString(). For details see:
// http://blogs.msdn.com/michkap/archive/2005/10/17/481600.aspx
- StringType::const_iterator i1 = string1.begin();
- StringType::const_iterator i2 = string2.begin();
- StringType::const_iterator string1end = string1.end();
- StringType::const_iterator string2end = string2.end();
+ StringPieceType::const_iterator i1 = string1.begin();
+ StringPieceType::const_iterator i2 = string2.begin();
+ StringPieceType::const_iterator string1end = string1.end();
+ StringPieceType::const_iterator string2end = string2.end();
for ( ; i1 != string1end && i2 != string2end; ++i1, ++i2) {
wchar_t c1 =
(wchar_t)LOWORD(::CharUpperW((LPWSTR)(DWORD_PTR)MAKELONG(*i1, 0)));
@@ -709,7 +710,7 @@ int FilePath::CompareIgnoreCase(const StringType& string1,
}
#elif defined(OS_MACOSX)
-// Mac OS X specific implementation of file string comparisons
+// Mac OS X specific implementation of file string comparisons.
// cf. http://developer.apple.com/mac/library/technotes/tn/tn1150.html#UnicodeSubtleties
//
@@ -1153,23 +1154,23 @@ inline int HFSReadNextNonIgnorableCodepoint(const char* string,
return codepoint;
}
-} // anonymous namespace
+} // namespace
// Special UTF-8 version of FastUnicodeCompare. Cf:
// http://developer.apple.com/mac/library/technotes/tn/tn1150.html#StringComparisonAlgorithm
// The input strings must be in the special HFS decomposed form.
-int FilePath::HFSFastUnicodeCompare(const StringType& string1,
- const StringType& string2) {
+int FilePath::HFSFastUnicodeCompare(StringPieceType string1,
+ StringPieceType string2) {
int length1 = string1.length();
int length2 = string2.length();
int index1 = 0;
int index2 = 0;
for (;;) {
- int codepoint1 = HFSReadNextNonIgnorableCodepoint(string1.c_str(),
+ int codepoint1 = HFSReadNextNonIgnorableCodepoint(string1.data(),
length1,
&index1);
- int codepoint2 = HFSReadNextNonIgnorableCodepoint(string2.c_str(),
+ int codepoint2 = HFSReadNextNonIgnorableCodepoint(string2.data(),
length2,
&index2);
if (codepoint1 != codepoint2)
@@ -1182,11 +1183,11 @@ int FilePath::HFSFastUnicodeCompare(const StringType& string1,
}
}
-StringType FilePath::GetHFSDecomposedForm(const StringType& string) {
+StringType FilePath::GetHFSDecomposedForm(StringPieceType string) {
ScopedCFTypeRef<CFStringRef> cfstring(
CFStringCreateWithBytesNoCopy(
NULL,
- reinterpret_cast<const UInt8*>(string.c_str()),
+ reinterpret_cast<const UInt8*>(string.data()),
string.length(),
kCFStringEncodingUTF8,
false,
@@ -1215,8 +1216,8 @@ StringType FilePath::GetHFSDecomposedForm(const StringType& string) {
return result;
}
-int FilePath::CompareIgnoreCase(const StringType& string1,
- const StringType& string2) {
+int FilePath::CompareIgnoreCase(StringPieceType string1,
+ StringPieceType string2) {
// Quick checks for empty strings - these speed things up a bit and make the
// following code cleaner.
if (string1.empty())
@@ -1233,7 +1234,7 @@ int FilePath::CompareIgnoreCase(const StringType& string1,
ScopedCFTypeRef<CFStringRef> cfstring1(
CFStringCreateWithBytesNoCopy(
NULL,
- reinterpret_cast<const UInt8*>(string1.c_str()),
+ reinterpret_cast<const UInt8*>(string1.data()),
string1.length(),
kCFStringEncodingUTF8,
false,
@@ -1241,7 +1242,7 @@ int FilePath::CompareIgnoreCase(const StringType& string1,
ScopedCFTypeRef<CFStringRef> cfstring2(
CFStringCreateWithBytesNoCopy(
NULL,
- reinterpret_cast<const UInt8*>(string2.c_str()),
+ reinterpret_cast<const UInt8*>(string2.data()),
string2.length(),
kCFStringEncodingUTF8,
false,
@@ -1256,11 +1257,12 @@ int FilePath::CompareIgnoreCase(const StringType& string1,
#else // << WIN. MACOSX | other (POSIX) >>
-// Generic (POSIX) implementation of file string comparison.
-// TODO(rolandsteiner) check if this is sufficient/correct.
-int FilePath::CompareIgnoreCase(const StringType& string1,
- const StringType& string2) {
- int comparison = strcasecmp(string1.c_str(), string2.c_str());
+// Generic Posix system comparisons.
+int FilePath::CompareIgnoreCase(StringPieceType string1,
+ StringPieceType string2) {
+ // Specifically need null termianted strings for this API call.
+ int comparison = strcasecmp(string1.as_string().c_str(),
+ string2.as_string().c_str());
if (comparison < 0)
return -1;
if (comparison > 0)
@@ -1313,7 +1315,7 @@ FilePath FilePath::NormalizePathSeparatorsTo(CharType /* separator */) const {
#if defined(OS_ANDROID)
bool FilePath::IsContentUri() const {
- return StartsWithASCII(path_, "content://", false /*case_sensitive*/);
+ return StartsWith(path_, "content://", base::CompareCase::INSENSITIVE_ASCII);
}
#endif
diff --git a/base/files/file_path.h b/base/files/file_path.h
index 0c84af6c85..89e9cbfb1d 100644
--- a/base/files/file_path.h
+++ b/base/files/file_path.h
@@ -111,8 +111,9 @@
#include "base/base_export.h"
#include "base/compiler_specific.h"
#include "base/containers/hash_tables.h"
+#include "base/macros.h"
#include "base/strings/string16.h"
-#include "base/strings/string_piece.h" // For implicit conversions.
+#include "base/strings/string_piece.h"
#include "build/build_config.h"
// Windows-style drive letter support and pathname separator characters can be
@@ -124,6 +125,15 @@
#define FILE_PATH_USES_WIN_SEPARATORS
#endif // OS_WIN
+// To print path names portably use PRIsFP (based on PRIuS and friends from
+// C99 and format_macros.h) like this:
+// base::StringPrintf("Path is %" PRIsFP ".\n", path.value().c_str());
+#if defined(OS_POSIX)
+#define PRIsFP "s"
+#elif defined(OS_WIN)
+#define PRIsFP "ls"
+#endif // OS_WIN
+
namespace base {
class Pickle;
@@ -144,6 +154,7 @@ class BASE_EXPORT FilePath {
typedef std::wstring StringType;
#endif // OS_WIN
+ typedef BasicStringPiece<StringType> StringPieceType;
typedef StringType::value_type CharType;
// Null-terminated array of separators used to separate components in
@@ -166,7 +177,7 @@ class BASE_EXPORT FilePath {
FilePath();
FilePath(const FilePath& that);
- explicit FilePath(const StringType& path);
+ explicit FilePath(StringPieceType path);
~FilePath();
FilePath& operator=(const FilePath& that);
@@ -268,25 +279,23 @@ class BASE_EXPORT FilePath {
// path == "C:\pics\jojo" suffix == " (1)", returns "C:\pics\jojo (1)"
// path == "C:\pics.old\jojo" suffix == " (1)", returns "C:\pics.old\jojo (1)"
FilePath InsertBeforeExtension(
- const StringType& suffix) const WARN_UNUSED_RESULT;
+ StringPieceType suffix) const WARN_UNUSED_RESULT;
FilePath InsertBeforeExtensionASCII(
- const base::StringPiece& suffix) const WARN_UNUSED_RESULT;
+ StringPiece suffix) const WARN_UNUSED_RESULT;
// Adds |extension| to |file_name|. Returns the current FilePath if
// |extension| is empty. Returns "" if BaseName() == "." or "..".
- FilePath AddExtension(
- const StringType& extension) const WARN_UNUSED_RESULT;
+ FilePath AddExtension(StringPieceType extension) const WARN_UNUSED_RESULT;
// Replaces the extension of |file_name| with |extension|. If |file_name|
// does not have an extension, then |extension| is added. If |extension| is
// empty, then the extension is removed from |file_name|.
// Returns "" if BaseName() == "." or "..".
- FilePath ReplaceExtension(
- const StringType& extension) const WARN_UNUSED_RESULT;
+ FilePath ReplaceExtension(StringPieceType extension) const WARN_UNUSED_RESULT;
// Returns true if the file path matches the specified extension. The test is
// case insensitive. Don't forget the leading period if appropriate.
- bool MatchesExtension(const StringType& extension) const;
+ bool MatchesExtension(StringPieceType extension) const;
// Returns a FilePath by appending a separator and the supplied path
// component to this object's path. Append takes care to avoid adding
@@ -294,7 +303,7 @@ class BASE_EXPORT FilePath {
// If this object's path is kCurrentDirectory, a new FilePath corresponding
// only to |component| is returned. |component| must be a relative path;
// it is an error to pass an absolute path.
- FilePath Append(const StringType& component) const WARN_UNUSED_RESULT;
+ FilePath Append(StringPieceType component) const WARN_UNUSED_RESULT;
FilePath Append(const FilePath& component) const WARN_UNUSED_RESULT;
// Although Windows StringType is std::wstring, since the encoding it uses for
@@ -303,8 +312,7 @@ class BASE_EXPORT FilePath {
// On Linux, although it can use any 8-bit encoding for paths, we assume that
// ASCII is a valid subset, regardless of the encoding, since many operating
// system paths will always be ASCII.
- FilePath AppendASCII(const base::StringPiece& component)
- const WARN_UNUSED_RESULT;
+ FilePath AppendASCII(StringPiece component) const WARN_UNUSED_RESULT;
// Returns true if this FilePath contains an absolute path. On Windows, an
// absolute path begins with either a drive letter specification followed by
@@ -388,14 +396,14 @@ class BASE_EXPORT FilePath {
// on parts of a file path, e.g., just the extension.
// CompareIgnoreCase() returns -1, 0 or 1 for less-than, equal-to and
// greater-than respectively.
- static int CompareIgnoreCase(const StringType& string1,
- const StringType& string2);
- static bool CompareEqualIgnoreCase(const StringType& string1,
- const StringType& string2) {
+ static int CompareIgnoreCase(StringPieceType string1,
+ StringPieceType string2);
+ static bool CompareEqualIgnoreCase(StringPieceType string1,
+ StringPieceType string2) {
return CompareIgnoreCase(string1, string2) == 0;
}
- static bool CompareLessIgnoreCase(const StringType& string1,
- const StringType& string2) {
+ static bool CompareLessIgnoreCase(StringPieceType string1,
+ StringPieceType string2) {
return CompareIgnoreCase(string1, string2) < 0;
}
@@ -405,14 +413,14 @@ class BASE_EXPORT FilePath {
// http://developer.apple.com/mac/library/technotes/tn/tn1150.html#UnicodeSubtleties
// for further comments.
// Returns the epmty string if the conversion failed.
- static StringType GetHFSDecomposedForm(const FilePath::StringType& string);
+ static StringType GetHFSDecomposedForm(StringPieceType string);
// Special UTF-8 version of FastUnicodeCompare. Cf:
// http://developer.apple.com/mac/library/technotes/tn/tn1150.html#StringComparisonAlgorithm
// IMPORTANT: The input strings must be in the special HFS decomposed form!
// (cf. above GetHFSDecomposedForm method)
- static int HFSFastUnicodeCompare(const StringType& string1,
- const StringType& string2);
+ static int HFSFastUnicodeCompare(StringPieceType string1,
+ StringPieceType string2);
#endif
#if defined(OS_ANDROID)
diff --git a/base/files/file_path_constants.cc b/base/files/file_path_constants.cc
index 34b17a60b7..0b748466c2 100644
--- a/base/files/file_path_constants.cc
+++ b/base/files/file_path_constants.cc
@@ -2,7 +2,10 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include <stddef.h>
+
#include "base/files/file_path.h"
+#include "base/macros.h"
namespace base {
diff --git a/base/files/file_path_unittest.cc b/base/files/file_path_unittest.cc
index 97851ea06d..b1d93a8bc0 100644
--- a/base/files/file_path_unittest.cc
+++ b/base/files/file_path_unittest.cc
@@ -2,12 +2,15 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include <stddef.h>
+
#include <sstream>
-#include "base/basictypes.h"
#include "base/files/file_path.h"
+#include "base/macros.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/scoped_locale.h"
+#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
@@ -1132,7 +1135,7 @@ TEST_F(FilePathTest, FromUTF8Unsafe_And_AsUTF8Unsafe) {
};
#if !defined(SYSTEM_NATIVE_UTF8) && defined(OS_LINUX)
- ScopedLocale locale("en_US.UTF-8");
+ ScopedLocale locale("en_US.UTF-8");
#endif
for (size_t i = 0; i < arraysize(cases); ++i) {
diff --git a/base/files/file_path_watcher.cc b/base/files/file_path_watcher.cc
index 59ae7059ca..955e6a2a5b 100644
--- a/base/files/file_path_watcher.cc
+++ b/base/files/file_path_watcher.cc
@@ -9,6 +9,7 @@
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
+#include "build/build_config.h"
#if defined(OS_MACOSX) && !defined(OS_IOS)
#include "base/mac/mac_util.h"
diff --git a/base/files/file_path_watcher.h b/base/files/file_path_watcher.h
index 4f132aff78..d5c6db1acf 100644
--- a/base/files/file_path_watcher.h
+++ b/base/files/file_path_watcher.h
@@ -8,9 +8,9 @@
#define BASE_FILES_FILE_PATH_WATCHER_H_
#include "base/base_export.h"
-#include "base/basictypes.h"
#include "base/callback.h"
#include "base/files/file_path.h"
+#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/single_thread_task_runner.h"
@@ -62,9 +62,8 @@ class BASE_EXPORT FilePathWatcher {
return task_runner_;
}
- void set_task_runner(
- scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
- task_runner_ = task_runner.Pass();
+ void set_task_runner(scoped_refptr<base::SingleThreadTaskRunner> runner) {
+ task_runner_ = std::move(runner);
}
// Must be called before the PlatformDelegate is deleted.
diff --git a/base/files/file_path_watcher_fsevents.cc b/base/files/file_path_watcher_fsevents.cc
index da01c431bf..78637aa5a5 100644
--- a/base/files/file_path_watcher_fsevents.cc
+++ b/base/files/file_path_watcher_fsevents.cc
@@ -12,6 +12,7 @@
#include "base/logging.h"
#include "base/mac/libdispatch_task_runner.h"
#include "base/mac/scoped_cftyperef.h"
+#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "base/thread_task_runner_handle.h"
diff --git a/base/files/file_path_watcher_fsevents.h b/base/files/file_path_watcher_fsevents.h
index 300aa761df..1ebe4636e4 100644
--- a/base/files/file_path_watcher_fsevents.h
+++ b/base/files/file_path_watcher_fsevents.h
@@ -6,11 +6,13 @@
#define BASE_FILES_FILE_PATH_WATCHER_FSEVENTS_H_
#include <CoreServices/CoreServices.h>
+#include <stddef.h>
#include <vector>
#include "base/files/file_path.h"
#include "base/files/file_path_watcher.h"
+#include "base/macros.h"
namespace base {
diff --git a/base/files/file_path_watcher_kqueue.cc b/base/files/file_path_watcher_kqueue.cc
index a126db708d..b6e61abe41 100644
--- a/base/files/file_path_watcher_kqueue.cc
+++ b/base/files/file_path_watcher_kqueue.cc
@@ -5,6 +5,7 @@
#include "base/files/file_path_watcher_kqueue.h"
#include <fcntl.h>
+#include <stddef.h>
#include <sys/param.h>
#include "base/bind.h"
diff --git a/base/files/file_path_watcher_kqueue.h b/base/files/file_path_watcher_kqueue.h
index 69555a3003..d9db8c2587 100644
--- a/base/files/file_path_watcher_kqueue.h
+++ b/base/files/file_path_watcher_kqueue.h
@@ -10,6 +10,7 @@
#include "base/files/file_path.h"
#include "base/files/file_path_watcher.h"
+#include "base/macros.h"
#include "base/message_loop/message_loop.h"
#include "base/single_thread_task_runner.h"
diff --git a/base/files/file_path_watcher_linux.cc b/base/files/file_path_watcher_linux.cc
index ba2f1d96c8..a75eaba7db 100644
--- a/base/files/file_path_watcher_linux.cc
+++ b/base/files/file_path_watcher_linux.cc
@@ -5,6 +5,7 @@
#include "base/files/file_path_watcher.h"
#include <errno.h>
+#include <stddef.h>
#include <string.h>
#include <sys/inotify.h>
#include <sys/ioctl.h>
@@ -25,9 +26,11 @@
#include "base/lazy_instance.h"
#include "base/location.h"
#include "base/logging.h"
+#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/posix/eintr_wrapper.h"
#include "base/single_thread_task_runner.h"
+#include "base/stl_util.h"
#include "base/synchronization/lock.h"
#include "base/thread_task_runner_handle.h"
#include "base/threading/thread.h"
@@ -167,9 +170,8 @@ class FilePathWatcherImpl : public FilePathWatcher::PlatformDelegate,
void RemoveRecursiveWatches();
// |path| is a symlink to a non-existent target. Attempt to add a watch to
- // the link target's parent directory. Returns true and update |watch_entry|
- // on success.
- bool AddWatchForBrokenSymlink(const FilePath& path, WatchEntry* watch_entry);
+ // the link target's parent directory. Update |watch_entry| on success.
+ void AddWatchForBrokenSymlink(const FilePath& path, WatchEntry* watch_entry);
bool HasValidWatchVector() const;
@@ -513,21 +515,19 @@ void FilePathWatcherImpl::UpdateWatches() {
// Walk the list of watches and update them as we go.
FilePath path(FILE_PATH_LITERAL("/"));
- bool path_valid = true;
for (size_t i = 0; i < watches_.size(); ++i) {
WatchEntry& watch_entry = watches_[i];
InotifyReader::Watch old_watch = watch_entry.watch;
watch_entry.watch = InotifyReader::kInvalidWatch;
watch_entry.linkname.clear();
- if (path_valid) {
- watch_entry.watch = g_inotify_reader.Get().AddWatch(path, this);
- if (watch_entry.watch == InotifyReader::kInvalidWatch) {
- if (IsLink(path)) {
- path_valid = AddWatchForBrokenSymlink(path, &watch_entry);
- } else {
- path_valid = false;
- }
- }
+ watch_entry.watch = g_inotify_reader.Get().AddWatch(path, this);
+ if (watch_entry.watch == InotifyReader::kInvalidWatch) {
+ // Ignore the error code (beyond symlink handling) to attempt to add
+ // watches on accessible children of unreadable directories. Note that
+ // this is a best-effort attempt; we may not catch events in this
+ // scenario.
+ if (IsLink(path))
+ AddWatchForBrokenSymlink(path, &watch_entry);
}
if (old_watch != watch_entry.watch)
g_inotify_reader.Get().RemoveWatch(old_watch, this);
@@ -643,12 +643,12 @@ void FilePathWatcherImpl::RemoveRecursiveWatches() {
recursive_watches_by_path_.clear();
}
-bool FilePathWatcherImpl::AddWatchForBrokenSymlink(const FilePath& path,
+void FilePathWatcherImpl::AddWatchForBrokenSymlink(const FilePath& path,
WatchEntry* watch_entry) {
DCHECK_EQ(InotifyReader::kInvalidWatch, watch_entry->watch);
FilePath link;
if (!ReadSymbolicLink(path, &link))
- return false;
+ return;
if (!link.IsAbsolute())
link = path.DirName().Append(link);
@@ -664,11 +664,10 @@ bool FilePathWatcherImpl::AddWatchForBrokenSymlink(const FilePath& path,
// exist. Ideally we should make sure we've watched all the components of
// the symlink path for changes. See crbug.com/91561 for details.
DPLOG(WARNING) << "Watch failed for " << link.DirName().value();
- return false;
+ return;
}
watch_entry->watch = watch;
watch_entry->linkname = link.BaseName().value();
- return true;
}
bool FilePathWatcherImpl::HasValidWatchVector() const {
diff --git a/base/files/file_path_watcher_mac.cc b/base/files/file_path_watcher_mac.cc
index 6f55ba4f89..7338eafa44 100644
--- a/base/files/file_path_watcher_mac.cc
+++ b/base/files/file_path_watcher_mac.cc
@@ -4,6 +4,7 @@
#include "base/files/file_path_watcher.h"
#include "base/files/file_path_watcher_kqueue.h"
+#include "build/build_config.h"
#if !defined(OS_IOS)
#include "base/files/file_path_watcher_fsevents.h"
diff --git a/base/files/file_path_watcher_unittest.cc b/base/files/file_path_watcher_unittest.cc
index 21e9dd137e..a860b13099 100644
--- a/base/files/file_path_watcher_unittest.cc
+++ b/base/files/file_path_watcher_unittest.cc
@@ -13,7 +13,6 @@
#include <set>
-#include "base/basictypes.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/compiler_specific.h"
@@ -21,6 +20,7 @@
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/location.h"
+#include "base/macros.h"
#include "base/run_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/stl_util.h"
@@ -30,6 +30,7 @@
#include "base/test/test_timeouts.h"
#include "base/thread_task_runner_handle.h"
#include "base/threading/thread.h"
+#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#if defined(OS_ANDROID)
diff --git a/base/files/file_posix.cc b/base/files/file_posix.cc
index 129f5faf4b..12f80c4f8f 100644
--- a/base/files/file_posix.cc
+++ b/base/files/file_posix.cc
@@ -6,6 +6,7 @@
#include <errno.h>
#include <fcntl.h>
+#include <stdint.h>
#include <sys/stat.h>
#include <unistd.h>
@@ -14,6 +15,7 @@
#include "base/posix/eintr_wrapper.h"
#include "base/strings/utf_string_conversions.h"
#include "base/threading/thread_restrictions.h"
+#include "build/build_config.h"
#if defined(OS_ANDROID)
#include "base/os_compat_android.h"
@@ -22,19 +24,19 @@
namespace base {
// Make sure our Whence mappings match the system headers.
-COMPILE_ASSERT(File::FROM_BEGIN == SEEK_SET &&
- File::FROM_CURRENT == SEEK_CUR &&
- File::FROM_END == SEEK_END, whence_matches_system);
+static_assert(File::FROM_BEGIN == SEEK_SET && File::FROM_CURRENT == SEEK_CUR &&
+ File::FROM_END == SEEK_END,
+ "whence mapping must match the system headers");
namespace {
#if defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL)
-static int CallFstat(int fd, stat_wrapper_t *sb) {
+int CallFstat(int fd, stat_wrapper_t *sb) {
ThreadRestrictions::AssertIOAllowed();
return fstat(fd, sb);
}
#else
-static int CallFstat(int fd, stat_wrapper_t *sb) {
+int CallFstat(int fd, stat_wrapper_t *sb) {
ThreadRestrictions::AssertIOAllowed();
return fstat64(fd, sb);
}
@@ -43,15 +45,15 @@ static int CallFstat(int fd, stat_wrapper_t *sb) {
// 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) {
+bool IsOpenAppend(PlatformFile file) {
return (fcntl(file, F_GETFL) & O_APPEND) != 0;
}
-static int CallFtruncate(PlatformFile file, int64 length) {
+int CallFtruncate(PlatformFile file, int64_t length) {
return HANDLE_EINTR(ftruncate(file, length));
}
-static int CallFutimes(PlatformFile file, const struct timeval times[2]) {
+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/
@@ -68,36 +70,36 @@ static int CallFutimes(PlatformFile file, const struct timeval times[2]) {
#endif
}
-static File::Error CallFctnlFlock(PlatformFile file, bool do_lock) {
+File::Error CallFcntlFlock(PlatformFile file, bool do_lock) {
struct flock lock;
- lock.l_type = F_WRLCK;
+ lock.l_type = do_lock ? F_WRLCK : F_UNLCK;
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)
+ if (HANDLE_EINTR(fcntl(file, F_SETLK, &lock)) == -1)
return File::OSErrorToFileError(errno);
return File::FILE_OK;
}
#else // defined(OS_NACL)
-static bool IsOpenAppend(PlatformFile file) {
+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) {
+int CallFtruncate(PlatformFile file, int64_t length) {
NOTIMPLEMENTED(); // NaCl doesn't implement ftruncate.
return 0;
}
-static int CallFutimes(PlatformFile file, const struct timeval times[2]) {
+int CallFutimes(PlatformFile file, const struct timeval times[2]) {
NOTIMPLEMENTED(); // NaCl doesn't implement futimes.
return 0;
}
-static File::Error CallFctnlFlock(PlatformFile file, bool do_lock) {
+File::Error CallFcntlFlock(PlatformFile file, bool do_lock) {
NOTIMPLEMENTED(); // NaCl doesn't implement flock struct.
return File::FILE_ERROR_INVALID_OPERATION;
}
@@ -112,32 +114,32 @@ void File::Info::FromStat(const stat_wrapper_t& stat_info) {
#if defined(OS_LINUX)
time_t last_modified_sec = stat_info.st_mtim.tv_sec;
- int64 last_modified_nsec = stat_info.st_mtim.tv_nsec;
+ int64_t last_modified_nsec = stat_info.st_mtim.tv_nsec;
time_t last_accessed_sec = stat_info.st_atim.tv_sec;
- int64 last_accessed_nsec = stat_info.st_atim.tv_nsec;
+ int64_t last_accessed_nsec = stat_info.st_atim.tv_nsec;
time_t creation_time_sec = stat_info.st_ctim.tv_sec;
- int64 creation_time_nsec = stat_info.st_ctim.tv_nsec;
+ int64_t creation_time_nsec = stat_info.st_ctim.tv_nsec;
#elif defined(OS_ANDROID)
time_t last_modified_sec = stat_info.st_mtime;
- int64 last_modified_nsec = stat_info.st_mtime_nsec;
+ int64_t last_modified_nsec = stat_info.st_mtime_nsec;
time_t last_accessed_sec = stat_info.st_atime;
- int64 last_accessed_nsec = stat_info.st_atime_nsec;
+ int64_t last_accessed_nsec = stat_info.st_atime_nsec;
time_t creation_time_sec = stat_info.st_ctime;
- int64 creation_time_nsec = stat_info.st_ctime_nsec;
+ int64_t creation_time_nsec = stat_info.st_ctime_nsec;
#elif defined(OS_MACOSX) || defined(OS_IOS) || defined(OS_BSD)
time_t last_modified_sec = stat_info.st_mtimespec.tv_sec;
- int64 last_modified_nsec = stat_info.st_mtimespec.tv_nsec;
+ int64_t last_modified_nsec = stat_info.st_mtimespec.tv_nsec;
time_t last_accessed_sec = stat_info.st_atimespec.tv_sec;
- int64 last_accessed_nsec = stat_info.st_atimespec.tv_nsec;
+ int64_t last_accessed_nsec = stat_info.st_atimespec.tv_nsec;
time_t creation_time_sec = stat_info.st_ctimespec.tv_sec;
- int64 creation_time_nsec = stat_info.st_ctimespec.tv_nsec;
+ int64_t creation_time_nsec = stat_info.st_ctimespec.tv_nsec;
#else
time_t last_modified_sec = stat_info.st_mtime;
- int64 last_modified_nsec = 0;
+ int64_t last_modified_nsec = 0;
time_t last_accessed_sec = stat_info.st_atime;
- int64 last_accessed_nsec = 0;
+ int64_t last_accessed_nsec = 0;
time_t creation_time_sec = stat_info.st_ctime;
- int64 creation_time_nsec = 0;
+ int64_t creation_time_nsec = 0;
#endif
last_modified =
@@ -177,7 +179,7 @@ void File::Close() {
file_.reset();
}
-int64 File::Seek(Whence whence, int64 offset) {
+int64_t File::Seek(Whence whence, int64_t offset) {
ThreadRestrictions::AssertIOAllowed();
DCHECK(IsValid());
@@ -186,17 +188,17 @@ int64 File::Seek(Whence whence, int64 offset) {
// Additionally check __BIONIC__ since older versions of Android don't define
// _FILE_OFFSET_BITS.
#if _FILE_OFFSET_BITS != 64 || defined(__BIONIC__)
- COMPILE_ASSERT(sizeof(int64) == sizeof(off64_t), off64_t_64_bit);
+ static_assert(sizeof(int64_t) == sizeof(off64_t), "off64_t must be 64 bits");
return lseek64(file_.get(), static_cast<off64_t>(offset),
static_cast<int>(whence));
#else
- COMPILE_ASSERT(sizeof(int64) == sizeof(off_t), off_t_64_bit);
+ static_assert(sizeof(int64_t) == sizeof(off_t), "off_t must be 64 bits");
return lseek(file_.get(), static_cast<off_t>(offset),
static_cast<int>(whence));
#endif
}
-int File::Read(int64 offset, char* data, int size) {
+int File::Read(int64_t offset, char* data, int size) {
ThreadRestrictions::AssertIOAllowed();
DCHECK(IsValid());
if (size < 0)
@@ -239,7 +241,7 @@ int File::ReadAtCurrentPos(char* data, int size) {
return bytes_read ? bytes_read : rv;
}
-int File::ReadNoBestEffort(int64 offset, char* data, int size) {
+int File::ReadNoBestEffort(int64_t offset, char* data, int size) {
ThreadRestrictions::AssertIOAllowed();
DCHECK(IsValid());
SCOPED_FILE_TRACE_WITH_SIZE("ReadNoBestEffort", size);
@@ -256,7 +258,7 @@ int File::ReadAtCurrentPosNoBestEffort(char* data, int size) {
return HANDLE_EINTR(read(file_.get(), data, size));
}
-int File::Write(int64 offset, const char* data, int size) {
+int File::Write(int64_t offset, const char* data, int size) {
ThreadRestrictions::AssertIOAllowed();
if (IsOpenAppend(file_.get()))
@@ -314,7 +316,7 @@ int File::WriteAtCurrentPosNoBestEffort(const char* data, int size) {
return HANDLE_EINTR(write(file_.get(), data, size));
}
-int64 File::GetLength() {
+int64_t File::GetLength() {
DCHECK(IsValid());
SCOPED_FILE_TRACE("GetLength");
@@ -326,7 +328,7 @@ int64 File::GetLength() {
return file_info.st_size;
}
-bool File::SetLength(int64 length) {
+bool File::SetLength(int64_t length) {
ThreadRestrictions::AssertIOAllowed();
DCHECK(IsValid());
@@ -362,12 +364,12 @@ bool File::GetInfo(Info* info) {
File::Error File::Lock() {
SCOPED_FILE_TRACE("Lock");
- return CallFctnlFlock(file_.get(), true);
+ return CallFcntlFlock(file_.get(), true);
}
File::Error File::Unlock() {
SCOPED_FILE_TRACE("Unlock");
- return CallFctnlFlock(file_.get(), false);
+ return CallFcntlFlock(file_.get(), false);
}
File File::Duplicate() {
@@ -383,7 +385,7 @@ File File::Duplicate() {
File other(other_fd);
if (async())
other.async_ = true;
- return other.Pass();
+ return other;
}
// Static.
@@ -422,53 +424,10 @@ File::Error File::OSErrorToFileError(int saved_errno) {
}
}
-File::MemoryCheckingScopedFD::MemoryCheckingScopedFD() {
- UpdateChecksum();
-}
-
-File::MemoryCheckingScopedFD::MemoryCheckingScopedFD(int fd) : file_(fd) {
- UpdateChecksum();
-}
-
-File::MemoryCheckingScopedFD::~MemoryCheckingScopedFD() {}
-
-// static
-void File::MemoryCheckingScopedFD::ComputeMemoryChecksum(
- unsigned int* out_checksum) const {
- // Use a single iteration of a linear congruentional generator (lcg) to
- // provide a cheap checksum unlikely to be accidentally matched by a random
- // memory corruption.
-
- // By choosing constants that satisfy the Hull-Duebell Theorem on lcg cycle
- // length, we insure that each distinct fd value maps to a distinct checksum,
- // which maximises the utility of our checksum.
-
- // This code uses "unsigned int" throughout for its defined modular semantics,
- // which implicitly gives us a divisor that is a power of two.
-
- const unsigned int kMultiplier = 13035 * 4 + 1;
- COMPILE_ASSERT(((kMultiplier - 1) & 3) == 0, pred_must_be_multiple_of_four);
- const unsigned int kIncrement = 1595649551;
- COMPILE_ASSERT(kIncrement & 1, must_be_coprime_to_powers_of_two);
-
- *out_checksum =
- static_cast<unsigned int>(file_.get()) * kMultiplier + kIncrement;
-}
-
-void File::MemoryCheckingScopedFD::Check() const {
- unsigned int computed_checksum;
- ComputeMemoryChecksum(&computed_checksum);
- CHECK_EQ(file_memory_checksum_, computed_checksum) << "corrupted fd memory";
-}
-
-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(uint32 flags) {
+void File::DoInitialize(const FilePath& path, uint32_t flags) {
ThreadRestrictions::AssertIOAllowed();
DCHECK(!IsValid());
@@ -516,14 +475,14 @@ void File::DoInitialize(uint32 flags) {
else if (flags & FLAG_APPEND)
open_flags |= O_APPEND | O_WRONLY;
- COMPILE_ASSERT(O_RDONLY == 0, O_RDONLY_must_equal_zero);
+ static_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(path_.value().c_str(), open_flags, mode));
+ int descriptor = HANDLE_EINTR(open(path.value().c_str(), open_flags, mode));
if (flags & FLAG_OPEN_ALWAYS) {
if (descriptor < 0) {
@@ -531,7 +490,7 @@ void File::DoInitialize(uint32 flags) {
if (flags & FLAG_EXCLUSIVE_READ || flags & FLAG_EXCLUSIVE_WRITE)
open_flags |= O_EXCL; // together with O_CREAT implies O_NOFOLLOW
- descriptor = HANDLE_EINTR(open(path_.value().c_str(), open_flags, mode));
+ descriptor = HANDLE_EINTR(open(path.value().c_str(), open_flags, mode));
if (descriptor >= 0)
created_ = true;
}
@@ -546,7 +505,7 @@ void File::DoInitialize(uint32 flags) {
created_ = true;
if (flags & FLAG_DELETE_ON_CLOSE)
- unlink(path_.value().c_str());
+ unlink(path.value().c_str());
async_ = ((flags & FLAG_ASYNC) == FLAG_ASYNC);
error_details_ = FILE_OK;
diff --git a/base/files/file_tracing.cc b/base/files/file_tracing.cc
index c25772db8a..6d11cbc746 100644
--- a/base/files/file_tracing.cc
+++ b/base/files/file_tracing.cc
@@ -13,6 +13,11 @@ FileTracing::Provider* g_provider = nullptr;
}
// static
+bool FileTracing::IsCategoryEnabled() {
+ return g_provider && g_provider->FileTracingCategoryIsEnabled();
+}
+
+// static
void FileTracing::SetProvider(FileTracing::Provider* provider) {
g_provider = provider;
}
@@ -34,19 +39,12 @@ FileTracing::ScopedTrace::~ScopedTrace() {
g_provider->FileTracingEventEnd(name_, id_);
}
-bool FileTracing::ScopedTrace::ShouldInitialize() const {
- return g_provider && g_provider->FileTracingCategoryIsEnabled();
-}
-
-void FileTracing::ScopedTrace::Initialize(
- const char* name, File* file, int64 size) {
- if (!g_provider)
- return;
-
+void FileTracing::ScopedTrace::Initialize(const char* name,
+ File* file,
+ int64_t size) {
id_ = &file->trace_enabler_;
name_ = name;
-
- g_provider->FileTracingEventBegin(name_, id_, file->path_, size);
+ g_provider->FileTracingEventBegin(name_, id_, file->tracing_path_, size);
}
} // namespace base
diff --git a/base/files/file_tracing.h b/base/files/file_tracing.h
index aca1ff7546..bedd7be64b 100644
--- a/base/files/file_tracing.h
+++ b/base/files/file_tracing.h
@@ -5,15 +5,16 @@
#ifndef BASE_FILES_FILE_TRACING_H_
#define BASE_FILES_FILE_TRACING_H_
+#include <stdint.h>
+
#include "base/base_export.h"
-#include "base/basictypes.h"
#include "base/macros.h"
#define FILE_TRACING_PREFIX "File"
#define SCOPED_FILE_TRACE_WITH_SIZE(name, size) \
FileTracing::ScopedTrace scoped_file_trace; \
- if (scoped_file_trace.ShouldInitialize()) \
+ if (FileTracing::IsCategoryEnabled()) \
scoped_file_trace.Initialize(FILE_TRACING_PREFIX "::" name, this, size)
#define SCOPED_FILE_TRACE(name) SCOPED_FILE_TRACE_WITH_SIZE(name, 0)
@@ -25,6 +26,9 @@ class FilePath;
class BASE_EXPORT FileTracing {
public:
+ // Whether the file tracing category is enabled.
+ static bool IsCategoryEnabled();
+
class Provider {
public:
virtual ~Provider() = default;
@@ -41,8 +45,10 @@ class BASE_EXPORT FileTracing {
// Begins an event for |id| with |name|. |path| tells where in the directory
// structure the event is happening (and may be blank). |size| is the number
// of bytes involved in the event.
- virtual void FileTracingEventBegin(
- const char* name, void* id, const FilePath& path, int64 size) = 0;
+ virtual void FileTracingEventBegin(const char* name,
+ void* id,
+ const FilePath& path,
+ int64_t size) = 0;
// Ends an event for |id| with |name|.
virtual void FileTracingEventEnd(const char* name, void* id) = 0;
@@ -63,14 +69,11 @@ class BASE_EXPORT FileTracing {
ScopedTrace();
~ScopedTrace();
- // Whether this trace should be initialized or not.
- bool ShouldInitialize() const;
-
// Called only if the tracing category is enabled. |name| is the name of the
// event to trace (e.g. "Read", "Write") and must have an application
// lifetime (e.g. static or literal). |file| is the file being traced; must
// outlive this class. |size| is the size (in bytes) of this event.
- void Initialize(const char* name, File* file, int64 size);
+ void Initialize(const char* name, File* file, int64_t size);
private:
// The ID of this trace. Based on the |file| passed to |Initialize()|. Must
diff --git a/base/files/file_unittest.cc b/base/files/file_unittest.cc
index 5c594242bc..2445f7e128 100644
--- a/base/files/file_unittest.cc
+++ b/base/files/file_unittest.cc
@@ -3,10 +3,15 @@
// found in the LICENSE file.
#include "base/files/file.h"
+
+#include <stdint.h>
+
+#include <utility>
+
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
-#include "base/memory/scoped_ptr.h"
#include "base/time/time.h"
+#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
using base::File;
@@ -200,7 +205,7 @@ TEST(FileTest, ReadWrite) {
EXPECT_EQ(kPartialWriteLength, bytes_written);
// Make sure the file was extended.
- int64 file_size = 0;
+ int64_t file_size = 0;
EXPECT_TRUE(GetFileSize(file_path, &file_size));
EXPECT_EQ(kOffsetBeyondEndOfFile + kPartialWriteLength, file_size);
@@ -241,7 +246,7 @@ TEST(FileTest, Append) {
ASSERT_TRUE(file2.IsValid());
// Test passing the file around.
- file = file2.Pass();
+ file = std::move(file2);
EXPECT_FALSE(file2.IsValid());
ASSERT_TRUE(file.IsValid());
@@ -282,7 +287,7 @@ TEST(FileTest, Length) {
// Extend the file.
const int kExtendedFileLength = 10;
- int64 file_size = 0;
+ int64_t file_size = 0;
EXPECT_TRUE(file.SetLength(kExtendedFileLength));
EXPECT_EQ(kExtendedFileLength, file.GetLength());
EXPECT_TRUE(GetFileSize(file_path, &file_size));
@@ -435,7 +440,7 @@ TEST(FileTest, Seek) {
base::File::FLAG_WRITE);
ASSERT_TRUE(file.IsValid());
- const int64 kOffset = 10;
+ const int64_t kOffset = 10;
EXPECT_EQ(kOffset, file.Seek(base::File::FROM_BEGIN, kOffset));
EXPECT_EQ(2 * kOffset, file.Seek(base::File::FROM_CURRENT, kOffset));
EXPECT_EQ(kOffset, file.Seek(base::File::FROM_CURRENT, -kOffset));
@@ -495,7 +500,7 @@ TEST(FileTest, GetInfoForDirectory) {
base::File dir(
::CreateFile(empty_dir.value().c_str(),
- FILE_ALL_ACCESS,
+ GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
@@ -510,71 +515,3 @@ TEST(FileTest, GetInfoForDirectory) {
EXPECT_EQ(0, info.size);
}
#endif // defined(OS_WIN)
-
-#if defined(OS_POSIX) && defined(GTEST_HAS_DEATH_TEST)
-TEST(FileTest, MemoryCorruption) {
- {
- // Test that changing the checksum value is detected.
- base::File file;
- EXPECT_NE(file.file_.file_memory_checksum_,
- implicit_cast<unsigned int>(file.GetPlatformFile()));
- file.file_.file_memory_checksum_ = file.GetPlatformFile();
- EXPECT_DEATH(file.IsValid(), "");
-
- file.file_.UpdateChecksum(); // Do not crash on File::~File().
- }
-
- {
- // Test that changing the file descriptor value is detected.
- base::File file;
- file.file_.file_.reset(17);
- EXPECT_DEATH(file.IsValid(), "");
-
- // Do not crash on File::~File().
- ignore_result(file.file_.file_.release());
- file.file_.UpdateChecksum();
- }
-
- {
- // Test that GetPlatformFile() checks for corruption.
- base::File file;
- file.file_.file_memory_checksum_ = file.GetPlatformFile();
- EXPECT_DEATH(file.GetPlatformFile(), "");
-
- file.file_.UpdateChecksum(); // Do not crash on File::~File().
- }
-
- {
- // Test that the base::File destructor checks for corruption.
- scoped_ptr<base::File> file(new File());
- file->file_.file_memory_checksum_ = file->GetPlatformFile();
- EXPECT_DEATH(file.reset(), "");
-
- // Do not crash on this thread's destructor call.
- file->file_.UpdateChecksum();
- }
-
- {
- // Test that the base::File constructor checks for corruption.
- base::File file;
- file.file_.file_memory_checksum_ = file.GetPlatformFile();
- EXPECT_DEATH(File f(file.Pass()), "");
-
- file.file_.UpdateChecksum(); // Do not crash on File::~File().
- }
-
- {
- // Test that doing IO checks for corruption.
- base::File file;
- file.file_.file_.reset(17); // A fake open FD value.
-
- EXPECT_DEATH(file.Seek(File::FROM_BEGIN, 0), "");
- EXPECT_DEATH(file.Read(0, NULL, 0), "");
- EXPECT_DEATH(file.ReadAtCurrentPos(NULL, 0), "");
- EXPECT_DEATH(file.Write(0, NULL, 0), "");
-
- ignore_result(file.file_.file_.release());
- file.file_.UpdateChecksum();
- }
-}
-#endif // defined(OS_POSIX)
diff --git a/base/files/file_util.cc b/base/files/file_util.cc
index 4b6b8886d8..9e35b671db 100644
--- a/base/files/file_util.cc
+++ b/base/files/file_util.cc
@@ -19,6 +19,7 @@
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
+#include "build/build_config.h"
namespace base {
@@ -34,8 +35,8 @@ static const int kMaxUniqueFiles = 100;
} // namespace
-int64 ComputeDirectorySize(const FilePath& root_path) {
- int64 running_size = 0;
+int64_t ComputeDirectorySize(const FilePath& root_path) {
+ int64_t running_size = 0;
FileEnumerator file_iter(root_path, true, FileEnumerator::FILES);
while (!file_iter.Next().empty())
running_size += file_iter.GetInfo().GetSize();
@@ -185,7 +186,7 @@ bool CreateDirectory(const FilePath& full_path) {
return CreateDirectoryAndGetError(full_path, NULL);
}
-bool GetFileSize(const FilePath& file_path, int64* file_size) {
+bool GetFileSize(const FilePath& file_path, int64_t* file_size) {
File::Info info;
if (!GetFileInfo(file_path, &info))
return false;
diff --git a/base/files/file_util.h b/base/files/file_util.h
index 7f169f1c54..dfc10a35bf 100644
--- a/base/files/file_util.h
+++ b/base/files/file_util.h
@@ -8,15 +8,8 @@
#ifndef BASE_FILES_FILE_UTIL_H_
#define BASE_FILES_FILE_UTIL_H_
-#include "build/build_config.h"
-
-#if defined(OS_WIN)
-#include <windows.h>
-#elif defined(OS_POSIX)
-#include <sys/stat.h>
-#include <unistd.h>
-#endif
-
+#include <stddef.h>
+#include <stdint.h>
#include <stdio.h>
#include <set>
@@ -24,11 +17,18 @@
#include <vector>
#include "base/base_export.h"
-#include "base/basictypes.h"
#include "base/files/file.h"
#include "base/files/file_path.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/string16.h"
+#include "build/build_config.h"
+
+#if defined(OS_WIN)
+#include <windows.h>
+#elif defined(OS_POSIX)
+#include <sys/stat.h>
+#include <unistd.h>
+#endif
#if defined(OS_POSIX)
#include "base/file_descriptor_posix.h"
@@ -53,7 +53,7 @@ BASE_EXPORT FilePath MakeAbsoluteFilePath(const FilePath& input);
//
// This function is implemented using the FileEnumerator class so it is not
// particularly speedy in any platform.
-BASE_EXPORT int64 ComputeDirectorySize(const FilePath& root_path);
+BASE_EXPORT int64_t ComputeDirectorySize(const FilePath& root_path);
// Deletes the given path, whether it's a file or a directory.
// If it's a directory, it's perfectly happy to delete all of the
@@ -265,7 +265,7 @@ BASE_EXPORT bool CreateDirectoryAndGetError(const FilePath& full_path,
BASE_EXPORT bool CreateDirectory(const FilePath& full_path);
// Returns the file size. Returns true on success.
-BASE_EXPORT bool GetFileSize(const FilePath& file_path, int64* file_size);
+BASE_EXPORT bool GetFileSize(const FilePath& file_path, int64_t* file_size);
// Sets |real_path| to |path| with symbolic links and junctions expanded.
// On windows, make sure the path starts with a lettered drive.
@@ -350,6 +350,11 @@ BASE_EXPORT bool SetCurrentDirectory(const FilePath& path);
BASE_EXPORT int GetUniquePathNumber(const FilePath& path,
const FilePath::StringType& suffix);
+// Sets the given |fd| to non-blocking mode.
+// Returns true if it was able to set it in the non-blocking mode, otherwise
+// false.
+BASE_EXPORT bool SetNonBlocking(int fd);
+
#if defined(OS_POSIX)
// Test that |path| can only be changed by a given user and members of
// a given set of groups.
diff --git a/base/files/file_util_mac.mm b/base/files/file_util_mac.mm
index a701bad928..e9c6c65159 100644
--- a/base/files/file_util_mac.mm
+++ b/base/files/file_util_mac.mm
@@ -7,7 +7,6 @@
#include <copyfile.h>
#import <Foundation/Foundation.h>
-#include "base/basictypes.h"
#include "base/files/file_path.h"
#include "base/mac/foundation_util.h"
#include "base/strings/string_util.h"
diff --git a/base/files/file_util_posix.cc b/base/files/file_util_posix.cc
index f70a82f241..e2e44465e5 100644
--- a/base/files/file_util_posix.cc
+++ b/base/files/file_util_posix.cc
@@ -9,6 +9,7 @@
#include <fcntl.h>
#include <libgen.h>
#include <limits.h>
+#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -21,18 +22,11 @@
#include <time.h>
#include <unistd.h>
-#if defined(OS_MACOSX)
-#include <AvailabilityMacros.h>
-#include "base/mac/foundation_util.h"
-#elif !defined(OS_CHROMEOS) && defined(USE_GLIB)
-#include <glib.h> // for g_get_home_dir()
-#endif
-
-#include "base/basictypes.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/files/scoped_file.h"
#include "base/logging.h"
+#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/singleton.h"
#include "base/posix/eintr_wrapper.h"
@@ -44,6 +38,12 @@
#include "base/sys_info.h"
#include "base/threading/thread_restrictions.h"
#include "base/time/time.h"
+#include "build/build_config.h"
+
+#if defined(OS_MACOSX)
+#include <AvailabilityMacros.h>
+#include "base/mac/foundation_util.h"
+#endif
#if defined(OS_ANDROID)
#include "base/android/content_uri_utils.h"
@@ -350,6 +350,17 @@ bool CopyDirectory(const FilePath& from_path,
}
#endif // !defined(OS_NACL_NONSFI)
+bool SetNonBlocking(int fd) {
+ int flags = fcntl(fd, F_GETFL, 0);
+ if (flags == -1)
+ return false;
+ if (flags & O_NONBLOCK)
+ return true;
+ if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
+ return false;
+ return true;
+}
+
bool PathExists(const FilePath& path) {
ThreadRestrictions::AssertIOAllowed();
#if defined(OS_ANDROID)
@@ -480,16 +491,6 @@ FilePath GetHomeDir() {
#if defined(OS_ANDROID)
DLOG(WARNING) << "OS_ANDROID: Home directory lookup not yet implemented.";
-#elif defined(USE_GLIB) && !defined(OS_CHROMEOS)
- // g_get_home_dir calls getpwent, which can fall through to LDAP calls so
- // this may do I/O. However, it should be rare that $HOME is not defined and
- // this is typically called from the path service which has no threading
- // restrictions. The path service will cache the result which limits the
- // badness of blocking on I/O. As a result, we don't have a thread
- // restriction here.
- home_dir = g_get_home_dir();
- if (home_dir && home_dir[0])
- return FilePath(home_dir);
#endif
FilePath rv;
diff --git a/base/files/important_file_writer.cc b/base/files/important_file_writer.cc
index 814fc7b9ad..b4293053a6 100644
--- a/base/files/important_file_writer.cc
+++ b/base/files/important_file_writer.cc
@@ -4,9 +4,11 @@
#include "base/files/important_file_writer.h"
+#include <stddef.h>
+#include <stdint.h>
#include <stdio.h>
-
#include <string>
+#include <utility>
#include "base/bind.h"
#include "base/critical_closure.h"
@@ -15,13 +17,16 @@
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/logging.h"
+#include "base/macros.h"
#include "base/metrics/histogram.h"
+#include "base/numerics/safe_conversions.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/task_runner.h"
#include "base/task_runner_util.h"
#include "base/threading/thread.h"
#include "base/time/time.h"
+#include "build/build_config.h"
namespace base {
@@ -47,8 +52,7 @@ void LogFailure(const FilePath& path, TempFileFailure failure_code,
const std::string& message) {
UMA_HISTOGRAM_ENUMERATION("ImportantFile.TempFileFailures", failure_code,
TEMP_FILE_FAILURE_MAX);
- DPLOG(WARNING) << "temp file failure: " << path.value().c_str()
- << " : " << message;
+ DPLOG(WARNING) << "temp file failure: " << path.value() << " : " << message;
}
// Helper function to call WriteFileAtomically() with a scoped_ptr<std::string>.
@@ -72,16 +76,16 @@ bool ImportantFileWriter::WriteFileAtomically(const FilePath& path,
char path[128];
} file_info;
file_info.data_size = data.size();
- base::strlcpy(file_info.path, path.value().c_str(),
- arraysize(file_info.path));
- base::debug::Alias(&file_info);
+ strlcpy(file_info.path, path.value().c_str(), arraysize(file_info.path));
+ debug::Alias(&file_info);
#endif
+
// Write the data to a temp file then rename to avoid data loss if we crash
// while writing the file. Ensure that the temp file is on the same volume
// as target file, so it can be moved in one step, and that the temp file
// is securely created.
FilePath tmp_file_path;
- if (!base::CreateTemporaryFileInDir(path.DirName(), &tmp_file_path)) {
+ if (!CreateTemporaryFileInDir(path.DirName(), &tmp_file_path)) {
LogFailure(path, FAILED_CREATING, "could not create temporary file");
return false;
}
@@ -92,29 +96,28 @@ bool ImportantFileWriter::WriteFileAtomically(const FilePath& path,
return false;
}
- // If this happens in the wild something really bad is going on.
- CHECK_LE(data.length(), static_cast<size_t>(kint32max));
- int bytes_written = tmp_file.Write(0, data.data(),
- static_cast<int>(data.length()));
+ // If this fails in the wild, something really bad is going on.
+ const int data_length = checked_cast<int32_t>(data.length());
+ int bytes_written = tmp_file.Write(0, data.data(), data_length);
bool flush_success = tmp_file.Flush();
tmp_file.Close();
- if (bytes_written < static_cast<int>(data.length())) {
+ if (bytes_written < data_length) {
LogFailure(path, FAILED_WRITING, "error writing, bytes_written=" +
IntToString(bytes_written));
- base::DeleteFile(tmp_file_path, false);
+ DeleteFile(tmp_file_path, false);
return false;
}
if (!flush_success) {
LogFailure(path, FAILED_FLUSHING, "error flushing");
- base::DeleteFile(tmp_file_path, false);
+ DeleteFile(tmp_file_path, false);
return false;
}
- if (!base::ReplaceFile(tmp_file_path, path, NULL)) {
+ if (!ReplaceFile(tmp_file_path, path, nullptr)) {
LogFailure(path, FAILED_RENAMING, "could not rename temporary file");
- base::DeleteFile(tmp_file_path, false);
+ DeleteFile(tmp_file_path, false);
return false;
}
@@ -123,11 +126,21 @@ bool ImportantFileWriter::WriteFileAtomically(const FilePath& path,
ImportantFileWriter::ImportantFileWriter(
const FilePath& path,
- const scoped_refptr<base::SequencedTaskRunner>& task_runner)
+ const scoped_refptr<SequencedTaskRunner>& task_runner)
+ : ImportantFileWriter(
+ path,
+ task_runner,
+ TimeDelta::FromMilliseconds(kDefaultCommitIntervalMs)) {
+}
+
+ImportantFileWriter::ImportantFileWriter(
+ const FilePath& path,
+ const scoped_refptr<SequencedTaskRunner>& task_runner,
+ TimeDelta interval)
: path_(path),
task_runner_(task_runner),
- serializer_(NULL),
- commit_interval_(TimeDelta::FromMilliseconds(kDefaultCommitIntervalMs)),
+ serializer_(nullptr),
+ commit_interval_(interval),
weak_factory_(this) {
DCHECK(CalledOnValidThread());
DCHECK(task_runner_);
@@ -147,7 +160,7 @@ bool ImportantFileWriter::HasPendingWrite() const {
void ImportantFileWriter::WriteNow(scoped_ptr<std::string> data) {
DCHECK(CalledOnValidThread());
- if (data->length() > static_cast<size_t>(kint32max)) {
+ if (!IsValueInRangeForNumericType<int32_t>(data->length())) {
NOTREACHED();
return;
}
@@ -182,16 +195,16 @@ void ImportantFileWriter::DoScheduledWrite() {
DCHECK(serializer_);
scoped_ptr<std::string> data(new std::string);
if (serializer_->SerializeData(data.get())) {
- WriteNow(data.Pass());
+ WriteNow(std::move(data));
} else {
DLOG(WARNING) << "failed to serialize data to be saved in "
- << path_.value().c_str();
+ << path_.value();
}
- serializer_ = NULL;
+ serializer_ = nullptr;
}
void ImportantFileWriter::RegisterOnNextSuccessfulWriteCallback(
- const base::Closure& on_next_successful_write) {
+ const Closure& on_next_successful_write) {
DCHECK(on_next_successful_write_.is_null());
on_next_successful_write_ = on_next_successful_write;
}
@@ -203,7 +216,7 @@ bool ImportantFileWriter::PostWriteTask(const Callback<bool()>& task) {
// suppressing all of those is unrealistic hence we avoid most of them by
// using PostTask() in the typical scenario below.
if (!on_next_successful_write_.is_null()) {
- return base::PostTaskAndReplyWithResult(
+ return PostTaskAndReplyWithResult(
task_runner_.get(),
FROM_HERE,
MakeCriticalClosure(task),
@@ -212,7 +225,7 @@ bool ImportantFileWriter::PostWriteTask(const Callback<bool()>& task) {
}
return task_runner_->PostTask(
FROM_HERE,
- MakeCriticalClosure(base::Bind(IgnoreResult(task))));
+ MakeCriticalClosure(Bind(IgnoreResult(task))));
}
void ImportantFileWriter::ForwardSuccessfulWrite(bool result) {
diff --git a/base/files/important_file_writer.h b/base/files/important_file_writer.h
index 99f1a7c681..1b2ad5caed 100644
--- a/base/files/important_file_writer.h
+++ b/base/files/important_file_writer.h
@@ -8,9 +8,9 @@
#include <string>
#include "base/base_export.h"
-#include "base/basictypes.h"
#include "base/callback.h"
#include "base/files/file_path.h"
+#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/threading/non_thread_safe.h"
#include "base/time/time.h"
@@ -62,9 +62,13 @@ class BASE_EXPORT ImportantFileWriter : public NonThreadSafe {
// |task_runner| is the SequencedTaskRunner instance where on which we will
// execute file I/O operations.
// All non-const methods, ctor and dtor must be called on the same thread.
- ImportantFileWriter(
- const FilePath& path,
- const scoped_refptr<base::SequencedTaskRunner>& task_runner);
+ ImportantFileWriter(const FilePath& path,
+ const scoped_refptr<SequencedTaskRunner>& task_runner);
+
+ // Same as above, but with a custom commit interval.
+ ImportantFileWriter(const FilePath& path,
+ const scoped_refptr<SequencedTaskRunner>& task_runner,
+ TimeDelta interval);
// You have to ensure that there are no pending writes at the moment
// of destruction.
@@ -77,7 +81,7 @@ class BASE_EXPORT ImportantFileWriter : public NonThreadSafe {
bool HasPendingWrite() const;
// Save |data| to target filename. Does not block. If there is a pending write
- // scheduled by ScheduleWrite, it is cancelled.
+ // scheduled by ScheduleWrite(), it is cancelled.
void WriteNow(scoped_ptr<std::string> data);
// Schedule a save to target filename. Data will be serialized and saved
@@ -94,16 +98,12 @@ class BASE_EXPORT ImportantFileWriter : public NonThreadSafe {
// Registers |on_next_successful_write| to be called once, on the next
// successful write event. Only one callback can be set at once.
void RegisterOnNextSuccessfulWriteCallback(
- const base::Closure& on_next_successful_write);
+ const Closure& on_next_successful_write);
TimeDelta commit_interval() const {
return commit_interval_;
}
- void set_commit_interval(const TimeDelta& interval) {
- commit_interval_ = interval;
- }
-
private:
// Helper method for WriteNow().
bool PostWriteTask(const Callback<bool()>& task);
@@ -113,22 +113,22 @@ class BASE_EXPORT ImportantFileWriter : public NonThreadSafe {
void ForwardSuccessfulWrite(bool result);
// Invoked once and then reset on the next successful write event.
- base::Closure on_next_successful_write_;
+ Closure on_next_successful_write_;
// Path being written to.
const FilePath path_;
// TaskRunner for the thread on which file I/O can be done.
- const scoped_refptr<base::SequencedTaskRunner> task_runner_;
+ const scoped_refptr<SequencedTaskRunner> task_runner_;
// Timer used to schedule commit after ScheduleWrite.
- OneShotTimer<ImportantFileWriter> timer_;
+ OneShotTimer timer_;
// Serializer which will provide the data to be saved.
DataSerializer* serializer_;
// Time delta after which scheduled data will be written to disk.
- TimeDelta commit_interval_;
+ const TimeDelta commit_interval_;
WeakPtrFactory<ImportantFileWriter> weak_factory_;
diff --git a/base/files/important_file_writer_unittest.cc b/base/files/important_file_writer_unittest.cc
index d376cdc35a..28e6001a00 100644
--- a/base/files/important_file_writer_unittest.cc
+++ b/base/files/important_file_writer_unittest.cc
@@ -11,6 +11,7 @@
#include "base/files/scoped_temp_dir.h"
#include "base/location.h"
#include "base/logging.h"
+#include "base/macros.h"
#include "base/run_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/thread_task_runner_handle.h"
@@ -145,8 +146,9 @@ TEST_F(ImportantFileWriterTest, BasicWithSuccessfulWriteObserver) {
}
TEST_F(ImportantFileWriterTest, ScheduleWrite) {
- ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get());
- writer.set_commit_interval(TimeDelta::FromMilliseconds(25));
+ ImportantFileWriter writer(file_,
+ ThreadTaskRunnerHandle::Get(),
+ TimeDelta::FromMilliseconds(25));
EXPECT_FALSE(writer.HasPendingWrite());
DataSerializer serializer("foo");
writer.ScheduleWrite(&serializer);
@@ -177,8 +179,9 @@ TEST_F(ImportantFileWriterTest, DoScheduledWrite) {
}
TEST_F(ImportantFileWriterTest, BatchingWrites) {
- ImportantFileWriter writer(file_, ThreadTaskRunnerHandle::Get());
- writer.set_commit_interval(TimeDelta::FromMilliseconds(25));
+ ImportantFileWriter writer(file_,
+ ThreadTaskRunnerHandle::Get(),
+ TimeDelta::FromMilliseconds(25));
DataSerializer foo("foo"), bar("bar"), baz("baz");
writer.ScheduleWrite(&foo);
writer.ScheduleWrite(&bar);
diff --git a/base/files/memory_mapped_file.h b/base/files/memory_mapped_file.h
index 96d1d91f93..6362e765cb 100644
--- a/base/files/memory_mapped_file.h
+++ b/base/files/memory_mapped_file.h
@@ -5,9 +5,12 @@
#ifndef BASE_FILES_MEMORY_MAPPED_FILE_H_
#define BASE_FILES_MEMORY_MAPPED_FILE_H_
+#include <stddef.h>
+#include <stdint.h>
+
#include "base/base_export.h"
-#include "base/basictypes.h"
#include "base/files/file.h"
+#include "base/macros.h"
#include "build/build_config.h"
#if defined(OS_WIN)
@@ -28,21 +31,14 @@ class BASE_EXPORT MemoryMappedFile {
struct BASE_EXPORT Region {
static const Region kWholeFile;
- Region();
- Region(int64 offset, int64 size);
-
bool operator==(const Region& other) const;
bool operator!=(const Region& other) const;
// Start of the region (measured in bytes from the beginning of the file).
- int64 offset;
+ int64_t offset;
// Length of the region in bytes.
- int64 size;
-
- private:
- // Used by kWholeFile.
- Region(base::LinkerInitialized);
+ int64_t size;
};
// Opens an existing file and maps it into memory. Access is restricted to
@@ -65,7 +61,7 @@ class BASE_EXPORT MemoryMappedFile {
bool InitializeAsImageSection(const FilePath& file_name);
#endif // OS_WIN
- const uint8* data() const { return data_; }
+ const uint8_t* data() const { return data_; }
size_t length() const { return length_; }
// Is file_ a valid file handle that points to an open, memory mapped file?
@@ -78,11 +74,11 @@ class BASE_EXPORT MemoryMappedFile {
// - |aligned_start| is page aligned and <= |start|.
// - |aligned_size| is a multiple of the VM granularity and >= |size|.
// - |offset| is the displacement of |start| w.r.t |aligned_start|.
- static void CalculateVMAlignedBoundaries(int64 start,
- int64 size,
- int64* aligned_start,
- int64* aligned_size,
- int32* offset);
+ static void CalculateVMAlignedBoundaries(int64_t start,
+ int64_t size,
+ int64_t* aligned_start,
+ int64_t* aligned_size,
+ int32_t* offset);
// Map the file to memory, set data_ to that memory address. Return true on
// success, false on any kind of failure. This is a helper for Initialize().
@@ -92,7 +88,7 @@ class BASE_EXPORT MemoryMappedFile {
void CloseHandles();
File file_;
- uint8* data_;
+ uint8_t* data_;
size_t length_;
#if defined(OS_WIN)
diff --git a/base/files/scoped_file.cc b/base/files/scoped_file.cc
index 39f064de1c..8971280776 100644
--- a/base/files/scoped_file.cc
+++ b/base/files/scoped_file.cc
@@ -5,6 +5,7 @@
#include "base/files/scoped_file.h"
#include "base/logging.h"
+#include "build/build_config.h"
#if defined(OS_POSIX)
#include <unistd.h>
diff --git a/base/files/scoped_temp_dir.h b/base/files/scoped_temp_dir.h
index 5f63e09cff..b1f2f5b874 100644
--- a/base/files/scoped_temp_dir.h
+++ b/base/files/scoped_temp_dir.h
@@ -17,6 +17,7 @@
#include "base/base_export.h"
#include "base/files/file_path.h"
+#include "base/macros.h"
namespace base {
diff --git a/base/files/scoped_temp_dir_unittest.cc b/base/files/scoped_temp_dir_unittest.cc
index a19f34ddce..3b2f28e50e 100644
--- a/base/files/scoped_temp_dir_unittest.cc
+++ b/base/files/scoped_temp_dir_unittest.cc
@@ -7,6 +7,7 @@
#include "base/files/file.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
+#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {