Blender V2.61 - r43446

util_cache.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright 2011, Blender Foundation.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  */
00018 
00019 #include <stdio.h>
00020 
00021 #include "util_cache.h"
00022 #include "util_debug.h"
00023 #include "util_foreach.h"
00024 #include "util_map.h"
00025 #include "util_md5.h"
00026 #include "util_path.h"
00027 #include "util_types.h"
00028 
00029 #define BOOST_FILESYSTEM_VERSION 2
00030 
00031 #include <boost/filesystem.hpp> 
00032 #include <boost/algorithm/string.hpp>
00033 
00034 CCL_NAMESPACE_BEGIN
00035 
00036 /* CacheData */
00037 
00038 CacheData::CacheData(const string& name_)
00039 {
00040     name = name_;
00041     f = NULL;
00042     have_filename = false;
00043 }
00044 
00045 CacheData::~CacheData()
00046 {
00047     if(f)
00048         fclose(f);
00049 }
00050 
00051 const string& CacheData::get_filename()
00052 {
00053     if(!have_filename) {
00054         MD5Hash hash;
00055 
00056         foreach(const CacheBuffer& buffer, buffers)
00057             if(buffer.size)
00058                 hash.append((uint8_t*)buffer.data, buffer.size);
00059         
00060         filename = name + "_" + hash.get_hex();
00061         have_filename = true;
00062     }
00063 
00064     return filename;
00065 }
00066 
00067 /* Cache */
00068 
00069 Cache Cache::global;
00070 
00071 string Cache::data_filename(CacheData& key)
00072 {
00073     return path_user_get(path_join("cache", key.get_filename()));
00074 }
00075 
00076 void Cache::insert(CacheData& key, CacheData& value)
00077 {
00078     string filename = data_filename(key);
00079     path_create_directories(filename);
00080     FILE *f = fopen(filename.c_str(), "wb");
00081 
00082     if(!f) {
00083         fprintf(stderr, "Failed to open file %s for writing.\n", filename.c_str());
00084         return;
00085     }
00086 
00087     foreach(CacheBuffer& buffer, value.buffers) {
00088         if(!fwrite(&buffer.size, sizeof(buffer.size), 1, f))
00089             fprintf(stderr, "Failed to write to file %s.\n", filename.c_str());
00090         if(buffer.size)
00091             if(!fwrite(buffer.data, buffer.size, 1, f))
00092                 fprintf(stderr, "Failed to write to file %s.\n", filename.c_str());
00093     }
00094     
00095     fclose(f);
00096 }
00097 
00098 bool Cache::lookup(CacheData& key, CacheData& value)
00099 {
00100     string filename = data_filename(key);
00101     FILE *f = fopen(filename.c_str(), "rb");
00102 
00103     if(!f)
00104         return false;
00105     
00106     value.name = key.name;
00107     value.f = f;
00108 
00109     return true;
00110 }
00111 
00112 void Cache::clear_except(const string& name, const set<string>& except)
00113 {
00114     string dir = path_user_get("cache");
00115 
00116     if(boost::filesystem::exists(dir)) {
00117         boost::filesystem::directory_iterator it(dir), it_end;
00118 
00119         for(; it != it_end; it++) {
00120             string filename = it->path().filename();
00121 
00122             if(boost::starts_with(filename, name))
00123                 if(except.find(filename) == except.end())
00124                     boost::filesystem::remove(it->path());
00125         }
00126     }
00127 }
00128 
00129 CCL_NAMESPACE_END
00130