Pepper.PluginHung histogram needed UMA_.
[chromium-blink-merge.git] / net / disk_cache / cache_util_win.cc
blob6adc456244cd03025cf480f9323b843c25d7f141
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/disk_cache/cache_util.h"
7 #include <windows.h>
9 #include "base/file_path.h"
10 #include "base/logging.h"
11 #include "base/message_loop.h"
12 #include "base/win/scoped_handle.h"
14 namespace {
16 // Deletes all the files on path that match search_name pattern.
17 void DeleteFiles(const FilePath& path, const wchar_t* search_name) {
18 FilePath name(path.Append(search_name));
20 WIN32_FIND_DATA data;
21 HANDLE handle = FindFirstFile(name.value().c_str(), &data);
22 if (handle == INVALID_HANDLE_VALUE)
23 return;
25 do {
26 if (data.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY ||
27 data.dwFileAttributes == FILE_ATTRIBUTE_REPARSE_POINT)
28 continue;
29 DeleteFile(path.Append(data.cFileName).value().c_str());
30 } while (FindNextFile(handle, &data));
32 FindClose(handle);
35 } // namespace
37 namespace disk_cache {
39 bool MoveCache(const FilePath& from_path, const FilePath& to_path) {
40 // I don't want to use the shell version of move because if something goes
41 // wrong, that version will attempt to move file by file and fail at the end.
42 if (!MoveFileEx(from_path.value().c_str(), to_path.value().c_str(), 0)) {
43 LOG(ERROR) << "Unable to move the cache: " << GetLastError();
44 return false;
46 return true;
49 void DeleteCache(const FilePath& path, bool remove_folder) {
50 DeleteFiles(path, L"*");
51 if (remove_folder)
52 RemoveDirectory(path.value().c_str());
55 bool DeleteCacheFile(const FilePath& name) {
56 // We do a simple delete, without ever falling back to SHFileOperation, as the
57 // version from base does.
58 if (!DeleteFile(name.value().c_str())) {
59 // There is an error, but we share delete access so let's see if there is a
60 // file to open. Note that this code assumes that we have a handle to the
61 // file at all times (even now), so nobody can have a handle that prevents
62 // us from opening the file again (unless it was deleted).
63 DWORD sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
64 DWORD access = SYNCHRONIZE;
65 base::win::ScopedHandle file(CreateFile(
66 name.value().c_str(), access, sharing, NULL, OPEN_EXISTING, 0, NULL));
67 if (file.IsValid())
68 return false;
70 // Most likely there is no file to open... and that's what we wanted.
72 return true;
75 } // namespace disk_cache