You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2006, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  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.
  15. *
  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.
  26. */
  27. #include "talk/base/win32.h"
  28. #include <shellapi.h>
  29. #include <shlobj.h>
  30. #include <tchar.h>
  31. #include <time.h>
  32. #include "talk/base/common.h"
  33. #include "talk/base/diskcache.h"
  34. #include "talk/base/pathutils.h"
  35. #include "talk/base/stream.h"
  36. #include "talk/base/stringencode.h"
  37. #include "talk/base/stringutils.h"
  38. #include "talk/base/diskcache_win32.h"
  39. namespace talk_base {
  40. bool DiskCacheWin32::InitializeEntries() {
  41. // Note: We could store the cache information in a separate file, for faster
  42. // initialization. Figuring it out empirically works, too.
  43. std::wstring path16 = ToUtf16(folder_);
  44. path16.append(1, '*');
  45. WIN32_FIND_DATA find_data;
  46. HANDLE find_handle = FindFirstFile(path16.c_str(), &find_data);
  47. if (find_handle != INVALID_HANDLE_VALUE) {
  48. do {
  49. size_t index;
  50. std::string id;
  51. if (!FilenameToId(ToUtf8(find_data.cFileName), &id, &index))
  52. continue;
  53. Entry* entry = GetOrCreateEntry(id, true);
  54. entry->size += find_data.nFileSizeLow;
  55. total_size_ += find_data.nFileSizeLow;
  56. entry->streams = _max(entry->streams, index + 1);
  57. FileTimeToUnixTime(find_data.ftLastWriteTime, &entry->last_modified);
  58. } while (FindNextFile(find_handle, &find_data));
  59. FindClose(find_handle);
  60. }
  61. return true;
  62. }
  63. bool DiskCacheWin32::PurgeFiles() {
  64. std::wstring path16 = ToUtf16(folder_);
  65. path16.append(1, '*');
  66. path16.append(1, '\0');
  67. SHFILEOPSTRUCT file_op = { 0 };
  68. file_op.wFunc = FO_DELETE;
  69. file_op.pFrom = path16.c_str();
  70. file_op.fFlags = FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT
  71. | FOF_NORECURSION | FOF_FILESONLY;
  72. if (0 != SHFileOperation(&file_op)) {
  73. LOG_F(LS_ERROR) << "Couldn't delete cache files";
  74. return false;
  75. }
  76. return true;
  77. }
  78. bool DiskCacheWin32::FileExists(const std::string& filename) const {
  79. DWORD result = ::GetFileAttributes(ToUtf16(filename).c_str());
  80. return (INVALID_FILE_ATTRIBUTES != result);
  81. }
  82. bool DiskCacheWin32::DeleteFile(const std::string& filename) const {
  83. return ::DeleteFile(ToUtf16(filename).c_str()) != 0;
  84. }
  85. }