4 * Copyright (C) 2008 The Geeqie Team
8 * This software is released under the GNU General Public License (GNU GPL).
9 * Please read the included file COPYING for more information.
10 * This software comes with no warranty of any kind, use at your own risk!
18 #include "secure_save.h"
19 #include "ui_fileops.h"
26 *-------------------------------------------------------------------
27 * Cache data file format:
28 *-------------------------------------------------------------------
32 * Dimensions=[<width> x <height>]
33 * Date=[<value in time_t format, or -1 if no embedded date>]
35 * MD5sum=[<32 character ascii text digest>]
36 * SimilarityGrid[32 x 32]=<3072 bytes of data (1024 pixels in RGB format, 1 pixel is 24bits)>
39 * The first line (9 bytes) indicates it is a SIMcache format file. (new line char must exist)
40 * Comment lines starting with a # are ignored up to a new line.
41 * All data lines should end with a new line char.
42 * Format is very strict, data must begin with the char immediately following '='.
43 * Currently SimilarityGrid is always assumed to be 32 x 32 RGB.
48 *-------------------------------------------------------------------
50 *-------------------------------------------------------------------
53 CacheData *cache_sim_data_new(void)
57 cd = g_new0(CacheData, 1);
63 void cache_sim_data_free(CacheData *cd)
68 image_sim_free(cd->sim);
73 *-------------------------------------------------------------------
75 *-------------------------------------------------------------------
78 static gint cache_sim_write_dimensions(SecureSaveInfo *ssi, CacheData *cd)
80 if (!cd || !cd->dimensions) return FALSE;
82 secure_fprintf(ssi, "Dimensions=[%d x %d]\n", cd->width, cd->height);
87 static gint cache_sim_write_date(SecureSaveInfo *ssi, CacheData *cd)
89 if (!cd || !cd->have_date) return FALSE;
91 secure_fprintf(ssi, "Date=[%ld]\n", cd->date);
96 static gint cache_sim_write_checksum(SecureSaveInfo *ssi, CacheData *cd)
98 if (!cd || !cd->have_checksum) return FALSE;
100 secure_fprintf(ssi, "Checksum=[%ld]\n", cd->checksum);
105 static gint cache_sim_write_md5sum(SecureSaveInfo *ssi, CacheData *cd)
109 if (!cd || !cd->have_md5sum) return FALSE;
111 text = md5_digest_to_text(cd->md5sum);
112 secure_fprintf(ssi, "MD5sum=[%s]\n", text);
118 static gint cache_sim_write_similarity(SecureSaveInfo *ssi, CacheData *cd)
120 gint success = FALSE;
122 if (!cd || !cd->similarity) return FALSE;
124 if (cd->sim && cd->sim->filled)
129 secure_fprintf(ssi, "SimilarityGrid[32 x 32]=");
130 for (y = 0; y < 32; y++)
137 for (x = 0; x < 32; x++)
139 *p = cd->sim->avg_r[s + x]; p++;
140 *p = cd->sim->avg_g[s + x]; p++;
141 *p = cd->sim->avg_b[s + x]; p++;
143 secure_fwrite(buf, sizeof(buf), 1, ssi);
146 secure_fputc(ssi, '\n');
153 gint cache_sim_data_save(CacheData *cd)
158 if (!cd || !cd->path) return FALSE;
160 pathl = path_from_utf8(cd->path);
161 ssi = secure_open(pathl);
166 printf("Unable to save sim cache data: %s\n", cd->path);
170 secure_fprintf(ssi, "SIMcache\n#%s %s\n", PACKAGE, VERSION);
171 cache_sim_write_dimensions(ssi, cd);
172 cache_sim_write_date(ssi, cd);
173 cache_sim_write_checksum(ssi, cd);
174 cache_sim_write_md5sum(ssi, cd);
175 cache_sim_write_similarity(ssi, cd);
177 if (secure_close(ssi))
179 printf_term(_("error saving sim cache data: %s\nerror: %s\n"), cd->path,
180 secsave_strerror(secsave_errno));
188 *-------------------------------------------------------------------
190 *-------------------------------------------------------------------
193 static gint cache_sim_read_skipline(FILE *f, int s)
195 if (!f) return FALSE;
197 if (fseek(f, 0 - s, SEEK_CUR) == 0)
200 while (fread(&b, sizeof(b), 1, f) == 1)
202 if (b == '\n') return TRUE;
210 static gint cache_sim_read_comment(FILE *f, char *buf, int s, CacheData *cd)
212 if (!f || !buf || !cd) return FALSE;
214 if (s < 1 || buf[0] != '#') return FALSE;
216 return cache_sim_read_skipline(f, s - 1);
219 static gint cache_sim_read_dimensions(FILE *f, char *buf, int s, CacheData *cd)
221 if (!f || !buf || !cd) return FALSE;
223 if (s < 10 || strncmp("Dimensions", buf, 10) != 0) return FALSE;
225 if (fseek(f, - s, SEEK_CUR) == 0)
235 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
237 while (b != ']' && p < 1023)
239 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
246 if (fread(&b, sizeof(b), 1, f) != 1) break;
250 if (sscanf(buf, "%d x %d", &w, &h) != 2) return FALSE;
254 cd->dimensions = TRUE;
262 static gint cache_sim_read_date(FILE *f, char *buf, int s, CacheData *cd)
264 if (!f || !buf || !cd) return FALSE;
266 if (s < 4 || strncmp("Date", buf, 4) != 0) return FALSE;
268 if (fseek(f, - s, SEEK_CUR) == 0)
277 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
279 while (b != ']' && p < 1023)
281 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
288 if (fread(&b, sizeof(b), 1, f) != 1) break;
292 cd->date = strtol(buf, NULL, 10);
294 cd->have_date = TRUE;
302 static gint cache_sim_read_checksum(FILE *f, char *buf, int s, CacheData *cd)
304 if (!f || !buf || !cd) return FALSE;
306 if (s < 8 || strncmp("Checksum", buf, 8) != 0) return FALSE;
308 if (fseek(f, - s, SEEK_CUR) == 0)
317 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
319 while (b != ']' && p < 1023)
321 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
328 if (fread(&b, sizeof(b), 1, f) != 1) break;
332 cd->checksum = strtol(buf, NULL, 10);
334 cd->have_checksum = TRUE;
342 static gint cache_sim_read_md5sum(FILE *f, char *buf, int s, CacheData *cd)
344 if (!f || !buf || !cd) return FALSE;
346 if (s < 8 || strncmp("MD5sum", buf, 6) != 0) return FALSE;
348 if (fseek(f, - s, SEEK_CUR) == 0)
357 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
359 while (b != ']' && p < 63)
361 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
367 if (fread(&b, sizeof(b), 1, f) != 1) break;
371 cd->have_md5sum = md5_digest_from_text(buf, cd->md5sum);
379 static gint cache_sim_read_similarity(FILE *f, char *buf, int s, CacheData *cd)
381 if (!f || !buf || !cd) return FALSE;
383 if (s < 11 || strncmp("Similarity", buf, 10) != 0) return FALSE;
385 if (strncmp("Grid[32 x 32]", buf + 10, 13) != 0) return FALSE;
387 if (fseek(f, - s, SEEK_CUR) == 0)
391 ImageSimilarityData *sd;
397 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
402 /* use current sim that may already contain data we will not touch here */
405 cd->similarity = FALSE;
409 sd = image_sim_new();
412 for (y = 0; y < 32; y++)
415 for (x = 0; x < 32; x++)
417 if (fread(&pixel_buf, sizeof(pixel_buf), 1, f) != 1)
422 sd->avg_r[s + x] = pixel_buf[0];
423 sd->avg_g[s + x] = pixel_buf[1];
424 sd->avg_b[s + x] = pixel_buf[2];
428 if (fread(&b, sizeof(b), 1, f) == 1)
430 if (b != '\n') fseek(f, -1, SEEK_CUR);
434 cd->sim->filled = TRUE;
435 cd->similarity = TRUE;
443 #define CACHE_LOAD_LINE_NOISE 8
445 CacheData *cache_sim_data_load(const gchar *path)
448 CacheData *cd = NULL;
450 gint success = CACHE_LOAD_LINE_NOISE;
453 if (!path) return NULL;
455 pathl = path_from_utf8(path);
456 f = fopen(pathl, "r");
461 cd = cache_sim_data_new();
462 cd->path = g_strdup(path);
464 if (fread(&buf, sizeof(char), 9, f) != 9 ||
465 strncmp(buf, "SIMcache", 8) != 0)
467 DEBUG_1("%s is not a cache file", cd->path);
474 s = fread(&buf, sizeof(char), sizeof(buf), f);
482 if (!cache_sim_read_comment(f, buf, s, cd) &&
483 !cache_sim_read_dimensions(f, buf, s, cd) &&
484 !cache_sim_read_date(f, buf, s, cd) &&
485 !cache_sim_read_checksum(f, buf, s, cd) &&
486 !cache_sim_read_md5sum(f, buf, s, cd) &&
487 !cache_sim_read_similarity(f, buf, s, cd))
489 if (!cache_sim_read_skipline(f, s))
500 success = CACHE_LOAD_LINE_NOISE;
507 if (!cd->dimensions &&
509 !cd->have_checksum &&
513 cache_sim_data_free(cd);
521 *-------------------------------------------------------------------
523 *-------------------------------------------------------------------
526 void cache_sim_data_set_dimensions(CacheData *cd, gint w, gint h)
532 cd->dimensions = TRUE;
535 void cache_sim_data_set_date(CacheData *cd, time_t date)
540 cd->have_date = TRUE;
543 void cache_sim_data_set_checksum(CacheData *cd, long checksum)
547 cd->checksum = checksum;
548 cd->have_checksum = TRUE;
551 void cache_sim_data_set_md5sum(CacheData *cd, guchar digest[16])
557 for (i = 0; i < 16; i++)
559 cd->md5sum[i] = digest[i];
561 cd->have_md5sum = TRUE;
564 void cache_sim_data_set_similarity(CacheData *cd, ImageSimilarityData *sd)
566 if (!cd || !sd || !sd->filled) return;
568 if (!cd->sim) cd->sim = image_sim_new();
570 memcpy(cd->sim->avg_r, sd->avg_r, 1024);
571 memcpy(cd->sim->avg_g, sd->avg_g, 1024);
572 memcpy(cd->sim->avg_b, sd->avg_b, 1024);
573 cd->sim->filled = TRUE;
575 cd->similarity = TRUE;
578 gint cache_sim_data_filled(ImageSimilarityData *sd)
580 if (!sd) return FALSE;
585 *-------------------------------------------------------------------
586 * cache path location utils
587 *-------------------------------------------------------------------
590 /* warning: this func modifies path string contents!, on fail it is set to fail point */
591 gint cache_ensure_dir_exists(gchar *path, mode_t mode)
593 if (!path) return FALSE;
601 if (p[0] == '/' || p[0] == '\0')
611 DEBUG_1("creating sub dir:%s", path);
612 if (!mkdir_utf8(path, mode))
614 printf("create dir failed: %s\n", path);
618 if (!end) p[0] = '/';
625 static void cache_path_parts(CacheType type,
626 const gchar **cache_rc, const gchar **cache_local, const gchar **cache_ext)
630 case CACHE_TYPE_THUMB:
631 *cache_rc = GQ_CACHE_RC_THUMB;
632 *cache_local = GQ_CACHE_LOCAL_THUMB;
633 *cache_ext = GQ_CACHE_EXT_THUMB;
636 *cache_rc = GQ_CACHE_RC_THUMB;
637 *cache_local = GQ_CACHE_LOCAL_THUMB;
638 *cache_ext = GQ_CACHE_EXT_SIM;
640 case CACHE_TYPE_METADATA:
641 *cache_rc = GQ_CACHE_RC_METADATA;
642 *cache_local = GQ_CACHE_LOCAL_METADATA;
643 *cache_ext = GQ_CACHE_EXT_METADATA;
648 gchar *cache_get_location(CacheType type, const gchar *source, gint include_name, mode_t *mode)
653 const gchar *cache_rc;
654 const gchar *cache_local;
655 const gchar *cache_ext;
657 if (!source) return NULL;
659 cache_path_parts(type, &cache_rc, &cache_local, &cache_ext);
661 base = remove_level_from_path(source);
664 name = g_strconcat("/", filename_from_path(source), NULL);
671 if (((type != CACHE_TYPE_METADATA && options->thumbnails.cache_into_dirs) ||
672 (type == CACHE_TYPE_METADATA && options->enable_metadata_dirs)) &&
673 access_file(base, W_OK))
675 path = g_strconcat(base, "/", cache_local, name, cache_ext, NULL);
676 if (mode) *mode = 0775;
681 path = g_strconcat(homedir(), "/", cache_rc, base, name, cache_ext, NULL);
682 if (mode) *mode = 0755;
686 if (name) g_free(name);
691 gchar *cache_find_location(CacheType type, const gchar *source)
696 const gchar *cache_rc;
697 const gchar *cache_local;
698 const gchar *cache_ext;
701 if (!source) return NULL;
703 cache_path_parts(type, &cache_rc, &cache_local, &cache_ext);
705 name = filename_from_path(source);
706 base = remove_level_from_path(source);
708 if (type == CACHE_TYPE_METADATA)
710 prefer_local = options->enable_metadata_dirs;
714 prefer_local = options->thumbnails.cache_into_dirs;
719 path = g_strconcat(base, "/", cache_local, "/", name, cache_ext, NULL);
723 path = g_strconcat(homedir(), "/", cache_rc, source, cache_ext, NULL);
730 /* try the opposite method if not found */
733 path = g_strconcat(base, "/", cache_local, "/", name, cache_ext, NULL);
737 path = g_strconcat(homedir(), "/", cache_rc, source, cache_ext, NULL);
752 gint cache_time_valid(const gchar *cache, const gchar *path)
754 struct stat cache_st;
760 if (!cache || !path) return FALSE;
762 cachel = path_from_utf8(cache);
763 pathl = path_from_utf8(path);
765 if (stat(cachel, &cache_st) == 0 &&
766 stat(pathl, &path_st) == 0)
768 if (cache_st.st_mtime == path_st.st_mtime)
772 else if (cache_st.st_mtime > path_st.st_mtime)
776 ut.actime = ut.modtime = cache_st.st_mtime;
777 if (utime(cachel, &ut) < 0 &&
780 DEBUG_1("cache permission workaround: %s", cachel);