svn merge ^/trunk/blender -r47023:HEAD
[blender-staging.git] / intern / cycles / util / util_cache.cpp
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 #include <stdio.h>
20
21 #include "util_cache.h"
22 #include "util_debug.h"
23 #include "util_foreach.h"
24 #include "util_map.h"
25 #include "util_md5.h"
26 #include "util_path.h"
27 #include "util_types.h"
28
29 #define BOOST_FILESYSTEM_VERSION 2
30
31 #include <boost/filesystem.hpp> 
32 #include <boost/algorithm/string.hpp>
33
34 CCL_NAMESPACE_BEGIN
35
36 /* CacheData */
37
38 CacheData::CacheData(const string& name_)
39 {
40         name = name_;
41         f = NULL;
42         have_filename = false;
43 }
44
45 CacheData::~CacheData()
46 {
47         if(f)
48                 fclose(f);
49 }
50
51 const string& CacheData::get_filename()
52 {
53         if(!have_filename) {
54                 MD5Hash hash;
55
56                 foreach(const CacheBuffer& buffer, buffers)
57                         if(buffer.size)
58                                 hash.append((uint8_t*)buffer.data, buffer.size);
59                 
60                 filename = name + "_" + hash.get_hex();
61                 have_filename = true;
62         }
63
64         return filename;
65 }
66
67 /* Cache */
68
69 Cache Cache::global;
70
71 string Cache::data_filename(CacheData& key)
72 {
73         return path_user_get(path_join("cache", key.get_filename()));
74 }
75
76 void Cache::insert(CacheData& key, CacheData& value)
77 {
78         string filename = data_filename(key);
79         path_create_directories(filename);
80         FILE *f = fopen(filename.c_str(), "wb");
81
82         if(!f) {
83                 fprintf(stderr, "Failed to open file %s for writing.\n", filename.c_str());
84                 return;
85         }
86
87         foreach(CacheBuffer& buffer, value.buffers) {
88                 if(!fwrite(&buffer.size, sizeof(buffer.size), 1, f))
89                         fprintf(stderr, "Failed to write to file %s.\n", filename.c_str());
90                 if(buffer.size)
91                         if(!fwrite(buffer.data, buffer.size, 1, f))
92                                 fprintf(stderr, "Failed to write to file %s.\n", filename.c_str());
93         }
94         
95         fclose(f);
96 }
97
98 bool Cache::lookup(CacheData& key, CacheData& value)
99 {
100         string filename = data_filename(key);
101         FILE *f = fopen(filename.c_str(), "rb");
102
103         if(!f)
104                 return false;
105         
106         value.name = key.name;
107         value.f = f;
108
109         return true;
110 }
111
112 void Cache::clear_except(const string& name, const set<string>& except)
113 {
114         string dir = path_user_get("cache");
115
116         if(boost::filesystem::exists(dir)) {
117                 boost::filesystem::directory_iterator it(dir), it_end;
118
119                 for(; it != it_end; it++) {
120                         string filename = it->path().filename();
121
122                         if(boost::starts_with(filename, name))
123                                 if(except.find(filename) == except.end())
124                                         boost::filesystem::remove(it->path());
125                 }
126         }
127 }
128
129 CCL_NAMESPACE_END
130