Krolyxon
Commit eee921e

Branches

Refs →

nsxiveee921e

Refactored thumbs.c

Author
Bert
Date
2011-04-08 10:24 +0200
Parent
a90bd1c
Commit
eee921efe0959251daec3d23e652b4f3a924df92

+98 −104

1 file changed

  1. M thumbs.c +98 −104
M thumbs.c View
@@ -32,9 +32,104 @@ extern Imlib_Image *im_invalid;
3232 const int thumb_dim = THUMB_SIZE + 10;
3333 char *cache_dir = NULL;
3434
35−int tns_cache_enabled();
36−Imlib_Image* tns_cache_load(const char*);
37−void tns_cache_write(thumb_t*, Bool);
35+int tns_cache_enabled() {
36+ struct stat stats;
37+
38+ return cache_dir && !stat(cache_dir, &stats) && S_ISDIR(stats.st_mode) &&
39+ !access(cache_dir, W_OK);
40+}
41+
42+char* tns_cache_filename(const char *filename) {
43+ size_t len;
44+ char *cfile = NULL;
45+ const char *abspath;
46+
47+ if (!cache_dir || !filename)
48+ return NULL;
49+
50+ if (*filename != '/') {
51+ if (!(abspath = absolute_path(filename)))
52+ return NULL;
53+ } else {
54+ abspath = filename;
55+ }
56+
57+ if (strncmp(abspath, cache_dir, strlen(cache_dir))) {
58+ len = strlen(cache_dir) + strlen(abspath) + 6;
59+ cfile = (char*) s_malloc(len);
60+ snprintf(cfile, len, "%s/%s.png", cache_dir, abspath + 1);
61+ }
62+
63+ if (abspath != filename)
64+ free((void*) abspath);
65+
66+ return cfile;
67+}
68+
69+Imlib_Image* tns_cache_load(const char *filename) {
70+ char *cfile;
71+ struct stat cstats, fstats;
72+ Imlib_Image *im = NULL;
73+
74+ if (!filename || stat(filename, &fstats))
75+ return NULL;
76+
77+ if ((cfile = tns_cache_filename(filename))) {
78+ if (!stat(cfile, &cstats) &&
79+ cstats.st_mtim.tv_sec == fstats.st_mtim.tv_sec &&
80+ cstats.st_mtim.tv_nsec == fstats.st_mtim.tv_nsec)
81+ {
82+ printf("cache hit: %s\n", filename);
83+ im = imlib_load_image(cfile);
84+ } else
85+ printf("cache MISS: %s\n", filename);
86+ free(cfile);
87+ }
88+
89+ return im;
90+}
91+
92+void tns_cache_write(thumb_t *t, Bool force) {
93+ char *cfile, *dirend;
94+ struct stat cstats, fstats;
95+ struct timeval times[2];
96+ Imlib_Load_Error err = 0;
97+
98+ if (!t || !t->im || !t->filename)
99+ return;
100+ if (stat(t->filename, &fstats))
101+ return;
102+
103+ if ((cfile = tns_cache_filename(t->filename))) {
104+ if (force || stat(cfile, &cstats) ||
105+ cstats.st_mtim.tv_sec != fstats.st_mtim.tv_sec ||
106+ cstats.st_mtim.tv_nsec != fstats.st_mtim.tv_nsec)
107+ {
108+ if ((dirend = strrchr(cfile, '/'))) {
109+ *dirend = '\0';
110+ err = r_mkdir(cfile);
111+ *dirend = '/';
112+ }
113+
114+ if (!err) {
115+ imlib_context_set_image(t->im);
116+ imlib_image_set_format("png");
117+ imlib_save_image_with_error_return(cfile, &err);
118+ }
119+
120+ if (err) {
121+ warn("could not cache thumbnail: %s", t->filename);
122+ } else {
123+ TIMESPEC_TO_TIMEVAL(&times[0], &fstats.st_atim);
124+ TIMESPEC_TO_TIMEVAL(&times[1], &fstats.st_mtim);
125+ utimes(cfile, times);
126+ printf("thumbnail cache file written: %s\n", t->filename);
127+ }
128+ }
129+ free(cfile);
130+ }
131+}
132+
38133
39134 void tns_init(tns_t *tns, int cnt) {
40135 int len;
@@ -314,104 +409,3 @@ int tns_translate(tns_t *tns, int x, int y) {
314409
315410 return -1;
316411 }
317−
318−
319−/* thumbnail caching */
320−
321−int tns_cache_enabled() {
322− struct stat stats;
323−
324− return cache_dir && !stat(cache_dir, &stats) && S_ISDIR(stats.st_mode) &&
325− !access(cache_dir, W_OK);
326−}
327−
328−char* tns_cache_filename(const char *filename) {
329− size_t len;
330− char *cfile = NULL;
331− const char *abspath;
332−
333− if (!cache_dir || !filename)
334− return NULL;
335−
336− if (*filename != '/') {
337− if (!(abspath = absolute_path(filename)))
338− return NULL;
339− } else {
340− abspath = filename;
341− }
342−
343− if (strncmp(abspath, cache_dir, strlen(cache_dir))) {
344− len = strlen(cache_dir) + strlen(abspath) + 6;
345− cfile = (char*) s_malloc(len);
346− snprintf(cfile, len, "%s/%s.png", cache_dir, abspath + 1);
347− }
348−
349− if (abspath != filename)
350− free((void*) abspath);
351−
352− return cfile;
353−}
354−
355−Imlib_Image* tns_cache_load(const char *filename) {
356− char *cfile;
357− struct stat cstats, fstats;
358− Imlib_Image *im = NULL;
359−
360− if (!filename || stat(filename, &fstats))
361− return NULL;
362−
363− if ((cfile = tns_cache_filename(filename))) {
364− if (!stat(cfile, &cstats) &&
365− cstats.st_mtim.tv_sec == fstats.st_mtim.tv_sec &&
366− cstats.st_mtim.tv_nsec == fstats.st_mtim.tv_nsec)
367− {
368− printf("cache hit: %s\n", filename);
369− im = imlib_load_image(cfile);
370− } else
371− printf("cache MISS: %s\n", filename);
372− free(cfile);
373− }
374−
375− return im;
376−}
377−
378−void tns_cache_write(thumb_t *t, Bool force) {
379− char *cfile, *dirend;
380− struct stat cstats, fstats;
381− struct timeval times[2];
382− Imlib_Load_Error err = 0;
383−
384− if (!t || !t->im || !t->filename)
385− return;
386− if (stat(t->filename, &fstats))
387− return;
388−
389− if ((cfile = tns_cache_filename(t->filename))) {
390− if (force || stat(cfile, &cstats) ||
391− cstats.st_mtim.tv_sec != fstats.st_mtim.tv_sec ||
392− cstats.st_mtim.tv_nsec != fstats.st_mtim.tv_nsec)
393− {
394− if ((dirend = strrchr(cfile, '/'))) {
395− *dirend = '\0';
396− err = create_dir_rec(cfile);
397− *dirend = '/';
398− }
399−
400− if (!err) {
401− imlib_context_set_image(t->im);
402− imlib_image_set_format("png");
403− imlib_save_image_with_error_return(cfile, &err);
404− }
405−
406− if (err) {
407− warn("could not cache thumbnail: %s", t->filename);
408− } else {
409− TIMESPEC_TO_TIMEVAL(&times[0], &fstats.st_atim);
410− TIMESPEC_TO_TIMEVAL(&times[1], &fstats.st_mtim);
411− utimes(cfile, times);
412− printf("thumbnail cache file written: %s\n", t->filename);
413− }
414− }
415− free(cfile);
416− }
417−}