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.
23 #include <sys/types.h>
30 #include <glib/gstdio.h>
32 #define CACHE_NAME "icon-theme.cache"
34 #define HAS_SUFFIX_XPM (1 << 0)
35 #define HAS_SUFFIX_SVG (1 << 1)
36 #define HAS_SUFFIX_PNG (1 << 2)
37 #define HAS_ICON_FILE (1 << 3)
39 #define MAJOR_VERSION 1
40 #define MINOR_VERSION 0
41 #define HASH_OFFSET 12
43 #define ALIGN_VALUE(this, boundary) \
44 (( ((unsigned long)(this)) + (((unsigned long)(boundary)) -1)) & (~(((unsigned long)(boundary))-1)))
47 is_cache_up_to_date (const gchar *path)
49 struct stat path_stat, cache_stat;
53 retval = g_stat (path, &path_stat);
57 /* We can't stat the path,
58 * assume we have a updated cache */
62 cache_path = g_build_filename (path, CACHE_NAME, NULL);
63 retval = g_stat (cache_path, &cache_stat);
66 if (retval < 0 && errno == ENOENT)
68 /* Cache file not found */
73 return cache_stat.st_mtime <= path_stat.st_mtime;
83 foreach_remove_func (gpointer key, gpointer value, gpointer user_data)
85 Image *image = (Image *)value;
86 GHashTable *files = user_data;
88 gboolean free_key = FALSE;
90 if (image->flags == HAS_ICON_FILE)
98 list = g_hash_table_lookup (files, key);
102 list = g_list_prepend (list, value);
103 g_hash_table_insert (files, key, list);
112 scan_directory (const gchar *base_path,
118 GHashTable *dir_hash;
122 gboolean dir_added = FALSE;
123 guint dir_index = 0xffff;
125 dir_path = g_build_filename (base_path, subdir, NULL);
127 /* FIXME: Use the gerror */
128 dir = g_dir_open (dir_path, 0, NULL);
133 dir_hash = g_hash_table_new (g_str_hash, g_str_equal);
135 while ((name = g_dir_read_name (dir)))
141 gchar *basename, *dot;
143 path = g_build_filename (dir_path, name, NULL);
144 retval = g_file_test (path, G_FILE_TEST_IS_DIR);
150 subsubdir = g_build_filename (subdir, name, NULL);
152 subsubdir = g_strdup (name);
153 directories = scan_directory (base_path, subsubdir, files,
154 directories, depth + 1);
160 retval = g_file_test (path, G_FILE_TEST_IS_REGULAR);
165 if (g_str_has_suffix (name, ".png"))
166 flags |= HAS_SUFFIX_PNG;
167 else if (g_str_has_suffix (name, ".svg"))
168 flags |= HAS_SUFFIX_SVG;
169 else if (g_str_has_suffix (name, ".xpm"))
170 flags |= HAS_SUFFIX_XPM;
171 else if (g_str_has_suffix (name, ".icon"))
172 flags |= HAS_ICON_FILE;
177 basename = g_strdup (name);
178 dot = strrchr (basename, '.');
181 image = g_hash_table_lookup (dir_hash, basename);
183 image->flags |= flags;
191 dir_index = g_list_length (directories);
192 directories = g_list_append (directories, g_strdup (subdir));
198 image = g_new0 (Image, 1);
199 image->flags = flags;
200 image->dir_index = dir_index;
202 g_hash_table_insert (dir_hash, g_strdup (basename), image);
211 /* Move dir into the big file hash */
212 g_hash_table_foreach_remove (dir_hash, foreach_remove_func, files);
214 g_hash_table_destroy (dir_hash);
219 typedef struct _HashNode HashNode;
229 icon_name_hash (gconstpointer key)
235 for (p += 1; *p != '\0'; p++)
236 h = (h << 5) - h + *p;
247 convert_to_hash (gpointer key, gpointer value, gpointer user_data)
249 HashContext *context = user_data;
253 hash = icon_name_hash (key) % context->size;
255 node = g_new0 (HashNode, 1);
258 node->image_list = value;
260 if (context->nodes[hash] != NULL)
261 node->next = context->nodes[hash];
263 context->nodes[hash] = node;
269 write_string (FILE *cache, const gchar *n)
274 l = ALIGN_VALUE (strlen (n) + 1, 4);
279 i = fwrite (s, l, 1, cache);
286 write_card16 (FILE *cache, guint16 n)
291 *((guint16 *)s) = GUINT16_TO_BE (n);
293 i = fwrite (s, 2, 1, cache);
299 write_card32 (FILE *cache, guint32 n)
304 *((guint32 *)s) = GUINT32_TO_BE (n);
306 i = fwrite (s, 4, 1, cache);
312 write_header (FILE *cache, guint32 dir_list_offset)
314 return (write_card16 (cache, MAJOR_VERSION) &&
315 write_card16 (cache, MINOR_VERSION) &&
316 write_card32 (cache, HASH_OFFSET) &&
317 write_card32 (cache, dir_list_offset));
322 get_single_node_size (HashNode *node)
330 len += ALIGN_VALUE (strlen (node->name) + 1, 4);
333 len += 4 + g_list_length (node->image_list) * 8;
339 get_bucket_size (HashNode *node)
345 len += get_single_node_size (node);
354 write_bucket (FILE *cache, HashNode *node, int *offset)
358 int next_offset = *offset + get_single_node_size (node);
363 if (node->next != NULL)
365 if (!write_card32 (cache, next_offset))
370 if (!write_card32 (cache, 0xffffffff))
374 /* Icon name offset */
375 if (!write_card32 (cache, *offset + 12))
378 /* Image list offset */
379 if (!write_card32 (cache, *offset + 12 + ALIGN_VALUE (strlen (node->name) + 1, 4)))
383 if (!write_string (cache, node->name))
387 len = g_list_length (node->image_list);
388 if (!write_card32 (cache, len))
391 list = node->image_list;
392 for (i = 0; i < len; i++)
394 Image *image = list->data;
396 /* Directory index */
397 if (!write_card16 (cache, image->dir_index))
401 if (!write_card16 (cache, image->flags))
404 /* Image data offset */
405 if (!write_card32 (cache, 0))
411 *offset = next_offset;
419 write_hash_table (FILE *cache, HashContext *context, int *new_offset)
421 int offset = HASH_OFFSET;
425 if (!(write_card32 (cache, context->size)))
428 /* Size int + size * 4 */
429 node_offset = offset + 4 + context->size * 4;
431 for (i = 0; i < context->size; i++)
433 if (context->nodes[i] != NULL)
435 if (!write_card32 (cache, node_offset))
438 node_offset += get_bucket_size (context->nodes[i]);
442 if (!write_card32 (cache, 0xffffffff))
449 *new_offset = node_offset;
451 /* Now write the buckets */
452 node_offset = offset + 4 + context->size * 4;
454 for (i = 0; i < context->size; i++)
456 if (!context->nodes[i])
459 if (!write_bucket (cache, context->nodes[i], &node_offset))
467 write_dir_index (FILE *cache, int offset, GList *directories)
473 n_dirs = g_list_length (directories);
475 if (!write_card32 (cache, n_dirs))
478 offset += 4 + n_dirs * 4;
480 for (d = directories; d; d = d->next)
483 if (!write_card32 (cache, offset))
486 offset += ALIGN_VALUE (strlen (dir) + 1, 4);
489 for (d = directories; d; d = d->next)
493 if (!write_string (cache, dir))
501 write_file (FILE *cache, GHashTable *files, GList *directories)
506 /* Convert the hash table into something looking a bit more
507 * like what we want to write to disk.
509 context.size = g_spaced_primes_closest (g_hash_table_size (files) / 3);
510 context.nodes = g_new0 (HashNode *, context.size);
512 g_hash_table_foreach_remove (files, convert_to_hash, &context);
514 /* Now write the file */
515 /* We write 0 as the directory list offset and go
516 * back and change it later */
517 if (!write_header (cache, 0))
519 g_printerr ("Failed to write header\n");
523 if (!write_hash_table (cache, &context, &new_offset))
525 g_printerr ("Failed to write hash table\n");
529 if (!write_dir_index (cache, new_offset, directories))
531 g_printerr ("Failed to write directory index\n");
537 if (!write_header (cache, new_offset))
539 g_printerr ("Failed to rewrite header\n");
547 build_cache (const gchar *path)
549 gchar *cache_path, *tmp_cache_path;
553 struct stat path_stat, cache_stat;
554 struct utimbuf utime_buf;
555 GList *directories = NULL;
557 tmp_cache_path = g_build_filename (path, "."CACHE_NAME, NULL);
558 cache = g_fopen (tmp_cache_path, "wb");
562 g_printerr ("Failed to write cache file: %s\n", g_strerror (errno));
566 files = g_hash_table_new (g_str_hash, g_str_equal);
568 directories = scan_directory (path, NULL, files, NULL, 0);
570 if (g_hash_table_size (files) == 0)
572 /* Empty table, just close and remove the file */
575 g_unlink (tmp_cache_path);
579 /* FIXME: Handle failure */
580 retval = write_file (cache, files, directories);
583 g_list_foreach (directories, (GFunc)g_free, NULL);
584 g_list_free (directories);
588 g_unlink (tmp_cache_path);
592 cache_path = g_build_filename (path, CACHE_NAME, NULL);
594 if (g_rename (tmp_cache_path, cache_path) == -1)
596 g_unlink (tmp_cache_path);
601 /* FIXME: What do do if an error occurs here? */
602 g_stat (path, &path_stat);
603 g_stat (cache_path, &cache_stat);
605 utime_buf.actime = path_stat.st_atime;
606 utime_buf.modtime = cache_stat.st_mtime;
607 utime (path, &utime_buf);
609 g_printerr ("Cache file created successfully.\n");
612 static gboolean force_update = FALSE;
614 static GOptionEntry args[] = {
615 { "force", 0, 0, G_OPTION_ARG_NONE, &force_update, "Overwrite an existing cache, even if uptodate", NULL },
620 main (int argc, char **argv)
623 GOptionContext *context;
628 context = g_option_context_new ("ICONPATH");
629 g_option_context_add_main_entries (context, args, NULL);
631 g_option_context_parse (context, &argc, &argv, NULL);
635 path = g_locale_to_utf8 (path, -1, NULL, NULL, NULL);
638 if (!force_update && is_cache_up_to_date (path))