diff --git a/core/file.c b/core/file.c index 6b0e1f61..10ad441e 100644 --- a/core/file.c +++ b/core/file.c @@ -25,6 +25,21 @@ static gchar *__file_build_path(const gchar *base, const gchar *dir, return __file_path(base, dir, name); } +static gchar *__file_build_tmp(const gchar *base, const gchar *dir, + const gchar *name) +{ + gchar *tmp, *res; + + if (string_length(name) == 0) + return g_strdup(""); + + tmp = g_strdup_printf(".%s.tmp", name); + res = __file_path(base, dir, tmp); + g_free(tmp); + + return res; +} + static FILE *__file_open(gchar *path, const gchar *mode) { FILE *ret = g_fopen(path, mode); @@ -101,16 +116,13 @@ gchar *cache_file_path(struct cache_file *file) gchar *file_write_path(struct file *file) { - gchar *fname, *res; + return __file_build_tmp(g_get_user_data_dir(), NULL, file->f_name); +} - if (string_length(file->f_name) == 0) - return g_strdup(""); - - fname = g_strdup_printf(".%s.tmp", file->f_name); - res = __file_path(g_get_user_data_dir(), NULL, fname); - g_free(fname); - - return res; +gchar *cache_file_write_path(struct cache_file *file) +{ + return __file_build_tmp(g_get_user_cache_dir(), file->cf_subdir, + file->cf_name); } const unsigned int file_version(struct file *file) diff --git a/include/core/file.h b/include/core/file.h index e1dfd2f7..2b02a0e0 100644 --- a/include/core/file.h +++ b/include/core/file.h @@ -82,6 +82,7 @@ gchar *cache_file_path(struct cache_file *); * This function allocates a new string that MUST be freed with g_free(). */ gchar *file_write_path(struct file *); +gchar *cache_file_write_path(struct cache_file *); /* Returns the version number of the file. */ const unsigned int file_version(struct file *); diff --git a/tests/core/file.c b/tests/core/file.c index 1cb928e4..af26267b 100644 --- a/tests/core/file.c +++ b/tests/core/file.c @@ -146,16 +146,18 @@ static void test_versioning() static void test_cache() { struct cache_file file = CACHE_FILE_INIT("dir", "file.txt"); - gchar *basepath, *filepath; + gchar *basepath, *filepath, *writepath; - basepath = g_strjoin("/", g_get_user_cache_dir(), OCARINA_NAME, NULL); - filepath = g_strjoin("/", basepath, "dir", "file.txt", NULL); + basepath = g_strjoin("/", g_get_user_cache_dir(), OCARINA_NAME, NULL); + filepath = g_strjoin("/", basepath, "dir", "file.txt", NULL); + writepath = g_strjoin("/", basepath, "dir", ".file.txt.tmp", 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); + test_str_equal(cache_file_write_path(&file), writepath); } DECLARE_UNIT_TESTS(