filechooser: validate cached thumbnail dimensions

This commit is contained in:
Daemonratte 2026-05-29 11:01:30 +02:00
commit d83e126644

View file

@ -1004,29 +1004,43 @@ typedef struct
static gboolean thumbnail_cache_path_is_sufficient (const gchar *path,
gint icon_size);
static gboolean thumbnail_debug_enabled (void);
static void thumbnail_debug_log_file (const gchar *where,
GFile *file,
const gchar *format,
...) G_GNUC_PRINTF (3, 4);
static gint
thumbnail_queue_get_cache_size (GtkFileChooserDefault *impl)
{
gint icon_size;
gint cache_size;
icon_size = thumbnail_queue_get_icon_size (impl);
/* Request the next freedesktop thumbnail cache size before the
* visible icon reaches the previous cache limit. This avoids
* upscaling 128px thumbnails at larger icon-view zoom levels.
/* Use the smallest freedesktop thumbnail cache class that can cover
* the visible icon size. The real thumbnail geometry is checked later,
* so broken undersized cache files do not get accepted just because they
* live in thumbnails/normal/.
*/
if (icon_size >= (FILECHOOSER_THUMBNAIL_X_LARGE_SIZE * 5) / 8)
return FILECHOOSER_THUMBNAIL_XX_LARGE_SIZE;
if (icon_size <= FILECHOOSER_THUMBNAIL_NORMAL_SIZE)
cache_size = FILECHOOSER_THUMBNAIL_NORMAL_SIZE;
else if (icon_size <= FILECHOOSER_THUMBNAIL_LARGE_SIZE)
cache_size = FILECHOOSER_THUMBNAIL_LARGE_SIZE;
else if (icon_size <= FILECHOOSER_THUMBNAIL_X_LARGE_SIZE)
cache_size = FILECHOOSER_THUMBNAIL_X_LARGE_SIZE;
else
cache_size = FILECHOOSER_THUMBNAIL_XX_LARGE_SIZE;
if (icon_size >= (FILECHOOSER_THUMBNAIL_LARGE_SIZE * 5) / 8)
return FILECHOOSER_THUMBNAIL_X_LARGE_SIZE;
if (thumbnail_debug_enabled ())
g_debug ("GtkFileChooser thumbnail: cache-size: visible=%d cache=%d",
icon_size,
cache_size);
if (icon_size >= (FILECHOOSER_THUMBNAIL_NORMAL_SIZE * 5) / 8)
return FILECHOOSER_THUMBNAIL_LARGE_SIZE;
return FILECHOOSER_THUMBNAIL_NORMAL_SIZE;
return cache_size;
}
static gboolean
thumbnail_queue_should_enqueue (GtkFileChooserDefault *impl,
GFile *file,
@ -1136,21 +1150,23 @@ thumbnail_cache_size_dir_for_dimension (gint dimension)
static gint
thumbnail_cache_max_dimension_for_path (const gchar *path)
{
gint width = 0;
gint height = 0;
if (path == NULL)
return 0;
if (strstr (path, G_DIR_SEPARATOR_S "normal" G_DIR_SEPARATOR_S))
return FILECHOOSER_THUMBNAIL_NORMAL_SIZE;
if (strstr (path, G_DIR_SEPARATOR_S "large" G_DIR_SEPARATOR_S))
return FILECHOOSER_THUMBNAIL_LARGE_SIZE;
if (strstr (path, G_DIR_SEPARATOR_S "x-large" G_DIR_SEPARATOR_S))
return FILECHOOSER_THUMBNAIL_X_LARGE_SIZE;
if (strstr (path, G_DIR_SEPARATOR_S "xx-large" G_DIR_SEPARATOR_S))
return FILECHOOSER_THUMBNAIL_XX_LARGE_SIZE;
/* Do not infer the thumbnail size from the cache directory name.
* Existing cache files can be smaller than their freedesktop cache class
* allows, for example a real 32x32 PNG inside thumbnails/normal/.
*/
if (gdk_pixbuf_get_file_info (path, &width, &height) == NULL)
return 0;
return G_MAXINT;
return MAX (width, height);
}
static gboolean
thumbnail_cache_path_is_sufficient (const gchar *path,
gint icon_size)
@ -1158,6 +1174,45 @@ thumbnail_cache_path_is_sufficient (const gchar *path,
return thumbnail_cache_max_dimension_for_path (path) >= icon_size;
}
static gboolean
thumbnail_debug_enabled (void)
{
static gint enabled = -1;
if (enabled == -1)
enabled = g_getenv ("GTK_FILE_CHOOSER_THUMBNAIL_DEBUG") != NULL;
return enabled;
}
static void
thumbnail_debug_log_file (const gchar *where,
GFile *file,
const gchar *format,
...)
{
va_list args;
gchar *message;
gchar *uri;
if (!thumbnail_debug_enabled ())
return;
va_start (args, format);
message = g_strdup_vprintf (format, args);
va_end (args);
uri = file ? g_file_get_uri (file) : NULL;
g_debug ("GtkFileChooser thumbnail: %s: %s: %s",
where,
uri ? uri : "(no file)",
message);
g_free (uri);
g_free (message);
}
static gchar *
thumbnail_cache_lookup (ThumbnailJob *job)
{
@ -1327,7 +1382,6 @@ thumbnail_job_apply_cb (gpointer data)
GFileInfo *info;
info = g_file_info_dup (_gtk_file_system_model_get_info (model, &iter));
if (job->pixbuf || job->thumbnail_path)
{
if (job->pixbuf)
@ -1341,9 +1395,51 @@ thumbnail_job_apply_cb (gpointer data)
}
if (job->thumbnail_path)
g_file_info_set_attribute_byte_string (info,
G_FILE_ATTRIBUTE_THUMBNAIL_PATH,
job->thumbnail_path);
{
const gchar *current_thumbnail_path;
gchar *debug_current_thumbnail_path;
gboolean accepted_thumbnail_path;
gint current_dimension;
gint job_dimension;
current_thumbnail_path = g_file_info_get_attribute_byte_string (info,
G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
debug_current_thumbnail_path = NULL;
if (thumbnail_debug_enabled ())
debug_current_thumbnail_path = g_strdup (current_thumbnail_path);
current_dimension = thumbnail_cache_max_dimension_for_path (current_thumbnail_path);
job_dimension = thumbnail_cache_max_dimension_for_path (job->thumbnail_path);
/* Prefer the smallest thumbnail that is still sufficient for
* the current target size. This allows replacing an existing
* x-large thumbnail with a normal one at 64px icon size, but
* still prevents replacing a sufficient large thumbnail with
* an undersized one at higher zoom levels.
*/
accepted_thumbnail_path = (!current_thumbnail_path ||
job_dimension >= current_dimension ||
job_dimension >= job->icon_size);
if (accepted_thumbnail_path)
g_file_info_set_attribute_byte_string (info,
G_FILE_ATTRIBUTE_THUMBNAIL_PATH,
job->thumbnail_path);
if (thumbnail_debug_enabled ())
{
thumbnail_debug_log_file ("worker-path-decision",
job->file,
"target-size=%d current=%s current-dim=%d job=%s job-dim=%d accepted=%d",
job->icon_size,
debug_current_thumbnail_path ? debug_current_thumbnail_path : "(none)",
current_dimension,
job->thumbnail_path,
job_dimension,
accepted_thumbnail_path);
g_free (debug_current_thumbnail_path);
}
}
}
else if (job->failed)
{
@ -1352,6 +1448,22 @@ thumbnail_job_apply_cb (gpointer data)
TRUE);
}
thumbnail_debug_log_file ("worker-apply",
job->file,
"target-size=%d pixbuf=%dx%d path=%s failed=%d",
job->icon_size,
job->pixbuf ? gdk_pixbuf_get_width (job->pixbuf) : 0,
job->pixbuf ? gdk_pixbuf_get_height (job->pixbuf) : 0,
job->thumbnail_path ? job->thumbnail_path : "(none)",
job->failed);
_gtk_file_system_model_clear_cache_for_iter (model,
&iter,
MODEL_COL_LIST_PIXBUF);
_gtk_file_system_model_clear_cache_for_iter (model,
&iter,
MODEL_COL_ICON_PIXBUF);
_gtk_file_system_model_update_file (model, job->file, info);
g_object_unref (info);
}
@ -1433,6 +1545,11 @@ thumbnail_worker_start (GtkFileChooserDefault *impl,
job->generation_serial = impl->thumbnail_generation_serial;
job->icon_size = thumbnail_queue_get_cache_size (impl);
thumbnail_debug_log_file ("worker-start",
job->file,
"target-size=%d",
job->icon_size);
#if GLIB_CHECK_VERSION (2, 32, 0)
thread = g_thread_new ("gtk-filechooser-thumbnail",
thumbnail_worker_thread,
@ -7335,11 +7452,11 @@ file_list_build_popup_menu (GtkFileChooserDefault *impl)
static void
file_list_update_popup_menu (GtkFileChooserDefault *impl)
{
guint selected_count;
gint file_count;
file_list_build_popup_menu (impl);
selected_count = current_selection_count_selected_rows (impl);
selection_check (impl, &file_count, NULL, NULL);
/* FIXME - handle OPERATION_MODE_SEARCH and OPERATION_MODE_RECENT */
@ -7349,12 +7466,12 @@ file_list_update_popup_menu (GtkFileChooserDefault *impl)
if (impl->browse_files_popup_menu_copy_item)
gtk_widget_set_sensitive (impl->browse_files_popup_menu_copy_item,
selected_count > 0);
file_count > 0);
if (impl->browse_files_popup_menu_cut_item)
gtk_widget_set_sensitive (impl->browse_files_popup_menu_cut_item,
impl->operation_mode == OPERATION_MODE_BROWSE &&
selected_count > 0);
file_count > 0);
if (impl->browse_files_popup_menu_paste_item)
gtk_widget_set_sensitive (impl->browse_files_popup_menu_paste_item,
@ -7364,7 +7481,7 @@ file_list_update_popup_menu (GtkFileChooserDefault *impl)
if (impl->browse_files_popup_menu_copy_location_item)
gtk_widget_set_sensitive (impl->browse_files_popup_menu_copy_location_item,
selected_count > 0);
file_count > 0);
if (impl->browse_files_popup_menu_open_containing_folder_item)
{
@ -7379,18 +7496,18 @@ file_list_update_popup_menu (GtkFileChooserDefault *impl)
gtk_widget_set_sensitive (impl->browse_files_popup_menu_open_containing_folder_item,
show_item &&
selected_count == 1);
file_count == 1);
}
if (impl->browse_files_popup_menu_rename_item)
gtk_widget_set_sensitive (impl->browse_files_popup_menu_rename_item,
impl->operation_mode == OPERATION_MODE_BROWSE &&
selected_count == 1);
file_count == 1);
if (impl->browse_files_popup_menu_trash_item)
gtk_widget_set_sensitive (impl->browse_files_popup_menu_trash_item,
impl->operation_mode == OPERATION_MODE_BROWSE &&
selected_count > 0);
file_count > 0);
/* 'Show Hidden Files' */
g_signal_handlers_block_by_func (impl->browse_files_popup_menu_hidden_files_item,
@ -11896,15 +12013,41 @@ file_system_model_got_thumbnail (GObject *object, GAsyncResult *res, gpointer da
info = g_file_info_dup (_gtk_file_system_model_get_info (model, &iter));
copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED);
thumbnail_debug_log_file ("gio-info",
file,
"queried-path=%s current-path=%s generated-size=%d generated=%d",
g_file_info_get_attribute_byte_string (queried, G_FILE_ATTRIBUTE_THUMBNAIL_PATH) ?
g_file_info_get_attribute_byte_string (queried, G_FILE_ATTRIBUTE_THUMBNAIL_PATH) : "(none)",
g_file_info_get_attribute_byte_string (info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH) ?
g_file_info_get_attribute_byte_string (info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH) : "(none)",
g_file_info_get_attribute_int32 (info, FILECHOOSER_GENERATED_THUMBNAIL_SIZE),
g_file_info_get_attribute_object (info, FILECHOOSER_GENERATED_THUMBNAIL) != NULL);
{
const gchar *current_thumbnail_path;
const gchar *queried_thumbnail_path;
current_thumbnail_path = g_file_info_get_attribute_byte_string (info,
G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
queried_thumbnail_path = g_file_info_get_attribute_byte_string (queried,
G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
/* GIO may report an already-existing larger freedesktop thumbnail.
* Keep it, but never replace an existing better path with a smaller one.
*/
if (queried_thumbnail_path &&
(!current_thumbnail_path ||
thumbnail_cache_max_dimension_for_path (queried_thumbnail_path) >=
thumbnail_cache_max_dimension_for_path (current_thumbnail_path)))
copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
}
copy_attribute (info, queried, G_FILE_ATTRIBUTE_STANDARD_ICON);
copy_attribute (info, queried, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
copy_attribute (info, queried, G_FILE_ATTRIBUTE_STANDARD_SIZE);
copy_attribute (info, queried, G_FILE_ATTRIBUTE_TIME_MODIFIED);
_gtk_file_system_model_update_file (model, file, info);
g_object_unref (info);
g_object_unref (queried);
@ -11971,7 +12114,23 @@ file_system_model_render_icon (GtkFileChooserDefault *impl,
if (impl->show_thumbnails ||
!g_file_info_has_attribute (info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH))
return _gtk_file_info_render_icon (info, GTK_WIDGET (impl), icon_size);
{
const gchar *thumbnail_path;
thumbnail_path = g_file_info_get_attribute_byte_string (info,
G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
pixbuf = _gtk_file_info_render_icon (info, GTK_WIDGET (impl), icon_size);
if (thumbnail_debug_enabled ())
g_debug ("GtkFileChooser thumbnail: render-path: icon-size=%d path=%s path-dim=%d pixbuf=%dx%d",
icon_size,
thumbnail_path ? thumbnail_path : "(none)",
thumbnail_cache_max_dimension_for_path (thumbnail_path),
pixbuf ? gdk_pixbuf_get_width (pixbuf) : 0,
pixbuf ? gdk_pixbuf_get_height (pixbuf) : 0);
return pixbuf;
}
icon_info = g_file_info_dup (info);
g_file_info_remove_attribute (icon_info, G_FILE_ATTRIBUTE_THUMBNAIL_PATH);