Krolyxon
Commit 92709b2

Branches

Refs →

nsxiv92709b2

Use directory structure in cache dir

Author
Bert
Date
2011-04-07 17:29 +0200
Parent
f52a99d
Commit
92709b2b2f579300b1c007e5a3ba869e78fcc922

+58 −13

3 files changed

  1. M thumbs.c +14 −13
  2. M util.c +43 −0
  3. M util.h +1 −0
M thumbs.c View
@@ -327,7 +327,6 @@ int tns_cache_enabled() {
327327
328328 char* tns_cache_filename(const char *filename) {
329329 size_t len;
330− int i;
331330 char *cfile, *abspath;
332331
333332 if (!cache_dir || !filename)
@@ -341,13 +340,7 @@ char* tns_cache_filename(const char *filename) {
341340 strcpy(abspath, filename);
342341 }
343342
344− len = strlen(abspath);
345− for (i = 0; i < len; ++i) {
346− if (abspath[i] == '/')
347− abspath[i] = '%';
348− }
349−
350− len += strlen(cache_dir) + 6;
343+ len = strlen(cache_dir) + strlen(abspath) + 6;
351344 cfile = (char*) s_malloc(len);
352345 snprintf(cfile, len, "%s/%s.png", cache_dir, abspath + 1);
353346
@@ -380,10 +373,10 @@ Imlib_Image* tns_cache_load(const char *filename) {
380373 }
381374
382375 void tns_cache_write(thumb_t *t, Bool force) {
383− char *cfile;
376+ char *cfile, *dirend;
384377 struct stat cstats, fstats;
385378 struct timeval times[2];
386− Imlib_Load_Error err;
379+ Imlib_Load_Error err = 0;
387380
388381 if (!t || !t->im || !t->filename)
389382 return;
@@ -395,9 +388,17 @@ void tns_cache_write(thumb_t *t, Bool force) {
395388 cstats.st_mtim.tv_sec != fstats.st_mtim.tv_sec ||
396389 cstats.st_mtim.tv_nsec != fstats.st_mtim.tv_nsec)
397390 {
398− imlib_context_set_image(t->im);
399− imlib_image_set_format("png");
400− imlib_save_image_with_error_return(cfile, &err);
391+ if ((dirend = strrchr(cfile, '/'))) {
392+ *dirend = '\0';
393+ err = create_dir_rec(cfile);
394+ *dirend = '/';
395+ }
396+
397+ if (!err) {
398+ imlib_context_set_image(t->im);
399+ imlib_image_set_format("png");
400+ imlib_save_image_with_error_return(cfile, &err);
401+ }
401402
402403 if (err) {
403404 warn("could not cache thumbnail: %s", t->filename);
M util.c View
@@ -18,6 +18,8 @@
1818
1919 #include <stdlib.h>
2020 #include <string.h>
21+#include <sys/types.h>
22+#include <sys/stat.h>
2123 #include <unistd.h>
2224 #include <errno.h>
2325
@@ -156,6 +158,47 @@ end:
156158 return path;
157159 }
158160
161+int create_dir_rec(const char *path) {
162+ char *dir, *d;
163+ struct stat stats;
164+ int err = 0;
165+
166+ if (!path || !*path)
167+ return -1;
168+
169+ if (!stat(path, &stats)) {
170+ if (S_ISDIR(stats.st_mode)) {
171+ return 0;
172+ } else {
173+ warn("not a directory: %s", path);
174+ return -1;
175+ }
176+ }
177+
178+ d = dir = (char*) s_malloc(strlen(path) + 1);
179+ strcpy(dir, path);
180+
181+ while (d != NULL && !err) {
182+ d = strchr(d + 1, '/');
183+ if (d != NULL)
184+ *d = '\0';
185+ if (access(dir, F_OK) && errno == ENOENT) {
186+ if (mkdir(dir, 0755)) {
187+ warn("could not create directory: %s", dir);
188+ err = -1;
189+ }
190+ } else if (stat(dir, &stats) || !S_ISDIR(stats.st_mode)) {
191+ warn("not a directory: %s", dir);
192+ err = -1;
193+ }
194+ if (d != NULL)
195+ *d = '/';
196+ }
197+ free(dir);
198+
199+ return err;
200+}
201+
159202 char* readline(FILE *stream) {
160203 size_t len;
161204 char *buf, *s, *end;
M util.h View
@@ -44,6 +44,7 @@ void die(const char*, ...);
4444 void size_readable(float*, const char**);
4545
4646 char* absolute_path(const char*);
47+int create_dir_rec(const char*);
4748
4849 char* readline(FILE*);
4950