diff --git a/gtk/updateiconcache.c b/gtk/updateiconcache.c index 4a3158e12..e167d2f8f 100644 --- a/gtk/updateiconcache.c +++ b/gtk/updateiconcache.c @@ -25,6 +25,15 @@ #include #include #include + +#ifdef _MSC_VER +#ifndef S_ISDIR +#define S_ISDIR(mode) (((mode) & _S_IFMT) == _S_IFDIR) +#endif +#ifndef S_ISREG +#define S_ISREG(mode) (((mode) & _S_IFMT) == _S_IFREG) +#endif +#endif #include #ifdef HAVE_UNISTD_H #include @@ -608,6 +617,16 @@ replace_backslashes_with_slashes (gchar *path) path[i] = '/'; } +static gint +compare_string_pointers (gconstpointer a, + gconstpointer b) +{ + const gchar * const *str_a = a; + const gchar * const *str_b = b; + + return strcmp (*str_a, *str_b); +} + static GList * scan_directory (const gchar *base_path, const gchar *subdir, @@ -617,10 +636,13 @@ scan_directory (const gchar *base_path, { GHashTable *dir_hash; GDir *dir; - const gchar *name; + GPtrArray *names; + const gchar *entry_name; + gchar *name; gchar *dir_path; gboolean dir_added = FALSE; guint dir_index = 0xffff; + guint i; dir_path = g_build_path ("/", base_path, subdir, NULL); @@ -628,91 +650,103 @@ scan_directory (const gchar *base_path, dir = g_dir_open (dir_path, 0, NULL); if (!dir) - return directories; + { + g_free (dir_path); + return directories; + } + + names = g_ptr_array_new_with_free_func (g_free); + + while ((entry_name = g_dir_read_name (dir))) + g_ptr_array_add (names, g_strdup (entry_name)); + + g_dir_close (dir); + + g_ptr_array_sort (names, compare_string_pointers); dir_hash = g_hash_table_new (g_str_hash, g_str_equal); - while ((name = g_dir_read_name (dir))) + for (i = 0; i < names->len; i++) { + GStatBuf stat_buf; gchar *path; - gboolean retval; - int flags = 0; - Image *image; - gchar *basename, *dot; + name = g_ptr_array_index (names, i); path = g_build_filename (dir_path, name, NULL); - retval = g_file_test (path, G_FILE_TEST_IS_DIR); - if (retval) - { - gchar *subsubdir; + if (g_stat (path, &stat_buf) == 0) + { + if (S_ISDIR (stat_buf.st_mode)) + { + gchar *subsubdir; - if (subdir) - subsubdir = g_build_path ("/", subdir, name, NULL); - else - subsubdir = g_strdup (name); - directories = scan_directory (base_path, subsubdir, files, - directories, depth + 1); - g_free (subsubdir); + if (subdir) + subsubdir = g_build_path ("/", subdir, name, NULL); + else + subsubdir = g_strdup (name); - continue; - } + directories = scan_directory (base_path, subsubdir, files, + directories, depth + 1); + g_free (subsubdir); + } + else if (subdir != NULL && S_ISREG (stat_buf.st_mode)) + { + gint flags = 0; + Image *image; + gchar *dot; - /* ignore images in the toplevel directory */ - if (subdir == NULL) - continue; + dot = strrchr (name, '.'); - retval = g_file_test (path, G_FILE_TEST_IS_REGULAR); - if (retval) - { - if (g_str_has_suffix (name, ".png")) - flags |= HAS_SUFFIX_PNG; - else if (g_str_has_suffix (name, ".svg")) - flags |= HAS_SUFFIX_SVG; - else if (g_str_has_suffix (name, ".xpm")) - flags |= HAS_SUFFIX_XPM; - else if (g_str_has_suffix (name, ".icon")) - flags |= HAS_ICON_FILE; + if (dot != NULL) + { + if (strcmp (dot, ".png") == 0) + flags = HAS_SUFFIX_PNG; + else if (strcmp (dot, ".svg") == 0) + flags = HAS_SUFFIX_SVG; + else if (strcmp (dot, ".xpm") == 0) + flags = HAS_SUFFIX_XPM; + else if (strcmp (dot, ".icon") == 0) + flags = HAS_ICON_FILE; + } - if (flags == 0) - continue; + if (flags != 0) + { + *dot = '\0'; - basename = g_strdup (name); - dot = strrchr (basename, '.'); - *dot = '\0'; + image = g_hash_table_lookup (dir_hash, name); + if (!image) + { + if (!dir_added) + { + dir_added = TRUE; + dir_index = g_list_length (directories); + directories = + g_list_append (directories, + g_strdup (subdir)); + } - image = g_hash_table_lookup (dir_hash, basename); - if (!image) - { - if (!dir_added) - { - dir_added = TRUE; - if (subdir) - { - dir_index = g_list_length (directories); - directories = g_list_append (directories, g_strdup (subdir)); - } - else - continue; - } + image = g_new0 (Image, 1); + image->dir_index = dir_index; + g_hash_table_insert (dir_hash, + g_strdup (name), + image); + } - image = g_new0 (Image, 1); - image->dir_index = dir_index; - g_hash_table_insert (dir_hash, g_strdup (basename), image); - } + image->flags |= flags; - image->flags |= flags; + maybe_cache_image_data (image, path); + maybe_cache_icon_data (image, path); - maybe_cache_image_data (image, path); - maybe_cache_icon_data (image, path); - - g_free (basename); - } + *dot = '.'; + } + } + } g_free (path); } - g_dir_close (dir); + g_ptr_array_free (names, TRUE); + g_free (dir_path); /* Move dir into the big file hash */ g_hash_table_foreach_remove (dir_hash, foreach_remove_func, files); @@ -1375,6 +1409,34 @@ write_dir_index (FILE *cache, int offset, GList *directories) return TRUE; } +static void +free_hash_context (HashContext *context) +{ + gint i; + + for (i = 0; i < context->size; i++) + { + HashNode *node = context->nodes[i]; + + while (node) + { + HashNode *next = node->next; + GList *l; + + for (l = node->image_list; l; l = l->next) + g_free (l->data); + + g_list_free (node->image_list); + g_free (node->name); + g_free (node); + + node = next; + } + } + + g_free (context->nodes); +} + static gboolean write_file (FILE *cache, GHashTable *files, GList *directories) { @@ -1418,6 +1480,11 @@ write_file (FILE *cache, GHashTable *files, GList *directories) return FALSE; } + g_hash_table_destroy (string_pool); + string_pool = NULL; + + free_hash_context (&context); + return TRUE; } @@ -1544,6 +1611,8 @@ opentmp: exit (1); } + g_hash_table_destroy (files); + if (!safe_fclose (cache)) { g_printerr (_("Failed to write cache file: %s\n"), g_strerror (errno)); @@ -1623,6 +1692,9 @@ opentmp: if (!quiet) g_printerr (_("Cache file created successfully.\n")); + + g_free (tmp_cache_path); + g_free (cache_path); } static void @@ -1658,6 +1730,9 @@ write_csource (const gchar *path) } g_printf ("0x%02x\n};\n", (guint8)data[i]); + + g_free (data); + g_free (cache_path); } static GOptionEntry args[] = { @@ -1721,6 +1796,7 @@ main (int argc, char **argv) g_option_context_add_main_entries (context, args, GETTEXT_PACKAGE); g_option_context_parse (context, &argc, &argv, NULL); + g_option_context_free (context); path = argv[1]; #ifdef G_OS_WIN32