From 9f8e6d14d94ad10decec439437c7a9e9cc50c95a Mon Sep 17 00:00:00 2001 From: Ali Abdallah Date: Thu, 15 Mar 2018 20:58:16 +0100 Subject: [PATCH] Fix xdg_cache_cache_cleanup that was causing high I/O activity bug #9231 According to the specs, 0 can be set as "since" mtime to ignore the threshold and to only cleanup based on the URI. So if 0 is received then xdg_cache_cache_cleanup will only unlink thumbnails specified in base uris. --- plugins/xdg-cache/xdg-cache-cache.c | 116 ++++++++++++++++++------------------ 1 file changed, 59 insertions(+), 57 deletions(-) diff --git a/plugins/xdg-cache/xdg-cache-cache.c b/plugins/xdg-cache/xdg-cache-cache.c index cd7b19c..bc727c2 100644 --- a/plugins/xdg-cache/xdg-cache-cache.c +++ b/plugins/xdg-cache/xdg-cache-cache.c @@ -189,78 +189,80 @@ xdg_cache_cache_cleanup (TumblerCache *cache, g_return_if_fail (XDG_CACHE_IS_CACHE (cache)); - /* iterate over all flavors */ - for (iter = xdg_cache->flavors; iter != NULL; iter = iter->next) + /* According to the spec 0 can be set to ignore the threshold and only cleanup based on the URI */ + if (since != 0) { - /* 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); - - /* attempt to open the directory for reading */ - dir = g_dir_open (dirname, 0, NULL); - if (dir != NULL) + /* iterate over all flavors */ + for (iter = xdg_cache->flavors; iter != NULL; iter = iter->next) { - /* iterate over all files in the directory */ - file_basename = g_dir_read_name (dir); - while (file_basename != NULL) + /* 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); + + /* attempt to open the directory for reading */ + dir = g_dir_open (dirname, 0, NULL); + if (dir != 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)) + /* iterate over all files in the directory */ + file_basename = g_dir_read_name (dir); + while (file_basename != NULL) { - /* check if the thumbnail information is valid or the mtime - * is too old */ - if (uri == NULL || mtime <= since) - { - /* it's invalid, so let's remove the thumbnail */ - g_unlink (filename); - } - else - { - /* 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); + /* build the thumbnail filename */ + filename = g_build_filename (dirname, file_basename, NULL); - for (n = 0; base_uris != NULL && base_uris[n] != NULL; ++n) + /* read thumbnail information from the file */ + if (xdg_cache_cache_read_thumbnail_info (filename, &uri, &mtime, + NULL, NULL)) + { + /* check if the thumbnail information is valid or the mtime + * is too old */ + if (uri == NULL || mtime <= since) { - /* create a GFile for the base URI */ - base_file = g_file_new_for_uri (base_uris[n]); - - /* delete the file if it is a descendant of the base URI */ - if (g_file_equal (original_file, base_file) - || g_file_has_prefix (original_file, base_file)) - { - g_unlink (filename); - } - - /* releas the base file */ - g_object_unref(base_file); + /* it's invalid, so let's remove the thumbnail */ + g_unlink (filename); } - - /* release the original file */ - g_object_unref (original_file); } + /* free the thumbnail filename */ + g_free (filename); + + /* try to determine the next filename in the directory */ + file_basename = g_dir_read_name (dir); } + /* close the handle used to reading from the directory */ + g_dir_close (dir); + } + /* free the thumbnail flavor directory filename */ + g_free (dirname); + } + + } + else + { + /* 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); - /* free the thumbnail filename */ - g_free (filename); + for (n = 0; base_uris != NULL && base_uris[n] != NULL; ++n) + { + /* create a GFile for the base URI */ + base_file = g_file_new_for_uri (base_uris[n]); - /* try to determine the next filename in the directory */ - file_basename = g_dir_read_name (dir); + /* delete the file if it is a descendant of the base URI */ + if (g_file_equal (original_file, base_file) + || g_file_has_prefix (original_file, base_file)) + { + g_unlink (filename); } - /* close the handle used to reading from the directory */ - g_dir_close (dir); + /* releas the base file */ + g_object_unref(base_file); } - /* free the thumbnail flavor directory filename */ - g_free (dirname); + /* release the original file */ + g_object_unref (original_file); } } -- 2.11.0