2 * Copyright (C) 2004 Anders Carlsson <andersca@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
26 #include <sys/types.h>
33 #include <sys/utime.h>
39 #include <glib/gstdio.h>
40 #include <gdk-pixbuf/gdk-pixdata.h>
41 #include <glib/gi18n.h>
42 #include "gtkiconcachevalidator.h"
44 static gboolean force_update = FALSE;
45 static gboolean ignore_theme_index = FALSE;
46 static gboolean quiet = FALSE;
47 static gboolean index_only = FALSE;
48 static gboolean validate = FALSE;
49 static gchar *var_name = "-";
51 /* Quite ugly - if we just add the c file to the
52 * list of sources in Makefile.am, libtool complains.
54 #include "gtkiconcachevalidator.c"
56 #define CACHE_NAME "icon-theme.cache"
58 #define HAS_SUFFIX_XPM (1 << 0)
59 #define HAS_SUFFIX_SVG (1 << 1)
60 #define HAS_SUFFIX_PNG (1 << 2)
61 #define HAS_ICON_FILE (1 << 3)
63 #define MAJOR_VERSION 1
64 #define MINOR_VERSION 0
65 #define HASH_OFFSET 12
67 #define ALIGN_VALUE(this, boundary) \
68 (( ((unsigned long)(this)) + (((unsigned long)(boundary)) -1)) & (~(((unsigned long)(boundary))-1)))
74 static struct stat cache_stat;
75 static gboolean cache_up_to_date;
77 static int check_dir_mtime (const char *dir,
78 const struct stat *sb,
81 if (tf != FTW_NS && sb->st_mtime > cache_stat.st_mtime)
83 cache_up_to_date = FALSE;
92 is_cache_up_to_date (const gchar *path)
97 cache_path = g_build_filename (path, CACHE_NAME, NULL);
98 retval = g_stat (cache_path, &cache_stat);
103 /* Cache file not found */
107 cache_up_to_date = TRUE;
109 ftw (path, check_dir_mtime, 20);
111 return cache_up_to_date;
114 #else /* !HAVE_FTW_H */
117 is_cache_up_to_date (const gchar *path)
119 struct stat path_stat, cache_stat;
123 retval = g_stat (path, &path_stat);
127 /* We can't stat the path,
128 * assume we have a updated cache */
132 cache_path = g_build_filename (path, CACHE_NAME, NULL);
133 retval = g_stat (cache_path, &cache_stat);
138 /* Cache file not found */
143 return cache_stat.st_mtime >= path_stat.st_mtime;
146 #endif /* !HAVE_FTW_H */
149 has_theme_index (const gchar *path)
154 index_path = g_build_filename (path, "index.theme", NULL);
156 result = g_file_test (index_path, G_FILE_TEST_IS_REGULAR);
167 gboolean has_pixdata;
174 int has_embedded_rect;
181 char **display_names;
187 static GHashTable *image_data_hash = NULL;
188 static GHashTable *icon_data_hash = NULL;
195 ImageData *image_data;
196 guint pixel_data_size;
199 guint icon_data_size;
203 free_icon_data (IconData *icon_data)
205 g_free (icon_data->attach_points);
206 g_strfreev (icon_data->display_names);
211 foreach_remove_func (gpointer key, gpointer value, gpointer user_data)
213 Image *image = (Image *)value;
214 GHashTable *files = user_data;
216 gboolean free_key = FALSE;
218 if (image->flags == HAS_ICON_FILE)
220 /* just a .icon file, throw away */
222 free_icon_data (image->icon_data);
228 list = g_hash_table_lookup (files, key);
232 list = g_list_prepend (list, value);
233 g_hash_table_insert (files, key, list);
242 load_icon_data (const char *path)
251 GError *error = NULL;
256 icon_file = g_key_file_new ();
257 g_key_file_set_list_separator (icon_file, ',');
258 g_key_file_load_from_file (icon_file, path, G_KEY_FILE_KEEP_TRANSLATIONS, &error);
261 g_error_free (error);
262 g_key_file_free (icon_file);
267 data = g_new0 (IconData, 1);
269 ivalues = g_key_file_get_integer_list (icon_file,
270 "Icon Data", "EmbeddedTextRectangle",
276 data->has_embedded_rect = TRUE;
277 data->x0 = ivalues[0];
278 data->y0 = ivalues[1];
279 data->x1 = ivalues[2];
280 data->y1 = ivalues[3];
286 str = g_key_file_get_string (icon_file, "Icon Data", "AttachPoints", NULL);
289 split = g_strsplit (str, "|", -1);
291 data->n_attach_points = g_strv_length (split);
292 data->attach_points = g_new (int, 2 * data->n_attach_points);
295 while (split[i] != NULL && i < data->n_attach_points)
297 split_point = strchr (split[i], ',');
302 data->attach_points[2 * i] = atoi (split[i]);
303 data->attach_points[2 * i + 1] = atoi (split_point);
312 keys = g_key_file_get_keys (icon_file, "Icon Data", &n_keys, &error);
313 data->display_names = g_new0 (gchar *, 2 * n_keys + 1);
314 data->n_display_names = 0;
316 for (i = 0; i < n_keys; i++)
320 if (g_str_has_prefix (keys[i], "DisplayName"))
322 gchar *open, *close = NULL;
324 open = strchr (keys[i], '[');
327 close = strchr (open, ']');
331 lang = g_strndup (open + 1, close - open - 1);
332 name = g_key_file_get_locale_string (icon_file,
333 "Icon Data", "DisplayName",
338 lang = g_strdup ("C");
339 name = g_key_file_get_string (icon_file,
340 "Icon Data", "DisplayName",
344 data->display_names[2 * data->n_display_names] = lang;
345 data->display_names[2 * data->n_display_names + 1] = name;
346 data->n_display_names++;
352 g_key_file_free (icon_file);
354 /* -1 means not computed yet, the real value depends
355 * on string pool state, and will be computed
364 * This function was copied from gtkfilesystemunix.c, it should
365 * probably go to GLib
368 canonicalize_filename (gchar *filename)
371 gboolean last_was_slash = FALSE;
378 if (*p == G_DIR_SEPARATOR)
381 *q++ = G_DIR_SEPARATOR;
383 last_was_slash = TRUE;
387 if (last_was_slash && *p == '.')
389 if (*(p + 1) == G_DIR_SEPARATOR ||
392 if (*(p + 1) == '\0')
397 else if (*(p + 1) == '.' &&
398 (*(p + 2) == G_DIR_SEPARATOR ||
401 if (q > filename + 1)
404 while (q > filename + 1 &&
405 *(q - 1) != G_DIR_SEPARATOR)
409 if (*(p + 2) == '\0')
417 last_was_slash = FALSE;
423 last_was_slash = FALSE;
430 if (q > filename + 1 && *(q - 1) == G_DIR_SEPARATOR)
437 follow_links (const gchar *path)
443 path2 = g_strdup (path);
444 while (g_file_test (path2, G_FILE_TEST_IS_SYMLINK))
446 target = g_file_read_link (path2, NULL);
450 if (g_path_is_absolute (target))
454 d = g_path_get_dirname (path2);
455 s = g_build_filename (d, target, NULL);
466 if (strcmp (path, path2) == 0)
476 maybe_cache_image_data (Image *image,
479 if (!index_only && !image->image_data &&
480 (g_str_has_suffix (path, ".png") || g_str_has_suffix (path, ".xpm")))
486 idata = g_hash_table_lookup (image_data_hash, path);
487 path2 = follow_links (path);
493 canonicalize_filename (path2);
495 idata2 = g_hash_table_lookup (image_data_hash, path2);
497 if (idata && idata2 && idata != idata2)
498 g_error (_("different idatas found for symlinked '%s' and '%s'\n"),
501 if (idata && !idata2)
502 g_hash_table_insert (image_data_hash, g_strdup (path2), idata);
504 if (!idata && idata2)
506 g_hash_table_insert (image_data_hash, g_strdup (path), idata2);
513 idata = g_new0 (ImageData, 1);
514 g_hash_table_insert (image_data_hash, g_strdup (path), idata);
516 g_hash_table_insert (image_data_hash, g_strdup (path2), idata);
519 if (!idata->has_pixdata)
521 pixbuf = gdk_pixbuf_new_from_file (path, NULL);
525 gdk_pixdata_from_pixbuf (&idata->pixdata, pixbuf, FALSE);
526 idata->size = idata->pixdata.length + 8;
527 idata->has_pixdata = TRUE;
531 image->image_data = idata;
538 maybe_cache_icon_data (Image *image,
541 if (g_str_has_suffix (path, ".icon"))
543 IconData *idata = NULL;
546 idata = g_hash_table_lookup (icon_data_hash, path);
547 path2 = follow_links (path);
553 canonicalize_filename (path2);
555 idata2 = g_hash_table_lookup (icon_data_hash, path2);
557 if (idata && idata2 && idata != idata2)
558 g_error (_("different idatas found for symlinked '%s' and '%s'\n"),
561 if (idata && !idata2)
562 g_hash_table_insert (icon_data_hash, g_strdup (path2), idata);
564 if (!idata && idata2)
566 g_hash_table_insert (icon_data_hash, g_strdup (path), idata2);
573 idata = load_icon_data (path);
574 g_hash_table_insert (icon_data_hash, g_strdup (path), idata);
576 g_hash_table_insert (icon_data_hash, g_strdup (path2), idata);
579 image->icon_data = idata;
586 scan_directory (const gchar *base_path,
592 GHashTable *dir_hash;
596 gboolean dir_added = FALSE;
597 guint dir_index = 0xffff;
599 dir_path = g_build_filename (base_path, subdir, NULL);
601 /* FIXME: Use the gerror */
602 dir = g_dir_open (dir_path, 0, NULL);
607 dir_hash = g_hash_table_new (g_str_hash, g_str_equal);
609 while ((name = g_dir_read_name (dir)))
615 gchar *basename, *dot;
617 path = g_build_filename (dir_path, name, NULL);
618 retval = g_file_test (path, G_FILE_TEST_IS_DIR);
624 subsubdir = g_build_filename (subdir, name, NULL);
626 subsubdir = g_strdup (name);
627 directories = scan_directory (base_path, subsubdir, files,
628 directories, depth + 1);
634 retval = g_file_test (path, G_FILE_TEST_IS_REGULAR);
637 if (g_str_has_suffix (name, ".png"))
638 flags |= HAS_SUFFIX_PNG;
639 else if (g_str_has_suffix (name, ".svg"))
640 flags |= HAS_SUFFIX_SVG;
641 else if (g_str_has_suffix (name, ".xpm"))
642 flags |= HAS_SUFFIX_XPM;
643 else if (g_str_has_suffix (name, ".icon"))
644 flags |= HAS_ICON_FILE;
649 basename = g_strdup (name);
650 dot = strrchr (basename, '.');
653 image = g_hash_table_lookup (dir_hash, basename);
661 dir_index = g_list_length (directories);
662 directories = g_list_append (directories, g_strdup (subdir));
668 image = g_new0 (Image, 1);
669 image->dir_index = dir_index;
670 g_hash_table_insert (dir_hash, g_strdup (basename), image);
673 image->flags |= flags;
675 maybe_cache_image_data (image, path);
676 maybe_cache_icon_data (image, path);
686 /* Move dir into the big file hash */
687 g_hash_table_foreach_remove (dir_hash, foreach_remove_func, files);
689 g_hash_table_destroy (dir_hash);
694 typedef struct _HashNode HashNode;
705 icon_name_hash (gconstpointer key)
707 const signed char *p = key;
711 for (p += 1; *p != '\0'; p++)
712 h = (h << 5) - h + *p;
723 convert_to_hash (gpointer key, gpointer value, gpointer user_data)
725 HashContext *context = user_data;
729 hash = icon_name_hash (key) % context->size;
731 node = g_new0 (HashNode, 1);
734 node->image_list = value;
736 if (context->nodes[hash] != NULL)
737 node->next = context->nodes[hash];
739 context->nodes[hash] = node;
744 static GHashTable *string_pool = NULL;
747 find_string (const gchar *n)
749 return GPOINTER_TO_INT (g_hash_table_lookup (string_pool, n));
753 add_string (const gchar *n, int offset)
755 g_hash_table_insert (string_pool, n, GINT_TO_POINTER (offset));
759 write_string (FILE *cache, const gchar *n)
764 l = ALIGN_VALUE (strlen (n) + 1, 4);
769 i = fwrite (s, l, 1, cache);
778 write_card16 (FILE *cache, guint16 n)
782 n = GUINT16_TO_BE (n);
784 i = fwrite ((char *)&n, 2, 1, cache);
790 write_card32 (FILE *cache, guint32 n)
794 n = GUINT32_TO_BE (n);
796 i = fwrite ((char *)&n, 4, 1, cache);
803 write_image_data (FILE *cache, ImageData *image_data, int offset)
808 GdkPixdata *pixdata = &image_data->pixdata;
810 /* Type 0 is GdkPixdata */
811 if (!write_card32 (cache, 0))
814 s = gdk_pixdata_serialize (pixdata, &len);
816 if (!write_card32 (cache, len))
822 i = fwrite (s, len, 1, cache);
830 write_icon_data (FILE *cache, IconData *icon_data, int offset)
832 int ofs = offset + 12;
836 if (icon_data->has_embedded_rect)
838 if (!write_card32 (cache, ofs))
845 if (!write_card32 (cache, 0))
849 if (icon_data->n_attach_points > 0)
851 if (!write_card32 (cache, ofs))
854 ofs += 4 + 4 * icon_data->n_attach_points;
858 if (!write_card32 (cache, 0))
862 if (icon_data->n_display_names > 0)
864 if (!write_card32 (cache, ofs))
869 if (!write_card32 (cache, 0))
873 if (icon_data->has_embedded_rect)
875 if (!write_card16 (cache, icon_data->x0) ||
876 !write_card16 (cache, icon_data->y0) ||
877 !write_card16 (cache, icon_data->x1) ||
878 !write_card16 (cache, icon_data->y1))
882 if (icon_data->n_attach_points > 0)
884 if (!write_card32 (cache, icon_data->n_attach_points))
887 for (j = 0; j < 2 * icon_data->n_attach_points; j++)
889 if (!write_card16 (cache, icon_data->attach_points[j]))
894 if (icon_data->n_display_names > 0)
896 if (!write_card32 (cache, icon_data->n_display_names))
899 ofs += 4 + 8 * icon_data->n_display_names;
902 for (j = 0; j < 2 * icon_data->n_display_names; j++)
904 tmp2 = find_string (icon_data->display_names[j]);
905 if (tmp2 == 0 || tmp2 == -1)
908 tmp += ALIGN_VALUE (strlen (icon_data->display_names[j]) + 1, 4);
909 /* We're playing a little game with negative
910 * offsets here to handle duplicate strings in
913 add_string (icon_data->display_names[j], -tmp2);
920 if (!write_card32 (cache, tmp2))
925 g_assert (ofs == ftell (cache));
926 for (j = 0; j < 2 * icon_data->n_display_names; j++)
928 tmp2 = find_string (icon_data->display_names[j]);
929 g_assert (tmp2 != 0 && tmp2 != -1);
933 g_assert (tmp2 == ftell (cache));
934 add_string (icon_data->display_names[j], tmp2);
935 if (!write_string (cache, icon_data->display_names[j]))
945 write_header (FILE *cache, guint32 dir_list_offset)
947 return (write_card16 (cache, MAJOR_VERSION) &&
948 write_card16 (cache, MINOR_VERSION) &&
949 write_card32 (cache, HASH_OFFSET) &&
950 write_card32 (cache, dir_list_offset));
954 get_image_meta_data_size (Image *image)
958 /* The complication with storing the size in both
959 * IconData and Image is necessary since we attribute
960 * the size of the IconData only to the first Image
961 * using it (at which time it is written out in the
962 * cache). Later Images just refer to the written out
963 * IconData via the offset.
965 if (image->icon_data_size == 0)
967 if (image->icon_data && image->icon_data->size < 0)
969 IconData *data = image->icon_data;
973 if (data->has_embedded_rect ||
974 data->n_attach_points > 0 ||
975 data->n_display_names > 0)
978 if (data->has_embedded_rect)
981 if (data->n_attach_points > 0)
982 data->size += 4 + data->n_attach_points * 4;
984 if (data->n_display_names > 0)
986 data->size += 4 + 8 * data->n_display_names;
988 for (i = 0; data->display_names[i]; i++)
991 if ((poolv = find_string (data->display_names[i])) == 0)
993 data->size += ALIGN_VALUE (strlen (data->display_names[i]) + 1, 4);
994 /* Adding the string to the pool with -1
995 * to indicate that it hasn't been written out
996 * to the cache yet. We still need it in the
997 * pool in case the same string occurs twice
998 * during a get_single_node_size() calculation.
1000 add_string (data->display_names[i], -1);
1005 image->icon_data_size = data->size;
1010 g_assert (image->icon_data_size % 4 == 0);
1012 return image->icon_data_size;
1016 get_image_pixel_data_size (Image *image)
1018 /* The complication with storing the size in both
1019 * ImageData and Image is necessary since we attribute
1020 * the size of the ImageData only to the first Image
1021 * using it (at which time it is written out in the
1022 * cache). Later Images just refer to the written out
1023 * ImageData via the offset.
1025 if (image->pixel_data_size == 0)
1027 if (image->image_data &&
1028 image->image_data->has_pixdata)
1030 image->pixel_data_size = image->image_data->size;
1031 image->image_data->size = 0;
1035 g_assert (image->pixel_data_size % 4 == 0);
1037 return image->pixel_data_size;
1041 get_image_data_size (Image *image)
1047 len += get_image_pixel_data_size (image);
1048 len += get_image_meta_data_size (image);
1050 /* Even if len is zero, we need to reserve space to
1051 * write the ImageData, unless this is an .svg without
1052 * .icon, in which case both image_data and icon_data
1055 if (len > 0 || image->image_data || image->icon_data)
1062 get_single_node_size (HashNode *node, int *node_size, int *image_data_size)
1070 if (find_string (node->name) == 0)
1072 *node_size += ALIGN_VALUE (strlen (node->name) + 1, 4);
1073 add_string (node->name, -1);
1077 *node_size += 4 + g_list_length (node->image_list) * 8;
1080 *image_data_size = 0;
1081 for (list = node->image_list; list; list = list->next)
1083 Image *image = list->data;
1085 *image_data_size += get_image_data_size (image);
1090 write_bucket (FILE *cache, HashNode *node, int *offset)
1092 while (node != NULL)
1094 int node_size, image_data_size;
1095 int next_offset, image_data_offset;
1099 int image_list_offset;
1104 g_assert (*offset == ftell (cache));
1106 node->offset = *offset;
1108 get_single_node_size (node, &node_size, &image_data_size);
1109 g_assert (node_size % 4 == 0);
1110 g_assert (image_data_size % 4 == 0);
1111 image_data_offset = *offset + node_size;
1112 next_offset = *offset + node_size + image_data_size;
1114 if (node->next != NULL)
1116 if (!write_card32 (cache, next_offset))
1121 if (!write_card32 (cache, 0xffffffff))
1126 name_offset = find_string (node->name);
1127 if (name_offset <= 0)
1129 name_offset = *offset + 12;
1130 name_size = ALIGN_VALUE (strlen (node->name) + 1, 4);
1131 add_string (node->name, name_offset);
1133 if (!write_card32 (cache, name_offset))
1136 image_list_offset = *offset + 12 + name_size;
1137 if (!write_card32 (cache, image_list_offset))
1143 if (!write_string (cache, node->name))
1148 len = g_list_length (node->image_list);
1149 if (!write_card32 (cache, len))
1152 /* Image data goes right after the image list */
1153 tmp = image_list_offset + 4 + len * 8;
1155 list = node->image_list;
1156 data_offset = image_data_offset;
1157 for (i = 0; i < len; i++)
1159 Image *image = list->data;
1160 int image_data_size = get_image_data_size (image);
1162 /* Directory index */
1163 if (!write_card16 (cache, image->dir_index))
1167 if (!write_card16 (cache, image->flags))
1170 /* Image data offset */
1171 if (image_data_size > 0)
1173 if (!write_card32 (cache, data_offset))
1175 data_offset += image_data_size;
1179 if (!write_card32 (cache, 0))
1186 /* Now write the image data */
1187 list = node->image_list;
1188 for (i = 0; i < len; i++, list = list->next)
1190 Image *image = list->data;
1191 int pixel_data_size = get_image_pixel_data_size (image);
1192 int meta_data_size = get_image_meta_data_size (image);
1194 if (get_image_data_size (image) == 0)
1198 if (pixel_data_size > 0)
1200 image->image_data->offset = image_data_offset + 8;
1201 if (!write_card32 (cache, image->image_data->offset))
1206 if (!write_card32 (cache, (guint32) image->image_data ? image->image_data->offset : 0))
1210 if (meta_data_size > 0)
1212 image->icon_data->offset = image_data_offset + pixel_data_size + 8;
1213 if (!write_card32 (cache, image->icon_data->offset))
1218 if (!write_card32 (cache, image->icon_data ? image->icon_data->offset : 0))
1222 if (pixel_data_size > 0)
1224 if (!write_image_data (cache, image->image_data, image->image_data->offset))
1228 if (meta_data_size > 0)
1230 if (!write_icon_data (cache, image->icon_data, image->icon_data->offset))
1234 image_data_offset += pixel_data_size + meta_data_size + 8;
1237 *offset = next_offset;
1245 write_hash_table (FILE *cache, HashContext *context, int *new_offset)
1247 int offset = HASH_OFFSET;
1251 if (!(write_card32 (cache, context->size)))
1255 node_offset = offset + context->size * 4;
1256 /* Just write zeros here, we will rewrite this later */
1257 for (i = 0; i < context->size; i++)
1259 if (!write_card32 (cache, 0))
1263 /* Now write the buckets */
1264 for (i = 0; i < context->size; i++)
1266 if (!context->nodes[i])
1269 g_assert (node_offset % 4 == 0);
1270 if (!write_bucket (cache, context->nodes[i], &node_offset))
1274 *new_offset = node_offset;
1276 /* Now write out the bucket offsets */
1278 fseek (cache, offset, SEEK_SET);
1280 for (i = 0; i < context->size; i++)
1282 if (context->nodes[i] != NULL)
1283 node_offset = context->nodes[i]->offset;
1285 node_offset = 0xffffffff;
1286 if (!write_card32 (cache, node_offset))
1290 fseek (cache, 0, SEEK_END);
1296 write_dir_index (FILE *cache, int offset, GList *directories)
1303 n_dirs = g_list_length (directories);
1305 if (!write_card32 (cache, n_dirs))
1308 offset += 4 + n_dirs * 4;
1311 for (d = directories; d; d = d->next)
1315 tmp2 = find_string (dir);
1317 if (tmp2 == 0 || tmp2 == -1)
1320 tmp += ALIGN_VALUE (strlen (dir) + 1, 4);
1321 /* We're playing a little game with negative
1322 * offsets here to handle duplicate strings in
1323 * the array, even though that should not
1324 * really happen for the directory index.
1326 add_string (dir, -tmp2);
1333 if (!write_card32 (cache, tmp2))
1337 g_assert (offset == ftell (cache));
1338 for (d = directories; d; d = d->next)
1342 tmp2 = find_string (dir);
1343 g_assert (tmp2 != 0 && tmp2 != -1);
1347 g_assert (tmp2 == ftell (cache));
1348 add_string (dir, tmp2);
1349 if (!write_string (cache, dir))
1358 write_file (FILE *cache, GHashTable *files, GList *directories)
1360 HashContext context;
1363 /* Convert the hash table into something looking a bit more
1364 * like what we want to write to disk.
1366 context.size = g_spaced_primes_closest (g_hash_table_size (files) / 3);
1367 context.nodes = g_new0 (HashNode *, context.size);
1369 g_hash_table_foreach_remove (files, convert_to_hash, &context);
1371 /* Now write the file */
1372 /* We write 0 as the directory list offset and go
1373 * back and change it later */
1374 if (!write_header (cache, 0))
1376 g_printerr (_("Failed to write header\n"));
1380 if (!write_hash_table (cache, &context, &new_offset))
1382 g_printerr (_("Failed to write hash table\n"));
1386 if (!write_dir_index (cache, new_offset, directories))
1388 g_printerr (_("Failed to write folder index\n"));
1394 if (!write_header (cache, new_offset))
1396 g_printerr (_("Failed to rewrite header\n"));
1404 validate_file (const gchar *file)
1409 map = g_mapped_file_new (file, FALSE, NULL);
1413 info.cache = g_mapped_file_get_contents (map);
1414 info.cache_size = g_mapped_file_get_length (map);
1415 info.n_directories = 0;
1416 info.flags = CHECK_OFFSETS|CHECK_STRINGS|CHECK_PIXBUFS;
1418 if (!_gtk_icon_cache_validate (&info))
1420 g_mapped_file_free (map);
1424 g_mapped_file_free (map);
1430 build_cache (const gchar *path)
1432 gchar *cache_path, *tmp_cache_path;
1434 gchar *bak_cache_path = NULL;
1439 struct stat path_stat, cache_stat;
1440 struct utimbuf utime_buf;
1441 GList *directories = NULL;
1443 tmp_cache_path = g_build_filename (path, "."CACHE_NAME, NULL);
1444 cache = g_fopen (tmp_cache_path, "wb");
1448 g_printerr (_("Failed to write cache file: %s\n"), g_strerror (errno));
1452 files = g_hash_table_new (g_str_hash, g_str_equal);
1453 image_data_hash = g_hash_table_new (g_str_hash, g_str_equal);
1454 icon_data_hash = g_hash_table_new (g_str_hash, g_str_equal);
1455 string_pool = g_hash_table_new (g_str_hash, g_str_equal);
1457 directories = scan_directory (path, NULL, files, NULL, 0);
1459 if (g_hash_table_size (files) == 0)
1461 /* Empty table, just close and remove the file */
1464 g_unlink (tmp_cache_path);
1468 /* FIXME: Handle failure */
1469 retval = write_file (cache, files, directories);
1472 g_list_foreach (directories, (GFunc)g_free, NULL);
1473 g_list_free (directories);
1477 g_unlink (tmp_cache_path);
1481 if (!validate_file (tmp_cache_path))
1483 g_printerr (_("The generated cache was invalid.\n"));
1484 g_unlink (tmp_cache_path);
1488 cache_path = g_build_filename (path, CACHE_NAME, NULL);
1491 if (g_file_test (cache_path, G_FILE_TEST_EXISTS))
1493 bak_cache_path = g_strconcat (cache_path, ".bak", NULL);
1494 g_unlink (bak_cache_path);
1495 if (g_rename (cache_path, bak_cache_path) == -1)
1497 g_printerr (_("Could not rename %s to %s: %s, removing %s then.\n"),
1498 cache_path, bak_cache_path,
1501 g_unlink (cache_path);
1502 bak_cache_path = NULL;
1507 if (g_rename (tmp_cache_path, cache_path) == -1)
1509 g_printerr (_("Could not rename %s to %s: %s\n"),
1510 tmp_cache_path, cache_path,
1511 g_strerror (errno));
1512 g_unlink (tmp_cache_path);
1514 if (bak_cache_path != NULL)
1515 if (g_rename (bak_cache_path, cache_path) == -1)
1516 g_printerr (_("Could not rename %s back to %s: %s.\n"),
1517 bak_cache_path, cache_path,
1518 g_strerror (errno));
1523 if (bak_cache_path != NULL)
1524 g_unlink (bak_cache_path);
1528 /* FIXME: What do do if an error occurs here? */
1529 if (g_stat (path, &path_stat) < 0 ||
1530 g_stat (cache_path, &cache_stat))
1533 utime_buf.actime = path_stat.st_atime;
1534 utime_buf.modtime = cache_stat.st_mtime;
1535 utime (path, &utime_buf);
1538 g_printerr (_("Cache file created successfully.\n"));
1542 write_csource (const gchar *path)
1549 cache_path = g_build_filename (path, CACHE_NAME, NULL);
1550 if (!g_file_get_contents (cache_path, &data, &len, NULL))
1553 g_printf ("#ifdef __SUNPRO_C\n");
1554 g_printf ("#pragma align 4 (%s)\n", var_name);
1555 g_printf ("#endif\n");
1557 g_printf ("#ifdef __GNUC__\n");
1558 g_printf ("static const guint8 %s[] __attribute__ ((__aligned__ (4))) = \n", var_name);
1559 g_printf ("#else\n");
1560 g_printf ("static const guint8 %s[] = \n", var_name);
1561 g_printf ("#endif\n");
1564 for (i = 0; i < len - 1; i++)
1568 g_printf ("0x%02x, ", (guint8)data[i]);
1573 g_printf ("0x%02x\n};\n", (guint8)data[i]);
1576 static GOptionEntry args[] = {
1577 { "force", 'f', 0, G_OPTION_ARG_NONE, &force_update, N_("Overwrite an existing cache, even if up to date"), NULL },
1578 { "ignore-theme-index", 't', 0, G_OPTION_ARG_NONE, &ignore_theme_index, N_("Don't check for the existence of index.theme"), NULL },
1579 { "index-only", 'i', 0, G_OPTION_ARG_NONE, &index_only, N_("Don't include image data in the cache"), NULL },
1580 { "source", 'c', 0, G_OPTION_ARG_STRING, &var_name, N_("Output a C header file"), "NAME" },
1581 { "quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet, N_("Turn off verbose output"), NULL },
1582 { "validate", 'v', 0, G_OPTION_ARG_NONE, &validate, N_("Validate existing icon cache"), NULL },
1587 main (int argc, char **argv)
1590 GOptionContext *context;
1595 setlocale (LC_ALL, "");
1597 bindtextdomain (GETTEXT_PACKAGE, GTK_LOCALEDIR);
1598 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
1600 context = g_option_context_new ("ICONPATH");
1601 g_option_context_add_main_entries (context, args, GETTEXT_PACKAGE);
1603 g_option_context_parse (context, &argc, &argv, NULL);
1607 path = g_locale_to_utf8 (path, -1, NULL, NULL, NULL);
1612 gchar *file = g_build_filename (path, CACHE_NAME, NULL);
1614 if (!g_file_test (file, G_FILE_TEST_IS_REGULAR))
1617 g_printerr (_("File not found: %s\n"), file);
1620 if (!validate_file (file))
1623 g_printerr (_("Not a valid icon cache: %s\n"), file);
1632 if (!ignore_theme_index && !has_theme_index (path))
1634 g_printerr (_("No theme index file in '%s'.\n"
1635 "If you really want to create an icon cache here, use --ignore-theme-index.\n"), path);
1639 if (!force_update && is_cache_up_to_date (path))
1645 if (strcmp (var_name, "-") != 0)
1646 write_csource (path);