3 * Copyright 2006, Google Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "talk/base/win32.h"
35 #include "talk/base/common.h"
36 #include "talk/base/diskcache.h"
37 #include "talk/base/pathutils.h"
38 #include "talk/base/stream.h"
39 #include "talk/base/stringencode.h"
40 #include "talk/base/stringutils.h"
42 #include "talk/base/diskcache_win32.h"
46 bool DiskCacheWin32::InitializeEntries() {
47 // Note: We could store the cache information in a separate file, for faster
48 // initialization. Figuring it out empirically works, too.
50 std::wstring path16 = ToUtf16(folder_);
51 path16.append(1, '*');
53 WIN32_FIND_DATA find_data;
54 HANDLE find_handle = FindFirstFile(path16.c_str(), &find_data);
55 if (find_handle != INVALID_HANDLE_VALUE) {
59 if (!FilenameToId(ToUtf8(find_data.cFileName), &id, &index))
62 Entry* entry = GetOrCreateEntry(id, true);
63 entry->size += find_data.nFileSizeLow;
64 total_size_ += find_data.nFileSizeLow;
65 entry->streams = _max(entry->streams, index + 1);
66 FileTimeToUnixTime(find_data.ftLastWriteTime, &entry->last_modified);
68 } while (FindNextFile(find_handle, &find_data));
70 FindClose(find_handle);
76 bool DiskCacheWin32::PurgeFiles() {
77 std::wstring path16 = ToUtf16(folder_);
78 path16.append(1, '*');
79 path16.append(1, '\0');
81 SHFILEOPSTRUCT file_op = { 0 };
82 file_op.wFunc = FO_DELETE;
83 file_op.pFrom = path16.c_str();
84 file_op.fFlags = FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT
85 | FOF_NORECURSION | FOF_FILESONLY;
86 if (0 != SHFileOperation(&file_op)) {
87 LOG_F(LS_ERROR) << "Couldn't delete cache files";
94 bool DiskCacheWin32::FileExists(const std::string& filename) const {
95 DWORD result = ::GetFileAttributes(ToUtf16(filename).c_str());
96 return (INVALID_FILE_ATTRIBUTES != result);
99 bool DiskCacheWin32::DeleteFile(const std::string& filename) const {
100 return ::DeleteFile(ToUtf16(filename).c_str()) != 0;