henrike@webrtc.org | 47be73b | 2014-05-13 18:00:26 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "webrtc/base/win32.h" |
| 12 | #include <shellapi.h> |
| 13 | #include <shlobj.h> |
| 14 | #include <tchar.h> |
| 15 | |
| 16 | #include <time.h> |
| 17 | |
| 18 | #include "webrtc/base/common.h" |
| 19 | #include "webrtc/base/diskcache.h" |
| 20 | #include "webrtc/base/pathutils.h" |
| 21 | #include "webrtc/base/stream.h" |
| 22 | #include "webrtc/base/stringencode.h" |
| 23 | #include "webrtc/base/stringutils.h" |
| 24 | |
| 25 | #include "webrtc/base/diskcache_win32.h" |
| 26 | |
| 27 | namespace rtc { |
| 28 | |
| 29 | bool DiskCacheWin32::InitializeEntries() { |
| 30 | // Note: We could store the cache information in a separate file, for faster |
| 31 | // initialization. Figuring it out empirically works, too. |
| 32 | |
| 33 | std::wstring path16 = ToUtf16(folder_); |
| 34 | path16.append(1, '*'); |
| 35 | |
| 36 | WIN32_FIND_DATA find_data; |
| 37 | HANDLE find_handle = FindFirstFile(path16.c_str(), &find_data); |
| 38 | if (find_handle != INVALID_HANDLE_VALUE) { |
| 39 | do { |
| 40 | size_t index; |
| 41 | std::string id; |
| 42 | if (!FilenameToId(ToUtf8(find_data.cFileName), &id, &index)) |
| 43 | continue; |
| 44 | |
| 45 | Entry* entry = GetOrCreateEntry(id, true); |
| 46 | entry->size += find_data.nFileSizeLow; |
| 47 | total_size_ += find_data.nFileSizeLow; |
| 48 | entry->streams = _max(entry->streams, index + 1); |
| 49 | FileTimeToUnixTime(find_data.ftLastWriteTime, &entry->last_modified); |
| 50 | |
| 51 | } while (FindNextFile(find_handle, &find_data)); |
| 52 | |
| 53 | FindClose(find_handle); |
| 54 | } |
| 55 | |
| 56 | return true; |
| 57 | } |
| 58 | |
| 59 | bool DiskCacheWin32::PurgeFiles() { |
| 60 | std::wstring path16 = ToUtf16(folder_); |
| 61 | path16.append(1, '*'); |
| 62 | path16.append(1, '\0'); |
| 63 | |
| 64 | SHFILEOPSTRUCT file_op = { 0 }; |
| 65 | file_op.wFunc = FO_DELETE; |
| 66 | file_op.pFrom = path16.c_str(); |
| 67 | file_op.fFlags = FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT |
| 68 | | FOF_NORECURSION | FOF_FILESONLY; |
| 69 | if (0 != SHFileOperation(&file_op)) { |
| 70 | LOG_F(LS_ERROR) << "Couldn't delete cache files"; |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | bool DiskCacheWin32::FileExists(const std::string& filename) const { |
| 78 | DWORD result = ::GetFileAttributes(ToUtf16(filename).c_str()); |
| 79 | return (INVALID_FILE_ATTRIBUTES != result); |
| 80 | } |
| 81 | |
| 82 | bool DiskCacheWin32::DeleteFile(const std::string& filename) const { |
| 83 | return ::DeleteFile(ToUtf16(filename).c_str()) != 0; |
| 84 | } |
| 85 | |
| 86 | } |