diff options
author | agl@chromium.org <agl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-11-11 17:59:27 +0000 |
---|---|---|
committer | agl@chromium.org <agl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-11-11 17:59:27 +0000 |
commit | 49dba2366b6cd198f26a88e4662d8e77c1015259 (patch) | |
tree | 1d7f3bd2eb91e0ef40dced9403eabe6e9993aa9d /base | |
parent | 1a6bb1a9d6805997a53130b55d651012034007df (diff) | |
download | chromium_src-49dba2366b6cd198f26a88e4662d8e77c1015259.zip chromium_src-49dba2366b6cd198f26a88e4662d8e77c1015259.tar.gz chromium_src-49dba2366b6cd198f26a88e4662d8e77c1015259.tar.bz2 |
Port flush_cache tool.
BUG=4160
Review URL: http://codereview.chromium.org/9639
Patch from Paweł Hajdan jr.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@5182 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/file_util.h | 4 | ||||
-rw-r--r-- | base/file_util_linux.cc | 12 | ||||
-rw-r--r-- | base/file_util_mac.mm | 6 | ||||
-rw-r--r-- | base/file_util_win.cc | 41 |
4 files changed, 63 insertions, 0 deletions
diff --git a/base/file_util.h b/base/file_util.h index 3df31e4..0bdb56d 100644 --- a/base/file_util.h +++ b/base/file_util.h @@ -389,6 +389,10 @@ bool RenameFileAndResetSecurityDescriptor( const std::wstring& source_file_path, const std::wstring& target_file_path); +// 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 FilePath path); + } // namespace file_util #endif // BASE_FILE_UTIL_H_ diff --git a/base/file_util_linux.cc b/base/file_util_linux.cc index f776c91..d2a6870 100644 --- a/base/file_util_linux.cc +++ b/base/file_util_linux.cc @@ -72,4 +72,16 @@ bool CopyFile(const FilePath& from_path, const FilePath& to_path) { return result; } +bool EvictFileFromSystemCache(const FilePath path) { + int fd = open(path.value().c_str(), O_RDONLY); + if (fd < 0) + return false; + if (fdatasync(fd) != 0) + return false; + if (posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED) != 0) + return false; + close(fd); + return true; +} + } // namespace file_util diff --git a/base/file_util_mac.mm b/base/file_util_mac.mm index f7f5632..0cf63d2 100644 --- a/base/file_util_mac.mm +++ b/base/file_util_mac.mm @@ -28,4 +28,10 @@ bool CopyFile(const FilePath& from_path, const FilePath& to_path) { to_path.value().c_str(), NULL, COPYFILE_ALL) == 0); } +bool EvictFileFromSystemCache(const FilePath path) { + // TODO(port): Implement. + NOTIMPLEMENTED(); + return false; +} + } // namespace diff --git a/base/file_util_win.cc b/base/file_util_win.cc index 355bdfd..5f75544 100644 --- a/base/file_util_win.cc +++ b/base/file_util_win.cc @@ -692,4 +692,45 @@ std::wstring FileEnumerator::Next() { return (file_type_ & FileEnumerator::FILES) ? cur_file : Next(); } +bool EvictFileFromSystemCache(const FilePath path) { + // Overwrite it with no buffering. + ScopedHandle file(CreateFile(path.value().c_str(), + GENERIC_READ | GENERIC_WRITE, + FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, + OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, NULL)); + if (!file.IsValid()) + return false; + + // Execute in chunks. It could be optimized. We want to do few of these since + // these operations will be slow without the cache. + char buffer[128 * 1024]; + int total_bytes = 0; + DWORD bytes_read; + for (;;) { + if (!ReadFile(file, buffer, sizeof(buffer), &bytes_read, NULL)) + return false; + if (bytes_read == 0) + break; + + bool final = false; + if (bytes_read < sizeof(buffer)) + final = true; + + DWORD to_write = final ? sizeof(buffer) : bytes_read; + + DWORD actual; + SetFilePointer(file, total_bytes, 0, FILE_BEGIN); + if (!WriteFile(file, buffer, to_write, &actual, NULL)) + return false; + total_bytes += bytes_read; + + if (final) { + SetFilePointer(file, total_bytes, 0, FILE_BEGIN); + SetEndOfFile(file); + break; + } + } + return true; +} + } // namespace file_util |