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 "ui_fileops.h"
25 *-------------------------------------------------------------------
26 * Cache data file format:
27 *-------------------------------------------------------------------
31 * Dimensions=[<width> x <height>]
32 * Date=[<value in time_t format, or -1 if no embedded date>]
34 * MD5sum=[<32 character ascii text digest>]
35 * SimilarityGrid[32 x 32]=<3072 bytes of data (1024 pixels in RGB format, 1 pixel is 24bits)>
38 * The first line (9 bytes) indicates it is a SIMcache format file. (new line char must exist)
39 * Comment lines starting with a # are ignored up to a new line.
40 * All data lines should end with a new line char.
41 * Format is very strict, data must begin with the char immediately following '='.
42 * Currently SimilarityGrid is always assumed to be 32 x 32 RGB.
47 *-------------------------------------------------------------------
49 *-------------------------------------------------------------------
52 CacheData *cache_sim_data_new(void)
56 cd = g_new0(CacheData, 1);
62 void cache_sim_data_free(CacheData *cd)
67 image_sim_free(cd->sim);
72 *-------------------------------------------------------------------
74 *-------------------------------------------------------------------
77 static gint cache_sim_write_dimensions(FILE *f, CacheData *cd)
79 if (!f || !cd || !cd->dimensions) return FALSE;
81 fprintf(f, "Dimensions=[%d x %d]\n", cd->width, cd->height);
86 static gint cache_sim_write_date(FILE *f, CacheData *cd)
88 if (!f || !cd || !cd->have_date) return FALSE;
90 fprintf(f, "Date=[%ld]\n", cd->date);
95 static gint cache_sim_write_checksum(FILE *f, CacheData *cd)
97 if (!f || !cd || !cd->have_checksum) return FALSE;
99 fprintf(f, "Checksum=[%ld]\n", cd->checksum);
104 static gint cache_sim_write_md5sum(FILE *f, CacheData *cd)
108 if (!f || !cd || !cd->have_md5sum) return FALSE;
110 text = md5_digest_to_text(cd->md5sum);
111 fprintf(f, "MD5sum=[%s]\n", text);
117 static gint cache_sim_write_similarity(FILE *f, CacheData *cd)
119 gint success = FALSE;
121 if (!f || !cd || !cd->similarity) return FALSE;
123 if (cd->sim && cd->sim->filled)
128 fprintf(f, "SimilarityGrid[32 x 32]=");
129 for (y = 0; y < 32; y++)
136 for (x = 0; x < 32; x++)
138 *p = cd->sim->avg_r[s + x]; p++;
139 *p = cd->sim->avg_g[s + x]; p++;
140 *p = cd->sim->avg_b[s + x]; p++;
142 fwrite(buf, sizeof(buf), 1, f);
152 gint cache_sim_data_save(CacheData *cd)
157 if (!cd || !cd->path) return FALSE;
159 pathl = path_from_utf8(cd->path);
160 f = fopen(pathl, "w");
165 printf("Unable to save sim cache data: %s\n", cd->path);
169 fprintf(f, "SIMcache\n#%s %s\n", PACKAGE, VERSION);
170 cache_sim_write_dimensions(f, cd);
171 cache_sim_write_date(f, cd);
172 cache_sim_write_checksum(f, cd);
173 cache_sim_write_md5sum(f, cd);
174 cache_sim_write_similarity(f, cd);
182 *-------------------------------------------------------------------
184 *-------------------------------------------------------------------
187 static gint cache_sim_read_skipline(FILE *f, int s)
189 if (!f) return FALSE;
191 if (fseek(f, 0 - s, SEEK_CUR) == 0)
194 while (fread(&b, sizeof(b), 1, f) == 1)
196 if (b == '\n') return TRUE;
204 static gint cache_sim_read_comment(FILE *f, char *buf, int s, CacheData *cd)
206 if (!f || !buf || !cd) return FALSE;
208 if (s < 1 || buf[0] != '#') return FALSE;
210 return cache_sim_read_skipline(f, s - 1);
213 static gint cache_sim_read_dimensions(FILE *f, char *buf, int s, CacheData *cd)
215 if (!f || !buf || !cd) return FALSE;
217 if (s < 10 || strncmp("Dimensions", buf, 10) != 0) return FALSE;
219 if (fseek(f, - s, SEEK_CUR) == 0)
229 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
231 while (b != ']' && p < 1023)
233 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
240 if (fread(&b, sizeof(b), 1, f) != 1) break;
244 if (sscanf(buf, "%d x %d", &w, &h) != 2) return FALSE;
248 cd->dimensions = TRUE;
256 static gint cache_sim_read_date(FILE *f, char *buf, int s, CacheData *cd)
258 if (!f || !buf || !cd) return FALSE;
260 if (s < 4 || strncmp("Date", buf, 4) != 0) return FALSE;
262 if (fseek(f, - s, SEEK_CUR) == 0)
271 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
273 while (b != ']' && p < 1023)
275 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
282 if (fread(&b, sizeof(b), 1, f) != 1) break;
286 cd->date = strtol(buf, NULL, 10);
288 cd->have_date = TRUE;
296 static gint cache_sim_read_checksum(FILE *f, char *buf, int s, CacheData *cd)
298 if (!f || !buf || !cd) return FALSE;
300 if (s < 8 || strncmp("Checksum", buf, 8) != 0) return FALSE;
302 if (fseek(f, - s, SEEK_CUR) == 0)
311 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
313 while (b != ']' && p < 1023)
315 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
322 if (fread(&b, sizeof(b), 1, f) != 1) break;
326 cd->checksum = strtol(buf, NULL, 10);
328 cd->have_checksum = TRUE;
336 static gint cache_sim_read_md5sum(FILE *f, char *buf, int s, CacheData *cd)
338 if (!f || !buf || !cd) return FALSE;
340 if (s < 8 || strncmp("MD5sum", buf, 6) != 0) return FALSE;
342 if (fseek(f, - s, SEEK_CUR) == 0)
351 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
353 while (b != ']' && p < 63)
355 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
361 if (fread(&b, sizeof(b), 1, f) != 1) break;
365 cd->have_md5sum = md5_digest_from_text(buf, cd->md5sum);
373 static gint cache_sim_read_similarity(FILE *f, char *buf, int s, CacheData *cd)
375 if (!f || !buf || !cd) return FALSE;
377 if (s < 11 || strncmp("Similarity", buf, 10) != 0) return FALSE;
379 if (strncmp("Grid[32 x 32]", buf + 10, 13) != 0) return FALSE;
381 if (fseek(f, - s, SEEK_CUR) == 0)
385 ImageSimilarityData *sd;
391 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
396 /* use current sim that may already contain data we will not touch here */
399 cd->similarity = FALSE;
403 sd = image_sim_new();
406 for (y = 0; y < 32; y++)
409 for (x = 0; x < 32; x++)
411 if (fread(&pixel_buf, sizeof(pixel_buf), 1, f) != 1)
416 sd->avg_r[s + x] = pixel_buf[0];
417 sd->avg_g[s + x] = pixel_buf[1];
418 sd->avg_b[s + x] = pixel_buf[2];
422 if (fread(&b, sizeof(b), 1, f) == 1)
424 if (b != '\n') fseek(f, -1, SEEK_CUR);
428 cd->sim->filled = TRUE;
429 cd->similarity = TRUE;
437 #define CACHE_LOAD_LINE_NOISE 8
439 CacheData *cache_sim_data_load(const gchar *path)
442 CacheData *cd = NULL;
444 gint success = CACHE_LOAD_LINE_NOISE;
447 if (!path) return NULL;
449 pathl = path_from_utf8(path);
450 f = fopen(pathl, "r");
455 cd = cache_sim_data_new();
456 cd->path = g_strdup(path);
458 if (fread(&buf, sizeof(char), 9, f) != 9 ||
459 strncmp(buf, "SIMcache", 8) != 0)
461 DEBUG_1("%s is not a cache file", cd->path);
468 s = fread(&buf, sizeof(char), sizeof(buf), f);
476 if (!cache_sim_read_comment(f, buf, s, cd) &&
477 !cache_sim_read_dimensions(f, buf, s, cd) &&
478 !cache_sim_read_date(f, buf, s, cd) &&
479 !cache_sim_read_checksum(f, buf, s, cd) &&
480 !cache_sim_read_md5sum(f, buf, s, cd) &&
481 !cache_sim_read_similarity(f, buf, s, cd))
483 if (!cache_sim_read_skipline(f, s))
494 success = CACHE_LOAD_LINE_NOISE;
501 if (!cd->dimensions &&
503 !cd->have_checksum &&
507 cache_sim_data_free(cd);
515 *-------------------------------------------------------------------
517 *-------------------------------------------------------------------
520 void cache_sim_data_set_dimensions(CacheData *cd, gint w, gint h)
526 cd->dimensions = TRUE;
529 void cache_sim_data_set_date(CacheData *cd, time_t date)
534 cd->have_date = TRUE;
537 void cache_sim_data_set_checksum(CacheData *cd, long checksum)
541 cd->checksum = checksum;
542 cd->have_checksum = TRUE;
545 void cache_sim_data_set_md5sum(CacheData *cd, guchar digest[16])
551 for (i = 0; i < 16; i++)
553 cd->md5sum[i] = digest[i];
555 cd->have_md5sum = TRUE;
558 void cache_sim_data_set_similarity(CacheData *cd, ImageSimilarityData *sd)
560 if (!cd || !sd || !sd->filled) return;
562 if (!cd->sim) cd->sim = image_sim_new();
564 memcpy(cd->sim->avg_r, sd->avg_r, 1024);
565 memcpy(cd->sim->avg_g, sd->avg_g, 1024);
566 memcpy(cd->sim->avg_b, sd->avg_b, 1024);
567 cd->sim->filled = TRUE;
569 cd->similarity = TRUE;
572 gint cache_sim_data_filled(ImageSimilarityData *sd)
574 if (!sd) return FALSE;
579 *-------------------------------------------------------------------
580 * cache path location utils
581 *-------------------------------------------------------------------
584 /* warning: this func modifies path string contents!, on fail it is set to fail point */
585 gint cache_ensure_dir_exists(gchar *path, mode_t mode)
587 if (!path) return FALSE;
595 if (p[0] == '/' || p[0] == '\0')
605 DEBUG_1("creating sub dir:%s", path);
606 if (!mkdir_utf8(path, mode))
608 printf("create dir failed: %s\n", path);
612 if (!end) p[0] = '/';
619 static void cache_path_parts(CacheType type,
620 const gchar **cache_rc, const gchar **cache_local, const gchar **cache_ext)
624 case CACHE_TYPE_THUMB:
625 *cache_rc = GQ_CACHE_RC_THUMB;
626 *cache_local = GQ_CACHE_LOCAL_THUMB;
627 *cache_ext = GQ_CACHE_EXT_THUMB;
630 *cache_rc = GQ_CACHE_RC_THUMB;
631 *cache_local = GQ_CACHE_LOCAL_THUMB;
632 *cache_ext = GQ_CACHE_EXT_SIM;
634 case CACHE_TYPE_METADATA:
635 *cache_rc = GQ_CACHE_RC_METADATA;
636 *cache_local = GQ_CACHE_LOCAL_METADATA;
637 *cache_ext = GQ_CACHE_EXT_METADATA;
642 gchar *cache_get_location(CacheType type, const gchar *source, gint include_name, mode_t *mode)
647 const gchar *cache_rc;
648 const gchar *cache_local;
649 const gchar *cache_ext;
651 if (!source) return NULL;
653 cache_path_parts(type, &cache_rc, &cache_local, &cache_ext);
655 base = remove_level_from_path(source);
658 name = g_strconcat("/", filename_from_path(source), NULL);
665 if (((type != CACHE_TYPE_METADATA && options->thumbnails.cache_into_dirs) ||
666 (type == CACHE_TYPE_METADATA && options->enable_metadata_dirs)) &&
667 access_file(base, W_OK))
669 path = g_strconcat(base, "/", cache_local, name, cache_ext, NULL);
670 if (mode) *mode = 0775;
675 path = g_strconcat(homedir(), "/", cache_rc, base, name, cache_ext, NULL);
676 if (mode) *mode = 0755;
680 if (name) g_free(name);
685 gchar *cache_find_location(CacheType type, const gchar *source)
690 const gchar *cache_rc;
691 const gchar *cache_local;
692 const gchar *cache_ext;
695 if (!source) return NULL;
697 cache_path_parts(type, &cache_rc, &cache_local, &cache_ext);
699 name = filename_from_path(source);
700 base = remove_level_from_path(source);
702 if (type == CACHE_TYPE_METADATA)
704 prefer_local = options->enable_metadata_dirs;
708 prefer_local = options->thumbnails.cache_into_dirs;
713 path = g_strconcat(base, "/", cache_local, "/", name, cache_ext, NULL);
717 path = g_strconcat(homedir(), "/", cache_rc, source, cache_ext, NULL);
724 /* try the opposite method if not found */
727 path = g_strconcat(base, "/", cache_local, "/", name, cache_ext, NULL);
731 path = g_strconcat(homedir(), "/", cache_rc, source, cache_ext, NULL);
746 gint cache_time_valid(const gchar *cache, const gchar *path)
748 struct stat cache_st;
754 if (!cache || !path) return FALSE;
756 cachel = path_from_utf8(cache);
757 pathl = path_from_utf8(path);
759 if (stat(cachel, &cache_st) == 0 &&
760 stat(pathl, &path_st) == 0)
762 if (cache_st.st_mtime == path_st.st_mtime)
766 else if (cache_st.st_mtime > path_st.st_mtime)
770 ut.actime = ut.modtime = cache_st.st_mtime;
771 if (utime(cachel, &ut) < 0 &&
774 DEBUG_1("cache permission workaround: %s", cachel);