7 * This software is released under the GNU General Public License (GNU GPL).
8 * Please read the included file COPYING for more information.
9 * This software comes with no warranty of any kind, use at your own risk!
16 #include "ui_fileops.h"
23 *-------------------------------------------------------------------
24 * Cache data file format:
25 *-------------------------------------------------------------------
29 * Dimensions=[<width> x <height>]
30 * Date=[<value in time_t format, or -1 if no embedded date>]
32 * MD5sum=[<32 character ascii text digest>]
33 * SimilarityGrid[32 x 32]=<3072 bytes of data (1024 pixels in RGB format, 1 pixel is 24bits)>
36 * The first line (9 bytes) indicates it is a SIMcache format file. (new line char must exist)
37 * Comment lines starting with a # are ignored up to a new line.
38 * All data lines should end with a new line char.
39 * Format is very strict, data must begin with the char immediately following '='.
40 * Currently SimilarityGrid is always assumed to be 32 x 32 RGB.
45 *-------------------------------------------------------------------
47 *-------------------------------------------------------------------
50 CacheData *cache_sim_data_new(void)
54 cd = g_new0(CacheData, 1);
60 void cache_sim_data_free(CacheData *cd)
65 image_sim_free(cd->sim);
70 *-------------------------------------------------------------------
72 *-------------------------------------------------------------------
75 static gint cache_sim_write_dimensions(FILE *f, CacheData *cd)
77 if (!f || !cd || !cd->dimensions) return FALSE;
79 fprintf(f, "Dimensions=[%d x %d]\n", cd->width, cd->height);
84 static gint cache_sim_write_date(FILE *f, CacheData *cd)
86 if (!f || !cd || !cd->have_date) return FALSE;
88 fprintf(f, "Date=[%ld]\n", cd->date);
93 static gint cache_sim_write_checksum(FILE *f, CacheData *cd)
95 if (!f || !cd || !cd->have_checksum) return FALSE;
97 fprintf(f, "Checksum=[%ld]\n", cd->checksum);
102 static gint cache_sim_write_md5sum(FILE *f, CacheData *cd)
106 if (!f || !cd || !cd->have_md5sum) return FALSE;
108 text = md5_digest_to_text(cd->md5sum);
109 fprintf(f, "MD5sum=[%s]\n", text);
115 static gint cache_sim_write_similarity(FILE *f, CacheData *cd)
117 gint success = FALSE;
119 if (!f || !cd || !cd->similarity) return FALSE;
121 if (cd->sim && cd->sim->filled)
126 fprintf(f, "SimilarityGrid[32 x 32]=");
127 for (y = 0; y < 32; y++)
134 for(x = 0; x < 32; x++)
136 *p = cd->sim->avg_r[s + x]; p++;
137 *p = cd->sim->avg_g[s + x]; p++;
138 *p = cd->sim->avg_b[s + x]; p++;
140 fwrite(buf, sizeof(buf), 1, f);
150 gint cache_sim_data_save(CacheData *cd)
155 if (!cd || !cd->path) return FALSE;
157 pathl = path_from_utf8(cd->path);
158 f = fopen(pathl, "w");
163 printf("Unable to save sim cache data: %s\n", cd->path);
167 fprintf(f, "SIMcache\n#%s %s\n", PACKAGE, VERSION);
168 cache_sim_write_dimensions(f, cd);
169 cache_sim_write_date(f, cd);
170 cache_sim_write_checksum(f, cd);
171 cache_sim_write_md5sum(f, cd);
172 cache_sim_write_similarity(f, cd);
180 *-------------------------------------------------------------------
182 *-------------------------------------------------------------------
185 static gint cache_sim_read_skipline(FILE *f, int s)
187 if (!f) return FALSE;
189 if (fseek(f, 0 - s, SEEK_CUR) == 0)
192 while (fread(&b, sizeof(b), 1, f) == 1)
194 if (b == '\n') return TRUE;
202 static gint cache_sim_read_comment(FILE *f, char *buf, int s, CacheData *cd)
204 if (!f || !buf || !cd) return FALSE;
206 if (s < 1 || buf[0] != '#') return FALSE;
208 return cache_sim_read_skipline(f, s - 1);
211 static gint cache_sim_read_dimensions(FILE *f, char *buf, int s, CacheData *cd)
213 if (!f || !buf || !cd) return FALSE;
215 if (s < 10 || strncmp("Dimensions", buf, 10) != 0) return FALSE;
217 if (fseek(f, - s, SEEK_CUR) == 0)
227 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
229 while (b != ']' && p < 1023)
231 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
238 if (fread(&b, sizeof(b), 1, f) != 1) break;
242 if (sscanf(buf, "%d x %d", &w, &h) != 2) return FALSE;
246 cd->dimensions = TRUE;
254 static gint cache_sim_read_date(FILE *f, char *buf, int s, CacheData *cd)
256 if (!f || !buf || !cd) return FALSE;
258 if (s < 4 || strncmp("Date", buf, 4) != 0) return FALSE;
260 if (fseek(f, - s, SEEK_CUR) == 0)
269 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
271 while (b != ']' && p < 1023)
273 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
280 if (fread(&b, sizeof(b), 1, f) != 1) break;
284 cd->date = strtol(buf, NULL, 10);
286 cd->have_date = TRUE;
294 static gint cache_sim_read_checksum(FILE *f, char *buf, int s, CacheData *cd)
296 if (!f || !buf || !cd) return FALSE;
298 if (s < 8 || strncmp("Checksum", buf, 8) != 0) return FALSE;
300 if (fseek(f, - s, SEEK_CUR) == 0)
309 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
311 while (b != ']' && p < 1023)
313 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
320 if (fread(&b, sizeof(b), 1, f) != 1) break;
324 cd->checksum = strtol(buf, NULL, 10);
326 cd->have_checksum = TRUE;
334 static gint cache_sim_read_md5sum(FILE *f, char *buf, int s, CacheData *cd)
336 if (!f || !buf || !cd) return FALSE;
338 if (s < 8 || strncmp("MD5sum", buf, 6) != 0) return FALSE;
340 if (fseek(f, - s, SEEK_CUR) == 0)
349 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
351 while (b != ']' && p < 63)
353 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
359 if (fread(&b, sizeof(b), 1, f) != 1) break;
363 cd->have_md5sum = md5_digest_from_text(buf, cd->md5sum);
371 static gint cache_sim_read_similarity(FILE *f, char *buf, int s, CacheData *cd)
373 if (!f || !buf || !cd) return FALSE;
375 if (s < 11 || strncmp("Similarity", buf, 10) != 0) return FALSE;
377 if (strncmp("Grid[32 x 32]", buf + 10, 13) != 0) return FALSE;
379 if (fseek(f, - s, SEEK_CUR) == 0)
383 ImageSimilarityData *sd;
389 if (fread(&b, sizeof(b), 1, f) != 1) return FALSE;
394 /* use current sim that may already contain data we will not touch here */
397 cd->similarity = FALSE;
401 sd = image_sim_new();
404 for (y = 0; y < 32; y++)
407 for (x = 0; x < 32; x++)
409 if (fread(&pixel_buf, sizeof(pixel_buf), 1, f) != 1)
414 sd->avg_r[s + x] = pixel_buf[0];
415 sd->avg_g[s + x] = pixel_buf[1];
416 sd->avg_b[s + x] = pixel_buf[2];
420 if (fread(&b, sizeof(b), 1, f) == 1)
422 if (b != '\n') fseek(f, -1, SEEK_CUR);
426 cd->sim->filled = TRUE;
427 cd->similarity = TRUE;
435 #define CACHE_LOAD_LINE_NOISE 8
437 CacheData *cache_sim_data_load(const gchar *path)
440 CacheData *cd = NULL;
442 gint success = CACHE_LOAD_LINE_NOISE;
445 if (!path) return NULL;
447 pathl = path_from_utf8(path);
448 f = fopen(pathl, "r");
453 cd = cache_sim_data_new();
454 cd->path = g_strdup(path);
456 if (fread(&buf, sizeof(char), 9, f) != 9 ||
457 strncmp(buf, "SIMcache", 8) != 0)
459 if (debug) printf("%s is not a cache file\n", cd->path);
466 s = fread(&buf, sizeof(char), sizeof(buf), f);
474 if (!cache_sim_read_comment(f, buf, s, cd) &&
475 !cache_sim_read_dimensions(f, buf, s, cd) &&
476 !cache_sim_read_date(f, buf, s, cd) &&
477 !cache_sim_read_checksum(f, buf, s, cd) &&
478 !cache_sim_read_md5sum(f, buf, s, cd) &&
479 !cache_sim_read_similarity(f, buf, s, cd))
481 if (!cache_sim_read_skipline(f, s))
492 success = CACHE_LOAD_LINE_NOISE;
499 if (!cd->dimensions &&
501 !cd->have_checksum &&
505 cache_sim_data_free(cd);
513 *-------------------------------------------------------------------
515 *-------------------------------------------------------------------
518 void cache_sim_data_set_dimensions(CacheData *cd, gint w, gint h)
524 cd->dimensions = TRUE;
527 void cache_sim_data_set_date(CacheData *cd, time_t date)
532 cd->have_date = TRUE;
535 void cache_sim_data_set_checksum(CacheData *cd, long checksum)
539 cd->checksum = checksum;
540 cd->have_checksum = TRUE;
543 void cache_sim_data_set_md5sum(CacheData *cd, guchar digest[16])
549 for (i = 0; i < 16; i++)
551 cd->md5sum[i] = digest[i];
553 cd->have_md5sum = TRUE;
556 void cache_sim_data_set_similarity(CacheData *cd, ImageSimilarityData *sd)
558 if (!cd || !sd || !sd->filled) return;
560 if (!cd->sim) cd->sim = image_sim_new();
562 memcpy(cd->sim->avg_r, sd->avg_r, 1024);
563 memcpy(cd->sim->avg_g, sd->avg_g, 1024);
564 memcpy(cd->sim->avg_b, sd->avg_b, 1024);
565 cd->sim->filled = TRUE;
567 cd->similarity = TRUE;
570 gint cache_sim_data_filled(ImageSimilarityData *sd)
572 if (!sd) return FALSE;
577 *-------------------------------------------------------------------
578 * cache path location utils
579 *-------------------------------------------------------------------
582 /* warning: this func modifies path string contents!, on fail it is set to fail point */
583 gint cache_ensure_dir_exists(gchar *path, mode_t mode)
585 if (!path) return FALSE;
593 if (p[0] == '/' || p[0] == '\0')
603 if (debug) printf("creating sub dir:%s\n", path);
604 if (!mkdir_utf8(path, mode))
606 printf("create dir failed: %s\n", path);
610 if (!end) p[0] = '/';
617 static void cache_path_parts(CacheType type,
618 const gchar **cache_rc, const gchar **cache_local, const gchar **cache_ext)
622 case CACHE_TYPE_THUMB:
623 *cache_rc = GQVIEW_CACHE_RC_THUMB;
624 *cache_local = GQVIEW_CACHE_LOCAL_THUMB;
625 *cache_ext = GQVIEW_CACHE_EXT_THUMB;
628 *cache_rc = GQVIEW_CACHE_RC_THUMB;
629 *cache_local = GQVIEW_CACHE_LOCAL_THUMB;
630 *cache_ext = GQVIEW_CACHE_EXT_SIM;
632 case CACHE_TYPE_METADATA:
633 *cache_rc = GQVIEW_CACHE_RC_METADATA;
634 *cache_local = GQVIEW_CACHE_LOCAL_METADATA;
635 *cache_ext = GQVIEW_CACHE_EXT_METADATA;
640 gchar *cache_get_location(CacheType type, const gchar *source, gint include_name, mode_t *mode)
645 const gchar *cache_rc;
646 const gchar *cache_local;
647 const gchar *cache_ext;
649 if (!source) return NULL;
651 cache_path_parts(type, &cache_rc, &cache_local, &cache_ext);
653 base = remove_level_from_path(source);
656 name = g_strconcat("/", filename_from_path(source), NULL);
663 if (((type != CACHE_TYPE_METADATA && enable_thumb_dirs) ||
664 (type == CACHE_TYPE_METADATA && enable_metadata_dirs)) &&
665 access_file(base, W_OK))
667 path = g_strconcat(base, "/", cache_local, name, cache_ext, NULL);
668 if (mode) *mode = 0775;
673 path = g_strconcat(homedir(), "/", cache_rc, base, name, cache_ext, NULL);
674 if (mode) *mode = 0755;
678 if (name) g_free(name);
683 gchar *cache_find_location(CacheType type, const gchar *source)
688 const gchar *cache_rc;
689 const gchar *cache_local;
690 const gchar *cache_ext;
693 if (!source) return NULL;
695 cache_path_parts(type, &cache_rc, &cache_local, &cache_ext);
697 name = filename_from_path(source);
698 base = remove_level_from_path(source);
700 if (type == CACHE_TYPE_METADATA)
702 prefer_local = enable_metadata_dirs;
706 prefer_local = enable_thumb_dirs;
711 path = g_strconcat(base, "/", cache_local, "/", name, cache_ext, NULL);
715 path = g_strconcat(homedir(), "/", cache_rc, source, cache_ext, NULL);
722 /* try the opposite method if not found */
725 path = g_strconcat(base, "/", cache_local, "/", name, cache_ext, NULL);
729 path = g_strconcat(homedir(), "/", cache_rc, source, cache_ext, NULL);
744 gint cache_time_valid(const gchar *cache, const gchar *path)
746 struct stat cache_st;
752 if (!cache || !path) return FALSE;
754 cachel = path_from_utf8(cache);
755 pathl = path_from_utf8(path);
757 if (stat(cachel, &cache_st) == 0 &&
758 stat(pathl, &path_st) == 0)
760 if (cache_st.st_mtime == path_st.st_mtime)
764 else if (cache_st.st_mtime > path_st.st_mtime)
768 ut.actime = ut.modtime = cache_st.st_mtime;
769 if (utime(cachel, &ut) < 0 &&
772 if (debug) printf("cache permission workaround: %s\n", cachel);