core/file: Move common items out of cache and data file structs

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2018-02-16 15:02:28 -05:00
parent 198fbf7f9b
commit 842547d735
4 changed files with 45 additions and 56 deletions

View File

@ -101,48 +101,46 @@ void data_file_init(struct file *data, const gchar *name, unsigned int min)
file->f_version = OCARINA_MINOR_VERSION;
file->f_prev = 0;
file->f_min = min;
file->f_file = NULL;
file->f_name = name;
data->f_file = NULL;
data->f_name = name;
}
void cache_file_init(struct file *cache, const gchar *subdir, const gchar *name)
{
struct cache_file *file = &cache->f_cache;
file->cf_file = NULL;
file->cf_name = name;
cache->f_file = NULL;
cache->f_name = name;
file->cf_subdir = subdir;
}
gchar *data_file_path(struct file *data)
{
struct data_file *file = &data->f_data;
return __file_build_path(g_get_user_data_dir(), NULL, file->f_name);
return __file_build_path(g_get_user_data_dir(), NULL, data->f_name);
}
gchar *cache_file_path(struct file *cache)
{
struct cache_file *file = &cache->f_cache;
return __file_build_path(g_get_user_cache_dir(), file->cf_subdir,
file->cf_name);
cache->f_name);
}
gchar *data_file_write_path(struct file *data)
{
struct data_file *file = &data->f_data;
return __file_build_tmp(g_get_user_data_dir(), NULL, file->f_name);
return __file_build_tmp(g_get_user_data_dir(), NULL, data->f_name);
}
gchar *cache_file_write_path(struct file *cache)
{
struct cache_file *file = &cache->f_cache;
return __file_build_tmp(g_get_user_cache_dir(), file->cf_subdir,
file->cf_name);
cache->f_name);
}
const unsigned int data_file_version(struct file *data)
{
struct data_file *file = &data->f_data;
if (file->f_file && (file->f_mode == OPEN_READ))
if (data->f_file && (file->f_mode == OPEN_READ))
return file->f_prev;
return file->f_version;
}
@ -163,20 +161,20 @@ static bool __file_open_read(struct file *data)
if (!data_file_exists(data))
return false;
file->f_file = __file_open(data_file_path(data), "r");
if (!file->f_file)
data->f_file = __file_open(data_file_path(data), "r");
if (!data->f_file)
return false;
file->f_mode = OPEN_READ;
if (data_file_readf(data, "%u\n", &file->f_prev) != 1)
return false;
if (file->f_prev < file->f_min) {
REPORT_ERROR(file->f_name, "File too old to be upgraded.");
REPORT_ERROR(data->f_name, "File too old to be upgraded.");
data_file_close(data);
exit(1);
}
if (file->f_prev > file->f_version) {
REPORT_ERROR(file->f_name, "File too new to be opened.");
REPORT_ERROR(data->f_name, "File too new to be opened.");
data_file_close(data);
exit(1);
}
@ -191,8 +189,8 @@ static bool __file_open_write(struct file *data)
if (!__file_can_write(data))
return false;
file->f_file = __file_open(data_file_write_path(data), "w");
if (!file->f_file)
data->f_file = __file_open(data_file_write_path(data), "w");
if (!data->f_file)
return false;
file->f_mode = OPEN_WRITE;
@ -201,8 +199,7 @@ static bool __file_open_write(struct file *data)
bool data_file_open(struct file *data, enum open_mode mode)
{
struct data_file *file = &data->f_data;
if ((string_length(file->f_name) == 0) || (file->f_file != NULL))
if ((string_length(data->f_name) == 0) || (data->f_file != NULL))
return false;
if (mode == OPEN_READ)
@ -215,41 +212,39 @@ bool cache_file_open(struct file *cache, enum open_mode mode)
struct cache_file *file = &cache->f_cache;
if (mode == OPEN_READ)
return false;
if ((string_length(file->cf_name) == 0) || (file->cf_file != NULL))
if ((string_length(cache->f_name) == 0) || (cache->f_file != NULL))
return false;
if (!__file_mkdir(g_get_user_cache_dir(), file->cf_subdir))
return false;
file->cf_file = __file_open(cache_file_write_path(cache), "wb");
return file->cf_file != NULL;
cache->f_file = __file_open(cache_file_write_path(cache), "wb");
return cache->f_file != NULL;
}
void data_file_close(struct file *data)
{
struct data_file *file = &data->f_data;
__file_close(file->f_file,
__file_close(data->f_file,
file->f_mode == OPEN_WRITE ? data_file_path(data) : NULL,
file->f_mode == OPEN_WRITE ? data_file_write_path(data) : NULL);
file->f_file = NULL;
data->f_file = NULL;
}
void cache_file_close(struct file *cache)
{
struct cache_file *file = &cache->f_cache;
__file_close(file->cf_file,
__file_close(cache->f_file,
cache_file_path(cache),
cache_file_write_path(cache));
file->cf_file = NULL;
cache->f_file = NULL;
}
int data_file_readf(struct file *data, const char *fmt, ...)
{
struct data_file *file = &data->f_data;
va_list argp;
int ret;
va_start(argp, fmt);
ret = vfscanf(file->f_file, fmt, argp);
ret = vfscanf(data->f_file, fmt, argp);
va_end(argp);
return ret;
@ -268,34 +263,31 @@ gchar *data_file_readl(struct file *data)
int data_file_writef(struct file *data, const char *fmt, ...)
{
struct data_file *file = &data->f_data;
va_list argp;
int ret;
va_start(argp, fmt);
ret = g_vfprintf(file->f_file, fmt, argp);
ret = g_vfprintf(data->f_file, fmt, argp);
va_end(argp);
if (ret < 0)
REPORT_ERRNO(file->f_name);
REPORT_ERRNO(data->f_name);
return ret;
}
int cache_file_write(struct file *cache, const void *data, size_t len)
{
struct cache_file *file = &cache->f_cache;
if (fwrite(data, len, 1, file->cf_file) == 1)
if (fwrite(data, len, 1, cache->f_file) == 1)
return len;
return -1;
}
bool cache_file_import(struct file *cache, const gchar *srcpath)
{
struct cache_file *file = &cache->f_cache;
gchar *contents = NULL;
gsize length = 0;
if (!file->cf_file || !srcpath)
if (!cache->f_file || !srcpath)
return false;
if (!g_file_get_contents(srcpath, &contents, &length, NULL))
return false;
@ -306,11 +298,10 @@ bool cache_file_import(struct file *cache, const gchar *srcpath)
bool data_file_remove(struct file *data)
{
struct data_file *file = &data->f_data;
int ret = -1;
gchar *path;
if (!file->f_file) {
if (!data->f_file) {
path = data_file_path(data);
ret = g_unlink(path);
g_free(path);

View File

@ -39,17 +39,15 @@ struct data_file {
unsigned int f_version; /* The file's current data version. */
unsigned int f_prev; /* The file's on-disk data version. */
unsigned int f_min; /* The file's minimum data version. */
FILE *f_file; /* The file's IO stream. */
const gchar *f_name; /* The file's basename. */
};
struct cache_file {
FILE *cf_file; /* The cache file's IO stream. */
const gchar *cf_name; /* The cache file's basename. */
const gchar *cf_subdir; /* The cache file's subdirectory. */
};
struct file {
FILE *f_file; /* The file's IO stream. */
const gchar *f_name; /* The file's basename. */
union {
struct data_file f_data;
struct cache_file f_cache;
@ -58,13 +56,13 @@ struct file {
#define DATA_FILE_INIT(fname, min) \
{ \
.f_file = NULL, \
.f_name = fname, \
.f_data = { \
.f_mode = OPEN_READ, \
.f_version = OCARINA_MINOR_VERSION, \
.f_prev = 0, \
.f_min = min, \
.f_file = NULL, \
.f_name = fname, \
} \
}

View File

@ -106,7 +106,7 @@ static void test_init()
g_assert_cmpuint(db.db_size, ==, 0);
g_assert_false(db.db_autosave);
g_assert_cmpuint(db.db_file.f_data.f_version, ==, OCARINA_MINOR_VERSION);
g_assert_cmpstr(db.db_file.f_data.f_name, ==, "init.db");
g_assert_cmpstr(db.db_file.f_name, ==, "init.db");
db_deinit(&db);
}

View File

@ -9,7 +9,7 @@
static void test_verify_constructor(struct file *file, gchar *fpath, gchar *ftmp)
{
g_assert_null(file->f_data.f_file);
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);
@ -24,9 +24,9 @@ static void test_invalid_file(gconstpointer path)
test_verify_constructor(&file, "", "");
g_assert_false(data_file_open(&file, OPEN_READ));
g_assert_null(file.f_data.f_file);
g_assert_null(file.f_file);
g_assert_false(data_file_open(&file, OPEN_WRITE));
g_assert_null(file.f_data.f_file);
g_assert_null(file.f_file);
g_assert_cmpuint(file.f_data.f_mode, ==, OPEN_READ);
g_assert_false(data_file_exists(&file));
@ -46,13 +46,13 @@ static void __test_file_subprocess()
g_assert_false(data_file_open(&file, OPEN_READ));
g_assert_true(data_file_open(&file, OPEN_WRITE));
g_assert_nonnull(file.f_data.f_file);
g_assert_nonnull(file.f_file);
g_assert_cmpuint(file.f_data.f_mode, ==, OPEN_WRITE);
g_assert_false(data_file_open(&file, OPEN_WRITE));
g_assert_false(data_file_exists(&file));
data_file_close(&file);
g_assert_null(file.f_data.f_file);
g_assert_null(file.f_file);
g_assert_cmpuint(file.f_data.f_mode, ==, OPEN_WRITE);
g_assert_true(data_file_exists(&file));
@ -63,7 +63,7 @@ static void __test_file_subprocess()
g_chmod(filepath, 0644);
g_assert_true(data_file_open(&file, OPEN_READ));
g_assert_nonnull(file.f_data.f_file);
g_assert_nonnull(file.f_file);
g_assert_cmpuint(file.f_data.f_mode, ==, OPEN_READ);
g_assert_false(data_file_open(&file, OPEN_READ));
@ -142,7 +142,7 @@ static void __test_versioning_subprocess(unsigned int out, unsigned int in)
g_assert_true(data_file_exists(&fout));
g_assert_false(data_file_open(&fin, OPEN_READ));
g_assert_null(fin.f_data.f_file);
g_assert_null(fin.f_file);
}
static void test_versioning_old()
@ -181,8 +181,8 @@ static void test_cache()
filepath = g_strjoin("/", basepath, "dir", "file.txt", NULL);
writepath = g_strjoin("/", basepath, "dir", ".file.txt.tmp", NULL);
g_assert_null(file.f_cache.cf_file);
g_assert_cmpstr(file.f_cache.cf_name, ==, "file.txt");
g_assert_null(file.f_file);
g_assert_cmpstr(file.f_name, ==, "file.txt");
g_assert_cmpstr(file.f_cache.cf_subdir, ==, "dir");
g_assert_cmpstr_free(cache_file_path(&file), ==, filepath);
@ -192,13 +192,13 @@ static void test_cache()
g_assert_false(cache_file_exists(&file));
g_assert_false(cache_file_open(&file, OPEN_READ));
g_assert_true(cache_file_open(&file, OPEN_WRITE));
g_assert_nonnull(file.f_cache.cf_file);
g_assert_nonnull(file.f_file);
g_assert_false(cache_file_open(&file, OPEN_WRITE));
g_assert_false(cache_file_exists(&file));
g_assert_cmpuint(cache_file_write(&file, "abcde", 5), ==, 5);
cache_file_close(&file);
g_assert_null(file.f_cache.cf_file);
g_assert_null(file.f_file);
g_assert_true(cache_file_exists(&file));
/* Test importing a file into the cache. */