core/file: Add cache_file_write_path() function

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2016-03-31 09:25:49 -04:00
parent 5dcf681e48
commit 79ecaa11fb
3 changed files with 27 additions and 12 deletions

View File

@ -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)

View File

@ -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 *);

View File

@ -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(