Refresh android relocation packer from AOSP bionic.
[chromium-blink-merge.git] / net / disk_cache / cache_util_win.cc
blob51ed2a032e2af4dedd1214c763355f3bcbc59c84
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/files/file_path.h"
10 #include "base/logging.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/win/scoped_handle.h"
14 namespace disk_cache {
16 bool MoveCache(const base::FilePath& from_path, const base::FilePath& to_path) {
17 // I don't want to use the shell version of move because if something goes
18 // wrong, that version will attempt to move file by file and fail at the end.
19 if (!MoveFileEx(from_path.value().c_str(), to_path.value().c_str(), 0)) {
20 LOG(ERROR) << "Unable to move the cache: " << GetLastError();
21 return false;
23 return true;
26 bool DeleteCacheFile(const base::FilePath& name) {
27 // We do a simple delete, without ever falling back to SHFileOperation, as the
28 // version from base does.
29 if (!DeleteFile(name.value().c_str())) {
30 // There is an error, but we share delete access so let's see if there is a
31 // file to open. Note that this code assumes that we have a handle to the
32 // file at all times (even now), so nobody can have a handle that prevents
33 // us from opening the file again (unless it was deleted).
34 DWORD sharing = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
35 DWORD access = SYNCHRONIZE;
36 base::win::ScopedHandle file(CreateFile(
37 name.value().c_str(), access, sharing, NULL, OPEN_EXISTING, 0, NULL));
38 if (file.IsValid())
39 return false;
41 // Most likely there is no file to open... and that's what we wanted.
43 return true;
46 } // namespace disk_cache