2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "secure_save.h"
27 #include "thumb_standard.h"
28 #include "ui_fileops.h"
36 *-------------------------------------------------------------------
37 * Cache data file format:
38 *-------------------------------------------------------------------
42 * Dimensions=[<width> x <height>] \n
43 * Date=[<value in time_t format, or -1 if no embedded date>] \n
44 * MD5sum=[<32 character ascii text digest>] \n
45 * SimilarityGrid[32 x 32]=<3072 bytes of data (1024 pixels in RGB format, 1 pixel is 24bits)>
47 * The first line (9 bytes) indicates it is a SIMcache format file. (new line char must exist) \n
48 * Comment lines starting with a # are ignored up to a new line. \n
49 * All data lines should end with a new line char. \n
50 * Format is very strict, data must begin with the char immediately following '='. \n
51 * Currently SimilarityGrid is always assumed to be 32 x 32 RGB. \n
56 *-------------------------------------------------------------------
58 *-------------------------------------------------------------------
61 CacheData *cache_sim_data_new(void)
65 cd = g_new0(CacheData, 1);
71 void cache_sim_data_free(CacheData *cd)
76 image_sim_free(cd->sim);
81 *-------------------------------------------------------------------
83 *-------------------------------------------------------------------
86 static gboolean cache_sim_write_dimensions(SecureSaveInfo *ssi, CacheData *cd)
88 if (!cd || !cd->dimensions) return FALSE;
90 secure_fprintf(ssi, "Dimensions=[%d x %d]\n", cd->width, cd->height);
95 static gboolean cache_sim_write_date(SecureSaveInfo *ssi, CacheData *cd)
97 if (!cd || !cd->have_date) return FALSE;
99 secure_fprintf(ssi, "Date=[%ld]\n", cd->date);
104 static gboolean cache_sim_write_md5sum(SecureSaveInfo *ssi, CacheData *cd)
108 if (!cd || !cd->have_md5sum) return FALSE;
110 text = md5_digest_to_text(cd->md5sum);
111 secure_fprintf(ssi, "MD5sum=[%s]\n", text);
117 static gboolean cache_sim_write_similarity(SecureSaveInfo *ssi, CacheData *cd)
122 if (!cd || !cd->similarity || !cd->sim || !cd->sim->filled) return FALSE;
124 secure_fprintf(ssi, "SimilarityGrid[32 x 32]=");
125 for (y = 0; y < 32; y++)
128 guint8 *avg_r = &cd->sim->avg_r[s];
129 guint8 *avg_g = &cd->sim->avg_g[s];
130 guint8 *avg_b = &cd->sim->avg_b[s];
133 for (x = 0; x < 32; x++)
140 secure_fwrite(buf, sizeof(buf), 1, ssi);
143 secure_fputc(ssi, '\n');
148 gboolean cache_sim_data_save(CacheData *cd)
153 if (!cd || !cd->path) return FALSE;
155 pathl = path_from_utf8(cd->path);
156 ssi = secure_open(pathl);
161 log_printf("Unable to save sim cache data: %s\n", cd->path);
165 secure_fprintf(ssi, "SIMcache\n#%s %s\n", PACKAGE, VERSION);
166 cache_sim_write_dimensions(ssi, cd);
167 cache_sim_write_date(ssi, cd);
168 cache_sim_write_md5sum(ssi, cd);
169 cache_sim_write_similarity(ssi, cd);
171 if (secure_close(ssi))
173 log_printf(_("error saving sim cache data: %s\nerror: %s\n"), cd->path,
174 secsave_strerror(secsave_errno));
182 *-------------------------------------------------------------------
184 *-------------------------------------------------------------------
187 static gboolean cache_sim_read_skipline(FILE *f, gint 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 gboolean cache_sim_read_comment(FILE *f, gchar *buf, gint 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 gboolean cache_sim_read_dimensions(FILE *f, gchar *buf, gint 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 < sizeof(buf) - 1)
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 gboolean cache_sim_read_date(FILE *f, gchar *buf, gint 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 < sizeof(buf) - 1)
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 gboolean cache_sim_read_md5sum(FILE *f, gchar *buf, gint s, CacheData *cd)
298 if (!f || !buf || !cd) return FALSE;
300 if (s < 8 || strncmp("MD5sum", buf, 6) != 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 < sizeof(buf) - 1)
315 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
321 if (fread(&b, sizeof(b), 1, f) != 1) break;
325 cd->have_md5sum = md5_digest_from_text(buf, cd->md5sum);
333 static gboolean cache_sim_read_similarity(FILE *f, gchar *buf, gint s, CacheData *cd)
335 if (!f || !buf || !cd) return FALSE;
337 if (s < 11 || strncmp("Similarity", buf, 10) != 0) return FALSE;
339 if (strncmp("Grid[32 x 32]", buf + 10, 13) != 0) return FALSE;
341 if (fseek(f, - s, SEEK_CUR) == 0)
345 ImageSimilarityData *sd;
351 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
356 /* use current sim that may already contain data we will not touch here */
359 cd->similarity = FALSE;
363 sd = image_sim_new();
366 for (y = 0; y < 32; y++)
369 for (x = 0; x < 32; x++)
371 if (fread(&pixel_buf, sizeof(pixel_buf), 1, f) != 1)
376 sd->avg_r[s + x] = pixel_buf[0];
377 sd->avg_g[s + x] = pixel_buf[1];
378 sd->avg_b[s + x] = pixel_buf[2];
382 if (fread(&b, sizeof(b), 1, f) == 1)
384 if (b != '\n') fseek(f, -1, SEEK_CUR);
388 cd->sim->filled = TRUE;
389 cd->similarity = TRUE;
397 #define CACHE_LOAD_LINE_NOISE 8
399 CacheData *cache_sim_data_load(const gchar *path)
402 CacheData *cd = NULL;
404 gint success = CACHE_LOAD_LINE_NOISE;
407 if (!path) return NULL;
409 pathl = path_from_utf8(path);
410 f = fopen(pathl, "r");
415 cd = cache_sim_data_new();
416 cd->path = g_strdup(path);
418 if (fread(&buf, sizeof(gchar), 9, f) != 9 ||
419 strncmp(buf, "SIMcache", 8) != 0)
421 DEBUG_1("%s is not a cache file", cd->path);
428 s = fread(&buf, sizeof(gchar), sizeof(buf), f);
436 if (!cache_sim_read_comment(f, buf, s, cd) &&
437 !cache_sim_read_dimensions(f, buf, s, cd) &&
438 !cache_sim_read_date(f, buf, s, cd) &&
439 !cache_sim_read_md5sum(f, buf, s, cd) &&
440 !cache_sim_read_similarity(f, buf, s, cd))
442 if (!cache_sim_read_skipline(f, s))
453 success = CACHE_LOAD_LINE_NOISE;
460 if (!cd->dimensions &&
465 cache_sim_data_free(cd);
473 *-------------------------------------------------------------------
475 *-------------------------------------------------------------------
478 void cache_sim_data_set_dimensions(CacheData *cd, gint w, gint h)
484 cd->dimensions = TRUE;
487 void cache_sim_data_set_date(CacheData *cd, time_t date)
492 cd->have_date = TRUE;
495 void cache_sim_data_set_md5sum(CacheData *cd, guchar digest[16])
501 for (i = 0; i < 16; i++)
503 cd->md5sum[i] = digest[i];
505 cd->have_md5sum = TRUE;
508 void cache_sim_data_set_similarity(CacheData *cd, ImageSimilarityData *sd)
510 if (!cd || !sd || !sd->filled) return;
512 if (!cd->sim) cd->sim = image_sim_new();
514 memcpy(cd->sim->avg_r, sd->avg_r, 1024);
515 memcpy(cd->sim->avg_g, sd->avg_g, 1024);
516 memcpy(cd->sim->avg_b, sd->avg_b, 1024);
517 cd->sim->filled = TRUE;
519 cd->similarity = TRUE;
522 gboolean cache_sim_data_filled(ImageSimilarityData *sd)
524 if (!sd) return FALSE;
529 *-------------------------------------------------------------------
530 * cache path location utils
531 *-------------------------------------------------------------------
535 static void cache_path_parts(CacheType type,
536 const gchar **cache_rc, const gchar **cache_local, const gchar **cache_ext)
540 case CACHE_TYPE_THUMB:
541 *cache_rc = get_thumbnails_cache_dir();
542 *cache_local = GQ_CACHE_LOCAL_THUMB;
543 *cache_ext = GQ_CACHE_EXT_THUMB;
546 *cache_rc = get_thumbnails_cache_dir();
547 *cache_local = GQ_CACHE_LOCAL_THUMB;
548 *cache_ext = GQ_CACHE_EXT_SIM;
550 case CACHE_TYPE_METADATA:
551 *cache_rc = get_metadata_cache_dir();
552 *cache_local = GQ_CACHE_LOCAL_METADATA;
553 *cache_ext = GQ_CACHE_EXT_METADATA;
555 case CACHE_TYPE_XMP_METADATA:
556 *cache_rc = get_metadata_cache_dir();
557 *cache_local = GQ_CACHE_LOCAL_METADATA;
558 *cache_ext = GQ_CACHE_EXT_XMP_METADATA;
563 gchar *cache_get_location(CacheType type, const gchar *source, gint include_name, mode_t *mode)
568 const gchar *cache_rc;
569 const gchar *cache_local;
570 const gchar *cache_ext;
572 if (!source) return NULL;
574 cache_path_parts(type, &cache_rc, &cache_local, &cache_ext);
576 base = remove_level_from_path(source);
579 name = g_strconcat(filename_from_path(source), cache_ext, NULL);
582 if (((type != CACHE_TYPE_METADATA && type != CACHE_TYPE_XMP_METADATA && options->thumbnails.cache_into_dirs) ||
583 ((type == CACHE_TYPE_METADATA || type == CACHE_TYPE_XMP_METADATA) && options->metadata.enable_metadata_dirs)) &&
584 access_file(base, W_OK))
586 path = g_build_filename(base, cache_local, name, NULL);
587 if (mode) *mode = 0775;
592 path = g_build_filename(cache_rc, base, name, NULL);
593 if (mode) *mode = 0755;
597 if (name) g_free(name);
602 static gchar *cache_build_path_local(const gchar *source, const gchar *cache_local, const gchar *cache_ext)
605 gchar *base = remove_level_from_path(source);
606 gchar *name = g_strconcat(filename_from_path(source), cache_ext, NULL);
607 path = g_build_filename(base, cache_local, name, NULL);
614 static gchar *cache_build_path_rc(const gchar *source, const gchar *cache_rc, const gchar *cache_ext)
617 gchar *name = g_strconcat(source, cache_ext, NULL);
618 path = g_build_filename(cache_rc, name, NULL);
624 gchar *cache_find_location(CacheType type, const gchar *source)
627 const gchar *cache_rc;
628 const gchar *cache_local;
629 const gchar *cache_ext;
630 gboolean prefer_local;
632 if (!source) return NULL;
634 cache_path_parts(type, &cache_rc, &cache_local, &cache_ext);
636 if (type == CACHE_TYPE_METADATA || type == CACHE_TYPE_XMP_METADATA)
638 prefer_local = options->metadata.enable_metadata_dirs;
642 prefer_local = options->thumbnails.cache_into_dirs;
647 path = cache_build_path_local(source, cache_local, cache_ext);
651 path = cache_build_path_rc(source, cache_rc, cache_ext);
658 /* try the opposite method if not found */
661 path = cache_build_path_local(source, cache_local, cache_ext);
665 path = cache_build_path_rc(source, cache_rc, cache_ext);
678 gboolean cache_time_valid(const gchar *cache, const gchar *path)
680 struct stat cache_st;
684 gboolean ret = FALSE;
686 if (!cache || !path) return FALSE;
688 cachel = path_from_utf8(cache);
689 pathl = path_from_utf8(path);
691 if (stat(cachel, &cache_st) == 0 &&
692 stat(pathl, &path_st) == 0)
694 if (cache_st.st_mtime == path_st.st_mtime)
698 else if (cache_st.st_mtime > path_st.st_mtime)
702 ut.actime = ut.modtime = cache_st.st_mtime;
703 if (utime(cachel, &ut) < 0 &&
706 DEBUG_1("cache permission workaround: %s", cachel);
718 const gchar *get_thumbnails_cache_dir(void)
720 static gchar *thumbnails_cache_dir = NULL;
722 if (thumbnails_cache_dir) return thumbnails_cache_dir;
726 thumbnails_cache_dir = g_build_filename(xdg_cache_home_get(),
727 GQ_APPNAME_LC, GQ_CACHE_THUMB, NULL);
731 thumbnails_cache_dir = g_build_filename(get_rc_dir(), GQ_CACHE_THUMB, NULL);
734 return thumbnails_cache_dir;
737 const gchar *get_thumbnails_standard_cache_dir(void)
739 static gchar *thumbnails_standard_cache_dir = NULL;
741 if (thumbnails_standard_cache_dir) return thumbnails_standard_cache_dir;
743 thumbnails_standard_cache_dir = g_build_filename(xdg_cache_home_get(),
744 THUMB_FOLDER_GLOBAL, NULL);
746 return thumbnails_standard_cache_dir;
749 const gchar *get_metadata_cache_dir(void)
751 static gchar *metadata_cache_dir = NULL;
753 if (metadata_cache_dir) return metadata_cache_dir;
757 /* Metadata go to $XDG_DATA_HOME.
758 * "Keywords and comments, among other things, are irreplaceable and cannot be auto-generated,
759 * so I don't think they'd be appropriate for the cache directory." -- Omari Stephens on geeqie-devel ml
761 metadata_cache_dir = g_build_filename(xdg_data_home_get(), GQ_APPNAME_LC, GQ_CACHE_METADATA, NULL);
765 metadata_cache_dir = g_build_filename(get_rc_dir(), GQ_CACHE_METADATA, NULL);
768 return metadata_cache_dir;
771 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */