summaryrefslogtreecommitdiffstats
path: root/net/disk_cache/cache_util_win.cc
diff options
context:
space:
mode:
authorrvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-13 18:30:22 +0000
committerrvargas@google.com <rvargas@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-13 18:30:22 +0000
commit408d35f5f5c316b2f8df12be606a8ff8921ba5ca (patch)
tree44323772354a63ac53bb48997523abf37707ef1e /net/disk_cache/cache_util_win.cc
parent9fa3c108d312d6664c51f02f203bc60fd4750156 (diff)
downloadchromium_src-408d35f5f5c316b2f8df12be606a8ff8921ba5ca.zip
chromium_src-408d35f5f5c316b2f8df12be606a8ff8921ba5ca.tar.gz
chromium_src-408d35f5f5c316b2f8df12be606a8ff8921ba5ca.tar.bz2
Move remaining OS dependent bits from the main logic on the disk cache.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@819 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/disk_cache/cache_util_win.cc')
-rw-r--r--net/disk_cache/cache_util_win.cc116
1 files changed, 116 insertions, 0 deletions
diff --git a/net/disk_cache/cache_util_win.cc b/net/disk_cache/cache_util_win.cc
new file mode 100644
index 0000000..a66f2c3
--- /dev/null
+++ b/net/disk_cache/cache_util_win.cc
@@ -0,0 +1,116 @@
+// Copyright 2008, Google Inc.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "net/disk_cache/cache_util.h"
+
+#include <windows.h>
+
+#include "base/scoped_handle.h"
+#include "base/file_util.h"
+
+namespace disk_cache {
+
+int64 GetFreeDiskSpace(const std::wstring& path) {
+ ULARGE_INTEGER available, total, free;
+ if (!GetDiskFreeSpaceExW(path.c_str(), &available, &total, &free)) {
+ return -1;
+ }
+ int64 rv = static_cast<int64>(available.QuadPart);
+ if (rv < 0)
+ rv = kint64max;
+ return rv;
+}
+
+int64 GetSystemMemory() {
+ MEMORYSTATUSEX memory_info;
+ memory_info.dwLength = sizeof(memory_info);
+ if (!GlobalMemoryStatusEx(&memory_info)) {
+ return -1;
+ }
+
+ int64 rv = static_cast<int64>(memory_info.ullTotalPhys);
+ if (rv < 0)
+ rv = kint64max;
+ return rv;
+}
+
+bool MoveCache(const std::wstring& from_path, const std::wstring& to_path) {
+ // I don't want to use the shell version of move because if something goes
+ // wrong, that version will attempt to move file by file and fail at the end.
+ return MoveFileEx(from_path.c_str(), to_path.c_str(), 0) != FALSE;
+}
+
+// Deletes all the files on path that match search_name pattern.
+// Do not call this function with "*" as search_name.
+bool DeleteFiles(const wchar_t* path, const wchar_t* search_name) {
+ std::wstring name(path);
+ file_util::AppendToPath(&name, search_name);
+
+ WIN32_FIND_DATA data;
+ ScopedFindFileHandle handle(FindFirstFile(name.c_str(), &data));
+ if (!handle.IsValid()) {
+ DWORD error = GetLastError();
+ return ERROR_FILE_NOT_FOUND == error;
+ }
+ std::wstring adjusted_path(path);
+ adjusted_path += L'\\';
+ do {
+ std::wstring current(adjusted_path);
+ current += data.cFileName;
+ if (!DeleteFile(current.c_str()))
+ return false;
+ } while (FindNextFile(handle, &data));
+ return true;
+}
+
+void DeleteCache(const std::wstring& path, bool remove_folder) {
+ DeleteFiles(path.c_str(), L"f_*");
+ DeleteFiles(path.c_str(), L"data_*");
+
+ std::wstring index(path);
+ file_util::AppendToPath(&index, L"index");
+ DeleteFile(index.c_str());
+
+ if (remove_folder)
+ RemoveDirectory(path.c_str());
+}
+
+void WaitForPendingIO(int num_pending_io) {
+ while (num_pending_io) {
+ // Asynchronous IO operations may be in flight and the completion may end
+ // up calling us back so let's wait for them (we need an alertable wait).
+ // The idea is to let other threads do usefull work and at the same time
+ // allow more than one IO to finish... 20 mS later, we process all queued
+ // APCs and see if we have to repeat the wait.
+ Sleep(20);
+ SleepEx(0, TRUE);
+ }
+}
+
+} // namespace disk_cache