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
68 *-------------------------------------------------------------------
70 *-------------------------------------------------------------------
73 CacheData *cache_sim_data_new()
77 cd = g_new0(CacheData, 1);
83 void cache_sim_data_free(CacheData *cd)
88 image_sim_free(cd->sim);
93 *-------------------------------------------------------------------
95 *-------------------------------------------------------------------
98 static gboolean cache_sim_write_dimensions(SecureSaveInfo *ssi, CacheData *cd)
100 if (!cd || !cd->dimensions) return FALSE;
102 secure_fprintf(ssi, "Dimensions=[%d x %d]\n", cd->width, cd->height);
107 static gboolean cache_sim_write_date(SecureSaveInfo *ssi, CacheData *cd)
109 if (!cd || !cd->have_date) return FALSE;
111 secure_fprintf(ssi, "Date=[%ld]\n", cd->date);
116 static gboolean cache_sim_write_md5sum(SecureSaveInfo *ssi, CacheData *cd)
120 if (!cd || !cd->have_md5sum) return FALSE;
122 text = md5_digest_to_text(cd->md5sum);
123 secure_fprintf(ssi, "MD5sum=[%s]\n", text);
129 static gboolean cache_sim_write_similarity(SecureSaveInfo *ssi, CacheData *cd)
135 if (!cd || !cd->similarity || !cd->sim || !cd->sim->filled) return FALSE;
137 secure_fprintf(ssi, "SimilarityGrid[32 x 32]=");
138 for (y = 0; y < 32; y++)
141 guint8 *avg_r = &cd->sim->avg_r[s];
142 guint8 *avg_g = &cd->sim->avg_g[s];
143 guint8 *avg_b = &cd->sim->avg_b[s];
146 for (x = 0; x < 32; x++)
153 secure_fwrite(buf, sizeof(buf), 1, ssi);
156 secure_fputc(ssi, '\n');
161 gboolean cache_sim_data_save(CacheData *cd)
166 if (!cd || !cd->path) return FALSE;
168 pathl = path_from_utf8(cd->path);
169 ssi = secure_open(pathl);
174 log_printf("Unable to save sim cache data: %s\n", cd->path);
178 secure_fprintf(ssi, "SIMcache\n#%s %s\n", PACKAGE, VERSION);
179 cache_sim_write_dimensions(ssi, cd);
180 cache_sim_write_date(ssi, cd);
181 cache_sim_write_md5sum(ssi, cd);
182 cache_sim_write_similarity(ssi, cd);
184 if (secure_close(ssi))
186 log_printf(_("error saving sim cache data: %s\nerror: %s\n"), cd->path,
187 secsave_strerror(secsave_errno));
195 *-------------------------------------------------------------------
197 *-------------------------------------------------------------------
200 static gboolean cache_sim_read_skipline(FILE *f, gint s)
202 if (!f) return FALSE;
204 if (fseek(f, 0 - s, SEEK_CUR) == 0)
207 while (fread(&b, sizeof(b), 1, f) == 1)
209 if (b == '\n') return TRUE;
217 static gboolean cache_sim_read_comment(FILE *f, const gchar *buf, gint s, CacheData *cd)
219 if (!f || !buf || !cd) return FALSE;
221 if (s < 1 || buf[0] != '#') return FALSE;
223 return cache_sim_read_skipline(f, s - 1);
226 static gboolean cache_sim_read_dimensions(FILE *f, gchar *buf, gint s, CacheData *cd)
228 if (!f || !buf || !cd) return FALSE;
230 if (s < 10 || strncmp("Dimensions", buf, 10) != 0) return FALSE;
232 if (fseek(f, - s, SEEK_CUR) == 0)
243 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
245 while (b != ']' && p < sizeof(buf) - 1)
247 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
254 if (fread(&b, sizeof(b), 1, f) != 1) break;
258 if (sscanf(buf, "%d x %d", &w, &h) != 2) return FALSE;
262 cd->dimensions = TRUE;
270 static gboolean cache_sim_read_date(FILE *f, gchar *buf, gint s, CacheData *cd)
272 if (!f || !buf || !cd) return FALSE;
274 if (s < 4 || strncmp("Date", buf, 4) != 0) return FALSE;
276 if (fseek(f, - s, SEEK_CUR) == 0)
285 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
287 while (b != ']' && p < sizeof(buf) - 1)
289 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
296 if (fread(&b, sizeof(b), 1, f) != 1) break;
300 cd->date = strtol(buf, nullptr, 10);
302 cd->have_date = TRUE;
310 static gboolean cache_sim_read_md5sum(FILE *f, gchar *buf, gint s, CacheData *cd)
312 if (!f || !buf || !cd) return FALSE;
314 if (s < 8 || strncmp("MD5sum", buf, 6) != 0) return FALSE;
316 if (fseek(f, - s, SEEK_CUR) == 0)
325 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
327 while (b != ']' && p < sizeof(buf) - 1)
329 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
335 if (fread(&b, sizeof(b), 1, f) != 1) break;
339 cd->have_md5sum = md5_digest_from_text(buf, cd->md5sum);
347 static gboolean cache_sim_read_similarity(FILE *f, gchar *buf, gint s, CacheData *cd)
349 if (!f || !buf || !cd) return FALSE;
351 if (s < 11 || strncmp("Similarity", buf, 10) != 0) return FALSE;
353 if (strncmp("Grid[32 x 32]", buf + 10, 13) != 0) return FALSE;
355 if (fseek(f, - s, SEEK_CUR) == 0)
359 ImageSimilarityData *sd;
366 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
371 /* use current sim that may already contain data we will not touch here */
374 cd->similarity = FALSE;
378 sd = image_sim_new();
381 for (y = 0; y < 32; y++)
384 for (x = 0; x < 32; x++)
386 if (fread(&pixel_buf, sizeof(pixel_buf), 1, f) != 1)
391 sd->avg_r[s + x] = pixel_buf[0];
392 sd->avg_g[s + x] = pixel_buf[1];
393 sd->avg_b[s + x] = pixel_buf[2];
397 if (fread(&b, sizeof(b), 1, f) == 1)
399 if (b != '\n') fseek(f, -1, SEEK_CUR);
403 cd->sim->filled = TRUE;
404 cd->similarity = TRUE;
413 CACHE_LOAD_LINE_NOISE = 8
416 CacheData *cache_sim_data_load(const gchar *path)
419 CacheData *cd = nullptr;
421 gint success = CACHE_LOAD_LINE_NOISE;
424 if (!path) return nullptr;
426 pathl = path_from_utf8(path);
427 f = fopen(pathl, "r");
430 if (!f) return nullptr;
432 cd = cache_sim_data_new();
433 cd->path = g_strdup(path);
435 if (fread(&buf, sizeof(gchar), 9, f) != 9 ||
436 strncmp(buf, "SIMcache", 8) != 0)
438 DEBUG_1("%s is not a cache file", cd->path);
445 s = fread(&buf, sizeof(gchar), sizeof(buf), f);
453 if (!cache_sim_read_comment(f, buf, s, cd) &&
454 !cache_sim_read_dimensions(f, buf, s, cd) &&
455 !cache_sim_read_date(f, buf, s, cd) &&
456 !cache_sim_read_md5sum(f, buf, s, cd) &&
457 !cache_sim_read_similarity(f, buf, s, cd))
459 if (!cache_sim_read_skipline(f, s))
470 success = CACHE_LOAD_LINE_NOISE;
477 if (!cd->dimensions &&
482 cache_sim_data_free(cd);
490 *-------------------------------------------------------------------
492 *-------------------------------------------------------------------
495 void cache_sim_data_set_dimensions(CacheData *cd, gint w, gint h)
501 cd->dimensions = TRUE;
504 #pragma GCC diagnostic push
505 #pragma GCC diagnostic ignored "-Wunused-function"
506 void cache_sim_data_set_date_unused(CacheData *cd, time_t date)
511 cd->have_date = TRUE;
513 #pragma GCC diagnostic pop
515 void cache_sim_data_set_md5sum(CacheData *cd, const guchar digest[16])
521 for (i = 0; i < 16; i++)
523 cd->md5sum[i] = digest[i];
525 cd->have_md5sum = TRUE;
528 void cache_sim_data_set_similarity(CacheData *cd, ImageSimilarityData *sd)
530 if (!cd || !sd || !sd->filled) return;
532 if (!cd->sim) cd->sim = image_sim_new();
534 memcpy(cd->sim->avg_r, sd->avg_r, 1024);
535 memcpy(cd->sim->avg_g, sd->avg_g, 1024);
536 memcpy(cd->sim->avg_b, sd->avg_b, 1024);
537 cd->sim->filled = TRUE;
539 cd->similarity = TRUE;
542 gboolean cache_sim_data_filled(ImageSimilarityData *sd)
544 if (!sd) return FALSE;
549 *-------------------------------------------------------------------
550 * cache path location utils
551 *-------------------------------------------------------------------
554 struct CachePathParts
556 CachePathParts(CacheType type)
560 case CACHE_TYPE_THUMB:
561 rc = get_thumbnails_cache_dir();
562 local = GQ_CACHE_LOCAL_THUMB;
563 ext = GQ_CACHE_EXT_THUMB;
566 rc = get_thumbnails_cache_dir();
567 local = GQ_CACHE_LOCAL_THUMB;
568 ext = GQ_CACHE_EXT_SIM;
570 case CACHE_TYPE_METADATA:
571 rc = get_metadata_cache_dir();
572 local = GQ_CACHE_LOCAL_METADATA;
573 ext = GQ_CACHE_EXT_METADATA;
575 case CACHE_TYPE_XMP_METADATA:
576 rc = get_metadata_cache_dir();
577 local = GQ_CACHE_LOCAL_METADATA;
578 ext = GQ_CACHE_EXT_XMP_METADATA;
583 gchar *build_path_local(const gchar *source) const
585 gchar *base = remove_level_from_path(source);
586 gchar *name = g_strconcat(filename_from_path(source), ext, nullptr);
587 gchar *path = g_build_filename(base, local, name, nullptr);
594 gchar *build_path_rc(const gchar *source) const
596 gchar *name = g_strconcat(source, ext, nullptr);
597 gchar *path = g_build_filename(rc, name, nullptr);
603 const gchar *rc = nullptr;
604 const gchar *local = nullptr;
605 const gchar *ext = nullptr;
608 gchar *cache_get_location(CacheType type, const gchar *source, gint include_name, mode_t *mode)
610 gchar *path = nullptr;
612 gchar *name = nullptr;
614 if (!source) return nullptr;
616 const CachePathParts cache{type};
618 base = remove_level_from_path(source);
621 name = g_strconcat(filename_from_path(source), cache.ext, NULL);
624 if (((type != CACHE_TYPE_METADATA && type != CACHE_TYPE_XMP_METADATA && options->thumbnails.cache_into_dirs) ||
625 ((type == CACHE_TYPE_METADATA || type == CACHE_TYPE_XMP_METADATA) && options->metadata.enable_metadata_dirs)) &&
626 access_file(base, W_OK))
628 path = g_build_filename(base, cache.local, name, NULL);
629 if (mode) *mode = 0775;
634 path = g_build_filename(cache.rc, base, name, NULL);
635 if (mode) *mode = 0755;
639 if (name) g_free(name);
644 gchar *cache_find_location(CacheType type, const gchar *source)
647 gboolean prefer_local;
649 if (!source) return nullptr;
651 const CachePathParts cache{type};
653 if (type == CACHE_TYPE_METADATA || type == CACHE_TYPE_XMP_METADATA)
655 prefer_local = options->metadata.enable_metadata_dirs;
659 prefer_local = options->thumbnails.cache_into_dirs;
664 path = cache.build_path_local(source);
668 path = cache.build_path_rc(source);
675 /* try the opposite method if not found */
678 path = cache.build_path_local(source);
682 path = cache.build_path_rc(source);
695 gboolean cache_time_valid(const gchar *cache, const gchar *path)
697 struct stat cache_st;
701 gboolean ret = FALSE;
703 if (!cache || !path) return FALSE;
705 cachel = path_from_utf8(cache);
706 pathl = path_from_utf8(path);
708 if (stat(cachel, &cache_st) == 0 &&
709 stat(pathl, &path_st) == 0)
711 if (cache_st.st_mtime == path_st.st_mtime)
715 else if (cache_st.st_mtime > path_st.st_mtime)
719 ut.actime = ut.modtime = cache_st.st_mtime;
720 if (utime(cachel, &ut) < 0 &&
723 DEBUG_1("cache permission workaround: %s", cachel);
735 const gchar *get_thumbnails_cache_dir()
737 static gchar *thumbnails_cache_dir = nullptr;
739 if (thumbnails_cache_dir) return thumbnails_cache_dir;
743 thumbnails_cache_dir = g_build_filename(xdg_cache_home_get(),
744 GQ_APPNAME_LC, GQ_CACHE_THUMB, NULL);
748 thumbnails_cache_dir = g_build_filename(get_rc_dir(), GQ_CACHE_THUMB, NULL);
751 return thumbnails_cache_dir;
754 const gchar *get_thumbnails_standard_cache_dir()
756 static gchar *thumbnails_standard_cache_dir = nullptr;
758 if (thumbnails_standard_cache_dir) return thumbnails_standard_cache_dir;
760 thumbnails_standard_cache_dir = g_build_filename(xdg_cache_home_get(),
761 THUMB_FOLDER_GLOBAL, NULL);
763 return thumbnails_standard_cache_dir;
766 const gchar *get_metadata_cache_dir()
768 static gchar *metadata_cache_dir = nullptr;
770 if (metadata_cache_dir) return metadata_cache_dir;
774 /* Metadata go to $XDG_DATA_HOME.
775 * "Keywords and comments, among other things, are irreplaceable and cannot be auto-generated,
776 * so I don't think they'd be appropriate for the cache directory." -- Omari Stephens on geeqie-devel ml
778 metadata_cache_dir = g_build_filename(xdg_data_home_get(), GQ_APPNAME_LC, GQ_CACHE_METADATA, NULL);
782 metadata_cache_dir = g_build_filename(get_rc_dir(), GQ_CACHE_METADATA, NULL);
785 return metadata_cache_dir;
788 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */