From cbdcdf28dd5fa1b49b0f919356325881036c7c81 Mon Sep 17 00:00:00 2001 From: Philip Withnall Date: Tue, 7 Jul 2015 18:24:17 +0100 Subject: [PATCH 1/4] xdg-cache: Factor out directory copy-or-move function This introduces no functional changes but will make later refactoring simpler. https://bugzilla.xfce.org/show_bug.cgi?id=12036 --- plugins/xdg-cache/xdg-cache-cache.c | 152 +++++++++++++++++++----------------- 1 file changed, 82 insertions(+), 70 deletions(-) diff --git a/plugins/xdg-cache/xdg-cache-cache.c b/plugins/xdg-cache/xdg-cache-cache.c index cd7b19c..f5c2a10 100644 --- a/plugins/xdg-cache/xdg-cache-cache.c +++ b/plugins/xdg-cache/xdg-cache-cache.c @@ -356,7 +356,86 @@ xdg_cache_cache_copy_or_move_file (TumblerCache *cache, g_object_unref (from_file); } +static void +xdg_cache_cache_copy_or_move_directory (TumblerCache *cache, + TumblerThumbnailFlavor *flavor, + gboolean do_copy, + const gchar *from_uri, + const gchar *to_uri) +{ + GFile *dummy_file; + GFile *parent; + gchar *dirname; + GDir *dir; + const gchar *file_basename; + gchar *filename; + gchar *uri; + GFile *original_file; + GFile *base_file; + gchar *file_to_uri; + guint64 mtime; + /* compute the flavor directory filename */ + dummy_file = xdg_cache_cache_get_file ("foo", flavor); + parent = g_file_get_parent (dummy_file); + dirname = g_file_get_path (parent); + g_object_unref (parent); + g_object_unref (dummy_file); + + /* the base path */ + base_file = g_file_new_for_uri (from_uri); + + /* attempt to open the directory for reading */ + dir = g_dir_open (dirname, 0, NULL); + if (dir != NULL) + { + /* iterate over all files in the directory */ + file_basename = g_dir_read_name (dir); + while (file_basename != NULL) + { + /* build the thumbnail filename */ + filename = g_build_filename (dirname, file_basename, NULL); + + /* read thumbnail information from the file */ + if (xdg_cache_cache_read_thumbnail_info (filename, &uri, &mtime, NULL, NULL) + && uri != NULL) + { + /* create a GFile for the original URI. we need this for + * reliably checking the ancestor/descendant relationship */ + original_file = g_file_new_for_uri (uri); + + /* check if we have a thumbnail that is located in the moved/copied folder */ + if (g_file_equal (original_file, base_file) + || g_file_has_prefix (original_file, base_file)) + { + /* build the new target (replace old base with new base) */ + file_to_uri = g_build_filename (to_uri, uri + strlen (from_uri), NULL); + + /* move or copy the thumbnail */ + xdg_cache_cache_copy_or_move_file (cache, flavor, + do_copy, + uri, file_to_uri, + mtime); + + g_free (file_to_uri); + } + + g_object_unref (original_file); + g_free (uri); + } + + g_free (filename); + + /* try to determine the next filename in the directory */ + file_basename = g_dir_read_name (dir); + } + + g_dir_close (dir); + } + + g_free (dirname); + g_object_unref (base_file); +} static void xdg_cache_cache_copy_or_move (TumblerCache *cache, @@ -370,16 +449,6 @@ xdg_cache_cache_copy_or_move (TumblerCache *cache, GFile *dest_source_file; GList *iter; guint n; - GFile *dummy_file; - GFile *parent; - gchar *dirname; - GDir *dir; - const gchar *file_basename; - gchar *filename; - gchar *uri; - GFile *original_file; - GFile *base_file; - gchar *to_uri; g_return_if_fail (XDG_CACHE_IS_CACHE (cache)); g_return_if_fail (from_uris != NULL); @@ -403,66 +472,9 @@ xdg_cache_cache_copy_or_move (TumblerCache *cache, if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY) { - /* compute the flavor directory filename */ - dummy_file = xdg_cache_cache_get_file ("foo", iter->data); - parent = g_file_get_parent (dummy_file); - dirname = g_file_get_path (parent); - g_object_unref (parent); - g_object_unref (dummy_file); - - /* the base path */ - base_file = g_file_new_for_uri (from_uris[n]); - - /* attempt to open the directory for reading */ - dir = g_dir_open (dirname, 0, NULL); - if (dir != NULL) - { - /* iterate over all files in the directory */ - file_basename = g_dir_read_name (dir); - while (file_basename != NULL) - { - /* build the thumbnail filename */ - filename = g_build_filename (dirname, file_basename, NULL); - - /* read thumbnail information from the file */ - if (xdg_cache_cache_read_thumbnail_info (filename, &uri, &mtime, NULL, NULL) - && uri != NULL) - { - /* create a GFile for the original URI. we need this for - * reliably checking the ancestor/descendant relationship */ - original_file = g_file_new_for_uri (uri); - - /* check if we have a thumbnail that is located in the moved/copied folder */ - if (g_file_equal (original_file, base_file) - || g_file_has_prefix (original_file, base_file)) - { - /* build the new target (replace old base with new base) */ - to_uri = g_build_filename (to_uris[n], uri + strlen (from_uris[n]), NULL); - - /* move or copy the thumbnail */ - xdg_cache_cache_copy_or_move_file (cache, iter->data, - do_copy, - uri, to_uri, - mtime); - - g_free (to_uri); - } - - g_object_unref (original_file); - g_free (uri); - } - - g_free (filename); - - /* try to determine the next filename in the directory */ - file_basename = g_dir_read_name (dir); - } - - g_dir_close (dir); - } - - g_free (dirname); - g_object_unref (base_file); + xdg_cache_cache_copy_or_move_directory (cache, iter->data, + do_copy, from_uris[n], + to_uris[n]); } else { -- 2.4.3