Skip to content

Commit 998829a

Browse files
qarkaicaclark
authored andcommitted
Add wrappers for cache_get_location()
cache_get_location() is called for two reasons: 1) get cache directory and immediately create it. 2) get cache filename. Add cache_create_location() for (1). Add cache_get_location() wrapper for (2). This allows to remove mode_t from parameters. Move old cache_get_location() implementation to anonymous namespace. Fix IWYU warnings after removing dependency on mode_t.
1 parent 8cdab99 commit 998829a

18 files changed

+127
-134
lines changed

geeqie.imp

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[
22
{ include: [ "<bits/types/sig_atomic_t.h>", private, "<csignal>", public ] },
3+
{ include: [ "<__stdarg_va_arg.h>", private, "<cstdarg>", public ] },
34
{ include: [ "<bits/types/struct_FILE.h>", private, "<cstdio>", public ] },
45
{ include: [ "<bits/std_abs.h>", private, "<cstdlib>", public ] },
56

src/cache-loader.cc

+3-9
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121

2222
#include "cache-loader.h"
2323

24-
#include <sys/types.h>
25-
2624
#include <cstdio>
2725
#include <cstring>
2826
#include <ctime>
@@ -186,20 +184,16 @@ static gboolean cache_loader_phase2_process(CacheLoader *cl)
186184
if (options->thumbnails.enable_caching &&
187185
cl->done_mask != CACHE_LOADER_NONE)
188186
{
189-
gchar *base;
190-
mode_t mode = 0755;
191-
192-
base = cache_get_location(CACHE_TYPE_SIM, cl->fd->path, FALSE, &mode);
193-
if (recursive_mkdir_if_not_exists(base, mode))
187+
g_autofree gchar *base = cache_create_location(CACHE_TYPE_SIM, cl->fd->path);
188+
if (base)
194189
{
195190
g_free(cl->cd->path);
196-
cl->cd->path = cache_get_location(CACHE_TYPE_SIM, cl->fd->path, TRUE, nullptr);
191+
cl->cd->path = cache_get_location(CACHE_TYPE_SIM, cl->fd->path);
197192
if (cache_sim_data_save(cl->cd))
198193
{
199194
filetime_set(cl->cd->path, filetime(cl->fd->path));
200195
}
201196
}
202-
g_free(base);
203197
}
204198

205199
cl->idle_id = 0;

src/cache-maint.cc

+8-11
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "cache-maint.h"
2323

2424
#include <dirent.h>
25-
#include <sys/types.h>
2625

2726
#include <cstdlib>
2827
#include <cstring>
@@ -476,7 +475,7 @@ static void cache_maint_moved(FileData *fd)
476475
g_autofree gchar *src_path = cache_find_location(cache_type, src);
477476
if (!src_path || !isfile(src_path)) return;
478477

479-
g_autofree gchar *dest_path = cache_get_location(cache_type, dest, TRUE, nullptr);
478+
g_autofree gchar *dest_path = cache_get_location(cache_type, dest);
480479
if (!dest_path) return;
481480

482481
if (!move_file(src_path, dest_path))
@@ -487,9 +486,8 @@ static void cache_maint_moved(FileData *fd)
487486
}
488487
};
489488

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))
489+
g_autofree gchar *dest_base = cache_create_location(CACHE_TYPE_THUMB, dest);
490+
if (dest_base)
493491
{
494492
cache_move(CACHE_TYPE_THUMB);
495493
cache_move(CACHE_TYPE_SIM);
@@ -500,8 +498,8 @@ static void cache_maint_moved(FileData *fd)
500498
}
501499

502500
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))
501+
dest_base = cache_create_location(CACHE_TYPE_METADATA, dest);
502+
if (dest_base)
505503
{
506504
cache_move(CACHE_TYPE_METADATA);
507505
}
@@ -536,11 +534,10 @@ static void cache_maint_copied(FileData *fd)
536534
g_autofree gchar *src_path = cache_find_location(CACHE_TYPE_METADATA, fd->change->source);
537535
if (!src_path) return;
538536

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;
537+
g_autofree gchar *dest_base = cache_create_location(CACHE_TYPE_METADATA, fd->change->dest);
538+
if (!dest_base) return;
542539

