Skip to content

Commit 8cdab99

Browse files
qarkaicaclark
authored andcommitted
Simplify cache maintenance a bit
Replace extension_find_dot() with strrchr(). Deduplicate cache_maint_removed() and inline cache_file_remove(). Deduplicate cache_maint_moved() and inline cache_file_move(). Flatten cache_maint_copied(). Unify names of variables.
1 parent 29a277a commit 8cdab99

File tree

1 file changed

+47
-97
lines changed

1 file changed

+47
-97
lines changed

src/cache-maint.cc

+47-97
Original file line numberDiff line numberDiff line change
@@ -142,21 +142,6 @@ void cache_maintenance(const gchar *path)
142142
*-------------------------------------------------------------------
143143
*/
144144

145-
static gchar *extension_find_dot(gchar *path)
146-
{
147-
gchar *dot = nullptr;
148-
149-
if (!path) return nullptr;
150-
151-
while (*path != '\0')
152-
{
153-
if (*path == '.') dot = path;
154-
path++;
155-
}
156-
157-
return dot;
158-
}
159-
160145
static gboolean isempty(const gchar *path)
161146
{
162147
DIR *dp;
@@ -268,9 +253,8 @@ static gboolean cache_maintain_home_cb(gpointer data)
268253
{
269254
auto fd_list = static_cast<FileData *>(work->data);
270255
gchar *path_buf = g_strdup(fd_list->path);
271-
gchar *dot;
272256

273-
dot = extension_find_dot(path_buf);
257+
gchar *dot = strrchr(path_buf, '.');
274258

275259
if (dot) *dot = '\0';
276260
if ((!cm->metadata && cm->clear) ||
@@ -480,121 +464,87 @@ void cache_maintain_home_remote(gboolean metadata, gboolean clear, GDestroyNotif
480464
cm->idle_id = g_idle_add_full(G_PRIORITY_LOW, cache_maintain_home_cb, cm, func);
481465
}
482466

483-
static void cache_file_move(const gchar *src, const gchar *dest)
484-
{
485-
if (!dest || !src || !isfile(src)) return;
486-
487-
if (!move_file(src, dest))
488-
{
489-
DEBUG_1("Failed to move cache file \"%s\" to \"%s\"", src, dest);
490-
/* we remove it anyway - it's stale */
491-
unlink_file(src);
492-
}
493-
}
494-
495467
static void cache_maint_moved(FileData *fd)
496468
{
497-
gchar *base;
498-
mode_t mode = 0755;
499469
const gchar *src = fd->change->source;
500470
const gchar *dest = fd->change->dest;
501471

502472
if (!src || !dest) return;
503473

504-
base = cache_get_location(CACHE_TYPE_THUMB, dest, FALSE, &mode);
505-
if (recursive_mkdir_if_not_exists(base, mode))
474+
const auto cache_move = [src, dest](CacheType cache_type)
475+
{
476+
g_autofree gchar *src_path = cache_find_location(cache_type, src);
477+
if (!src_path || !isfile(src_path)) return;
478+
479+
g_autofree gchar *dest_path = cache_get_location(cache_type, dest, TRUE, nullptr);
480+
if (!dest_path) return;
481+
482+
if (!move_file(src_path, dest_path))
483+
{
484+
DEBUG_1("Failed to move cache file \"%s\" to \"%s\"", src_path, dest_path);
485+
/* we remove it anyway - it's stale */
486+
unlink_file(src_path);
487+
}
488+
};
489+
490+
mode_t mode = 0755;
491+
g_autofree gchar *dest_base = cache_get_location(CACHE_TYPE_THUMB, dest, FALSE, &mode);
492+
if (recursive_mkdir_if_not_exists(dest_base, mode))
506493
{
507-
gchar *buf;
508-
gchar *d;
509-
510-
buf = cache_find_location(CACHE_TYPE_THUMB, src);
511-
d = cache_get_location(CACHE_TYPE_THUMB, dest, TRUE, nullptr);
512-
cache_file_move(buf, d);
513-
g_free(d);
514-
g_free(buf);
515-
516-
buf = cache_find_location(CACHE_TYPE_SIM, src);
517-
d = cache_get_location(CACHE_TYPE_SIM, dest, TRUE, nullptr);
518-
cache_file_move(buf, d);
519-
g_free(d);
520-
g_free(buf);
494+
cache_move(CACHE_TYPE_THUMB);
495+
cache_move(CACHE_TYPE_SIM);
521496
}
522497
else
523498
{
524-
log_printf("Failed to create cache dir for move %s\n", base);
499+
log_printf("Failed to create cache dir for move %s\n", dest_base);
525500
}
526-
g_free(base);
527501

528-
base = cache_get_location(CACHE_TYPE_METADATA, dest, FALSE, &mode);
529-
if (recursive_mkdir_if_not_exists(base, mode))
502+
g_free(dest_base);
503+
dest_base = cache_get_location(CACHE_TYPE_METADATA, dest, FALSE, &mode);
504+
if (recursive_mkdir_if_not_exists(dest_base, mode))
530505
{
531-
gchar *buf;
532-
gchar *d;
533-
534-
buf = cache_find_location(CACHE_TYPE_METADATA, src);
535-
d = cache_get_location(CACHE_TYPE_METADATA, dest, TRUE, nullptr);
536-
cache_file_move(buf, d);
537-
g_free(d);
538-
g_free(buf);
506+
cache_move(CACHE_TYPE_METADATA);
539507
}
540-
g_free(base);
541508

542509
if (options->thumbnails.enable_caching && options->thumbnails.spec_standard)
543510
thumb_std_maint_moved(src, dest);
544511
}
545512

546-
static void cache_file_remove(const gchar *path)
547-
{
548-
if (path && isfile(path) && !unlink_file(path))
549-
{
550-
DEBUG_1("Failed to remove cache file %s", path);
551-
}
552-
}
553-
554513
static void cache_maint_removed(FileData *fd)
555514
{
556-
gchar *buf;
557-
558-
buf = cache_find_location(CACHE_TYPE_THUMB, fd->path);
559-
cache_file_remove(buf);
560-
g_free(buf);
515+
const auto cache_remove = [fd](CacheType cache_type)
516+
{
517+
g_autofree gchar *path = cache_find_location(cache_type, fd->path);
518+
if (!path || !isfile(path)) return;
561519

562-
buf = cache_find_location(CACHE_TYPE_SIM, fd->path);
563-
cache_file_remove(buf);
564-
g_free(buf);
520+
if (!unlink_file(path))
521+
{
522+
DEBUG_1("Failed to remove cache file %s", path);
523+
}
524+
};
565525

566-
buf = cache_find_location(CACHE_TYPE_METADATA, fd->path);
567-
cache_file_remove(buf);
568-
g_free(buf);
526+
cache_remove(CACHE_TYPE_THUMB);
527+
cache_remove(CACHE_TYPE_SIM);
528+
cache_remove(CACHE_TYPE_METADATA);
569529

570530
if (options->thumbnails.enable_caching && options->thumbnails.spec_standard)
571531
thumb_std_maint_removed(fd->path);
572532
}
573533

574534
static void cache_maint_copied(FileData *fd)
575535
{
576-
gchar *dest_base;
577-
gchar *src_cache;
578-
mode_t mode = 0755;
536+
g_autofree gchar *src_path = cache_find_location(CACHE_TYPE_METADATA, fd->change->source);
537+
if (!src_path) return;
579538

580-
src_cache = cache_find_location(CACHE_TYPE_METADATA, fd->change->source);
581-
if (!src_cache) return;
539+
mode_t mode = 0755;
540+
g_autofree gchar *dest_base = cache_get_location(CACHE_TYPE_METADATA, fd->change->dest, FALSE, &mode);
541+
if (!recursive_mkdir_if_not_exists(dest_base, mode)) return;
582542

583-
dest_base = cache_get_location(CACHE_TYPE_METADATA, fd->change->dest, FALSE, &mode);
584-
if (recursive_mkdir_if_not_exists(dest_base, mode))
543+
g_autofree gchar *dest_path = cache_get_location(CACHE_TYPE_METADATA, fd->change->dest, TRUE, nullptr);
544+
if (!copy_file(src_path, dest_path))
585545
{
586-
gchar *path;
587-
588-
path = cache_get_location(CACHE_TYPE_METADATA, fd->change->dest, TRUE, nullptr);
589-
if (!copy_file(src_cache, path))
590-
{
591-
DEBUG_1("failed to copy metadata %s to %s", src_cache, path);
592-
}
593-
g_free(path);
546+
DEBUG_1("failed to copy metadata %s to %s", src_path, dest_path);
594547
}
595-
596-
g_free(dest_base);
597-
g_free(src_cache);
598548
}
599549

600550
void cache_notify_cb(FileData *fd, NotifyType type, gpointer)

0 commit comments

Comments
 (0)