Home | History | Annotate | Download | only in base
      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 
     28 #include "talk/base/win32.h"
     29 #include <shellapi.h>
     30 #include <shlobj.h>
     31 #include <tchar.h>
     32 
     33 #include <time.h>
     34 
     35 #include "talk/base/common.h"
     36 #include "talk/base/diskcache.h"
     37 #include "talk/base/pathutils.h"
     38 #include "talk/base/stream.h"
     39 #include "talk/base/stringencode.h"
     40 #include "talk/base/stringutils.h"
     41 
     42 #include "talk/base/diskcache_win32.h"
     43 
     44 namespace talk_base {
     45 
     46 bool DiskCacheWin32::InitializeEntries() {
     47   // Note: We could store the cache information in a separate file, for faster
     48   // initialization.  Figuring it out empirically works, too.
     49 
     50   std::wstring path16 = ToUtf16(folder_);
     51   path16.append(1, '*');
     52 
     53   WIN32_FIND_DATA find_data;
     54   HANDLE find_handle = FindFirstFile(path16.c_str(), &find_data);
     55   if (find_handle != INVALID_HANDLE_VALUE) {
     56     do {
     57       size_t index;
     58       std::string id;
     59       if (!FilenameToId(ToUtf8(find_data.cFileName), &id, &index))
     60         continue;
     61 
     62       Entry* entry = GetOrCreateEntry(id, true);
     63       entry->size += find_data.nFileSizeLow;
     64       total_size_ += find_data.nFileSizeLow;
     65       entry->streams = _max(entry->streams, index + 1);
     66       FileTimeToUnixTime(find_data.ftLastWriteTime, &entry->last_modified);
     67 
     68     } while (FindNextFile(find_handle, &find_data));
     69 
     70     FindClose(find_handle);
     71   }
     72 
     73   return true;
     74 }
     75 
     76 bool DiskCacheWin32::PurgeFiles() {
     77   std::wstring path16 = ToUtf16(folder_);
     78   path16.append(1, '*');
     79   path16.append(1, '\0');
     80 
     81   SHFILEOPSTRUCT file_op = { 0 };
     82   file_op.wFunc = FO_DELETE;
     83   file_op.pFrom = path16.c_str();
     84   file_op.fFlags = FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT
     85                    | FOF_NORECURSION | FOF_FILESONLY;
     86   if (0 != SHFileOperation(&file_op)) {
     87     LOG_F(LS_ERROR) << "Couldn't delete cache files";
     88     return false;
     89   }
     90 
     91   return true;
     92 }
     93 
     94 bool DiskCacheWin32::FileExists(const std::string& filename) const {
     95   DWORD result = ::GetFileAttributes(ToUtf16(filename).c_str());
     96   return (INVALID_FILE_ATTRIBUTES != result);
     97 }
     98 
     99 bool DiskCacheWin32::DeleteFile(const std::string& filename) const {
    100   return ::DeleteFile(ToUtf16(filename).c_str()) != 0;
    101 }
    102 
    103 }
    104