543-
g_autofree gchar *dest_path = cache_get_location(CACHE_TYPE_METADATA, fd->change->dest, TRUE, nullptr);
540+
g_autofree gchar *dest_path = cache_get_location(CACHE_TYPE_METADATA, fd->change->dest);
544541
if (!copy_file(src_path, dest_path))
545542
{
546543
DEBUG_1("failed to copy metadata %s to %s", src_path, dest_path);

src/cache.cc

+90-83
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,90 @@
6666
namespace
6767
{
6868

69+
struct CachePathParts
70+
{
71+
CachePathParts(CacheType type)
72+
{
73+
switch (type)
74+
{
75+
case CACHE_TYPE_THUMB:
76+
rc = get_thumbnails_cache_dir();
77+
local = GQ_CACHE_LOCAL_THUMB;
78+
ext = GQ_CACHE_EXT_THUMB;
79+
break;
80+
case CACHE_TYPE_SIM:
81+
rc = get_thumbnails_cache_dir();
82+
local = GQ_CACHE_LOCAL_THUMB;
83+
ext = GQ_CACHE_EXT_SIM;
84+
break;
85+
case CACHE_TYPE_METADATA:
86+
rc = get_metadata_cache_dir();
87+
local = GQ_CACHE_LOCAL_METADATA;
88+
ext = GQ_CACHE_EXT_METADATA;
89+
break;
90+
case CACHE_TYPE_XMP_METADATA:
91+
rc = get_metadata_cache_dir();
92+
local = GQ_CACHE_LOCAL_METADATA;
93+
ext = GQ_CACHE_EXT_XMP_METADATA;
94+
break;
95+
}
96+
}
97+
98+
gchar *build_path_local(const gchar *source) const
99+
{
100+
g_autofree gchar *base = remove_level_from_path(source);
101+
g_autofree gchar *name = g_strconcat(filename_from_path(source), ext, nullptr);
102+
103+
return g_build_filename(base, local, name, nullptr);
104+
}
105+
106+
gchar *build_path_rc(const gchar *source) const
107+
{
108+
g_autofree gchar *name = g_strconcat(source, ext, nullptr);
109+
110+
return g_build_filename(rc, name, nullptr);
111+
}
112+
113+
const gchar *rc = nullptr;
114+
const gchar *local = nullptr;
115+
const gchar *ext = nullptr;
116+
};
117+
69118
constexpr gint CACHE_LOAD_LINE_NOISE = 8;
70119

120+
gchar *cache_get_location(CacheType type, const gchar *source, gint include_name, mode_t *mode)
121+
{
122+
if (!source) return nullptr;
123+
124+
g_autofree gchar *base = remove_level_from_path(source);
125+
126+
const CachePathParts cache{type};
127+
128+
g_autofree gchar *name = nullptr;
129+
if (include_name)
130+
{
131+
name = g_strconcat(filename_from_path(source), cache.ext, NULL);
132+
}
133+
134+
gchar *path = nullptr;
135+
136+
if (((type != CACHE_TYPE_METADATA && type != CACHE_TYPE_XMP_METADATA && options->thumbnails.cache_into_dirs) ||
137+
((type == CACHE_TYPE_METADATA || type == CACHE_TYPE_XMP_METADATA) && options->metadata.enable_metadata_dirs)) &&
138+
access_file(base, W_OK))
139+
{
140+
path = g_build_filename(base, cache.local, name, NULL);
141+
if (mode) *mode = 0775;
142+
}
143+
144+
if (!path)
145+
{
146+
path = g_build_filename(cache.rc, base, name, NULL);
147+
if (mode) *mode = 0755;
148+
}
149+
150+
return path;
151+
}
152+
71153
} // namespace
72154

73155
/*
@@ -542,94 +624,19 @@ gboolean cache_sim_data_filled(ImageSimilarityData *sd)
542624
*-------------------------------------------------------------------
543625
*/
544626

545-
struct CachePathParts
627+
gchar *cache_create_location(CacheType cache_type, const gchar *source)
546628
{
547-
CachePathParts(CacheType type)
548-
{
549-
switch (type)
550-
{
551-
case CACHE_TYPE_THUMB:
552-
rc = get_thumbnails_cache_dir();
553-
local = GQ_CACHE_LOCAL_THUMB;
554-
ext = GQ_CACHE_EXT_THUMB;
555-
break;
556-
case CACHE_TYPE_SIM:
557-
rc = get_thumbnails_cache_dir();
558-
local = GQ_CACHE_LOCAL_THUMB;
559-
ext = GQ_CACHE_EXT_SIM;
560-
break;
561-
case CACHE_TYPE_METADATA:
562-
rc = get_metadata_cache_dir();
563-
local = GQ_CACHE_LOCAL_METADATA;
564-
ext = GQ_CACHE_EXT_METADATA;
565-
break;
566-
case CACHE_TYPE_XMP_METADATA:
567-
rc = get_metadata_cache_dir();
568-
local = GQ_CACHE_LOCAL_METADATA;
569-
ext = GQ_CACHE_EXT_XMP_METADATA;
570-
break;
571-
}
572-
}
573-
574-
gchar *build_path_local(const gchar *source) const
575-
{
576-
gchar *base = remove_level_from_path(source);
577-
gchar *name = g_strconcat(filename_from_path(source), ext, nullptr);
578-
gchar *path = g_build_filename(base, local, name, nullptr);
579-
g_free(name);
580-
g_free(base);
581-
582-
return path;
583-
}
584-
585-
gchar *build_path_rc(const gchar *source) const
586-
{
587-
gchar *name = g_strconcat(source, ext, nullptr);
588-
gchar *path = g_build_filename(rc, name, nullptr);
589-
g_free(name);
629+
mode_t mode = 0755;
630+
g_autofree gchar *path = cache_get_location(cache_type, source, FALSE, &mode);
590631

591-
return path;
592-
}
632+
if (!recursive_mkdir_if_not_exists(path, mode)) return nullptr;
593633

594-
const gchar *rc = nullptr;
595-
const gchar *local = nullptr;
596-
const gchar *ext = nullptr;
597-
};
634+
return g_steal_pointer(&path);
635+
}
598636

