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.
37 #include "main-defines.h"
40 #include "secure-save.h"
42 #include "thumb-standard.h"
43 #include "ui-fileops.h"
48 *-------------------------------------------------------------------
49 * Cache data file format:
50 *-------------------------------------------------------------------
54 * Dimensions=[<width> x <height>] \n
55 * Date=[<value in time_t format, or -1 if no embedded date>] \n
56 * MD5sum=[<32 character ascii text digest>] \n
57 * SimilarityGrid[32 x 32]=<3072 bytes of data (1024 pixels in RGB format, 1 pixel is 24bits)>
59 * The first line (9 bytes) indicates it is a SIMcache format file. (new line char must exist) \n
60 * Comment lines starting with a # are ignored up to a new line. \n
61 * All data lines should end with a new line char. \n
62 * Format is very strict, data must begin with the char immediately following '='. \n
63 * Currently SimilarityGrid is always assumed to be 32 x 32 RGB. \n
69 constexpr gint CACHE_LOAD_LINE_NOISE = 8;
74 *-------------------------------------------------------------------
76 *-------------------------------------------------------------------
79 CacheData *cache_sim_data_new()
83 cd = g_new0(CacheData, 1);
89 void cache_sim_data_free(CacheData *cd)
94 image_sim_free(cd->sim);
99 *-------------------------------------------------------------------
101 *-------------------------------------------------------------------
104 static gboolean cache_sim_write_dimensions(SecureSaveInfo *ssi, CacheData *cd)
106 if (!cd || !cd->dimensions) return FALSE;
108 secure_fprintf(ssi, "Dimensions=[%d x %d]\n", cd->width, cd->height);
113 static gboolean cache_sim_write_date(SecureSaveInfo *ssi, CacheData *cd)
115 if (!cd || !cd->have_date) return FALSE;
117 secure_fprintf(ssi, "Date=[%ld]\n", cd->date);
122 static gboolean cache_sim_write_md5sum(SecureSaveInfo *ssi, CacheData *cd)
126 if (!cd || !cd->have_md5sum) return FALSE;
128 text = md5_digest_to_text(cd->md5sum);
129 secure_fprintf(ssi, "MD5sum=[%s]\n", text);
135 static gboolean cache_sim_write_similarity(SecureSaveInfo *ssi, CacheData *cd)
141 if (!cd || !cd->similarity || !cd->sim || !cd->sim->filled) return FALSE;
143 secure_fprintf(ssi, "SimilarityGrid[32 x 32]=");
144 for (y = 0; y < 32; y++)
147 guint8 *avg_r = &cd->sim->avg_r[s];
148 guint8 *avg_g = &cd->sim->avg_g[s];
149 guint8 *avg_b = &cd->sim->avg_b[s];
152 for (x = 0; x < 32; x++)
159 secure_fwrite(buf, sizeof(buf), 1, ssi);
162 secure_fputc(ssi, '\n');
167 gboolean cache_sim_data_save(CacheData *cd)
172 if (!cd || !cd->path) return FALSE;
174 pathl = path_from_utf8(cd->path);
175 ssi = secure_open(pathl);
180 log_printf("Unable to save sim cache data: %s\n", cd->path);
184 secure_fprintf(ssi, "SIMcache\n#%s %s\n", PACKAGE, VERSION);
185 cache_sim_write_dimensions(ssi, cd);
186 cache_sim_write_date(ssi, cd);
187 cache_sim_write_md5sum(ssi, cd);
188 cache_sim_write_similarity(ssi, cd);
190 if (secure_close(ssi))
192 log_printf(_("error saving sim cache data: %s\nerror: %s\n"), cd->path,
193 secsave_strerror(secsave_errno));
201 *-------------------------------------------------------------------
203 *-------------------------------------------------------------------
206 static gboolean cache_sim_read_skipline(FILE *f, gint s)
208 if (!f) return FALSE;
210 if (fseek(f, 0 - s, SEEK_CUR) == 0)
213 while (fread(&b, sizeof(b), 1, f) == 1)
215 if (b == '\n') return TRUE;
223 static gboolean cache_sim_read_comment(FILE *f, const gchar *buf, gint s, CacheData *cd)
225 if (!f || !buf || !cd) return FALSE;
227 if (s < 1 || buf[0] != '#') return FALSE;
229 return cache_sim_read_skipline(f, s - 1);
232 static gboolean cache_sim_read_dimensions(FILE *f, gchar *buf, gint s, CacheData *cd)
234 if (!f || !buf || !cd) return FALSE;
236 if (s < 10 || strncmp("Dimensions", buf, 10) != 0) return FALSE;
238 if (fseek(f, - s, SEEK_CUR) == 0)
249 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
251 while (b != ']' && p < sizeof(buf) - 1)
253 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
260 if (fread(&b, sizeof(b), 1, f) != 1) break;
264 if (sscanf(buf, "%d x %d", &w, &h) != 2) return FALSE;
268 cd->dimensions = TRUE;
276 static gboolean cache_sim_read_date(FILE *f, gchar *buf, gint s, CacheData *cd)
278 if (!f || !buf || !cd) return FALSE;
280 if (s < 4 || strncmp("Date", buf, 4) != 0) return FALSE;
282 if (fseek(f, - s, SEEK_CUR) == 0)
291 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
293 while (b != ']' && p < sizeof(buf) - 1)
295 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
302 if (fread(&b, sizeof(b), 1, f) != 1) break;
306 cd->date = strtol(buf, nullptr, 10);
308 cd->have_date = TRUE;
316 static gboolean cache_sim_read_md5sum(FILE *f, gchar *buf, gint s, CacheData *cd)
318 if (!f || !buf || !cd) return FALSE;
320 if (s < 8 || strncmp("MD5sum", buf, 6) != 0) return FALSE;
322 if (fseek(f, - s, SEEK_CUR) == 0)
331 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
333 while (b != ']' && p < sizeof(buf) - 1)
335 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
341 if (fread(&b, sizeof(b), 1, f) != 1) break;
345 cd->have_md5sum = md5_digest_from_text(buf, cd->md5sum);
353 static gboolean cache_sim_read_similarity(FILE *f, gchar *buf, gint s, CacheData *cd)
355 if (!f || !buf || !cd) return FALSE;
357 if (s < 11 || strncmp("Similarity", buf, 10) != 0) return FALSE;
359 if (strncmp("Grid[32 x 32]", buf + 10, 13) != 0) return FALSE;
361 if (fseek(f, - s, SEEK_CUR) == 0)
365 ImageSimilarityData *sd;
372 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
377 /* use current sim that may already contain data we will not touch here */
380 cd->similarity = FALSE;
384 sd = image_sim_new();
387 for (y = 0; y < 32; y++)
390 for (x = 0; x < 32; x++)
392 if (fread(&pixel_buf, sizeof(pixel_buf), 1, f) != 1)
397 sd->avg_r[s + x] = pixel_buf[0];
398 sd->avg_g[s + x] = pixel_buf[1];
399 sd->avg_b[s + x] = pixel_buf[2];
403 if (fread(&b, sizeof(b), 1, f) == 1)
405 if (b != '\n') fseek(f, -1, SEEK_CUR);
409 cd->sim->filled = TRUE;
410 cd->similarity = TRUE;
418 CacheData *cache_sim_data_load(const gchar *path)
421 CacheData *cd = nullptr;
423 gint success = CACHE_LOAD_LINE_NOISE;
426 if (!path) return nullptr;
428 pathl = path_from_utf8(path);
429 f = fopen(pathl, "r");
432 if (!f) return nullptr;
434 cd = cache_sim_data_new();
435 cd->path = g_strdup(path);
437 if (fread(&buf, sizeof(gchar), 9, f) != 9 ||
438 strncmp(buf, "SIMcache", 8) != 0)
440 DEBUG_1("%s is not a cache file", cd->path);
447 s = fread(&buf, sizeof(gchar), sizeof(buf), f);
455 if (!cache_sim_read_comment(f, buf, s, cd) &&
456 !cache_sim_read_dimensions(f, buf, s, cd) &&
457 !cache_sim_read_date(f, buf, s, cd) &&
458 !cache_sim_read_md5sum(f, buf, s, cd) &&
459 !cache_sim_read_similarity(f, buf, s, cd))
461 if (!cache_sim_read_skipline(f, s))
472 success = CACHE_LOAD_LINE_NOISE;
479 if (!cd->dimensions &&
484 cache_sim_data_free(cd);
492 *-------------------------------------------------------------------
494 *-------------------------------------------------------------------
497 void cache_sim_data_set_dimensions(CacheData *cd, gint w, gint h)
503 cd->dimensions = TRUE;
506 #pragma GCC diagnostic push
507 #pragma GCC diagnostic ignored "-Wunused-function"
508 void cache_sim_data_set_date_unused(CacheData *cd, time_t date)
513 cd->have_date = TRUE;
515 #pragma GCC diagnostic pop
517 void cache_sim_data_set_md5sum(CacheData *cd, const guchar digest[16])
523 for (i = 0; i < 16; i++)
525 cd->md5sum[i] = digest[i];
527 cd->have_md5sum = TRUE;
530 void cache_sim_data_set_similarity(CacheData *cd, ImageSimilarityData *sd)
532 if (!cd || !sd || !sd->filled) return;
534 if (!cd->sim) cd->sim = image_sim_new();
536 memcpy(cd->sim->avg_r, sd->avg_r, 1024);
537 memcpy(cd->sim->avg_g, sd->avg_g, 1024);
538 memcpy(cd->sim->avg_b, sd->avg_b, 1024);
539 cd->sim->filled = TRUE;
541 cd->similarity = TRUE;
544 gboolean cache_sim_data_filled(ImageSimilarityData *sd)
546 if (!sd) return FALSE;
551 *-------------------------------------------------------------------
552 * cache path location utils
553 *-------------------------------------------------------------------
556 struct CachePathParts
558 CachePathParts(CacheType type)
562 case CACHE_TYPE_THUMB:
563 rc = get_thumbnails_cache_dir();
564 local = GQ_CACHE_LOCAL_THUMB;
565 ext = GQ_CACHE_EXT_THUMB;
568 rc = get_thumbnails_cache_dir();
569 local = GQ_CACHE_LOCAL_THUMB;
570 ext = GQ_CACHE_EXT_SIM;
572 case CACHE_TYPE_METADATA:
573 rc = get_metadata_cache_dir();
574 local = GQ_CACHE_LOCAL_METADATA;
575 ext = GQ_CACHE_EXT_METADATA;
577 case CACHE_TYPE_XMP_METADATA:
578 rc = get_metadata_cache_dir();
579 local = GQ_CACHE_LOCAL_METADATA;
580 ext = GQ_CACHE_EXT_XMP_METADATA;
585 gchar *build_path_local(const gchar *source) const
587 gchar *base = remove_level_from_path(source);
588 gchar *name = g_strconcat(filename_from_path(source), ext, nullptr);
589 gchar *path = g_build_filename(base, local, name, nullptr);
596 gchar *build_path_rc(const gchar *source) const
598 gchar *name = g_strconcat(source, ext, nullptr);
599 gchar *path = g_build_filename(rc, name, nullptr);
605 const gchar *rc = nullptr;
606 const gchar *local = nullptr;
607 const gchar *ext = nullptr;
610 gchar *cache_get_location(CacheType type, const gchar *source, gint include_name, mode_t *mode)
612 gchar *path = nullptr;
614 gchar *name = nullptr;
616 if (!source) return nullptr;
618 const CachePathParts cache{type};
620 base = remove_level_from_path(source);
623 name = g_strconcat(filename_from_path(source), cache.ext, NULL);
626 if (((type != CACHE_TYPE_METADATA && type != CACHE_TYPE_XMP_METADATA && options->thumbnails.cache_into_dirs) ||
627 ((type == CACHE_TYPE_METADATA || type == CACHE_TYPE_XMP_METADATA) && options->metadata.enable_metadata_dirs)) &&
628 access_file(base, W_OK))
630 path = g_build_filename(base, cache.local, name, NULL);
631 if (mode) *mode = 0775;
636 path = g_build_filename(cache.rc, base, name, NULL);
637 if (mode) *mode = 0755;
641 if (name) g_free(name);
646 gchar *cache_find_location(CacheType type, const gchar *source)
649 gboolean prefer_local;
651 if (!source) return nullptr;
653 const CachePathParts cache{type};
655 if (type == CACHE_TYPE_METADATA || type == CACHE_TYPE_XMP_METADATA)
657 prefer_local = options->metadata.enable_metadata_dirs;
661 prefer_local = options->thumbnails.cache_into_dirs;
666 path = cache.build_path_local(source);
670 path = cache.build_path_rc(source);
677 /* try the opposite method if not found */
680 path = cache.build_path_local(source);
684 path = cache.build_path_rc(source);
697 gboolean cache_time_valid(const gchar *cache, const gchar *path)
699 struct stat cache_st;
703 gboolean ret = FALSE;
705 if (!cache || !path) return FALSE;
707 cachel = path_from_utf8(cache);
708 pathl = path_from_utf8(path);
710 if (stat(cachel, &cache_st) == 0 &&
711 stat(pathl, &path_st) == 0)
713 if (cache_st.st_mtime == path_st.st_mtime)
717 else if (cache_st.st_mtime > path_st.st_mtime)
721 ut.actime = ut.modtime = cache_st.st_mtime;
722 if (utime(cachel, &ut) < 0 &&
725 DEBUG_1("cache permission workaround: %s", cachel);
737 const gchar *get_thumbnails_cache_dir()
739 static gchar *thumbnails_cache_dir = nullptr;
741 if (thumbnails_cache_dir) return thumbnails_cache_dir;
745 thumbnails_cache_dir = g_build_filename(xdg_cache_home_get(),
746 GQ_APPNAME_LC, GQ_CACHE_THUMB, NULL);
750 thumbnails_cache_dir = g_build_filename(get_rc_dir(), GQ_CACHE_THUMB, NULL);
753 return thumbnails_cache_dir;
756 const gchar *get_thumbnails_standard_cache_dir()
758 static gchar *thumbnails_standard_cache_dir = nullptr;
760 if (thumbnails_standard_cache_dir) return thumbnails_standard_cache_dir;
762 thumbnails_standard_cache_dir = g_build_filename(xdg_cache_home_get(),
763 THUMB_FOLDER_GLOBAL, NULL);
765 return thumbnails_standard_cache_dir;
768 const gchar *get_metadata_cache_dir()
770 static gchar *metadata_cache_dir = nullptr;
772 if (metadata_cache_dir) return metadata_cache_dir;
776 /* Metadata go to $XDG_DATA_HOME.
777 * "Keywords and comments, among other things, are irreplaceable and cannot be auto-generated,
778 * so I don't think they'd be appropriate for the cache directory." -- Omari Stephens on geeqie-devel ml
780 metadata_cache_dir = g_build_filename(xdg_data_home_get(), GQ_APPNAME_LC, GQ_CACHE_METADATA, NULL);
784 metadata_cache_dir = g_build_filename(get_rc_dir(), GQ_CACHE_METADATA, NULL);
787 return metadata_cache_dir;
790 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */