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, see <http://www.gnu.org/licenses/>.
24 #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 GStatBuf cache_stat;
75 static gboolean cache_up_to_date;
77 static int check_dir_mtime (const char *dir,
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 GStatBuf 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;
204 foreach_remove_func (gpointer key, gpointer value, gpointer user_data)
206 Image *image = (Image *)value;
207 GHashTable *files = user_data;
209 gboolean free_key = FALSE;
211 if (image->flags == HAS_ICON_FILE)
213 /* just a .icon file, throw away */
220 list = g_hash_table_lookup (files, key);
224 list = g_list_prepend (list, value);
225 g_hash_table_insert (files, key, list);
234 load_icon_data (const char *path)
243 GError *error = NULL;
248 icon_file = g_key_file_new ();
249 g_key_file_set_list_separator (icon_file, ',');
250 g_key_file_load_from_file (icon_file, path, G_KEY_FILE_KEEP_TRANSLATIONS, &error);
253 g_error_free (error);
254 g_key_file_free (icon_file);
259 data = g_new0 (IconData, 1);
261 ivalues = g_key_file_get_integer_list (icon_file,
262 "Icon Data", "EmbeddedTextRectangle",
268 data->has_embedded_rect = TRUE;
269 data->x0 = ivalues[0];
270 data->y0 = ivalues[1];
271 data->x1 = ivalues[2];
272 data->y1 = ivalues[3];
278 str = g_key_file_get_string (icon_file, "Icon Data", "AttachPoints", NULL);
281 split = g_strsplit (str, "|", -1);
283 data->n_attach_points = g_strv_length (split);
284 data->attach_points = g_new (int, 2 * data->n_attach_points);
287 while (split[i] != NULL && i < data->n_attach_points)
289 split_point = strchr (split[i], ',');
294 data->attach_points[2 * i] = atoi (split[i]);
295 data->attach_points[2 * i + 1] = atoi (split_point);
304 keys = g_key_file_get_keys (icon_file, "Icon Data", &n_keys, &error);
305 data->display_names = g_new0 (gchar *, 2 * n_keys + 1);
306 data->n_display_names = 0;
308 for (i = 0; i < n_keys; i++)
312 if (g_str_has_prefix (keys[i], "DisplayName"))
314 gchar *open, *close = NULL;
316 open = strchr (keys[i], '[');
319 close = strchr (open, ']');
323 lang = g_strndup (open + 1, close - open - 1);
324 name = g_key_file_get_locale_string (icon_file,
325 "Icon Data", "DisplayName",
330 lang = g_strdup ("C");
331 name = g_key_file_get_string (icon_file,
332 "Icon Data", "DisplayName",
336 data->display_names[2 * data->n_display_names] = lang;
337 data->display_names[2 * data->n_display_names + 1] = name;
338 data->n_display_names++;
344 g_key_file_free (icon_file);
346 /* -1 means not computed yet, the real value depends
347 * on string pool state, and will be computed
356 * This function was copied from gtkfilesystemunix.c, it should
357 * probably go to GLib
360 canonicalize_filename (gchar *filename)
363 gboolean last_was_slash = FALSE;
370 if (*p == G_DIR_SEPARATOR)
373 *q++ = G_DIR_SEPARATOR;
375 last_was_slash = TRUE;
379 if (last_was_slash && *p == '.')
381 if (*(p + 1) == G_DIR_SEPARATOR ||
384 if (*(p + 1) == '\0')
389 else if (*(p + 1) == '.' &&
390 (*(p + 2) == G_DIR_SEPARATOR ||
393 if (q > filename + 1)
396 while (q > filename + 1 &&
397 *(q - 1) != G_DIR_SEPARATOR)
401 if (*(p + 2) == '\0')
409 last_was_slash = FALSE;
415 last_was_slash = FALSE;
422 if (q > filename + 1 && *(q - 1) == G_DIR_SEPARATOR)
429 follow_links (const gchar *path)
435 path2 = g_strdup (path);
436 while (g_file_test (path2, G_FILE_TEST_IS_SYMLINK))
438 target = g_file_read_link (path2, NULL);
442 if (g_path_is_absolute (target))
446 d = g_path_get_dirname (path2);
447 s = g_build_filename (d, target, NULL);
458 if (strcmp (path, path2) == 0)
468 maybe_cache_image_data (Image *image,
471 if (!index_only && !image->image_data &&
472 (g_str_has_suffix (path, ".png") || g_str_has_suffix (path, ".xpm")))
478 idata = g_hash_table_lookup (image_data_hash, path);
479 path2 = follow_links (path);
485 canonicalize_filename (path2);
487 idata2 = g_hash_table_lookup (image_data_hash, path2);
489 if (idata && idata2 && idata != idata2)
490 g_error ("different idatas found for symlinked '%s' and '%s'\n",
493 if (idata && !idata2)
494 g_hash_table_insert (image_data_hash, g_strdup (path2), idata);
496 if (!idata && idata2)
498 g_hash_table_insert (image_data_hash, g_strdup (path), idata2);
505 idata = g_new0 (ImageData, 1);
506 g_hash_table_insert (image_data_hash, g_strdup (path), idata);
508 g_hash_table_insert (image_data_hash, g_strdup (path2), idata);
511 if (!idata->has_pixdata)
513 pixbuf = gdk_pixbuf_new_from_file (path, NULL);
517 gdk_pixdata_from_pixbuf (&idata->pixdata, pixbuf, FALSE);
518 idata->size = idata->pixdata.length + 8;
519 idata->has_pixdata = TRUE;
523 image->image_data = idata;
530 maybe_cache_icon_data (Image *image,
533 if (g_str_has_suffix (path, ".icon"))
535 IconData *idata = NULL;
538 idata = g_hash_table_lookup (icon_data_hash, path);
539 path2 = follow_links (path);
545 canonicalize_filename (path2);
547 idata2 = g_hash_table_lookup (icon_data_hash, path2);
549 if (idata && idata2 && idata != idata2)
550 g_error ("different idatas found for symlinked '%s' and '%s'\n",
553 if (idata && !idata2)
554 g_hash_table_insert (icon_data_hash, g_strdup (path2), idata);
556 if (!idata && idata2)
558 g_hash_table_insert (icon_data_hash, g_strdup (path), idata2);
565 idata = load_icon_data (path);
566 g_hash_table_insert (icon_data_hash, g_strdup (path), idata);
568 g_hash_table_insert (icon_data_hash, g_strdup (path2), idata);
571 image->icon_data = idata;
578 scan_directory (const gchar *base_path,
584 GHashTable *dir_hash;
588 gboolean dir_added = FALSE;
589 guint dir_index = 0xffff;
591 dir_path = g_build_filename (base_path, subdir, NULL);
593 /* FIXME: Use the gerror */
594 dir = g_dir_open (dir_path, 0, NULL);
599 dir_hash = g_hash_table_new (g_str_hash, g_str_equal);
601 while ((name = g_dir_read_name (dir)))
607 gchar *basename, *dot;
609 path = g_build_filename (dir_path, name, NULL);
610 retval = g_file_test (path, G_FILE_TEST_IS_DIR);
616 subsubdir = g_build_filename (subdir, name, NULL);
618 subsubdir = g_strdup (name);
619 directories = scan_directory (base_path, subsubdir, files,
620 directories, depth + 1);
626 /* ignore images in the toplevel directory */
630 retval = g_file_test (path, G_FILE_TEST_IS_REGULAR);
633 if (g_str_has_suffix (name, ".png"))
634 flags |= HAS_SUFFIX_PNG;
635 else if (g_str_has_suffix (name, ".svg"))
636 flags |= HAS_SUFFIX_SVG;
637 else if (g_str_has_suffix (name, ".xpm"))
638 flags |= HAS_SUFFIX_XPM;
639 else if (g_str_has_suffix (name, ".icon"))
640 flags |= HAS_ICON_FILE;
645 basename = g_strdup (name);
646 dot = strrchr (basename, '.');
649 image = g_hash_table_lookup (dir_hash, basename);
657 dir_index = g_list_length (directories);
658 directories = g_list_append (directories, g_strdup (subdir));
664 image = g_new0 (Image, 1);
665 image->dir_index = dir_index;
666 g_hash_table_insert (dir_hash, g_strdup (basename), image);
669 image->flags |= flags;
671 maybe_cache_image_data (image, path);
672 maybe_cache_icon_data (image, path);
682 /* Move dir into the big file hash */
683 g_hash_table_foreach_remove (dir_hash, foreach_remove_func, files);
685 g_hash_table_destroy (dir_hash);
690 typedef struct _HashNode HashNode;
701 icon_name_hash (gconstpointer key)
703 const signed char *p = key;
707 for (p += 1; *p != '\0'; p++)
708 h = (h << 5) - h + *p;
719 convert_to_hash (gpointer key, gpointer value, gpointer user_data)
721 HashContext *context = user_data;
725 hash = icon_name_hash (key) % context->size;
727 node = g_new0 (HashNode, 1);
730 node->image_list = value;
732 if (context->nodes[hash] != NULL)
733 node->next = context->nodes[hash];
735 context->nodes[hash] = node;
740 static GHashTable *string_pool = NULL;
743 find_string (const gchar *n)
745 return GPOINTER_TO_INT (g_hash_table_lookup (string_pool, n));
749 add_string (const gchar *n, int offset)
751 g_hash_table_insert (string_pool, (gpointer) n, GINT_TO_POINTER (offset));
755 write_string (FILE *cache, const gchar *n)
760 l = ALIGN_VALUE (strlen (n) + 1, 4);
765 i = fwrite (s, l, 1, cache);
774 write_card16 (FILE *cache, guint16 n)
778 n = GUINT16_TO_BE (n);
780 i = fwrite ((char *)&n, 2, 1, cache);
786 write_card32 (FILE *cache, guint32 n)
790 n = GUINT32_TO_BE (n);
792 i = fwrite ((char *)&n, 4, 1, cache);
799 write_image_data (FILE *cache, ImageData *image_data, int offset)
804 GdkPixdata *pixdata = &image_data->pixdata;
806 /* Type 0 is GdkPixdata */
807 if (!write_card32 (cache, 0))
810 s = gdk_pixdata_serialize (pixdata, &len);
812 if (!write_card32 (cache, len))
818 i = fwrite (s, len, 1, cache);
826 write_icon_data (FILE *cache, IconData *icon_data, int offset)
828 int ofs = offset + 12;
832 if (icon_data->has_embedded_rect)
834 if (!write_card32 (cache, ofs))
841 if (!write_card32 (cache, 0))
845 if (icon_data->n_attach_points > 0)
847 if (!write_card32 (cache, ofs))
850 ofs += 4 + 4 * icon_data->n_attach_points;
854 if (!write_card32 (cache, 0))
858 if (icon_data->n_display_names > 0)
860 if (!write_card32 (cache, ofs))
865 if (!write_card32 (cache, 0))
869 if (icon_data->has_embedded_rect)
871 if (!write_card16 (cache, icon_data->x0) ||
872 !write_card16 (cache, icon_data->y0) ||
873 !write_card16 (cache, icon_data->x1) ||
874 !write_card16 (cache, icon_data->y1))
878 if (icon_data->n_attach_points > 0)
880 if (!write_card32 (cache, icon_data->n_attach_points))
883 for (j = 0; j < 2 * icon_data->n_attach_points; j++)
885 if (!write_card16 (cache, icon_data->attach_points[j]))
890 if (icon_data->n_display_names > 0)
892 if (!write_card32 (cache, icon_data->n_display_names))
895 ofs += 4 + 8 * icon_data->n_display_names;
898 for (j = 0; j < 2 * icon_data->n_display_names; j++)
900 tmp2 = find_string (icon_data->display_names[j]);
901 if (tmp2 == 0 || tmp2 == -1)
904 tmp += ALIGN_VALUE (strlen (icon_data->display_names[j]) + 1, 4);
905 /* We're playing a little game with negative
906 * offsets here to handle duplicate strings in
909 add_string (icon_data->display_names[j], -tmp2);
916 if (!write_card32 (cache, tmp2))
921 g_assert (ofs == ftell (cache));
922 for (j = 0; j < 2 * icon_data->n_display_names; j++)
924 tmp2 = find_string (icon_data->display_names[j]);
925 g_assert (tmp2 != 0 && tmp2 != -1);
929 g_assert (tmp2 == ftell (cache));
930 add_string (icon_data->display_names[j], tmp2);
931 if (!write_string (cache, icon_data->display_names[j]))
941 write_header (FILE *cache, guint32 dir_list_offset)
943 return (write_card16 (cache, MAJOR_VERSION) &&
944 write_card16 (cache, MINOR_VERSION) &&
945 write_card32 (cache, HASH_OFFSET) &&
946 write_card32 (cache, dir_list_offset));
950 get_image_meta_data_size (Image *image)
954 /* The complication with storing the size in both
955 * IconData and Image is necessary since we attribute
956 * the size of the IconData only to the first Image
957 * using it (at which time it is written out in the
958 * cache). Later Images just refer to the written out
959 * IconData via the offset.
961 if (image->icon_data_size == 0)
963 if (image->icon_data && image->icon_data->size < 0)
965 IconData *data = image->icon_data;
969 if (data->has_embedded_rect ||
970 data->n_attach_points > 0 ||
971 data->n_display_names > 0)
974 if (data->has_embedded_rect)
977 if (data->n_attach_points > 0)
978 data->size += 4 + data->n_attach_points * 4;
980 if (data->n_display_names > 0)
982 data->size += 4 + 8 * data->n_display_names;
984 for (i = 0; data->display_names[i]; i++)
987 if ((poolv = find_string (data->display_names[i])) == 0)
989 data->size += ALIGN_VALUE (strlen (data->display_names[i]) + 1, 4);
990 /* Adding the string to the pool with -1
991 * to indicate that it hasn't been written out
992 * to the cache yet. We still need it in the
993 * pool in case the same string occurs twice
994 * during a get_single_node_size() calculation.
996 add_string (data->display_names[i], -1);
1001 image->icon_data_size = data->size;
1006 g_assert (image->icon_data_size % 4 == 0);
1008 return image->icon_data_size;
1012 get_image_pixel_data_size (Image *image)
1014 /* The complication with storing the size in both
1015 * ImageData and Image is necessary since we attribute
1016 * the size of the ImageData only to the first Image
1017 * using it (at which time it is written out in the
1018 * cache). Later Images just refer to the written out
1019 * ImageData via the offset.
1021 if (image->pixel_data_size == 0)
1023 if (image->image_data &&
1024 image->image_data->has_pixdata)
1026 image->pixel_data_size = image->image_data->size;
1027 image->image_data->size = 0;
1031 g_assert (image->pixel_data_size % 4 == 0);
1033 return image->pixel_data_size;
1037 get_image_data_size (Image *image)
1043 len += get_image_pixel_data_size (image);
1044 len += get_image_meta_data_size (image);
1046 /* Even if len is zero, we need to reserve space to
1047 * write the ImageData, unless this is an .svg without
1048 * .icon, in which case both image_data and icon_data
1051 if (len > 0 || image->image_data || image->icon_data)
1058 get_single_node_size (HashNode *node, int *node_size, int *image_data_size)
1066 if (find_string (node->name) == 0)
1068 *node_size += ALIGN_VALUE (strlen (node->name) + 1, 4);
1069 add_string (node->name, -1);
1073 *node_size += 4 + g_list_length (node->image_list) * 8;
1076 *image_data_size = 0;
1077 for (list = node->image_list; list; list = list->next)
1079 Image *image = list->data;
1081 *image_data_size += get_image_data_size (image);
1086 write_bucket (FILE *cache, HashNode *node, int *offset)
1088 while (node != NULL)
1090 int node_size, image_data_size;
1091 int next_offset, image_data_offset;
1095 int image_list_offset;
1099 g_assert (*offset == ftell (cache));
1101 node->offset = *offset;
1103 get_single_node_size (node, &node_size, &image_data_size);
1104 g_assert (node_size % 4 == 0);
1105 g_assert (image_data_size % 4 == 0);
1106 image_data_offset = *offset + node_size;
1107 next_offset = *offset + node_size + image_data_size;
1109 if (node->next != NULL)
1111 if (!write_card32 (cache, next_offset))
1116 if (!write_card32 (cache, 0xffffffff))
1121 name_offset = find_string (node->name);
1122 if (name_offset <= 0)
1124 name_offset = *offset + 12;
1125 name_size = ALIGN_VALUE (strlen (node->name) + 1, 4);
1126 add_string (node->name, name_offset);
1128 if (!write_card32 (cache, name_offset))
1131 image_list_offset = *offset + 12 + name_size;
1132 if (!write_card32 (cache, image_list_offset))
1138 if (!write_string (cache, node->name))
1143 len = g_list_length (node->image_list);
1144 if (!write_card32 (cache, len))
1147 list = node->image_list;
1148 data_offset = image_data_offset;
1149 for (i = 0; i < len; i++)
1151 Image *image = list->data;
1152 int image_data_size = get_image_data_size (image);
1154 /* Directory index */
1155 if (!write_card16 (cache, image->dir_index))
1159 if (!write_card16 (cache, image->flags))
1162 /* Image data offset */
1163 if (image_data_size > 0)
1165 if (!write_card32 (cache, data_offset))
1167 data_offset += image_data_size;
1171 if (!write_card32 (cache, 0))
1178 /* Now write the image data */
1179 list = node->image_list;
1180 for (i = 0; i < len; i++, list = list->next)
1182 Image *image = list->data;
1183 int pixel_data_size = get_image_pixel_data_size (image);
1184 int meta_data_size = get_image_meta_data_size (image);
1186 if (get_image_data_size (image) == 0)
1190 if (pixel_data_size > 0)
1192 image->image_data->offset = image_data_offset + 8;
1193 if (!write_card32 (cache, image->image_data->offset))
1198 if (!write_card32 (cache, (guint32) (image->image_data ? image->image_data->offset : 0)))
1202 if (meta_data_size > 0)
1204 image->icon_data->offset = image_data_offset + pixel_data_size + 8;
1205 if (!write_card32 (cache, image->icon_data->offset))
1210 if (!write_card32 (cache, image->icon_data ? image->icon_data->offset : 0))
1214 if (pixel_data_size > 0)
1216 if (!write_image_data (cache, image->image_data, image->image_data->offset))
1220 if (meta_data_size > 0)
1222 if (!write_icon_data (cache, image->icon_data, image->icon_data->offset))
1226 image_data_offset += pixel_data_size + meta_data_size + 8;
1229 *offset = next_offset;
1237 write_hash_table (FILE *cache, HashContext *context, int *new_offset)
1239 int offset = HASH_OFFSET;
1243 if (!(write_card32 (cache, context->size)))
1247 node_offset = offset + context->size * 4;
1248 /* Just write zeros here, we will rewrite this later */
1249 for (i = 0; i < context->size; i++)
1251 if (!write_card32 (cache, 0))
1255 /* Now write the buckets */
1256 for (i = 0; i < context->size; i++)
1258 if (!context->nodes[i])
1261 g_assert (node_offset % 4 == 0);
1262 if (!write_bucket (cache, context->nodes[i], &node_offset))
1266 *new_offset = node_offset;
1268 /* Now write out the bucket offsets */
1270 fseek (cache, offset, SEEK_SET);
1272 for (i = 0; i < context->size; i++)
1274 if (context->nodes[i] != NULL)
1275 node_offset = context->nodes[i]->offset;
1277 node_offset = 0xffffffff;
1278 if (!write_card32 (cache, node_offset))
1282 fseek (cache, 0, SEEK_END);
1288 write_dir_index (FILE *cache, int offset, GList *directories)
1295 n_dirs = g_list_length (directories);
1297 if (!write_card32 (cache, n_dirs))
1300 offset += 4 + n_dirs * 4;
1303 for (d = directories; d; d = d->next)
1307 tmp2 = find_string (dir);
1309 if (tmp2 == 0 || tmp2 == -1)
1312 tmp += ALIGN_VALUE (strlen (dir) + 1, 4);
1313 /* We're playing a little game with negative
1314 * offsets here to handle duplicate strings in
1315 * the array, even though that should not
1316 * really happen for the directory index.
1318 add_string (dir, -tmp2);
1325 if (!write_card32 (cache, tmp2))
1329 g_assert (offset == ftell (cache));
1330 for (d = directories; d; d = d->next)
1334 tmp2 = find_string (dir);
1335 g_assert (tmp2 != 0 && tmp2 != -1);
1339 g_assert (tmp2 == ftell (cache));
1340 add_string (dir, tmp2);
1341 if (!write_string (cache, dir))
1350 write_file (FILE *cache, GHashTable *files, GList *directories)
1352 HashContext context;
1355 /* Convert the hash table into something looking a bit more
1356 * like what we want to write to disk.
1358 context.size = g_spaced_primes_closest (g_hash_table_size (files) / 3);
1359 context.nodes = g_new0 (HashNode *, context.size);
1361 g_hash_table_foreach_remove (files, convert_to_hash, &context);
1363 /* Now write the file */
1364 /* We write 0 as the directory list offset and go
1365 * back and change it later */
1366 if (!write_header (cache, 0))
1368 g_printerr (_("Failed to write header\n"));
1372 if (!write_hash_table (cache, &context, &new_offset))
1374 g_printerr (_("Failed to write hash table\n"));
1378 if (!write_dir_index (cache, new_offset, directories))
1380 g_printerr (_("Failed to write folder index\n"));
1386 if (!write_header (cache, new_offset))
1388 g_printerr (_("Failed to rewrite header\n"));
1396 validate_file (const gchar *file)
1401 map = g_mapped_file_new (file, FALSE, NULL);
1405 info.cache = g_mapped_file_get_contents (map);
1406 info.cache_size = g_mapped_file_get_length (map);
1407 info.n_directories = 0;
1408 info.flags = CHECK_OFFSETS|CHECK_STRINGS|CHECK_PIXBUFS;
1410 if (!_gtk_icon_cache_validate (&info))
1412 g_mapped_file_unref (map);
1416 g_mapped_file_unref (map);
1423 * @f: A FILE* stream, must have underlying fd
1425 * Unix defaults for data preservation after system crash
1426 * are unspecified, and many systems will eat your data
1427 * in this situation unless you explicitly fsync().
1429 * Returns: %TRUE on success, %FALSE on failure, and will set errno()
1432 safe_fclose (FILE *f)
1434 int fd = fileno (f);
1436 if (fflush (f) == EOF)
1442 if (fclose (f) == EOF)
1448 build_cache (const gchar *path)
1450 gchar *cache_path, *tmp_cache_path;
1452 gchar *bak_cache_path = NULL;
1456 GStatBuf path_stat, cache_stat;
1457 struct utimbuf utime_buf;
1458 GList *directories = NULL;
1460 int retry_count = 0;
1462 mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
1464 int mode = _S_IWRITE | _S_IREAD;
1470 tmp_cache_path = g_build_filename (path, "."CACHE_NAME, NULL);
1471 cache_path = g_build_filename (path, CACHE_NAME, NULL);
1474 if ((fd = g_open (tmp_cache_path, O_WRONLY | O_CREAT | O_EXCL | O_TRUNC | _O_BINARY, mode)) == -1)
1476 if (force_update && retry_count == 0)
1479 g_remove (tmp_cache_path);
1482 g_printerr (_("Failed to open file %s : %s\n"), tmp_cache_path, g_strerror (errno));
1486 cache = fdopen (fd, "wb");
1490 g_printerr (_("Failed to write cache file: %s\n"), g_strerror (errno));
1494 files = g_hash_table_new (g_str_hash, g_str_equal);
1495 image_data_hash = g_hash_table_new (g_str_hash, g_str_equal);
1496 icon_data_hash = g_hash_table_new (g_str_hash, g_str_equal);
1497 string_pool = g_hash_table_new (g_str_hash, g_str_equal);
1499 directories = scan_directory (path, NULL, files, NULL, 0);
1501 if (g_hash_table_size (files) == 0)
1503 /* Empty table, just close and remove the file */
1506 g_unlink (tmp_cache_path);
1507 g_unlink (cache_path);
1511 /* FIXME: Handle failure */
1512 if (!write_file (cache, files, directories))
1514 g_unlink (tmp_cache_path);
1518 if (!safe_fclose (cache))
1520 g_printerr (_("Failed to write cache file: %s\n"), g_strerror (errno));
1521 g_unlink (tmp_cache_path);
1526 g_list_free_full (directories, g_free);
1528 if (!validate_file (tmp_cache_path))
1530 g_printerr (_("The generated cache was invalid.\n"));
1531 /*g_unlink (tmp_cache_path);*/
1536 if (g_file_test (cache_path, G_FILE_TEST_EXISTS))
1538 bak_cache_path = g_strconcat (cache_path, ".bak", NULL);
1539 g_unlink (bak_cache_path);
1540 if (g_rename (cache_path, bak_cache_path) == -1)
1544 g_printerr (_("Could not rename %s to %s: %s, removing %s then.\n"),
1545 cache_path, bak_cache_path,
1548 g_unlink (cache_path);
1549 bak_cache_path = NULL;
1554 if (g_rename (tmp_cache_path, cache_path) == -1)
1558 g_printerr (_("Could not rename %s to %s: %s\n"),
1559 tmp_cache_path, cache_path,
1560 g_strerror (errsv));
1561 g_unlink (tmp_cache_path);
1563 if (bak_cache_path != NULL)
1564 if (g_rename (bak_cache_path, cache_path) == -1)
1568 g_printerr (_("Could not rename %s back to %s: %s.\n"),
1569 bak_cache_path, cache_path,
1570 g_strerror (errsv));
1576 if (bak_cache_path != NULL)
1577 g_unlink (bak_cache_path);
1581 /* FIXME: What do do if an error occurs here? */
1582 if (g_stat (path, &path_stat) < 0 ||
1583 g_stat (cache_path, &cache_stat))
1586 utime_buf.actime = path_stat.st_atime;
1587 utime_buf.modtime = cache_stat.st_mtime;
1588 #if GLIB_CHECK_VERSION (2, 17, 1)
1589 g_utime (path, &utime_buf);
1591 utime (path, &utime_buf);
1595 g_printerr (_("Cache file created successfully.\n"));
1599 write_csource (const gchar *path)
1606 cache_path = g_build_filename (path, CACHE_NAME, NULL);
1607 if (!g_file_get_contents (cache_path, &data, &len, NULL))
1610 g_printf ("#ifdef __SUNPRO_C\n");
1611 g_printf ("#pragma align 4 (%s)\n", var_name);
1612 g_printf ("#endif\n");
1614 g_printf ("#ifdef __GNUC__\n");
1615 g_printf ("static const guint8 %s[] __attribute__ ((__aligned__ (4))) = \n", var_name);
1616 g_printf ("#else\n");
1617 g_printf ("static const guint8 %s[] = \n", var_name);
1618 g_printf ("#endif\n");
1621 for (i = 0; i < len - 1; i++)
1625 g_printf ("0x%02x, ", (guint8)data[i]);
1630 g_printf ("0x%02x\n};\n", (guint8)data[i]);
1633 static GOptionEntry args[] = {
1634 { "force", 'f', 0, G_OPTION_ARG_NONE, &force_update, N_("Overwrite an existing cache, even if up to date"), NULL },
1635 { "ignore-theme-index", 't', 0, G_OPTION_ARG_NONE, &ignore_theme_index, N_("Don't check for the existence of index.theme"), NULL },
1636 { "index-only", 'i', 0, G_OPTION_ARG_NONE, &index_only, N_("Don't include image data in the cache"), NULL },
1637 { "source", 'c', 0, G_OPTION_ARG_STRING, &var_name, N_("Output a C header file"), "NAME" },
1638 { "quiet", 'q', 0, G_OPTION_ARG_NONE, &quiet, N_("Turn off verbose output"), NULL },
1639 { "validate", 'v', 0, G_OPTION_ARG_NONE, &validate, N_("Validate existing icon cache"), NULL },
1644 printerr_handler (const gchar *string)
1646 const gchar *charset;
1648 fputs (g_get_prgname (), stderr);
1649 fputs (": ", stderr);
1650 if (g_get_charset (&charset))
1651 fputs (string, stderr); /* charset is UTF-8 already */
1656 result = g_convert_with_fallback (string, -1, charset, "UTF-8", "?", NULL, NULL, NULL);
1660 fputs (result, stderr);
1670 main (int argc, char **argv)
1673 GOptionContext *context;
1678 g_set_printerr_handler (printerr_handler);
1680 setlocale (LC_ALL, "");
1683 bindtextdomain (GETTEXT_PACKAGE, GTK_LOCALEDIR);
1684 #ifdef HAVE_BIND_TEXTDOMAIN_CODESET
1685 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
1689 context = g_option_context_new ("ICONPATH");
1690 g_option_context_add_main_entries (context, args, GETTEXT_PACKAGE);
1692 g_option_context_parse (context, &argc, &argv, NULL);
1696 path = g_locale_to_utf8 (path, -1, NULL, NULL, NULL);
1701 gchar *file = g_build_filename (path, CACHE_NAME, NULL);
1703 if (!g_file_test (file, G_FILE_TEST_IS_REGULAR))
1706 g_printerr (_("File not found: %s\n"), file);
1709 if (!validate_file (file))
1712 g_printerr (_("Not a valid icon cache: %s\n"), file);
1721 if (!ignore_theme_index && !has_theme_index (path))
1725 g_printerr (_("No theme index file.\n"));
1729 g_printerr (_("No theme index file in '%s'.\n"
1730 "If you really want to create an icon cache here, use --ignore-theme-index.\n"), path);
1736 if (!force_update && is_cache_up_to_date (path))
1741 if (strcmp (var_name, "-") != 0)
1742 write_csource (path);