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