599-
gchar *cache_get_location(CacheType type, const gchar *source, gint include_name, mode_t *mode)
637+
gchar *cache_get_location(CacheType cache_type, const gchar *source)
600638
{
601-
gchar *path = nullptr;
602-
gchar *base;
603-
gchar *name = nullptr;
604-
605-
if (!source) return nullptr;
606-
607-
const CachePathParts cache{type};
608-
609-
base = remove_level_from_path(source);
610-
if (include_name)
611-
{
612-
name = g_strconcat(filename_from_path(source), cache.ext, NULL);
613-
}
614-
615-
if (((type != CACHE_TYPE_METADATA && type != CACHE_TYPE_XMP_METADATA && options->thumbnails.cache_into_dirs) ||
616-
((type == CACHE_TYPE_METADATA || type == CACHE_TYPE_XMP_METADATA) && options->metadata.enable_metadata_dirs)) &&
617-
access_file(base, W_OK))
618-
{
619-
path = g_build_filename(base, cache.local, name, NULL);
620-
if (mode) *mode = 0775;
621-
}
622-
623-
if (!path)
624-
{
625-
path = g_build_filename(cache.rc, base, name, NULL);
626-
if (mode) *mode = 0755;
627-
}
628-
629-
g_free(base);
630-
if (name) g_free(name);
631-
632-
return path;
639+
return cache_get_location(cache_type, source, TRUE, nullptr);
633640
}
634641

635642
gchar *cache_find_location(CacheType type, const gchar *source)

src/cache.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ void cache_sim_data_set_md5sum(CacheData *cd, const guchar digest[16]);
7676
void cache_sim_data_set_similarity(CacheData *cd, ImageSimilarityData *sd);
7777
gint cache_sim_data_filled(ImageSimilarityData *sd);
7878

