@@ -142,21 +142,6 @@ void cache_maintenance(const gchar *path)
142
142
*-------------------------------------------------------------------
143
143
*/
144
144
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
-
160
145
static gboolean isempty (const gchar *path)
161
146
{
162
147
DIR *dp;
@@ -268,9 +253,8 @@ static gboolean cache_maintain_home_cb(gpointer data)
268
253
{
269
254
auto fd_list = static_cast <FileData *>(work->data );
270
255
gchar *path_buf = g_strdup (fd_list->path );
271
- gchar *dot;
272
256
273
- dot = extension_find_dot (path_buf);
257
+ gchar * dot = strrchr (path_buf, ' . ' );
274
258
275
259
if (dot) *dot = ' \0 ' ;
276
260
if ((!cm->metadata && cm->clear ) ||
@@ -480,121 +464,87 @@ void cache_maintain_home_remote(gboolean metadata, gboolean clear, GDestroyNotif
480
464
cm->idle_id = g_idle_add_full (G_PRIORITY_LOW, cache_maintain_home_cb, cm, func);
481
465
}
482
466
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
-
495
467
static void cache_maint_moved (FileData *fd)
496
468
{
497
- gchar *base;
498
- mode_t mode = 0755 ;
499
469
const gchar *src = fd->change ->source ;
500
470
const gchar *dest = fd->change ->dest ;
501
471
502
472
if (!src || !dest) return ;
503
473
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))
506
493
{
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);
521
496
}
522
497
else
523
498
{
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 );
525
500
}
526
- g_free (base);
527
501
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))
530
505
{
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);
539
507
}
540
- g_free (base);
541
508
542
509
if (options->thumbnails .enable_caching && options->thumbnails .spec_standard )
543
510
thumb_std_maint_moved (src, dest);
544
511
}
545
512
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
-
554
513
static void cache_maint_removed (FileData *fd)
555
514
{
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 ;
561
519
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
+ };
565
525
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 );
569
529
570
530
if (options->thumbnails .enable_caching && options->thumbnails .spec_standard )
571
531
thumb_std_maint_removed (fd->path );
572
532
}
573
533
574
534
static void cache_maint_copied (FileData *fd)
575
535
{
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 ;
579
538
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 ;
582
542
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 ))
585
545
{
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);
594
547
}
595
-
596
- g_free (dest_base);
597
- g_free (src_cache);
598
548
}
599
549
600
550
void cache_notify_cb (FileData *fd, NotifyType type, gpointer)
0 commit comments