133
134
void ThumbnailCachePrivate::clear() {
134
135
cleardir(smalldir);
135
136
cleardir(largedir);
137
cleardir(originaldir);
139
141
void ThumbnailCachePrivate::prune() {
140
142
prune_dir(smalldir, MAX_FILES);
141
143
prune_dir(largedir, MAX_FILES);
144
prune_dir(originaldir, MAX_FILES);
144
147
void ThumbnailCachePrivate::delete_from_cache(const std::string &abs_path) {
145
148
unlink(get_cache_file_name(abs_path, TN_SIZE_SMALL).c_str());
146
149
unlink(get_cache_file_name(abs_path, TN_SIZE_LARGE).c_str());
150
unlink(get_cache_file_name(abs_path, TN_SIZE_ORIGINAL).c_str());
153
static string makedir(const string &base, const string &subdir) {
154
string dirname = base;
159
ec = mkdir(dirname.c_str(), S_IRUSR | S_IWUSR | S_IXUSR);
160
if (ec < 0 && errno != EEXIST) {
161
string s("Could not create ");
164
s += strerror(errno);
165
throw runtime_error(s);
150
170
ThumbnailCachePrivate::ThumbnailCachePrivate() {
181
201
if(!use_global) {
182
202
string app_pkgname = get_app_pkg_name();
183
xdg_base += "/" + app_pkgname;
185
ec = mkdir(xdg_base.c_str(), S_IRUSR | S_IWUSR | S_IXUSR);
186
if (ec < 0 && errno != EEXIST) {
187
string s("Could not create app local dir ");
190
s += strerror(errno);
191
throw runtime_error(s);
203
xdg_base = makedir(xdg_base, app_pkgname);
194
205
tndir = xdg_base + "/thumbnails";
195
ec = mkdir(tndir.c_str(), S_IRUSR | S_IWUSR | S_IXUSR);
196
if (ec < 0 && errno != EEXIST) {
197
string s("Could not create thumbnail dir - ");
198
s += strerror(errno);
199
throw runtime_error(s);
201
smalldir = tndir + "/normal";
202
ec = mkdir(smalldir.c_str(), S_IRUSR | S_IWUSR | S_IXUSR);
203
if (ec < 0 && errno != EEXIST) {
204
string s("Could not create small dir - ");
205
s += strerror(errno);
206
throw runtime_error(s);
208
largedir = tndir + "/large";
209
ec = mkdir(largedir.c_str(), S_IRUSR | S_IWUSR | S_IXUSR);
210
if (ec < 0 && errno != EEXIST) {
211
string s("Could not create large dir - ");
212
s += strerror(errno);
213
throw runtime_error(s);
206
tndir = makedir(xdg_base, "thumbnails");
207
smalldir = makedir(tndir,"normal");
208
largedir = makedir(tndir, "large");;
209
originaldir = makedir(tndir, "original");
217
212
string ThumbnailCachePrivate::md5(const string &str) const {
235
230
string ThumbnailCachePrivate::get_cache_file_name(const std::string & abs_original, ThumbnailSize desired) const {
236
231
assert(abs_original[0] == '/');
237
string path = desired == TN_SIZE_SMALL ? smalldir : largedir;
234
case TN_SIZE_SMALL : path = smalldir; break;
235
case TN_SIZE_LARGE : path = largedir; break;
236
case TN_SIZE_ORIGINAL : path = originaldir; break;
237
default : throw runtime_error("Unreachable code");
238
239
path += "/" + md5("file://" + abs_original) + ".png";