Home | History | Annotate | Download | only in base
      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 #include <algorithm>
     18 
     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 
     28 namespace rtc {
     29 
     30 bool 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;
     49       entry->streams = std::max(entry->streams, index + 1);
     50       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 
     60 bool 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 
     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 
     83 bool DiskCacheWin32::DeleteFile(const std::string& filename) const {
     84   return ::DeleteFile(ToUtf16(filename).c_str()) != 0;
     85 }
     86 
     87 }
     88