1 // Copyright (c) 2006-2008 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. 4 5 #include "net/disk_cache/cache_util.h" 6 7 #include <windows.h> 8 9 #include "base/logging.h" 10 #include "base/message_loop.h" 11 #include "base/scoped_handle.h" 12 #include "base/file_util.h" 13 14 namespace { 15 16 // Deletes all the files on path that match search_name pattern. 17 void DeleteFiles(const wchar_t* path, const wchar_t* search_name) { 18 std::wstring name(path); 19 file_util::AppendToPath(&name, search_name); 20 21 WIN32_FIND_DATA data; 22 ScopedFindFileHandle handle(FindFirstFile(name.c_str(), &data)); 23 if (!handle.IsValid()) 24 return; 25 26 std::wstring adjusted_path(path); 27 adjusted_path += L'\\'; 28 do { 29 if (data.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY || 30 data.dwFileAttributes == FILE_ATTRIBUTE_REPARSE_POINT) 31 continue; 32 std::wstring current(adjusted_path); 33 current += data.cFileName; 34 DeleteFile(current.c_str()); 35 } while (FindNextFile(handle, &data)); 36 } 37 38 } // namespace 39 40 namespace disk_cache { 41 42 bool MoveCache(const FilePath& from_path, const FilePath& to_path) { 43 // I don't want to use the shell version of move because if something goes 44 // wrong, that version will attempt to move file by file and fail at the end. 45 if (!MoveFileEx(from_path.value().c_str(), to_path.value().c_str(), 0)) { 46 LOG(ERROR) << "Unable to move the cache: " << GetLastError(); 47 return false; 48 } 49 return true; 50 } 51 52 void DeleteCache(const FilePath& path, bool remove_folder) { 53 DeleteFiles(path.value().c_str(), L"*"); 54 if (remove_folder) 55 RemoveDirectory(path.value().c_str()); 56 } 57 58 bool DeleteCacheFile(const FilePath& name) { 59 // We do a simple delete, without ever falling back to SHFileOperation, as the 60 // version from base does. 61 return DeleteFile(name.value().c_str()) ? true : false; 62 } 63 64 } // namespace disk_cache 65