core/file: Create a single file_path() function
I also introduce a "file_operations" struct that will be used in the future to do slightly different things for cache and data files. Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
b6d45e666e
commit
22854b2f25
49
core/file.c
49
core/file.c
|
@ -13,11 +13,12 @@
|
|||
#define REPORT_ERRNO(fname) REPORT_ERROR(fname, strerror(errno))
|
||||
|
||||
static void __file_init_common(struct file *file, const gchar *subdir,
|
||||
const gchar *name)
|
||||
const gchar *name, const gchar *(*user_dir)(void))
|
||||
{
|
||||
file->f_file = NULL;
|
||||
file->f_name = name;
|
||||
file->f_subdir = subdir;
|
||||
file->f_file = NULL;
|
||||
file->f_name = name;
|
||||
file->f_subdir = subdir;
|
||||
file->f_user_dir = user_dir;
|
||||
}
|
||||
|
||||
static gchar *__file_path(const gchar *base, const gchar *dir,
|
||||
|
@ -26,14 +27,6 @@ static gchar *__file_path(const gchar *base, const gchar *dir,
|
|||
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 gchar *__file_build_tmp(const gchar *base, const gchar *dir,
|
||||
const gchar *name)
|
||||
{
|
||||
|
@ -91,7 +84,7 @@ static bool __file_mkdir(const gchar *basedir, const gchar *subdir)
|
|||
|
||||
static bool __file_can_write(struct file *data)
|
||||
{
|
||||
gchar *path = data_file_path(data);
|
||||
gchar *path = file_path(data);
|
||||
bool ret = true;
|
||||
|
||||
if (g_access(path, F_OK) == 0 && g_access(path, W_OK) < 0)
|
||||
|
@ -105,7 +98,8 @@ static bool __file_can_write(struct file *data)
|
|||
void file_init_data(struct file *file, const gchar *subdir,
|
||||
const gchar *name, unsigned int min)
|
||||
{
|
||||
__file_init_common(file, subdir, name);
|
||||
__file_init_common(file, subdir, name, g_get_user_data_dir);
|
||||
|
||||
file->f_data.f_mode = OPEN_READ;
|
||||
file->f_data.f_version = OCARINA_MINOR_VERSION;
|
||||
file->f_data.f_prev = 0;
|
||||
|
@ -114,18 +108,15 @@ void file_init_data(struct file *file, const gchar *subdir,
|
|||
|
||||
void file_init_cache(struct file *file, const gchar *subdir, const gchar *name)
|
||||
{
|
||||
__file_init_common(file, subdir, name);
|
||||
__file_init_common(file, subdir, name, g_get_user_cache_dir);
|
||||
}
|
||||
|
||||
gchar *data_file_path(struct file *data)
|
||||
gchar *file_path(struct file *file)
|
||||
{
|
||||
return __file_build_path(g_get_user_data_dir(), NULL, data->f_name);
|
||||
}
|
||||
|
||||
gchar *cache_file_path(struct file *cache)
|
||||
{
|
||||
return __file_build_path(g_get_user_cache_dir(), cache->f_subdir,
|
||||
cache->f_name);
|
||||
if (string_length(file->f_name) == 0)
|
||||
return g_strdup("");
|
||||
return g_build_filename(file->f_user_dir(), OCARINA_NAME,
|
||||
file->f_subdir, file->f_name, NULL);
|
||||
}
|
||||
|
||||
gchar *data_file_write_path(struct file *data)
|
||||
|
@ -149,12 +140,12 @@ const unsigned int data_file_version(struct file *data)
|
|||
|
||||
bool data_file_exists(struct file *file)
|
||||
{
|
||||
return __file_exists(data_file_path(file));
|
||||
return __file_exists(file_path(file));
|
||||
}
|
||||
|
||||
bool cache_file_exists(struct file *file)
|
||||
{
|
||||
return __file_exists(cache_file_path(file));
|
||||
return __file_exists(file_path(file));
|
||||
}
|
||||
|
||||
static bool __file_open_read(struct file *data)
|
||||
|
@ -163,7 +154,7 @@ static bool __file_open_read(struct file *data)
|
|||
if (!data_file_exists(data))
|
||||
return false;
|
||||
|
||||
data->f_file = __file_open(data_file_path(data), "r");
|
||||
data->f_file = __file_open(file_path(data), "r");
|
||||
if (!data->f_file)
|
||||
return false;
|
||||
|
||||
|
@ -226,7 +217,7 @@ void data_file_close(struct file *data)
|
|||
{
|
||||
struct data_file *file = &data->f_data;
|
||||
__file_close(data->f_file,
|
||||
file->f_mode == OPEN_WRITE ? data_file_path(data) : NULL,
|
||||
file->f_mode == OPEN_WRITE ? file_path(data) : NULL,
|
||||
file->f_mode == OPEN_WRITE ? data_file_write_path(data) : NULL);
|
||||
data->f_file = NULL;
|
||||
}
|
||||
|
@ -234,7 +225,7 @@ void data_file_close(struct file *data)
|
|||
void cache_file_close(struct file *cache)
|
||||
{
|
||||
__file_close(cache->f_file,
|
||||
cache_file_path(cache),
|
||||
file_path(cache),
|
||||
cache_file_write_path(cache));
|
||||
cache->f_file = NULL;
|
||||
}
|
||||
|
@ -303,7 +294,7 @@ bool data_file_remove(struct file *data)
|
|||
gchar *path;
|
||||
|
||||
if (!data->f_file) {
|
||||
path = data_file_path(data);
|
||||
path = file_path(data);
|
||||
ret = g_unlink(path);
|
||||
g_free(path);
|
||||
}
|
||||
|
|
|
@ -379,7 +379,7 @@ gchar *album_artwork_path(struct album *album)
|
|||
|
||||
file = __album_alloc_file(album);
|
||||
if (cache_file_exists(&file->ac_file))
|
||||
ret = cache_file_path(&file->ac_file);
|
||||
ret = file_path(&file->ac_file);
|
||||
__album_free_file(file);
|
||||
|
||||
return ret;
|
||||
|
|
|
@ -47,6 +47,7 @@ struct file {
|
|||
const gchar *f_subdir; /* The file's subdirectory. */
|
||||
|
||||
struct data_file f_data;
|
||||
const gchar *(*f_user_dir)(void); /* The file's user directory. */
|
||||
};
|
||||
|
||||
#define FILE_INIT_DATA(fdir, fname, min) \
|
||||
|
@ -54,6 +55,7 @@ struct file {
|
|||
.f_file = NULL, \
|
||||
.f_name = fname, \
|
||||
.f_subdir = fdir, \
|
||||
.f_user_dir = g_get_user_data_dir, \
|
||||
.f_data = { \
|
||||
.f_mode = OPEN_READ, \
|
||||
.f_version = OCARINA_MINOR_VERSION, \
|
||||
|
@ -68,10 +70,9 @@ void file_init_cache(struct file *, const gchar *, const gchar *);
|
|||
|
||||
/*
|
||||
* Returns the full path of the file or an empty string if filename is not set.
|
||||
* These functions 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_path(struct file *);
|
||||
gchar *cache_file_path(struct file *);
|
||||
gchar *file_path(struct file *);
|
||||
|
||||
/*
|
||||
* Returns the path to the temporary file used for writes.
|
||||
|
|
|
@ -12,7 +12,7 @@ static void test_verify_constructor(struct file *file, gchar *fpath, gchar *ftmp
|
|||
g_assert_null(file->f_file);
|
||||
g_assert_cmpuint(data_file_version(file), ==, OCARINA_MINOR_VERSION);
|
||||
g_assert_cmpuint(file->f_data.f_mode, ==, OPEN_READ);
|
||||
g_assert_cmpstr_free(data_file_path(file), ==, fpath);
|
||||
g_assert_cmpstr_free(file_path(file), ==, fpath);
|
||||
g_assert_cmpstr_free(data_file_write_path(file), ==, ftmp);
|
||||
}
|
||||
|
||||
|
@ -185,7 +185,7 @@ static void test_cache()
|
|||
g_assert_cmpstr(file.f_name, ==, "file.txt");
|
||||
g_assert_cmpstr(file.f_subdir, ==, "dir");
|
||||
|
||||
g_assert_cmpstr_free(cache_file_path(&file), ==, filepath);
|
||||
g_assert_cmpstr_free(file_path(&file), ==, filepath);
|
||||
g_assert_cmpstr_free(cache_file_write_path(&file), ==, writepath);
|
||||
|
||||
/* Test writing data to a cache file. */
|
||||
|
|
Loading…
Reference in New Issue