core/file: Create a single file_write_path() function

Similar to file_path(), I rely on the file ops struct to put files in
the right place.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2018-02-16 16:22:30 -05:00
parent 22854b2f25
commit 3fdf89c75e
3 changed files with 19 additions and 31 deletions

View File

@ -27,21 +27,6 @@ static gchar *__file_path(const gchar *base, const gchar *dir,
return g_build_filename(base, OCARINA_NAME, dir ? dir : "", name, NULL); return g_build_filename(base, OCARINA_NAME, dir ? dir : "", name, NULL);
} }
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 bool __file_exists(gchar *path) static bool __file_exists(gchar *path)
{ {
bool ret = g_file_test(path, G_FILE_TEST_EXISTS); bool ret = g_file_test(path, G_FILE_TEST_EXISTS);
@ -119,15 +104,19 @@ gchar *file_path(struct file *file)
file->f_subdir, file->f_name, NULL); file->f_subdir, file->f_name, NULL);
} }
gchar *data_file_write_path(struct file *data) gchar *file_write_path(struct file *file)
{ {
return __file_build_tmp(g_get_user_data_dir(), NULL, data->f_name); gchar *tmp, *res;
}
gchar *cache_file_write_path(struct file *cache) if (string_length(file->f_name) == 0)
{ return g_strdup("");
return __file_build_tmp(g_get_user_cache_dir(), cache->f_subdir,
cache->f_name); tmp = g_strdup_printf(".%s.tmp", file->f_name);
res = g_build_filename(file->f_user_dir(), OCARINA_NAME,
file->f_subdir, tmp, NULL);
g_free(tmp);
return res;
} }
const unsigned int data_file_version(struct file *data) const unsigned int data_file_version(struct file *data)
@ -182,7 +171,7 @@ static bool __file_open_write(struct file *data)
if (!__file_can_write(data)) if (!__file_can_write(data))
return false; return false;
data->f_file = __file_open(data_file_write_path(data), "w"); data->f_file = __file_open(file_write_path(data), "w");
if (!data->f_file) if (!data->f_file)
return false; return false;
@ -209,7 +198,7 @@ bool cache_file_open(struct file *cache, enum open_mode mode)
if (!__file_mkdir(g_get_user_cache_dir(), cache->f_subdir)) if (!__file_mkdir(g_get_user_cache_dir(), cache->f_subdir))
return false; return false;
cache->f_file = __file_open(cache_file_write_path(cache), "wb"); cache->f_file = __file_open(file_write_path(cache), "wb");
return cache->f_file != NULL; return cache->f_file != NULL;
} }
@ -218,7 +207,7 @@ void data_file_close(struct file *data)
struct data_file *file = &data->f_data; struct data_file *file = &data->f_data;
__file_close(data->f_file, __file_close(data->f_file,
file->f_mode == OPEN_WRITE ? file_path(data) : NULL, file->f_mode == OPEN_WRITE ? file_path(data) : NULL,
file->f_mode == OPEN_WRITE ? data_file_write_path(data) : NULL); file->f_mode == OPEN_WRITE ? file_write_path(data) : NULL);
data->f_file = NULL; data->f_file = NULL;
} }
@ -226,7 +215,7 @@ void cache_file_close(struct file *cache)
{ {
__file_close(cache->f_file, __file_close(cache->f_file,
file_path(cache), file_path(cache),
cache_file_write_path(cache)); file_write_path(cache));
cache->f_file = NULL; cache->f_file = NULL;
} }

View File

@ -76,10 +76,9 @@ gchar *file_path(struct file *);
/* /*
* Returns the path to the temporary file used for writes. * Returns the path to the temporary file used for writes.
* This function allocates a new string that MUST be freed with g_free(). * NOTE: This function allocates a new string that MUST be freed with g_free().
*/ */
gchar *data_file_write_path(struct file *); gchar *file_write_path(struct file *);
gchar *cache_file_write_path(struct file *);
/* Returns the version number of the file. */ /* Returns the version number of the file. */
const unsigned int data_file_version(struct file *); const unsigned int data_file_version(struct file *);

View File

@ -13,7 +13,7 @@ static void test_verify_constructor(struct file *file, gchar *fpath, gchar *ftmp
g_assert_cmpuint(data_file_version(file), ==, OCARINA_MINOR_VERSION); g_assert_cmpuint(data_file_version(file), ==, OCARINA_MINOR_VERSION);
g_assert_cmpuint(file->f_data.f_mode, ==, OPEN_READ); g_assert_cmpuint(file->f_data.f_mode, ==, OPEN_READ);
g_assert_cmpstr_free(file_path(file), ==, fpath); g_assert_cmpstr_free(file_path(file), ==, fpath);
g_assert_cmpstr_free(data_file_write_path(file), ==, ftmp); g_assert_cmpstr_free(file_write_path(file), ==, ftmp);
} }
static void test_invalid_file(gconstpointer path) static void test_invalid_file(gconstpointer path)
@ -186,7 +186,7 @@ static void test_cache()
g_assert_cmpstr(file.f_subdir, ==, "dir"); g_assert_cmpstr(file.f_subdir, ==, "dir");
g_assert_cmpstr_free(file_path(&file), ==, filepath); g_assert_cmpstr_free(file_path(&file), ==, filepath);
g_assert_cmpstr_free(cache_file_write_path(&file), ==, writepath); g_assert_cmpstr_free(file_write_path(&file), ==, writepath);
/* Test writing data to a cache file. */ /* Test writing data to a cache file. */
g_assert_false(cache_file_exists(&file)); g_assert_false(cache_file_exists(&file));