diff options
author | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-17 23:09:59 +0000 |
---|---|---|
committer | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-03-17 23:09:59 +0000 |
commit | 5f6d952f1b8460ffc12691d32b22abe8f1fc48bb (patch) | |
tree | 8290216ab13c68eb943fb74fa9896c0ef452e5d4 /base/test | |
parent | 2f3b1cce4c8d4d6a4b016f4d03fca5ee88348a2c (diff) | |
download | chromium_src-5f6d952f1b8460ffc12691d32b22abe8f1fc48bb.zip chromium_src-5f6d952f1b8460ffc12691d32b22abe8f1fc48bb.tar.gz chromium_src-5f6d952f1b8460ffc12691d32b22abe8f1fc48bb.tar.bz2 |
Move some functions in test_file_util to base namespace.
R=viettrungluu@chromium.org
TBR=viettrungluu
Review URL: https://codereview.chromium.org/200883002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@257515 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/test')
-rw-r--r-- | base/test/test_file_util.cc | 2 | ||||
-rw-r--r-- | base/test/test_file_util.h | 18 | ||||
-rw-r--r-- | base/test/test_file_util_linux.cc | 16 | ||||
-rw-r--r-- | base/test/test_file_util_mac.cc | 10 | ||||
-rw-r--r-- | base/test/test_file_util_posix.cc | 23 | ||||
-rw-r--r-- | base/test/test_file_util_win.cc | 40 |
6 files changed, 61 insertions, 48 deletions
diff --git a/base/test/test_file_util.cc b/base/test/test_file_util.cc index eef89de..8dafc58 100644 --- a/base/test/test_file_util.cc +++ b/base/test/test_file_util.cc @@ -13,7 +13,7 @@ bool EvictFileFromSystemCacheWithRetry(const FilePath& path) { const int kCycles = 10; const TimeDelta kDelay = TestTimeouts::action_timeout() / kCycles; for (int i = 0; i < kCycles; i++) { - if (file_util::EvictFileFromSystemCache(path)) + if (EvictFileFromSystemCache(path)) return true; PlatformThread::Sleep(kDelay); } diff --git a/base/test/test_file_util.h b/base/test/test_file_util.h index 656babd..776b776 100644 --- a/base/test/test_file_util.h +++ b/base/test/test_file_util.h @@ -27,31 +27,31 @@ class FilePath; // rely on uncached files. bool EvictFileFromSystemCacheWithRetry(const FilePath& file); -} // namespace base - -// TODO(brettw) move all of this to the base namespace. -namespace file_util { - // Wrapper over base::Delete. On Windows repeatedly invokes Delete in case // of failure to workaround Windows file locking semantics. Returns true on // success. -bool DieFileDie(const base::FilePath& file, bool recurse); +bool DieFileDie(const FilePath& file, bool recurse); // Clear a specific file from the system cache. After this call, trying // to access this file will result in a cold load from the hard drive. -bool EvictFileFromSystemCache(const base::FilePath& file); +bool EvictFileFromSystemCache(const FilePath& file); #if defined(OS_WIN) // Returns true if the volume supports Alternate Data Streams. -bool VolumeSupportsADS(const base::FilePath& path); +bool VolumeSupportsADS(const FilePath& path); // Returns true if the ZoneIdentifier is correctly set to "Internet" (3). // Note that this function must be called from the same process as // the one that set the zone identifier. I.e. don't use it in UI/automation // based tests. -bool HasInternetZoneIdentifier(const base::FilePath& full_path); +bool HasInternetZoneIdentifier(const FilePath& full_path); #endif // defined(OS_WIN) +} // namespace base + +// TODO(brettw) move all of this to the base namespace. +namespace file_util { + // In general it's not reliable to convert a FilePath to a wstring and we use // string16 elsewhere for Unicode strings, but in tests it is frequently // convenient to be able to compare paths to literals like L"foobar". diff --git a/base/test/test_file_util_linux.cc b/base/test/test_file_util_linux.cc index 7eb0533..0ef5c0a 100644 --- a/base/test/test_file_util_linux.cc +++ b/base/test/test_file_util_linux.cc @@ -10,19 +10,19 @@ #include <unistd.h> #include "base/files/file_path.h" +#include "base/files/scoped_file.h" -namespace file_util { +namespace base { -bool EvictFileFromSystemCache(const base::FilePath& file) { - int fd = open(file.value().c_str(), O_RDONLY); - if (fd < 0) +bool EvictFileFromSystemCache(const FilePath& file) { + ScopedFD fd(open(file.value().c_str(), O_RDONLY)); + if (!fd.is_valid()) return false; - if (fdatasync(fd) != 0) + if (fdatasync(fd.get()) != 0) return false; - if (posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED) != 0) + if (posix_fadvise(fd.get(), 0, 0, POSIX_FADV_DONTNEED) != 0) return false; - close(fd); return true; } -} // namespace file_util +} // namespace base diff --git a/base/test/test_file_util_mac.cc b/base/test/test_file_util_mac.cc index d31a9a6..a4ed1d3 100644 --- a/base/test/test_file_util_mac.cc +++ b/base/test/test_file_util_mac.cc @@ -11,16 +11,16 @@ #include "base/file_util.h" #include "base/files/memory_mapped_file.h" -namespace file_util { +namespace base { -bool EvictFileFromSystemCache(const base::FilePath& file) { +bool EvictFileFromSystemCache(const FilePath& file) { // There aren't any really direct ways to purge a file from the UBC. From // talking with Amit Singh, the safest is to mmap the file with MAP_FILE (the // default) + MAP_SHARED, then do an msync to invalidate the memory. The next // open should then have to load the file from disk. int64 length; - if (!base::GetFileSize(file, &length)) { + if (!GetFileSize(file, &length)) { DLOG(ERROR) << "failed to get size of " << file.value(); return false; } @@ -32,7 +32,7 @@ bool EvictFileFromSystemCache(const base::FilePath& file) { return true; } - base::MemoryMappedFile mapped_file; + MemoryMappedFile mapped_file; if (!mapped_file.Initialize(file)) { DLOG(WARNING) << "failed to memory map " << file.value(); return false; @@ -48,4 +48,4 @@ bool EvictFileFromSystemCache(const base::FilePath& file) { return true; } -} // namespace file_util +} // namespace base diff --git a/base/test/test_file_util_posix.cc b/base/test/test_file_util_posix.cc index 1f9efd6..ae14bc8 100644 --- a/base/test/test_file_util_posix.cc +++ b/base/test/test_file_util_posix.cc @@ -19,12 +19,12 @@ using base::MakeAbsoluteFilePath; -namespace file_util { +namespace base { namespace { // Deny |permission| on the file |path|. -bool DenyFilePermission(const base::FilePath& path, mode_t permission) { +bool DenyFilePermission(const FilePath& path, mode_t permission) { struct stat stat_buf; if (stat(path.value().c_str(), &stat_buf) != 0) return false; @@ -37,7 +37,7 @@ bool DenyFilePermission(const base::FilePath& path, mode_t permission) { // Gets a blob indicating the permission information for |path|. // |length| is the length of the blob. Zero on failure. // Returns the blob pointer, or NULL on failure. -void* GetPermissionInfo(const base::FilePath& path, size_t* length) { +void* GetPermissionInfo(const FilePath& path, size_t* length) { DCHECK(length); *length = 0; @@ -57,8 +57,7 @@ void* GetPermissionInfo(const base::FilePath& path, size_t* length) { // |info| is the pointer to the blob. // |length| is the length of the blob. // Either |info| or |length| may be NULL/0, in which case nothing happens. -bool RestorePermissionInfo(const base::FilePath& path, - void* info, size_t length) { +bool RestorePermissionInfo(const FilePath& path, void* info, size_t length) { if (!info || (length == 0)) return false; @@ -74,20 +73,28 @@ bool RestorePermissionInfo(const base::FilePath& path, } // namespace -bool DieFileDie(const base::FilePath& file, bool recurse) { +bool DieFileDie(const FilePath& file, bool recurse) { // There is no need to workaround Windows problems on POSIX. // Just pass-through. - return base::DeleteFile(file, recurse); + return DeleteFile(file, recurse); } #if !defined(OS_LINUX) && !defined(OS_MACOSX) -bool EvictFileFromSystemCache(const base::FilePath& file) { +bool EvictFileFromSystemCache(const FilePath& file) { // There doesn't seem to be a POSIX way to cool the disk cache. NOTIMPLEMENTED(); return false; } #endif +} // namespace base + +namespace file_util { + +using base::DenyFilePermission; +using base::GetPermissionInfo; +using base::RestorePermissionInfo; + std::wstring FilePathAsWString(const base::FilePath& path) { return base::UTF8ToWide(path.value()); } diff --git a/base/test/test_file_util_win.cc b/base/test/test_file_util_win.cc index 63c8da2..ae3f0d5 100644 --- a/base/test/test_file_util_win.cc +++ b/base/test/test_file_util_win.cc @@ -17,7 +17,7 @@ #include "base/threading/platform_thread.h" #include "base/win/scoped_handle.h" -namespace file_util { +namespace base { static const ptrdiff_t kOneMB = 1024 * 1024; @@ -29,7 +29,7 @@ struct PermissionInfo { }; // Deny |permission| on the file |path|, for the current user. -bool DenyFilePermission(const base::FilePath& path, DWORD permission) { +bool DenyFilePermission(const FilePath& path, DWORD permission) { PACL old_dacl; PSECURITY_DESCRIPTOR security_descriptor; if (GetNamedSecurityInfo(const_cast<wchar_t*>(path.value().c_str()), @@ -67,7 +67,7 @@ bool DenyFilePermission(const base::FilePath& path, DWORD permission) { // Gets a blob indicating the permission information for |path|. // |length| is the length of the blob. Zero on failure. // Returns the blob pointer, or NULL on failure. -void* GetPermissionInfo(const base::FilePath& path, size_t* length) { +void* GetPermissionInfo(const FilePath& path, size_t* length) { DCHECK(length != NULL); *length = 0; PACL dacl = NULL; @@ -93,8 +93,7 @@ void* GetPermissionInfo(const base::FilePath& path, size_t* length) { // |info| is the pointer to the blob. // |length| is the length of the blob. // Either |info| or |length| may be NULL/0, in which case nothing happens. -bool RestorePermissionInfo(const base::FilePath& path, - void* info, size_t length) { +bool RestorePermissionInfo(const FilePath& path, void* info, size_t length) { if (!info || !length) return false; @@ -113,27 +112,26 @@ bool RestorePermissionInfo(const base::FilePath& path, } // namespace -bool DieFileDie(const base::FilePath& file, bool recurse) { +bool DieFileDie(const FilePath& file, bool recurse) { // It turns out that to not induce flakiness a long timeout is needed. const int kIterations = 25; - const base::TimeDelta kTimeout = base::TimeDelta::FromSeconds(10) / - kIterations; + const TimeDelta kTimeout = TimeDelta::FromSeconds(10) / kIterations; - if (!base::PathExists(file)) + if (!PathExists(file)) return true; // Sometimes Delete fails, so try a few more times. Divide the timeout // into short chunks, so that if a try succeeds, we won't delay the test // for too long. for (int i = 0; i < kIterations; ++i) { - if (base::DeleteFile(file, recurse)) + if (DeleteFile(file, recurse)) return true; - base::PlatformThread::Sleep(kTimeout); + PlatformThread::Sleep(kTimeout); } return false; } -bool EvictFileFromSystemCache(const base::FilePath& file) { +bool EvictFileFromSystemCache(const FilePath& file) { // Request exclusive access to the file and overwrite it with no buffering. base::win::ScopedHandle file_handle( CreateFile(file.value().c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, @@ -218,7 +216,7 @@ bool EvictFileFromSystemCache(const base::FilePath& file) { // Checks if the volume supports Alternate Data Streams. This is required for // the Zone Identifier implementation. -bool VolumeSupportsADS(const base::FilePath& path) { +bool VolumeSupportsADS(const FilePath& path) { wchar_t drive[MAX_PATH] = {0}; wcscpy_s(drive, MAX_PATH, path.value().c_str()); @@ -238,15 +236,15 @@ bool VolumeSupportsADS(const base::FilePath& path) { // Return whether the ZoneIdentifier is correctly set to "Internet" (3) // Only returns a valid result when called from same process as the // one that (was supposed to have) set the zone identifier. -bool HasInternetZoneIdentifier(const base::FilePath& full_path) { - base::FilePath zone_path(full_path.value() + L":Zone.Identifier"); +bool HasInternetZoneIdentifier(const FilePath& full_path) { + FilePath zone_path(full_path.value() + L":Zone.Identifier"); std::string zone_path_contents; - if (!base::ReadFileToString(zone_path, &zone_path_contents)) + if (!ReadFileToString(zone_path, &zone_path_contents)) return false; std::vector<std::string> lines; // This call also trims whitespaces, including carriage-returns (\r). - base::SplitString(zone_path_contents, '\n', &lines); + SplitString(zone_path_contents, '\n', &lines); switch (lines.size()) { case 3: @@ -261,6 +259,14 @@ bool HasInternetZoneIdentifier(const base::FilePath& full_path) { } } +} // namespace base + +namespace file_util { + +using base::DenyFilePermission; +using base::GetPermissionInfo; +using base::RestorePermissionInfo; + std::wstring FilePathAsWString(const base::FilePath& path) { return path.value(); } |