blob: bca8ef51484155c0dd46c53335c652f186a7fc43 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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>
andresp@webrtc.orgff689be2015-02-12 11:54:26 +000017#include <algorithm>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018
19#include "webrtc/base/common.h"
20#include "webrtc/base/diskcache.h"
21#include "webrtc/base/pathutils.h"
22#include "webrtc/base/stream.h"
23#include "webrtc/base/stringencode.h"
24#include "webrtc/base/stringutils.h"
25
26#include "webrtc/base/diskcache_win32.h"
27
28namespace rtc {
29
30bool DiskCacheWin32::InitializeEntries() {
31 // Note: We could store the cache information in a separate file, for faster
32 // initialization. Figuring it out empirically works, too.
33
34 std::wstring path16 = ToUtf16(folder_);
35 path16.append(1, '*');
36
37 WIN32_FIND_DATA find_data;
38 HANDLE find_handle = FindFirstFile(path16.c_str(), &find_data);
39 if (find_handle != INVALID_HANDLE_VALUE) {
40 do {
41 size_t index;
42 std::string id;
43 if (!FilenameToId(ToUtf8(find_data.cFileName), &id, &index))
44 continue;
45
46 Entry* entry = GetOrCreateEntry(id, true);
47 entry->size += find_data.nFileSizeLow;
48 total_size_ += find_data.nFileSizeLow;
andresp@webrtc.orgff689be2015-02-12 11:54:26 +000049 entry->streams = std::max(entry->streams, index + 1);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000050 FileTimeToUnixTime(find_data.ftLastWriteTime, &entry->last_modified);
51
52 } while (FindNextFile(find_handle, &find_data));
53
54 FindClose(find_handle);
55 }
56
57 return true;
58}
59
60bool DiskCacheWin32::PurgeFiles() {
61 std::wstring path16 = ToUtf16(folder_);
62 path16.append(1, '*');
63 path16.append(1, '\0');
64
65 SHFILEOPSTRUCT file_op = { 0 };
66 file_op.wFunc = FO_DELETE;
67 file_op.pFrom = path16.c_str();
68 file_op.fFlags = FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT
69 | FOF_NORECURSION | FOF_FILESONLY;
70 if (0 != SHFileOperation(&file_op)) {
71 LOG_F(LS_ERROR) << "Couldn't delete cache files";
72 return false;
73 }
74
75 return true;
76}
77
78bool DiskCacheWin32::FileExists(const std::string& filename) const {
79 DWORD result = ::GetFileAttributes(ToUtf16(filename).c_str());
80 return (INVALID_FILE_ATTRIBUTES != result);
81}
82
83bool DiskCacheWin32::DeleteFile(const std::string& filename) const {
84 return ::DeleteFile(ToUtf16(filename).c_str()) != 0;
85}
86
87}