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"
9 #include "base/file_path.h"
10 #include "base/logging.h"
11 #include "base/message_loop.h"
12 #include "base/win/scoped_handle.h"
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
));
21 HANDLE handle
= FindFirstFile(name
.value().c_str(), &data
);
22 if (handle
== INVALID_HANDLE_VALUE
)
26 if (data
.dwFileAttributes
== FILE_ATTRIBUTE_DIRECTORY
||
27 data
.dwFileAttributes
== FILE_ATTRIBUTE_REPARSE_POINT
)
29 DeleteFile(path
.Append(data
.cFileName
).value().c_str());
30 } while (FindNextFile(handle
, &data
));
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();
49 void DeleteCache(const FilePath
& path
, bool remove_folder
) {
50 DeleteFiles(path
, L
"*");
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
));
70 // Most likely there is no file to open... and that's what we wanted.
75 } // namespace disk_cache