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>
35 #include <sys/utime.h>
41 #include <glib/gstdio.h>
42 #include <gdk-pixbuf/gdk-pixdata.h>
43 #include <glib/gi18n.h>
44 #include "gtkiconcachevalidator.h"
46 static gboolean force_update = FALSE;
47 static gboolean ignore_theme_index = FALSE;
48 static gboolean quiet = FALSE;
49 static gboolean index_only = FALSE;
50 static gboolean validate = FALSE;
51 static gchar *var_name = "-";
53 /* Quite ugly - if we just add the c file to the
54 * list of sources in Makefile.am, libtool complains.
56 #include "gtkiconcachevalidator.c"
58 #define CACHE_NAME "icon-theme.cache"
60 #define HAS_SUFFIX_XPM (1 << 0)
61 #define HAS_SUFFIX_SVG (1 << 1)
62 #define HAS_SUFFIX_PNG (1 << 2)
63 #define HAS_ICON_FILE (1 << 3)
65 #define MAJOR_VERSION 1
66 #define MINOR_VERSION 0
67 #define HASH_OFFSET 12
69 #define ALIGN_VALUE(this, boundary) \
70 (( ((unsigned long)(this)) + (((unsigned long)(boundary)) -1)) & (~(((unsigned long)(boundary))-1)))
76 static struct stat cache_stat;
77 static gboolean cache_up_to_date;
79 static int check_dir_mtime (const char *dir,
80 const struct stat *sb,
83 if (tf != FTW_NS && sb->st_mtime > cache_stat.st_mtime)
85 cache_up_to_date = FALSE;
94 is_cache_up_to_date (const gchar *path)
99 cache_path = g_build_filename (path, CACHE_NAME, NULL);
100 retval = g_stat (cache_path, &cache_stat);
105 /* Cache file not found */
109 cache_up_to_date = TRUE;
111 ftw (path, check_dir_mtime, 20);
113 return cache_up_to_date;
116 #else /* !HAVE_FTW_H */
119 is_cache_up_to_date (const gchar *path)
121 struct stat path_stat, cache_stat;
125 retval = g_stat (path, &path_stat);
129 /* We can't stat the path,
130 * assume we have a updated cache */
134 cache_path = g_build_filename (path, CACHE_NAME, NULL);
135 retval = g_stat (cache_path, &cache_stat);
140 /* Cache file not found */
145 return cache_stat.st_mtime >= path_stat.st_mtime;
148 #endif /* !HAVE_FTW_H */
151 has_theme_index (const gchar *path)
156 index_path = g_build_filename (path, "index.theme", NULL);
158 result = g_file_test (index_path, G_FILE_TEST_IS_REGULAR);
169 gboolean has_pixdata;
176 int has_embedded_rect;
183 char **display_names;
189 static GHashTable *image_data_hash = NULL;
190 static GHashTable *icon_data_hash = NULL;
197 ImageData *image_data;
198 guint pixel_data_size;
201 guint icon_data_size;
206 foreach_remove_func (gpointer key, gpointer value, gpointer user_data)
208 Image *image = (Image *)value;
209 GHashTable *files = user_data;
211 gboolean free_key = FALSE;
213 if (image->flags == HAS_ICON_FILE)
215 /* just a .icon file, throw away */
222 list = g_hash_table_lookup (files, key);
226 list = g_list_prepend (list, value);
227 g_hash_table_insert (files, key, list);
236 load_icon_data (const char *path)
245 GError *error = NULL;
250 icon_file = g_key_file_new ();
251 g_key_file_set_list_separator (icon_file, ',');
252 g_key_file_load_from_file (icon_file, path, G_KEY_FILE_KEEP_TRANSLATIONS, &error);
255 g_error_free (error);
256 g_key_file_free (icon_file);
261 data = g_new0 (IconData, 1);
263 ivalues = g_key_file_get_integer_list (icon_file,
264 "Icon Data", "EmbeddedTextRectangle",
270 data->has_embedded_rect = TRUE;
271 data->x0 = ivalues[0];
272 data->y0 = ivalues[1];
273 data->x1 = ivalues[2];
274 data->y1 = ivalues[3];
280 str = g_key_file_get_string (icon_file, "Icon Data", "AttachPoints", NULL);
283 split = g_strsplit (str, "|", -1);
285 data->n_attach_points = g_strv_length (split);
286 data->attach_points = g_new (int, 2 * data->n_attach_points);
289 while (split[i] != NULL && i < data->n_attach_points)
291 split_point = strchr (split[i], ',');
296 data->attach_points[2 * i] = atoi (split[i]);
297 data->attach_points[2 * i + 1] = atoi (split_point);
306 keys = g_key_file_get_keys (icon_file, "Icon Data", &n_keys, &error);
307 data->display_names = g_new0 (gchar *, 2 * n_keys + 1);
308 data->n_display_names = 0;
310 for (i = 0; i < n_keys; i++)
314 if (g_str_has_prefix (keys[i], "DisplayName"))
316 gchar *open, *close = NULL;
318 open = strchr (keys[i], '[');
321 close = strchr (open, ']');
325 lang = g_strndup (open + 1, close - open - 1);
326 name = g_key_file_get_locale_string (icon_file,
327 "Icon Data", "DisplayName",
332 lang = g_strdup ("C");
333 name = g_key_file_get_string (icon_file,
334 "Icon Data", "DisplayName",
338 data->display_names[2 * data->n_display_names] = lang;
339 data->display_names[2 * data->n_display_names + 1] = name;
340 data->n_display_names++;
346 g_key_file_free (icon_file);
348 /* -1 means not computed yet, the real value depends
349 * on string pool state, and will be computed
358 * This function was copied from gtkfilesystemunix.c, it should
359 * probably go to GLib
362 canonicalize_filename (gchar *filename)
365 gboolean last_was_slash = FALSE;
372 if (*p == G_DIR_SEPARATOR)
375 *q++ = G_DIR_SEPARATOR;
377 last_was_slash = TRUE;
381 if (last_was_slash && *p == '.')
383 if (*(p + 1) == G_DIR_SEPARATOR ||
386 if (*(p + 1) == '\0')
391 else if (*(p + 1) == '.' &&
392 (*(p + 2) == G_DIR_SEPARATOR ||
395 if (q > filename + 1)
398 while (q > filename + 1 &&
399 *(q - 1) != G_DIR_SEPARATOR)
403 if (*(p + 2) == '\0')
411 last_was_slash = FALSE;
417 last_was_slash = FALSE;
424 if (q > filename + 1 && *(q - 1) == G_DIR_SEPARATOR)
431 follow_links (const gchar *path)
437 path2 = g_strdup (path);
438 while (g_file_test (path2, G_FILE_TEST_IS_SYMLINK))
440 target = g_file_read_link (path2, NULL);
444 if (g_path_is_absolute (target))
448 d = g_path_get_dirname (path2);
449 s = g_build_filename (d, target, NULL);
460 if (strcmp (path, path2) == 0)
470 maybe_cache_image_data (Image *image,
473 if (!index_only && !image->image_data &&
474 (g_str_has_suffix (path, ".png") || g_str_has_suffix (path, ".xpm")))
480 idata = g_hash_table_lookup (image_data_hash, path);
481 path2 = follow_links (path);
487 canonicalize_filename (path2);
489 idata2 = g_hash_table_lookup (image_data_hash, path2);
491 if (idata && idata2 && idata != idata2)
492 g_error (_("different idatas found for symlinked '%s' and '%s'\n"),
495 if (idata && !idata2)
496 g_hash_table_insert (image_data_hash, g_strdup (path2), idata);
498 if (!idata && idata2)
500 g_hash_table_insert (image_data_hash, g_strdup (path), idata2);
507 idata = g_new0 (ImageData, 1);
508 g_hash_table_insert (image_data_hash, g_strdup (path), idata);
510 g_hash_table_insert (image_data_hash, g_strdup (path2), idata);
513 if (!idata->has_pixdata)
515 pixbuf = gdk_pixbuf_new_from_file (path, NULL);
519 gdk_pixdata_from_pixbuf (&idata->pixdata, pixbuf, FALSE);
520 idata->size = idata->pixdata.length + 8;
521 idata->has_pixdata = TRUE;
525 image->image_data = idata;
532 maybe_cache_icon_data (Image *image,
535 if (g_str_has_suffix (path, ".icon"))
537 IconData *idata = NULL;
540 idata = g_hash_table_lookup (icon_data_hash, path);
541 path2 = follow_links (path);
547 canonicalize_filename (path2);
549 idata2 = g_hash_table_lookup (icon_data_hash, path2);
551 if (idata && idata2 && idata != idata2)
552 g_error (_("different idatas found for symlinked '%s' and '%s'\n"),
555 if (idata && !idata2)
556 g_hash_table_insert (icon_data_hash, g_strdup (path2), idata);
558 if (!idata && idata2)
560 g_hash_table_insert (icon_data_hash, g_strdup (path), idata2);
567 idata = load_icon_data (path);
568 g_hash_table_insert (icon_data_hash, g_strdup (path), idata);
570 g_hash_table_insert (icon_data_hash, g_strdup (path2), idata);
573 image->icon_data = idata;
580 scan_directory (const gchar *base_path,
586 GHashTable *dir_hash;
590 gboolean dir_added = FALSE;
591 guint dir_index = 0xffff;
593 dir_path = g_build_filename (base_path, subdir, NULL);
595 /* FIXME: Use the gerror */
596 dir = g_dir_open (dir_path, 0, NULL);
601 dir_hash = g_hash_table_new (g_str_hash, g_str_equal);
603 while ((name = g_dir_read_name (dir)))
609 gchar *basename, *dot;
611 path = g_build_filename (dir_path, name, NULL);
612 retval = g_file_test (path, G_FILE_TEST_IS_DIR);
618 subsubdir = g_build_filename (subdir, name, NULL);
620 subsubdir = g_strdup (name);
621 directories = scan_directory (base_path, subsubdir, files,
622 directories, depth + 1);
628 /* ignore images in the toplevel directory */
632 retval = g_file_test (path, G_FILE_TEST_IS_REGULAR);
635 if (g_str_has_suffix (name, ".png"))
636 flags |= HAS_SUFFIX_PNG;
637 else if (g_str_has_suffix (name, ".svg"))
638 flags |= HAS_SUFFIX_SVG;
639 else if (g_str_has_suffix (name, ".xpm"))
640 flags |= HAS_SUFFIX_XPM;
641 else if (g_str_has_suffix (name, ".icon"))
642 flags |= HAS_ICON_FILE;
647 basename = g_strdup (name);
648 dot = strrchr (basename, '.');
651 image = g_hash_table_lookup (dir_hash, basename);
659 dir_index = g_list_length (directories);
660 directories = g_list_append (directories, g_strdup (subdir));
666 image = g_new0 (Image, 1);
667 image->dir_index = dir_index;
668 g_hash_table_insert (dir_hash, g_strdup (basename), image);
671 image->flags |= flags;
673 maybe_cache_image_data (image, path);
674 maybe_cache_icon_data (image, path);
684 /* Move dir into the big file hash */
685 g_hash_table_foreach_remove (dir_hash, foreach_remove_func, files);
687 g_hash_table_destroy (dir_hash);
692 typedef struct _HashNode HashNode;
703 icon_name_hash (gconstpointer key)
705 const signed char *p = key;
709 for (p += 1; *p != '\0'; p++)
710 h = (h << 5) - h + *p;
721 convert_to_hash (gpointer key, gpointer value, gpointer user_data)
723 HashContext *context = user_data;
727 hash = icon_name_hash (key) % context->size;
729 node = g_new0 (HashNode, 1);
732 node->image_list = value;
734 if (context->nodes[hash] != NULL)
735 node->next = context->nodes[hash];
737 context->nodes[hash] = node;
742 static GHashTable *string_pool = NULL;
745 find_string (const gchar *n)
747 return GPOINTER_TO_INT (g_hash_table_lookup (string_pool, n));
751 add_string (const gchar *n, int offset)
753 g_hash_table_insert (string_pool, (gpointer) n, GINT_TO_POINTER (offset));
757 write_string (FILE *cache, const gchar *n)
762 l = ALIGN_VALUE (strlen (n) + 1, 4);
767 i = fwrite (s, l, 1, cache);
776 write_card16 (FILE *cache, guint16 n)
780 n = GUINT16_TO_BE (n);
782 i = fwrite ((char *)&n, 2, 1, cache);
788 write_card32 (FILE *cache, guint32 n)
792 n = GUINT32_TO_BE (n);
794 i = fwrite ((char *)&n, 4, 1, cache);
801 write_image_data (FILE *cache, ImageData *image_data, int offset)
806 GdkPixdata *pixdata = &image_data->pixdata;
808 /* Type 0 is GdkPixdata */
809 if (!write_card32 (cache, 0))
812 s = gdk_pixdata_serialize (pixdata, &len);
814 if (!write_card32 (cache, len))
820 i = fwrite (s, len, 1, cache);
828 write_icon_data (FILE *cache, IconData *icon_data, int offset)
830 int ofs = offset + 12;
834 if (icon_data->has_embedded_rect)
836 if (!write_card32 (cache, ofs))
843 if (!write_card32 (cache, 0))
847 if (icon_data->n_attach_points > 0)
849 if (!write_card32 (cache, ofs))
852 ofs += 4 + 4 * icon_data->n_attach_points;
856 if (!write_card32 (cache, 0))
860 if (icon_data->n_display_names > 0)
862 if (!write_card32 (cache, ofs))
867 if (!write_card32 (cache, 0))
871 if (icon_data->has_embedded_rect)
873 if (!write_card16 (cache, icon_data->x0) ||
874 !write_card16 (cache, icon_data->y0) ||
875 !write_card16 (cache, icon_data->x1) ||
876 !write_card16 (cache, icon_data->y1))
880 if (icon_data->n_attach_points > 0)
882 if (!write_card32 (cache, icon_data->n_attach_points))
885 for (j = 0; j < 2 * icon_data->n_attach_points; j++)
887 if (!write_card16 (cache, icon_data->attach_points[j]))
892 if (icon_data->n_display_names > 0)
894 if (!write_card32 (cache, icon_data->n_display_names))
897 ofs += 4 + 8 * icon_data->n_display_names;
900 for (j = 0; j < 2 * icon_data->n_display_names; j++)
902 tmp2 = find_string (icon_data->display_names[j]);
903 if (tmp2 == 0 || tmp2 == -1)
906 tmp += ALIGN_VALUE (strlen (icon_data->display_names[j]) + 1, 4);
907 /* We're playing a little game with negative
908 * offsets here to handle duplicate strings in
911 add_string (icon_data->display_names[j], -tmp2);
918 if (!write_card32 (cache, tmp2))
923 g_assert (ofs == ftell (cache));
924 for (j = 0; j < 2 * icon_data->n_display_names; j++)
926 tmp2 = find_string (icon_data->display_names[j]);
927 g_assert (tmp2 != 0 && tmp2 != -1);
931 g_assert (tmp2 == ftell (cache));
932 add_string (icon_data->display_names[j], tmp2);
933 if (!write_string (cache, icon_data->display_names[j]))
943 write_header (FILE *cache, guint32 dir_list_offset)
945 return (write_card16 (cache, MAJOR_VERSION) &&
946 write_card16 (cache, MINOR_VERSION) &&
947 write_card32 (cache, HASH_OFFSET) &&
948 write_card32 (cache, dir_list_offset));
952 get_image_meta_data_size (Image *image)
956 /* The complication with storing the size in both
957 * IconData and Image is necessary since we attribute
958 * the size of the IconData only to the first Image
959 * using it (at which time it is written out in the
960 * cache). Later Images just refer to the written out
961 * IconData via the offset.
963 if (image->icon_data_size == 0)
965 if (image->icon_data && image->icon_data->size < 0)
967 IconData *data = image->icon_data;
971 if (data->has_embedded_rect ||
972 data->n_attach_points > 0 ||
973 data->n_display_names > 0)
976 if (data->has_embedded_rect)
979 if (data->n_attach_points > 0)
980 data->size += 4 + data->n_attach_points * 4;
982 if (data->n_display_names > 0)
984 data->size += 4 + 8 * data->n_display_names;
986 for (i = 0; data->display_names[i]; i++)
989 if ((poolv = find_string (data->display_names[i])) == 0)
991 data->size += ALIGN_VALUE (strlen (data->display_names[i]) + 1, 4);
992 /* Adding the string to the pool with -1
993 * to indicate that it hasn't been written out
994 * to the cache yet. We still need it in the
995 * pool in case the same string occurs twice
996 * during a get_single_node_size() calculation.
998 add_string (data->display_names[i], -1);
1003 image->icon_data_size = data->size;
1008 g_assert (image->icon_data_size % 4 == 0);
1010 return image->icon_data_size;
1014 get_image_pixel_data_size (Image *image)
1016 /* The complication with storing the size in both
1017 * ImageData and Image is necessary since we attribute
1018 * the size of the ImageData only to the first Image
1019 * using it (at which time it is written out in the
1020 * cache). Later Images just refer to the written out
1021 * ImageData via the offset.
1023 if (image->pixel_data_size == 0)
1025 if (image->image_data &&
1026 image->image_data->has_pixdata)
1028 image->pixel_data_size = image->image_data->size;
1029 image->image_data->size = 0;
1033 g_assert (image->pixel_data_size % 4 == 0);
1035 return image->pixel_data_size;
1039 get_image_data_size (Image *image)
1045 len += get_image_pixel_data_size (image);
1046 len += get_image_meta_data_size (image);
1048 /* Even if len is zero, we need to reserve space to
1049 * write the ImageData, unless this is an .svg without
1050 * .icon, in which case both image_data and icon_data
1053 if (len > 0 || image->image_data || image->icon_data)
1060 get_single_node_size (HashNode *node, int *node_size, int *image_data_size)
1068 if (find_string (node->name) == 0)
1070 *node_size += ALIGN_VALUE (strlen (node->name) + 1, 4);
1071 add_string (node->name, -1);
1075 *node_size += 4 + g_list_length (node->image_list) * 8;
1078 *image_data_size = 0;
1079 for (list = node->image_list; list; list = list->next)
1081 Image *image = list->data;
1083 *image_data_size += get_image_data_size (image);
1088 write_bucket (FILE *cache, HashNode *node, int *offset)
1090 while (node != NULL)
1092 int node_size, image_data_size;
1093 int next_offset, image_data_offset;
1097 int image_list_offset;
1101 g_assert (*offset == ftell (cache));
1103 node->offset = *offset;
1105 get_single_node_size (node, &node_size, &image_data_size);
1106 g_assert (node_size % 4 == 0);
1107 g_assert (image_data_size % 4 == 0);
1108 image_data_offset = *offset + node_size;
1109 next_offset = *offset + node_size + image_data_size;
1111 if (node->next != NULL)
1113 if (!write_card32 (cache, next_offset))
1118 if (!write_card32 (cache, 0xffffffff))
1123 name_offset = find_string (node->name);
1124 if (name_offset <= 0)
1126 name_offset = *offset + 12;
1127 name_size = ALIGN_VALUE (strlen (node->name) + 1, 4);
1128 add_string (node->name, name_offset);
1130 if (!write_card32 (cache, name_offset))
1133 image_list_offset = *offset + 12 + name_size;
1134 if (!write_card32 (cache, image_list_offset))
1140 if (!write_string (cache, node->name))
1145 len = g_list_length (node->image_list);
1146 if (!write_card32 (cache, len))
1149 list = node->image_list;
1150 data_offset = image_data_offset;
1151 for (i = 0; i < len; i++)
1153 Image *image = list->data;
1154 int image_data_size = get_image_data_size (image);
1156 /* Directory index */
1157 if (!write_card16 (cache, image->dir_index))
1161 if (!write_card16 (cache, image->flags))
1164 /* Image data offset */
1165 if (image_data_size > 0)
1167 if (!write_card32 (cache, data_offset))
1169 data_offset += image_data_size;
1173 if (!write_card32 (cache, 0))
1180 /* Now write the image data */
1181 list = node->image_list;
1182 for (i = 0; i < len; i++, list = list->next)
1184 Image *image = list->data;
1185 int pixel_data_size = get_image_pixel_data_size (image);
1186 int meta_data_size = get_image_meta_data_size (image);
1188 if (get_image_data_size (image) == 0)
1192 if (pixel_data_size > 0)
1194 image->image_data->offset = image_data_offset + 8;
1195 if (!write_card32 (cache, image->image_data->offset))
1200 if (!write_card32 (cache, (guint32) (image->image_data ? image->image_data->offset : 0)))
1204 if (meta_data_size > 0)
1206 image->icon_data->offset = image_data_offset + pixel_data_size + 8;
1207 if (!write_card32 (cache, image->icon_data->offset))
1212 if (!write_card32 (cache, image->icon_data ? image->icon_data->offset : 0))
1216 if (pixel_data_size > 0)
1218 if (!write_image_data (cache, image->image_data, image->image_data->offset))
1222 if (meta_data_size > 0)
1224 if (!write_icon_data (cache, image->icon_data, image->icon_data->offset))
1228 image_data_offset += pixel_data_size + meta_data_size + 8;
1231 *offset = next_offset;
1239 write_hash_table (FILE *cache, HashContext *context, int *new_offset)
1241 int offset = HASH_OFFSET;
1245 if (!(write_card32 (cache, context->size)))
1249 node_offset = offset + context->size * 4;
1250 /* Just write zeros here, we will rewrite this later */
1251 for (i = 0; i < context->size; i++)
1253 if (!write_card32 (cache, 0))
1257 /* Now write the buckets */
1258 for (i = 0; i < context->size; i++)
1260 if (!context->nodes[i])
1263 g_assert (node_offset % 4 == 0);
1264 if (!write_bucket (cache, context->nodes[i], &node_offset))
1268 *new_offset = node_offset;
1270 /* Now write out the bucket offsets */
1272 fseek (cache, offset, SEEK_SET);
1274 for (i = 0; i < context->size; i++)
1276 if (context->nodes[i] != NULL)
1277 node_offset = context->nodes[i]->offset;
1279 node_offset = 0xffffffff;
1280 if (!write_card32 (cache, node_offset))
1284 fseek (cache, 0, SEEK_END);
1290 write_dir_index (FILE *cache, int offset, GList *directories)
1297 n_dirs = g_list_length (directories);
1299 if (!write_card32 (cache, n_dirs))
1302 offset += 4 + n_dirs * 4;
1305 for (d = directories; d; d = d->next)
1309 tmp2 = find_string (dir);
1311 if (tmp2 == 0 || tmp2 == -1)
1314 tmp += ALIGN_VALUE (strlen (dir) + 1, 4);
1315 /* We're playing a little game with negative
1316 * offsets here to handle duplicate strings in
1317 * the array, even though that should not
1318 * really happen for the directory index.
1320 add_string (dir, -tmp2);
1327 if (!write_card32 (cache, tmp2))
1331 g_assert (offset == ftell (cache));
1332 for (d = directories; d; d = d->next)
1336 tmp2 = find_string (dir);
1337 g_assert (tmp2 != 0 && tmp2 != -1);
1341 g_assert (tmp2 == ftell (cache));
1342 add_string (dir, tmp2);
1343 if (!write_string (cache, dir))
1352 write_file (FILE *cache, GHashTable *files, GList *directories)
1354 HashContext context;
1357 /* Convert the hash table into something looking a bit more
1358 * like what we want to write to disk.
1360 context.size = g_spaced_primes_closest (g_hash_table_size (files) / 3);
1361 context.nodes = g_new0 (HashNode *, context.size);
1363 g_hash_table_foreach_remove (files, convert_to_hash, &context);
1365 /* Now write the file */
1366 /* We write 0 as the directory list offset and go
1367 * back and change it later */
1368 if (!write_header (cache, 0))
1370 g_printerr (_("Failed to write header\n"));
1374 if (!write_hash_table (cache, &context, &new_offset))
1376 g_printerr (_("Failed to write hash table\n"));
1380 if (!write_dir_index (cache, new_offset, directories))
1382 g_printerr (_("Failed to write folder index\n"));
1388 if (!write_header (cache, new_offset))
1390 g_printerr (_("Failed to rewrite header\n"));
1398 validate_file (const gchar *file)
1403 map = g_mapped_file_new (file, FALSE, NULL);
1407 info.cache = g_mapped_file_get_contents (map);
1408 info.cache_size = g_mapped_file_get_length (map);
1409 info.n_directories = 0;
1410 info.flags = CHECK_OFFSETS|CHECK_STRINGS|CHECK_PIXBUFS;
1412 if (!_gtk_icon_cache_validate (&info))
1414 g_mapped_file_unref (map);
1418 g_mapped_file_unref (map);
1425 * @f: A FILE* stream, must have underlying fd
1427 * Unix defaults for data preservation after system crash
1428 * are unspecified, and many systems will eat your data
1429 * in this situation unless you explicitly fsync().
1431 * Returns: %TRUE on success, %FALSE on failure, and will set errno()
1434 safe_fclose (FILE *f)
1436 int fd = fileno (f);
1438 if (fflush (f) == EOF)
1444 if (fclose (f) == EOF)
1450 build_cache (const gchar *path)
1452 gchar *cache_path, *tmp_cache_path;
1454 gchar *bak_cache_path = NULL;
1458 struct stat path_stat, cache_stat;
1459 struct utimbuf utime_buf;
1460 GList *directories = NULL;
1462 int retry_count = 0;
1464 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
1466 int mode = _S_IWRITE | _S_IREAD;
1472 tmp_cache_path = g_build_filename (path, "."CACHE_NAME, NULL);
1473 cache_path = g_build_filename (path, CACHE_NAME, NULL);
1476 if ((fd = g_open (tmp_cache_path, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC | _O_BINARY, mode)) == -1)
1478 if (force_update && retry_count == 0)
1481 g_remove (tmp_cache_path);
1484 g_printerr (_("Failed to open file %s : %s\n"), tmp_cache_path, g_strerror (errno));
1488 cache = fdopen (fd, "wb");
1492 g_printerr (_("Failed to write cache file: %s\n"), g_strerror (errno));
1496 files = g_hash_table_new (g_str_hash, g_str_equal);
1497 image_data_hash = g_hash_table_new (g_str_hash, g_str_equal);
1498 icon_data_hash = g_hash_table_new (g_str_hash, g_str_equal);
1499 string_pool = g_hash_table_new (g_str_hash, g_str_equal);
1501 directories = scan_directory (path, NULL, files, NULL, 0);
1503 if (g_hash_table_size (files) == 0)
1505 /* Empty table, just close and remove the file */
1508 g_unlink (tmp_cache_path);
1509 g_unlink (cache_path);
1513 /* FIXME: Handle failure */
1514 if (!write_file (cache, files, directories))
1516 g_unlink (tmp_cache_path);
1520 if (!safe_fclose (cache))
1522 g_printerr (_("Failed to write cache file: %s\n"), g_strerror (errno));
1523 g_unlink (tmp_cache_path);
1528 g_list_foreach (directories, (GFunc)g_free, NULL);
1529 g_list_free (directories);
1531 if (!validate_file (tmp_cache_path))
1533 g_printerr (_("The generated cache was invalid.\n"));
1534 /*g_unlink (tmp_cache_path);*/
1539 if (g_file_test (cache_path, G_FILE_TEST_EXISTS))
1541 bak_cache_path = g_strconcat (cache_path, ".bak", NULL);
1542 g_unlink (bak_cache_path);
1543 if (g_rename (cache_path, bak_cache_path) == -1)
1547 g_printerr (_("Could not rename %s to %s: %s, removing %s then.\n"),
1548 cache_path, bak_cache_path,
1551 g_unlink (cache_path);
1552 bak_cache_path = NULL;
1557 if (g_rename (tmp_cache_path, cache_path) == -1)
1561 g_printerr (_("Could not rename %s to %s: %s\n"),
1562 tmp_cache_path, cache_path,
1563 g_strerror (errsv));
1564 g_unlink (tmp_cache_path);
1566 if (bak_cache_path != NULL)
1567 if (g_rename (bak_cache_path, cache_path) == -1)
1571 g_printerr (_("Could not rename %s back to %s: %s.\n"),
1572 bak_cache_path, cache_path,
1573 g_strerror (errsv));
1579 if (bak_cache_path != NULL)
1580 g_unlink (bak_cache_path);
1584 /* FIXME: What do do if an error occurs here? */
1585 if (g_stat (path, &path_stat) < 0 ||
1586 g_stat (cache_path, &cache_stat))
1589 utime_buf.actime = path_stat.st_atime;
1590 utime_buf.modtime = cache_stat.st_mtime;
1591 #if GLIB_CHECK_VERSION (2, 17, 1)
1592 g_utime (path, &utime_buf);
1594 utime (path, &utime_buf);
1598 g_printerr (_("Cache file created successfully.\n"));
1602 write_csource (const gchar *path)
1609 cache_path = g_build_filename (path, CACHE_NAME, NULL);
1610 if (!g_file_get_contents (cache_path, &data, &len, NULL))
1613 g_printf ("#ifdef __SUNPRO_C\n");
1614 g_printf ("#pragma align 4 (%s)\n", var_name);
1615 g_printf ("#endif\n");
1617 g_printf ("#ifdef __GNUC__\n");
1618 g_printf ("static const guint8 %s[] __attribute__ ((__aligned__ (4))) = \n", var_name);
1619 g_printf ("#else\n");
1620 g_printf ("static const guint8 %s[] = \n", var_name);
1621 g_printf ("#endif\n");
1624 for (i = 0; i < len - 1; i++)
1628 g_printf ("0x%02x, ", (guint8)data[i]);
1633 g_printf ("0x%02x\n};\n", (guint8)data[i]);
1636 static GOptionEntry args[] = {
1637 { "force", 'f', 0, G_OPTION_ARG_NONE, &force_update, N_("Overwrite an existing cache, even if up to date"), NULL },
1638 { "ignore-theme-index", 't', 0, G_OPTION_ARG_NONE, &ignore_theme_index, N_("Don't check for the existence of index.theme"), NULL },
1639 { "index-only", 'i', 0, G_OPTION_ARG_NONE, &index_only, N_("Don't include image data in the cache"), NULL },
1640 { "source", 'c', 0, G_OPTION_ARG_STRING, &var_name, N_("Output a C header file"), "NAME" },
1641 { "quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet, N_("Turn off verbose output"), NULL },
1642 { "validate", 'v', 0, G_OPTION_ARG_NONE, &validate, N_("Validate existing icon cache"), NULL },
1647 printerr_handler (const gchar *string)
1649 const gchar *charset;
1651 fputs (g_get_prgname (), stderr);
1652 fputs (": ", stderr);
1653 if (g_get_charset (&charset))
1654 fputs (string, stderr); /* charset is UTF-8 already */
1659 result = g_convert_with_fallback (string, -1, charset, "UTF-8", "?", NULL, NULL, NULL);
1663 fputs (result, stderr);
1673 main (int argc, char **argv)
1676 GOptionContext *context;
1681 g_set_printerr_handler (printerr_handler);
1683 setlocale (LC_ALL, "");
1686 bindtextdomain (GETTEXT_PACKAGE, GTK_LOCALEDIR);
1687 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
1688 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
1692 context = g_option_context_new ("ICONPATH");
1693 g_option_context_add_main_entries (context, args, GETTEXT_PACKAGE);
1695 g_option_context_parse (context, &argc, &argv, NULL);
1699 path = g_locale_to_utf8 (path, -1, NULL, NULL, NULL);
1704 gchar *file = g_build_filename (path, CACHE_NAME, NULL);
1706 if (!g_file_test (file, G_FILE_TEST_IS_REGULAR))
1709 g_printerr (_("File not found: %s\n"), file);
1712 if (!validate_file (file))
1715 g_printerr (_("Not a valid icon cache: %s\n"), file);
1724 if (!ignore_theme_index && !has_theme_index (path))
1728 g_printerr (_("No theme index file.\n"));
1732 g_printerr (_("No theme index file in '%s'.\n"
1733 "If you really want to create an icon cache here, use --ignore-theme-index.\n"), path);
1739 if (!force_update && is_cache_up_to_date (path))
1745 if (strcmp (var_name, "-") != 0)
1746 write_csource (path);