gtk-update-icon-cache: read directories sorted, stat once

This commit is contained in:
Daemonratte 2026-10-15 09:08:00 +02:00
commit b422502d7a

View file

@ -33,6 +33,12 @@
#ifdef _MSC_VER
#include <io.h>
#include <sys/utime.h>
#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
#else
#include <utime.h>
#endif
@ -608,6 +614,13 @@ replace_backslashes_with_slashes (gchar *path)
path[i] = '/';
}
static gint
compare_names (gconstpointer a,
gconstpointer b)
{
return strcmp (*(const gchar * const *) a, *(const gchar * const *) b);
}
static GList *
scan_directory (const gchar *base_path,
const gchar *subdir,
@ -617,10 +630,12 @@ scan_directory (const gchar *base_path,
{
GHashTable *dir_hash;
GDir *dir;
GPtrArray *names;
const 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,22 +643,38 @@ scan_directory (const gchar *base_path,
dir = g_dir_open (dir_path, 0, NULL);
if (!dir)
return directories;
{
g_free (dir_path);
return directories;
}
/* In name order, so the cache does not depend on the file system */
names = g_ptr_array_new_with_free_func (g_free);
while ((name = g_dir_read_name (dir)))
g_ptr_array_add (names, g_strdup (name));
g_dir_close (dir);
g_ptr_array_sort (names, compare_names);
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++)
{
gchar *path;
gboolean retval;
GStatBuf st;
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)
if (g_stat (path, &st) != 0)
{
g_free (path);
continue;
}
if (S_ISDIR (st.st_mode))
{
gchar *subsubdir;
@ -654,16 +685,9 @@ scan_directory (const gchar *base_path,
directories = scan_directory (base_path, subsubdir, files,
directories, depth + 1);
g_free (subsubdir);
continue;
}
/* ignore images in the toplevel directory */
if (subdir == NULL)
continue;
retval = g_file_test (path, G_FILE_TEST_IS_REGULAR);
if (retval)
else if (subdir != NULL && S_ISREG (st.st_mode))
{
if (g_str_has_suffix (name, ".png"))
flags |= HAS_SUFFIX_PNG;
@ -675,7 +699,10 @@ scan_directory (const gchar *base_path,
flags |= HAS_ICON_FILE;
if (flags == 0)
continue;
{
g_free (path);
continue;
}
basename = g_strdup (name);
dot = strrchr (basename, '.');
@ -712,7 +739,8 @@ scan_directory (const gchar *base_path,
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);