2011-04-27 11:58:34 +00:00
|
|
|
/*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Copyright 2011-2013 Blender Foundation
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License
|
2011-04-27 11:58:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#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-07-29 13:52:38 +00:00
|
|
|
#include <boost/version.hpp>
|
|
|
|
|
2012-07-25 20:25:47 +00:00
|
|
|
#if (BOOST_VERSION < 104400)
|
|
|
|
# define BOOST_FILESYSTEM_VERSION 2
|
|
|
|
#endif
|
|
|
|
|
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);
|
2014-01-10 23:47:58 +00:00
|
|
|
FILE *f = path_fopen(filename, "wb");
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
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);
|
2014-01-10 23:47:58 +00:00
|
|
|
FILE *f = path_fopen(filename, "rb");
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2014-01-23 00:13:09 +00:00
|
|
|
path_cache_clear_except(name, except);
|
2012-01-16 13:13:37 +00:00
|
|
|
}
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|