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.
28 #include "main-defines.h"
31 #include "secure-save.h"
32 #include "thumb-standard.h"
33 #include "ui-fileops.h"
40 *-------------------------------------------------------------------
41 * Cache data file format:
42 *-------------------------------------------------------------------
46 * Dimensions=[<width> x <height>] \n
47 * Date=[<value in time_t format, or -1 if no embedded date>] \n
48 * MD5sum=[<32 character ascii text digest>] \n
49 * SimilarityGrid[32 x 32]=<3072 bytes of data (1024 pixels in RGB format, 1 pixel is 24bits)>
51 * The first line (9 bytes) indicates it is a SIMcache format file. (new line char must exist) \n
52 * Comment lines starting with a # are ignored up to a new line. \n
53 * All data lines should end with a new line char. \n
54 * Format is very strict, data must begin with the char immediately following '='. \n
55 * Currently SimilarityGrid is always assumed to be 32 x 32 RGB. \n
60 *-------------------------------------------------------------------
62 *-------------------------------------------------------------------
65 CacheData *cache_sim_data_new()
69 cd = g_new0(CacheData, 1);
75 void cache_sim_data_free(CacheData *cd)
80 image_sim_free(cd->sim);
85 *-------------------------------------------------------------------
87 *-------------------------------------------------------------------
90 static gboolean cache_sim_write_dimensions(SecureSaveInfo *ssi, CacheData *cd)
92 if (!cd || !cd->dimensions) return FALSE;
94 secure_fprintf(ssi, "Dimensions=[%d x %d]\n", cd->width, cd->height);
99 static gboolean cache_sim_write_date(SecureSaveInfo *ssi, CacheData *cd)
101 if (!cd || !cd->have_date) return FALSE;
103 secure_fprintf(ssi, "Date=[%ld]\n", cd->date);
108 static gboolean cache_sim_write_md5sum(SecureSaveInfo *ssi, CacheData *cd)
112 if (!cd || !cd->have_md5sum) return FALSE;
114 text = md5_digest_to_text(cd->md5sum);
115 secure_fprintf(ssi, "MD5sum=[%s]\n", text);
121 static gboolean cache_sim_write_similarity(SecureSaveInfo *ssi, CacheData *cd)
127 if (!cd || !cd->similarity || !cd->sim || !cd->sim->filled) return FALSE;
129 secure_fprintf(ssi, "SimilarityGrid[32 x 32]=");
130 for (y = 0; y < 32; y++)
133 guint8 *avg_r = &cd->sim->avg_r[s];
134 guint8 *avg_g = &cd->sim->avg_g[s];
135 guint8 *avg_b = &cd->sim->avg_b[s];
138 for (x = 0; x < 32; x++)
145 secure_fwrite(buf, sizeof(buf), 1, ssi);
148 secure_fputc(ssi, '\n');
153 gboolean 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 log_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_md5sum(ssi, cd);
174 cache_sim_write_similarity(ssi, cd);
176 if (secure_close(ssi))
178 log_printf(_("error saving sim cache data: %s\nerror: %s\n"), cd->path,
179 secsave_strerror(secsave_errno));
187 *-------------------------------------------------------------------
189 *-------------------------------------------------------------------
192 static gboolean cache_sim_read_skipline(FILE *f, gint s)
194 if (!f) return FALSE;
196 if (fseek(f, 0 - s, SEEK_CUR) == 0)
199 while (fread(&b, sizeof(b), 1, f) == 1)
201 if (b == '\n') return TRUE;
209 static gboolean cache_sim_read_comment(FILE *f, const gchar *buf, gint s, CacheData *cd)
211 if (!f || !buf || !cd) return FALSE;
213 if (s < 1 || buf[0] != '#') return FALSE;
215 return cache_sim_read_skipline(f, s - 1);
218 static gboolean cache_sim_read_dimensions(FILE *f, gchar *buf, gint s, CacheData *cd)
220 if (!f || !buf || !cd) return FALSE;
222 if (s < 10 || strncmp("Dimensions", buf, 10) != 0) return FALSE;
224 if (fseek(f, - s, SEEK_CUR) == 0)
235 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
237 while (b != ']' && p < sizeof(buf) - 1)
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 gboolean cache_sim_read_date(FILE *f, gchar *buf, gint 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 < sizeof(buf) - 1)
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, nullptr, 10);
294 cd->have_date = TRUE;
302 static gboolean cache_sim_read_md5sum(FILE *f, gchar *buf, gint s, CacheData *cd)
304 if (!f || !buf || !cd) return FALSE;
306 if (s < 8 || strncmp("MD5sum", buf, 6) != 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 < sizeof(buf) - 1)
321 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
327 if (fread(&b, sizeof(b), 1, f) != 1) break;
331 cd->have_md5sum = md5_digest_from_text(buf, cd->md5sum);
339 static gboolean cache_sim_read_similarity(FILE *f, gchar *buf, gint s, CacheData *cd)
341 if (!f || !buf || !cd) return FALSE;
343 if (s < 11 || strncmp("Similarity", buf, 10) != 0) return FALSE;
345 if (strncmp("Grid[32 x 32]", buf + 10, 13) != 0) return FALSE;
347 if (fseek(f, - s, SEEK_CUR) == 0)
351 ImageSimilarityData *sd;
358 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
363 /* use current sim that may already contain data we will not touch here */
366 cd->similarity = FALSE;
370 sd = image_sim_new();
373 for (y = 0; y < 32; y++)
376 for (x = 0; x < 32; x++)
378 if (fread(&pixel_buf, sizeof(pixel_buf), 1, f) != 1)
383 sd->avg_r[s + x] = pixel_buf[0];
384 sd->avg_g[s + x] = pixel_buf[1];
385 sd->avg_b[s + x] = pixel_buf[2];
389 if (fread(&b, sizeof(b), 1, f) == 1)
391 if (b != '\n') fseek(f, -1, SEEK_CUR);
395 cd->sim->filled = TRUE;
396 cd->similarity = TRUE;
405 CACHE_LOAD_LINE_NOISE = 8
408 CacheData *cache_sim_data_load(const gchar *path)
411 CacheData *cd = nullptr;
413 gint success = CACHE_LOAD_LINE_NOISE;
416 if (!path) return nullptr;
418 pathl = path_from_utf8(path);
419 f = fopen(pathl, "r");
422 if (!f) return nullptr;
424 cd = cache_sim_data_new();
425 cd->path = g_strdup(path);
427 if (fread(&buf, sizeof(gchar), 9, f) != 9 ||
428 strncmp(buf, "SIMcache", 8) != 0)
430 DEBUG_1("%s is not a cache file", cd->path);
437 s = fread(&buf, sizeof(gchar), sizeof(buf), f);
445 if (!cache_sim_read_comment(f, buf, s, cd) &&
446 !cache_sim_read_dimensions(f, buf, s, cd) &&
447 !cache_sim_read_date(f, buf, s, cd) &&
448 !cache_sim_read_md5sum(f, buf, s, cd) &&
449 !cache_sim_read_similarity(f, buf, s, cd))
451 if (!cache_sim_read_skipline(f, s))
462 success = CACHE_LOAD_LINE_NOISE;
469 if (!cd->dimensions &&
474 cache_sim_data_free(cd);
482 *-------------------------------------------------------------------
484 *-------------------------------------------------------------------
487 void cache_sim_data_set_dimensions(CacheData *cd, gint w, gint h)
493 cd->dimensions = TRUE;
496 #pragma GCC diagnostic push
497 #pragma GCC diagnostic ignored "-Wunused-function"
498 void cache_sim_data_set_date_unused(CacheData *cd, time_t date)
503 cd->have_date = TRUE;
505 #pragma GCC diagnostic pop
507 void cache_sim_data_set_md5sum(CacheData *cd, const guchar digest[16])
513 for (i = 0; i < 16; i++)
515 cd->md5sum[i] = digest[i];
517 cd->have_md5sum = TRUE;
520 void cache_sim_data_set_similarity(CacheData *cd, ImageSimilarityData *sd)
522 if (!cd || !sd || !sd->filled) return;
524 if (!cd->sim) cd->sim = image_sim_new();
526 memcpy(cd->sim->avg_r, sd->avg_r, 1024);
527 memcpy(cd->sim->avg_g, sd->avg_g, 1024);
528 memcpy(cd->sim->avg_b, sd->avg_b, 1024);
529 cd->sim->filled = TRUE;
531 cd->similarity = TRUE;
534 gboolean cache_sim_data_filled(ImageSimilarityData *sd)
536 if (!sd) return FALSE;
541 *-------------------------------------------------------------------
542 * cache path location utils
543 *-------------------------------------------------------------------
546 struct CachePathParts
548 CachePathParts(CacheType type)
552 case CACHE_TYPE_THUMB:
553 rc = get_thumbnails_cache_dir();
554 local = GQ_CACHE_LOCAL_THUMB;
555 ext = GQ_CACHE_EXT_THUMB;
558 rc = get_thumbnails_cache_dir();
559 local = GQ_CACHE_LOCAL_THUMB;
560 ext = GQ_CACHE_EXT_SIM;
562 case CACHE_TYPE_METADATA:
563 rc = get_metadata_cache_dir();
564 local = GQ_CACHE_LOCAL_METADATA;
565 ext = GQ_CACHE_EXT_METADATA;
567 case CACHE_TYPE_XMP_METADATA:
568 rc = get_metadata_cache_dir();
569 local = GQ_CACHE_LOCAL_METADATA;
570 ext = GQ_CACHE_EXT_XMP_METADATA;
575 gchar *build_path_local(const gchar *source) const
577 gchar *base = remove_level_from_path(source);
578 gchar *name = g_strconcat(filename_from_path(source), ext, nullptr);
579 gchar *path = g_build_filename(base, local, name, nullptr);
586 gchar *build_path_rc(const gchar *source) const
588 gchar *name = g_strconcat(source, ext, nullptr);
589 gchar *path = g_build_filename(rc, name, nullptr);
595 const gchar *rc = nullptr;
596 const gchar *local = nullptr;
597 const gchar *ext = nullptr;
600 gchar *cache_get_location(CacheType type, const gchar *source, gint include_name, mode_t *mode)
602 gchar *path = nullptr;
604 gchar *name = nullptr;
606 if (!source) return nullptr;
608 const CachePathParts cache{type};
610 base = remove_level_from_path(source);
613 name = g_strconcat(filename_from_path(source), cache.ext, NULL);
616 if (((type != CACHE_TYPE_METADATA && type != CACHE_TYPE_XMP_METADATA && options->thumbnails.cache_into_dirs) ||
617 ((type == CACHE_TYPE_METADATA || type == CACHE_TYPE_XMP_METADATA) && options->metadata.enable_metadata_dirs)) &&
618 access_file(base, W_OK))
620 path = g_build_filename(base, cache.local, name, NULL);
621 if (mode) *mode = 0775;
626 path = g_build_filename(cache.rc, base, name, NULL);
627 if (mode) *mode = 0755;
631 if (name) g_free(name);
636 gchar *cache_find_location(CacheType type, const gchar *source)
639 gboolean prefer_local;
641 if (!source) return nullptr;
643 const CachePathParts cache{type};
645 if (type == CACHE_TYPE_METADATA || type == CACHE_TYPE_XMP_METADATA)
647 prefer_local = options->metadata.enable_metadata_dirs;
651 prefer_local = options->thumbnails.cache_into_dirs;
656 path = cache.build_path_local(source);
660 path = cache.build_path_rc(source);
667 /* try the opposite method if not found */
670 path = cache.build_path_local(source);
674 path = cache.build_path_rc(source);
687 gboolean cache_time_valid(const gchar *cache, const gchar *path)
689 struct stat cache_st;
693 gboolean ret = FALSE;
695 if (!cache || !path) return FALSE;
697 cachel = path_from_utf8(cache);
698 pathl = path_from_utf8(path);
700 if (stat(cachel, &cache_st) == 0 &&
701 stat(pathl, &path_st) == 0)
703 if (cache_st.st_mtime == path_st.st_mtime)
707 else if (cache_st.st_mtime > path_st.st_mtime)
711 ut.actime = ut.modtime = cache_st.st_mtime;
712 if (utime(cachel, &ut) < 0 &&
715 DEBUG_1("cache permission workaround: %s", cachel);
727 const gchar *get_thumbnails_cache_dir()
729 static gchar *thumbnails_cache_dir = nullptr;
731 if (thumbnails_cache_dir) return thumbnails_cache_dir;
735 thumbnails_cache_dir = g_build_filename(xdg_cache_home_get(),
736 GQ_APPNAME_LC, GQ_CACHE_THUMB, NULL);
740 thumbnails_cache_dir = g_build_filename(get_rc_dir(), GQ_CACHE_THUMB, NULL);
743 return thumbnails_cache_dir;
746 const gchar *get_thumbnails_standard_cache_dir()
748 static gchar *thumbnails_standard_cache_dir = nullptr;
750 if (thumbnails_standard_cache_dir) return thumbnails_standard_cache_dir;
752 thumbnails_standard_cache_dir = g_build_filename(xdg_cache_home_get(),
753 THUMB_FOLDER_GLOBAL, NULL);
755 return thumbnails_standard_cache_dir;
758 const gchar *get_metadata_cache_dir()
760 static gchar *metadata_cache_dir = nullptr;
762 if (metadata_cache_dir) return metadata_cache_dir;
766 /* Metadata go to $XDG_DATA_HOME.
767 * "Keywords and comments, among other things, are irreplaceable and cannot be auto-generated,
768 * so I don't think they'd be appropriate for the cache directory." -- Omari Stephens on geeqie-devel ml
770 metadata_cache_dir = g_build_filename(xdg_data_home_get(), GQ_APPNAME_LC, GQ_CACHE_METADATA, NULL);
774 metadata_cache_dir = g_build_filename(get_rc_dir(), GQ_CACHE_METADATA, NULL);
777 return metadata_cache_dir;
780 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */