From 5dcf681e488f09591b816ca080f5c1a04a056cd8 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Thu, 31 Mar 2016 08:52:59 -0400 Subject: [PATCH] core/file: Add cache_file_path() function Signed-off-by: Anna Schumaker --- core/file.c | 31 ++++++++++++++++++++++--------- include/core/file.h | 3 ++- tests/core/file.c | 6 ++++++ 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/core/file.c b/core/file.c index 6e088f59..6b0e1f61 100644 --- a/core/file.c +++ b/core/file.c @@ -11,9 +11,18 @@ g_printerr("%s (%s:%d): %s: %s\n", __func__, __FILE__, __LINE__, fname, error) #define REPORT_ERRNO(fname) REPORT_ERROR(fname, strerror(errno)) -static gchar *__file_path(const gchar *name) +static gchar *__file_path(const gchar *base, const gchar *dir, + const gchar *name) { - return g_strjoin("/", g_get_user_data_dir(), OCARINA_NAME, name, NULL); + return g_build_filename(base, OCARINA_NAME, dir ? dir : "", name, NULL); +} + +static gchar *__file_build_path(const gchar *base, const gchar *dir, + const gchar *name) +{ + if (string_length(name) == 0) + return g_strdup(""); + return __file_path(base, dir, name); } static FILE *__file_open(gchar *path, const gchar *mode) @@ -26,9 +35,9 @@ static FILE *__file_open(gchar *path, const gchar *mode) return ret; } -static bool __file_mkdir() +static bool __file_mkdir(const gchar *basedir) { - gchar *dir = __file_path(NULL); + gchar *dir = __file_path(basedir, NULL, NULL); int ret = g_mkdir_with_parents(dir, 0755); if (ret != 0) @@ -81,9 +90,13 @@ void cache_file_init(struct cache_file *file, gchar *subdir, gchar *name) gchar *file_path(struct file *file) { - if (string_length(file->f_name) != 0) - return __file_path(file->f_name); - return g_strdup(""); + return __file_build_path(g_get_user_data_dir(), NULL, file->f_name); +} + +gchar *cache_file_path(struct cache_file *file) +{ + return __file_build_path(g_get_user_cache_dir(), file->cf_subdir, + file->cf_name); } gchar *file_write_path(struct file *file) @@ -94,7 +107,7 @@ gchar *file_write_path(struct file *file) return g_strdup(""); fname = g_strdup_printf(".%s.tmp", file->f_name); - res = __file_path(fname); + res = __file_path(g_get_user_data_dir(), NULL, fname); g_free(fname); return res; @@ -138,7 +151,7 @@ static bool __file_open_read(struct file *file) static bool __file_open_write(struct file *file) { - if (!__file_mkdir()) + if (!__file_mkdir(g_get_user_data_dir())) return false; if (!__file_can_write(file)) return false; diff --git a/include/core/file.h b/include/core/file.h index 76fc93d7..e1dfd2f7 100644 --- a/include/core/file.h +++ b/include/core/file.h @@ -72,9 +72,10 @@ void cache_file_init(struct cache_file *, gchar *, gchar *); /* * Returns the full path of the file or an empty string if filename is not set. - * This function allocates a new string that MUST be freed with g_free(). + * These functions allocates a new string that MUST be freed with g_free(). */ gchar *file_path(struct file *); +gchar *cache_file_path(struct cache_file *); /* * Returns the path to the temporary file used for writes. diff --git a/tests/core/file.c b/tests/core/file.c index ada2cb61..1cb928e4 100644 --- a/tests/core/file.c +++ b/tests/core/file.c @@ -146,10 +146,16 @@ static void test_versioning() static void test_cache() { struct cache_file file = CACHE_FILE_INIT("dir", "file.txt"); + gchar *basepath, *filepath; + + basepath = g_strjoin("/", g_get_user_cache_dir(), OCARINA_NAME, NULL); + filepath = g_strjoin("/", basepath, "dir", "file.txt", NULL); test_equal((void *)file.cf_file, NULL); test_equal(file.cf_name, "file.txt"); test_equal(file.cf_subdir, "dir"); + + test_str_equal(cache_file_path(&file), filepath); } DECLARE_UNIT_TESTS(