79-
gchar *cache_get_location(CacheType type, const gchar *source, gint include_name, mode_t *mode);
79+
gchar *cache_create_location(CacheType cache_type, const gchar *source);
80+
gchar *cache_get_location(CacheType cache_type, const gchar *source);
8081
gchar *cache_find_location(CacheType type, const gchar *source);
8182

8283
const gchar *get_thumbnails_cache_dir();

src/dupe.cc

+3-8
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "dupe.h"
2323

2424
#include <sys/time.h>
25-
#include <sys/types.h>
2625

2726
#include <array>
2827
#include <cinttypes>
@@ -526,18 +525,15 @@ static void dupe_item_read_cache(DupeItem *di)
526525

527526
static void dupe_item_write_cache(DupeItem *di)
528527
{
529-
gchar *base;
530-
mode_t mode = 0755;
531-
532528
if (!di) return;
533529

534-
base = cache_get_location(CACHE_TYPE_SIM, di->fd->path, FALSE, &mode);
535-
if (recursive_mkdir_if_not_exists(base, mode))
530+
g_autofree gchar *base = cache_create_location(CACHE_TYPE_SIM, di->fd->path);
531+
if (base)
536532
{
537533
CacheData *cd;
538534

539535
cd = cache_sim_data_new();
540-
cd->path = cache_get_location(CACHE_TYPE_SIM, di->fd->path, TRUE, nullptr);
536+
cd->path = cache_get_location(CACHE_TYPE_SIM, di->fd->path);
541537

542538
if (di->width != 0) cache_sim_data_set_dimensions(cd, di->width, di->height);
543539
if (di->md5sum)
@@ -553,7 +549,6 @@ static void dupe_item_write_cache(DupeItem *di)
553549
}
554550
cache_sim_data_free(cd);
555551
}
556-
g_free(base);
557552
}
558553

559554
/*

src/filedata/filedata.cc

+2-4
Original file line numberDiff line numberDiff line change
@@ -2185,10 +2185,8 @@ gint FileData::file_data_verify_ci(FileData *fd, GList *list)
21852185

21862186
if (!metadata_path)
21872187
{
2188-
mode_t mode = 0755;
2189-
2190-
dest_dir = cache_get_location(CACHE_TYPE_METADATA, fd->path, FALSE, &mode);
2191-
if (recursive_mkdir_if_not_exists(dest_dir, mode))
2188+
dest_dir = cache_create_location(CACHE_TYPE_METADATA, fd->path);
2189+
if (dest_dir)
21922190
{
21932191
gchar *filename = g_strconcat(fd->name, options->metadata.save_legacy_format ? GQ_CACHE_EXT_METADATA : GQ_CACHE_EXT_XMP_METADATA, NULL);
21942192

src/image-load-libraw.cc

+2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
#include <sys/stat.h>
3939
#include <unistd.h>
4040

41+
#include <cstddef>
42+
4143
#include <libraw/libraw.h>
4244

4345
#include "debug.h"

src/img-view.cc

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
#include "img-view.h"
2323

24+
#include <array>
25+
2426
#include <gdk/gdk.h>
2527
#include <glib-object.h>
2628
#include <gtk/gtk.h>

src/layout-image.cc

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "layout-image.h"
2323

24+
#include <array>
2425
#include <cstring>
2526

2627
#include <gdk-pixbuf/gdk-pixbuf.h>

src/main.cc

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include "main.h"
2323

24+
#include <sys/types.h>
2425
#include <unistd.h>
2526

2627
#include <clocale>

src/pan-view/pan-view.cc

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "pan-view.h"
2323

2424
#include <algorithm>
25+
#include <array>
2526
#include <cmath>
2627
#include <cstring>
2728

src/preferences.cc

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <algorithm>
2525
#include <cstdlib>
2626
#include <cstring>
27+
#include <vector>
2728

2829
#include <config.h>
2930

0 commit comments

Comments
 (0)