core/file: Add cache_file_path() function
Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
b5e13af30c
commit
5dcf681e48
31
core/file.c
31
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;
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Reference in New Issue