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"
35 *-------------------------------------------------------------------
36 * Cache data file format:
37 *-------------------------------------------------------------------
41 * Dimensions=[<width> x <height>] \n
42 * Date=[<value in time_t format, or -1 if no embedded date>] \n
43 * MD5sum=[<32 character ascii text digest>] \n
44 * SimilarityGrid[32 x 32]=<3072 bytes of data (1024 pixels in RGB format, 1 pixel is 24bits)>
46 * The first line (9 bytes) indicates it is a SIMcache format file. (new line char must exist) \n
47 * Comment lines starting with a # are ignored up to a new line. \n
48 * All data lines should end with a new line char. \n
49 * Format is very strict, data must begin with the char immediately following '='. \n
50 * Currently SimilarityGrid is always assumed to be 32 x 32 RGB. \n
55 *-------------------------------------------------------------------
57 *-------------------------------------------------------------------
60 CacheData *cache_sim_data_new()
64 cd = g_new0(CacheData, 1);
70 void cache_sim_data_free(CacheData *cd)
75 image_sim_free(cd->sim);
80 *-------------------------------------------------------------------
82 *-------------------------------------------------------------------
85 static gboolean cache_sim_write_dimensions(SecureSaveInfo *ssi, CacheData *cd)
87 if (!cd || !cd->dimensions) return FALSE;
89 secure_fprintf(ssi, "Dimensions=[%d x %d]\n", cd->width, cd->height);
94 static gboolean cache_sim_write_date(SecureSaveInfo *ssi, CacheData *cd)
96 if (!cd || !cd->have_date) return FALSE;
98 secure_fprintf(ssi, "Date=[%ld]\n", cd->date);
103 static gboolean cache_sim_write_md5sum(SecureSaveInfo *ssi, CacheData *cd)
107 if (!cd || !cd->have_md5sum) return FALSE;
109 text = md5_digest_to_text(cd->md5sum);
110 secure_fprintf(ssi, "MD5sum=[%s]\n", text);
116 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, const 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)
230 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
232 while (b != ']' && p < sizeof(buf) - 1)
234 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
241 if (fread(&b, sizeof(b), 1, f) != 1) break;
245 if (sscanf(buf, "%d x %d", &w, &h) != 2) return FALSE;
249 cd->dimensions = TRUE;
257 static gboolean cache_sim_read_date(FILE *f, gchar *buf, gint s, CacheData *cd)
259 if (!f || !buf || !cd) return FALSE;
261 if (s < 4 || strncmp("Date", buf, 4) != 0) return FALSE;
263 if (fseek(f, - s, SEEK_CUR) == 0)
272 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
274 while (b != ']' && p < sizeof(buf) - 1)
276 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
283 if (fread(&b, sizeof(b), 1, f) != 1) break;
287 cd->date = strtol(buf, nullptr, 10);
289 cd->have_date = TRUE;
297 static gboolean cache_sim_read_md5sum(FILE *f, gchar *buf, gint s, CacheData *cd)
299 if (!f || !buf || !cd) return FALSE;
301 if (s < 8 || strncmp("MD5sum", buf, 6) != 0) return FALSE;
303 if (fseek(f, - s, SEEK_CUR) == 0)
312 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
314 while (b != ']' && p < sizeof(buf) - 1)
316 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
322 if (fread(&b, sizeof(b), 1, f) != 1) break;
326 cd->have_md5sum = md5_digest_from_text(buf, cd->md5sum);
334 static gboolean cache_sim_read_similarity(FILE *f, gchar *buf, gint s, CacheData *cd)
336 if (!f || !buf || !cd) return FALSE;
338 if (s < 11 || strncmp("Similarity", buf, 10) != 0) return FALSE;
340 if (strncmp("Grid[32 x 32]", buf + 10, 13) != 0) return FALSE;
342 if (fseek(f, - s, SEEK_CUR) == 0)
346 ImageSimilarityData *sd;
353 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
358 /* use current sim that may already contain data we will not touch here */
361 cd->similarity = FALSE;
365 sd = image_sim_new();
368 for (y = 0; y < 32; y++)
371 for (x = 0; x < 32; x++)
373 if (fread(&pixel_buf, sizeof(pixel_buf), 1, f) != 1)
378 sd->avg_r[s + x] = pixel_buf[0];
379 sd->avg_g[s + x] = pixel_buf[1];
380 sd->avg_b[s + x] = pixel_buf[2];
384 if (fread(&b, sizeof(b), 1, f) == 1)
386 if (b != '\n') fseek(f, -1, SEEK_CUR);
390 cd->sim->filled = TRUE;
391 cd->similarity = TRUE;
400 CACHE_LOAD_LINE_NOISE = 8
403 CacheData *cache_sim_data_load(const gchar *path)
406 CacheData *cd = nullptr;
408 gint success = CACHE_LOAD_LINE_NOISE;
411 if (!path) return nullptr;
413 pathl = path_from_utf8(path);
414 f = fopen(pathl, "r");
417 if (!f) return nullptr;
419 cd = cache_sim_data_new();
420 cd->path = g_strdup(path);
422 if (fread(&buf, sizeof(gchar), 9, f) != 9 ||
423 strncmp(buf, "SIMcache", 8) != 0)
425 DEBUG_1("%s is not a cache file", cd->path);
432 s = fread(&buf, sizeof(gchar), sizeof(buf), f);
440 if (!cache_sim_read_comment(f, buf, s, cd) &&
441 !cache_sim_read_dimensions(f, buf, s, cd) &&
442 !cache_sim_read_date(f, buf, s, cd) &&
443 !cache_sim_read_md5sum(f, buf, s, cd) &&
444 !cache_sim_read_similarity(f, buf, s, cd))
446 if (!cache_sim_read_skipline(f, s))
457 success = CACHE_LOAD_LINE_NOISE;
464 if (!cd->dimensions &&
469 cache_sim_data_free(cd);
477 *-------------------------------------------------------------------
479 *-------------------------------------------------------------------
482 void cache_sim_data_set_dimensions(CacheData *cd, gint w, gint h)
488 cd->dimensions = TRUE;
491 #pragma GCC diagnostic push
492 #pragma GCC diagnostic ignored "-Wunused-function"
493 void cache_sim_data_set_date_unused(CacheData *cd, time_t date)
498 cd->have_date = TRUE;
500 #pragma GCC diagnostic pop
502 void cache_sim_data_set_md5sum(CacheData *cd, const guchar digest[16])
508 for (i = 0; i < 16; i++)
510 cd->md5sum[i] = digest[i];
512 cd->have_md5sum = TRUE;
515 void cache_sim_data_set_similarity(CacheData *cd, ImageSimilarityData *sd)
517 if (!cd || !sd || !sd->filled) return;
519 if (!cd->sim) cd->sim = image_sim_new();
521 memcpy(cd->sim->avg_r, sd->avg_r, 1024);
522 memcpy(cd->sim->avg_g, sd->avg_g, 1024);
523 memcpy(cd->sim->avg_b, sd->avg_b, 1024);
524 cd->sim->filled = TRUE;
526 cd->similarity = TRUE;
529 gboolean cache_sim_data_filled(ImageSimilarityData *sd)
531 if (!sd) return FALSE;
536 *-------------------------------------------------------------------
537 * cache path location utils
538 *-------------------------------------------------------------------
541 struct CachePathParts
543 CachePathParts(CacheType type)
547 case CACHE_TYPE_THUMB:
548 rc = get_thumbnails_cache_dir();
549 local = GQ_CACHE_LOCAL_THUMB;
550 ext = GQ_CACHE_EXT_THUMB;
553 rc = get_thumbnails_cache_dir();
554 local = GQ_CACHE_LOCAL_THUMB;
555 ext = GQ_CACHE_EXT_SIM;
557 case CACHE_TYPE_METADATA:
558 rc = get_metadata_cache_dir();
559 local = GQ_CACHE_LOCAL_METADATA;
560 ext = GQ_CACHE_EXT_METADATA;
562 case CACHE_TYPE_XMP_METADATA:
563 rc = get_metadata_cache_dir();
564 local = GQ_CACHE_LOCAL_METADATA;
565 ext = GQ_CACHE_EXT_XMP_METADATA;
570 gchar *build_path_local(const gchar *source) const
572 gchar *base = remove_level_from_path(source);
573 gchar *name = g_strconcat(filename_from_path(source), ext, nullptr);
574 gchar *path = g_build_filename(base, local, name, nullptr);
581 gchar *build_path_rc(const gchar *source) const
583 gchar *name = g_strconcat(source, ext, nullptr);
584 gchar *path = g_build_filename(rc, name, nullptr);
590 const gchar *rc = nullptr;
591 const gchar *local = nullptr;
592 const gchar *ext = nullptr;
595 gchar *cache_get_location(CacheType type, const gchar *source, gint include_name, mode_t *mode)
597 gchar *path = nullptr;
599 gchar *name = nullptr;
601 if (!source) return nullptr;
603 const CachePathParts cache{type};
605 base = remove_level_from_path(source);
608 name = g_strconcat(filename_from_path(source), cache.ext, NULL);
611 if (((type != CACHE_TYPE_METADATA && type != CACHE_TYPE_XMP_METADATA && options->thumbnails.cache_into_dirs) ||
612 ((type == CACHE_TYPE_METADATA || type == CACHE_TYPE_XMP_METADATA) && options->metadata.enable_metadata_dirs)) &&
613 access_file(base, W_OK))
615 path = g_build_filename(base, cache.local, name, NULL);
616 if (mode) *mode = 0775;
621 path = g_build_filename(cache.rc, base, name, NULL);
622 if (mode) *mode = 0755;
626 if (name) g_free(name);
631 gchar *cache_find_location(CacheType type, const gchar *source)
634 gboolean prefer_local;
636 if (!source) return nullptr;
638 const CachePathParts cache{type};
640 if (type == CACHE_TYPE_METADATA || type == CACHE_TYPE_XMP_METADATA)
642 prefer_local = options->metadata.enable_metadata_dirs;
646 prefer_local = options->thumbnails.cache_into_dirs;
651 path = cache.build_path_local(source);
655 path = cache.build_path_rc(source);
662 /* try the opposite method if not found */
665 path = cache.build_path_local(source);
669 path = cache.build_path_rc(source);
682 gboolean cache_time_valid(const gchar *cache, const gchar *path)
684 struct stat cache_st;
688 gboolean ret = FALSE;
690 if (!cache || !path) return FALSE;
692 cachel = path_from_utf8(cache);
693 pathl = path_from_utf8(path);
695 if (stat(cachel, &cache_st) == 0 &&
696 stat(pathl, &path_st) == 0)
698 if (cache_st.st_mtime == path_st.st_mtime)
702 else if (cache_st.st_mtime > path_st.st_mtime)
706 ut.actime = ut.modtime = cache_st.st_mtime;
707 if (utime(cachel, &ut) < 0 &&
710 DEBUG_1("cache permission workaround: %s", cachel);
722 const gchar *get_thumbnails_cache_dir()
724 static gchar *thumbnails_cache_dir = nullptr;
726 if (thumbnails_cache_dir) return thumbnails_cache_dir;
730 thumbnails_cache_dir = g_build_filename(xdg_cache_home_get(),
731 GQ_APPNAME_LC, GQ_CACHE_THUMB, NULL);
735 thumbnails_cache_dir = g_build_filename(get_rc_dir(), GQ_CACHE_THUMB, NULL);
738 return thumbnails_cache_dir;
741 const gchar *get_thumbnails_standard_cache_dir()
743 static gchar *thumbnails_standard_cache_dir = nullptr;
745 if (thumbnails_standard_cache_dir) return thumbnails_standard_cache_dir;
747 thumbnails_standard_cache_dir = g_build_filename(xdg_cache_home_get(),
748 THUMB_FOLDER_GLOBAL, NULL);
750 return thumbnails_standard_cache_dir;
753 const gchar *get_metadata_cache_dir()
755 static gchar *metadata_cache_dir = nullptr;
757 if (metadata_cache_dir) return metadata_cache_dir;
761 /* Metadata go to $XDG_DATA_HOME.
762 * "Keywords and comments, among other things, are irreplaceable and cannot be auto-generated,
763 * so I don't think they'd be appropriate for the cache directory." -- Omari Stephens on geeqie-devel ml
765 metadata_cache_dir = g_build_filename(xdg_data_home_get(), GQ_APPNAME_LC, GQ_CACHE_METADATA, NULL);
769 metadata_cache_dir = g_build_filename(get_rc_dir(), GQ_CACHE_METADATA, NULL);
772 return metadata_cache_dir;
775 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */