From da071bcb337fe4b1c4d46c02c1a0217c0d32b042 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Fri, 15 Aug 2014 15:10:19 -0600 Subject: [PATCH] xdg-cache: Store removable media thumbnails on removable media MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This means the files don’t have to be re-thumbnailed for every computer the drive is mounted on; and conversely, each computer doesn’t have to worry about the cache lifetime of files on removable disks which it may never see again. Based on a patch by Jeremy Whiting . https://bugzilla.xfce.org/show_bug.cgi?id=12036 --- plugins/xdg-cache/xdg-cache-cache.c | 90 +++++++++++++++++++++++++++++++------ 1 file changed, 76 insertions(+), 14 deletions(-) diff --git a/plugins/xdg-cache/xdg-cache-cache.c b/plugins/xdg-cache/xdg-cache-cache.c index cd7b19c..8ce3546 100644 --- a/plugins/xdg-cache/xdg-cache-cache.c +++ b/plugins/xdg-cache/xdg-cache-cache.c @@ -557,30 +557,97 @@ xdg_cache_cache_get_flavors (TumblerCache *cache) +static gboolean +xdg_cache_is_uri_removable (const gchar *uri) +{ + GFile *file = NULL; + GMount *mount = NULL; + gboolean is_removable; + GError *error = NULL; + + file = g_file_new_for_uri (uri); + mount = g_file_find_enclosing_mount (file, NULL, &error); + + if (mount != NULL) { + is_removable = g_mount_can_eject (mount); + } else { + g_debug ("%s: Error finding enclosing mount for ‘%s’: %s", + G_STRFUNC, uri, error->message); + g_error_free (error); + + is_removable = FALSE; + } + + g_clear_object (&mount); + g_clear_object (&file); + + g_debug ("%s: Determined URI ‘%s’ %s removable.", + G_STRFUNC, uri, is_removable ? "is" : "is not"); + + return is_removable; +} + +/* Build a GFile* pointing to the cache file to use for the given + * @uri in @dirname. @cache_filename gives the computed filename to use for + * the cache file (for example, a hash). */ +static GFile * +build_cache_file_path (const gchar *uri, + const gchar *dirname, + const gchar *cache_filename) +{ + gchar *path = NULL; + GFile *file = NULL; + + if (xdg_cache_is_uri_removable (uri)) { + GFile *uri_file = NULL; + GFile *uri_parent = NULL; + gchar *parent_path = NULL; + + /* For files on a removable device, store the thumbnails on the device too. */ + uri_file = g_file_new_for_uri (uri); + uri_parent = g_file_get_parent (uri_file); + parent_path = g_file_get_path (uri_parent); + + path = g_build_filename (parent_path, ".thumbnails", dirname, cache_filename, NULL); + + g_free (parent_path); + g_object_unref (uri_parent); + g_object_unref (uri_file); + } else { + const gchar *cachedir; + + /* For files on a non-removable partition, store the thumbnails in the + * user’s cache directory. */ + cachedir = g_get_user_cache_dir (); + + path = g_build_filename (cachedir, "thumbnails", dirname, cache_filename, NULL); + } + + g_debug ("%s: Using path ‘%s’.", G_STRFUNC, path); + file = g_file_new_for_path (path); + g_free (path); + + return file; +} + GFile * xdg_cache_cache_get_file (const gchar *uri, TumblerThumbnailFlavor *flavor) { - const gchar *cachedir; const gchar *dirname; GFile *file; gchar *filename; gchar *md5_hash; - gchar *path; g_return_val_if_fail (uri != NULL && *uri != '\0', NULL); g_return_val_if_fail (TUMBLER_IS_THUMBNAIL_FLAVOR (flavor), NULL); - cachedir = g_get_user_cache_dir (); - dirname = tumbler_thumbnail_flavor_get_name (flavor); - md5_hash = g_compute_checksum_for_string (G_CHECKSUM_MD5, uri, -1); + dirname = tumbler_thumbnail_flavor_get_name (flavor); filename = g_strdup_printf ("%s.png", md5_hash); - path = g_build_filename (cachedir, "thumbnails", dirname, filename, NULL); - file = g_file_new_for_path (path); + file = build_cache_file_path (uri, dirname, filename); - g_free (path); g_free (filename); g_free (md5_hash); @@ -593,18 +660,15 @@ GFile * xdg_cache_cache_get_temp_file (const gchar *uri, TumblerThumbnailFlavor *flavor) { - const gchar *cachedir; const gchar *dirname; GTimeVal current_time = { 0, 0 }; GFile *file; gchar *filename; gchar *md5_hash; - gchar *path; g_return_val_if_fail (uri != NULL && *uri != '\0', NULL); g_return_val_if_fail (TUMBLER_IS_THUMBNAIL_FLAVOR (flavor), NULL); - cachedir = g_get_user_cache_dir (); dirname = tumbler_thumbnail_flavor_get_name (flavor); g_get_current_time (¤t_time); @@ -612,11 +676,9 @@ xdg_cache_cache_get_temp_file (const gchar *uri, md5_hash = g_compute_checksum_for_string (G_CHECKSUM_MD5, uri, -1); filename = g_strdup_printf ("%s-%ld-%ld.png", md5_hash, current_time.tv_sec, current_time.tv_usec); - path = g_build_filename (cachedir, "thumbnails", dirname, filename, NULL); - file = g_file_new_for_path (path); + file = build_cache_file_path (uri, dirname, filename); - g_free (path); g_free (filename); g_free (md5_hash); -- 2.4.3