2011-04-27 11:58:34 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2011, Blender Foundation.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "util_cache.h"
|
2012-01-16 13:13:37 +00:00
|
|
|
#include "util_debug.h"
|
2011-04-27 11:58:34 +00:00
|
|
|
#include "util_foreach.h"
|
2012-01-16 13:13:37 +00:00
|
|
|
#include "util_map.h"
|
2011-04-27 11:58:34 +00:00
|
|
|
#include "util_md5.h"
|
|
|
|
#include "util_path.h"
|
|
|
|
#include "util_types.h"
|
|
|
|
|
2012-01-16 13:13:37 +00:00
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
/* CacheData */
|
|
|
|
|
|
|
|
CacheData::CacheData(const string& name_)
|
|
|
|
{
|
|
|
|
name = name_;
|
|
|
|
f = NULL;
|
2012-01-16 13:13:37 +00:00
|
|
|
have_filename = false;
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CacheData::~CacheData()
|
|
|
|
{
|
|
|
|
if(f)
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
2012-01-16 13:13:37 +00:00
|
|
|
const string& CacheData::get_filename()
|
|
|
|
{
|
|
|
|
if(!have_filename) {
|
|
|
|
MD5Hash hash;
|
|
|
|
|
|
|
|
foreach(const CacheBuffer& buffer, buffers)
|
|
|
|
if(buffer.size)
|
|
|
|
hash.append((uint8_t*)buffer.data, buffer.size);
|
|
|
|
|
|
|
|
filename = name + "_" + hash.get_hex();
|
|
|
|
have_filename = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
/* Cache */
|
|
|
|
|
|
|
|
Cache Cache::global;
|
|
|
|
|
2012-01-16 13:13:37 +00:00
|
|
|
string Cache::data_filename(CacheData& key)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
2012-01-16 13:13:37 +00:00
|
|
|
return path_user_get(path_join("cache", key.get_filename()));
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
2012-01-16 13:13:37 +00:00
|
|
|
void Cache::insert(CacheData& key, CacheData& value)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
string filename = data_filename(key);
|
2012-01-16 13:13:37 +00:00
|
|
|
path_create_directories(filename);
|
2011-04-27 11:58:34 +00:00
|
|
|
FILE *f = fopen(filename.c_str(), "wb");
|
|
|
|
|
|
|
|
if(!f) {
|
|
|
|
fprintf(stderr, "Failed to open file %s for writing.\n", filename.c_str());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-01-16 13:13:37 +00:00
|
|
|
foreach(CacheBuffer& buffer, value.buffers) {
|
2011-04-27 11:58:34 +00:00
|
|
|
if(!fwrite(&buffer.size, sizeof(buffer.size), 1, f))
|
|
|
|
fprintf(stderr, "Failed to write to file %s.\n", filename.c_str());
|
2012-01-16 13:13:37 +00:00
|
|
|
if(buffer.size)
|
|
|
|
if(!fwrite(buffer.data, buffer.size, 1, f))
|
|
|
|
fprintf(stderr, "Failed to write to file %s.\n", filename.c_str());
|
2011-04-27 11:58:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
2012-01-16 13:13:37 +00:00
|
|
|
bool Cache::lookup(CacheData& key, CacheData& value)
|
2011-04-27 11:58:34 +00:00
|
|
|
{
|
|
|
|
string filename = data_filename(key);
|
|
|
|
FILE *f = fopen(filename.c_str(), "rb");
|
|
|
|
|
|
|
|
if(!f)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
value.name = key.name;
|
|
|
|
value.f = f;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-01-16 13:13:37 +00:00
|
|
|
void Cache::clear_except(const string& name, const set<string>& except)
|
|
|
|
{
|
|
|
|
string dir = path_user_get("cache");
|
|
|
|
|
|
|
|
if(boost::filesystem::exists(dir)) {
|
|
|
|
boost::filesystem::directory_iterator it(dir), it_end;
|
|
|
|
|
|
|
|
for(; it != it_end; it++) {
|
2012-07-11 14:48:47 +00:00
|
|
|
string filename = it->path().filename().string();
|
2012-01-16 13:13:37 +00:00
|
|
|
|
|
|
|
if(boost::starts_with(filename, name))
|
|
|
|
if(except.find(filename) == except.end())
|
|
|
|
boost::filesystem::remove(it->path());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|