core/file: Remove struct cache_file
And rework the init functions at the same time to reflect that data files can now be placed under a subdirectory. Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
842547d735
commit
b6d45e666e
|
@ -13,7 +13,7 @@
|
|||
static const char *SETTINGS_TRACK = "core.audio.cur";
|
||||
static const char *SETTINGS_VOLUME = "core.audio.volume";
|
||||
|
||||
static struct file audio_file = DATA_FILE_INIT("cur_track", 0);
|
||||
static struct file audio_file = FILE_INIT_DATA("", "cur_track", 0);
|
||||
static struct track *audio_track = NULL;
|
||||
static int audio_pause_count = -1;
|
||||
|
||||
|
|
|
@ -76,7 +76,7 @@ void db_init(struct database *db, const char *filepath, bool autosave,
|
|||
db->db_autosave = autosave;
|
||||
db->db_entries = g_ptr_array_new();
|
||||
db->db_keys = g_hash_table_new(g_str_hash, g_str_equal);
|
||||
data_file_init(&db->db_file, filepath, fmin);
|
||||
file_init_data(&db->db_file, "", filepath, fmin);
|
||||
}
|
||||
|
||||
void db_deinit(struct database *db)
|
||||
|
|
41
core/file.c
41
core/file.c
|
@ -12,6 +12,14 @@
|
|||
g_printerr("%s (%s:%d): %s: %s\n", __func__, __FILE__, __LINE__, fname, error)
|
||||
#define REPORT_ERRNO(fname) REPORT_ERROR(fname, strerror(errno))
|
||||
|
||||
static void __file_init_common(struct file *file, const gchar *subdir,
|
||||
const gchar *name)
|
||||
{
|
||||
file->f_file = NULL;
|
||||
file->f_name = name;
|
||||
file->f_subdir = subdir;
|
||||
}
|
||||
|
||||
static gchar *__file_path(const gchar *base, const gchar *dir,
|
||||
const gchar *name)
|
||||
{
|
||||
|
@ -94,23 +102,19 @@ static bool __file_can_write(struct file *data)
|
|||
}
|
||||
|
||||
|
||||
void data_file_init(struct file *data, const gchar *name, unsigned int min)
|
||||
void file_init_data(struct file *file, const gchar *subdir,
|
||||
const gchar *name, unsigned int min)
|
||||
{
|
||||
struct data_file *file = &data->f_data;
|
||||
file->f_mode = OPEN_READ;
|
||||
file->f_version = OCARINA_MINOR_VERSION;
|
||||
file->f_prev = 0;
|
||||
file->f_min = min;
|
||||
data->f_file = NULL;
|
||||
data->f_name = name;
|
||||
__file_init_common(file, subdir, name);
|
||||
file->f_data.f_mode = OPEN_READ;
|
||||
file->f_data.f_version = OCARINA_MINOR_VERSION;
|
||||
file->f_data.f_prev = 0;
|
||||
file->f_data.f_min = min;
|
||||
}
|
||||
|
||||
void cache_file_init(struct file *cache, const gchar *subdir, const gchar *name)
|
||||
void file_init_cache(struct file *file, const gchar *subdir, const gchar *name)
|
||||
{
|
||||
struct cache_file *file = &cache->f_cache;
|
||||
cache->f_file = NULL;
|
||||
cache->f_name = name;
|
||||
file->cf_subdir = subdir;
|
||||
__file_init_common(file, subdir, name);
|
||||
}
|
||||
|
||||
gchar *data_file_path(struct file *data)
|
||||
|
@ -120,8 +124,7 @@ gchar *data_file_path(struct file *data)
|
|||
|
||||
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,
|
||||
return __file_build_path(g_get_user_cache_dir(), cache->f_subdir,
|
||||
cache->f_name);
|
||||
}
|
||||
|
||||
|
@ -132,9 +135,8 @@ gchar *data_file_write_path(struct file *data)
|
|||
|
||||
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,
|
||||
cache->f_name);
|
||||
return __file_build_tmp(g_get_user_cache_dir(), cache->f_subdir,
|
||||
cache->f_name);
|
||||
}
|
||||
|
||||
const unsigned int data_file_version(struct file *data)
|
||||
|
@ -209,12 +211,11 @@ bool data_file_open(struct file *data, enum open_mode mode)
|
|||
|
||||
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(cache->f_name) == 0) || (cache->f_file != NULL))
|
||||
return false;
|
||||
if (!__file_mkdir(g_get_user_cache_dir(), file->cf_subdir))
|
||||
if (!__file_mkdir(g_get_user_cache_dir(), cache->f_subdir))
|
||||
return false;
|
||||
|
||||
cache->f_file = __file_open(cache_file_write_path(cache), "wb");
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
#include <core/playlists/artist.h>
|
||||
#include <core/string.h>
|
||||
|
||||
static struct file artist_file = DATA_FILE_INIT("playlist.artist", 0);
|
||||
static struct file artist_file = FILE_INIT_DATA("", "playlist.artist", 0);
|
||||
|
||||
static struct playlist_ops pl_artist_ops = {
|
||||
.pl_can_select = playlist_generic_can_select,
|
||||
|
|
|
@ -14,7 +14,7 @@ struct scan_data {
|
|||
};
|
||||
|
||||
static bool __lib_pl_scan_dir(void *);
|
||||
static struct file lib_file = DATA_FILE_INIT("playlist.library", 0);
|
||||
static struct file lib_file = FILE_INIT_DATA("", "playlist.library", 0);
|
||||
|
||||
static struct playlist_ops pl_library_ops;
|
||||
|
||||
|
|
|
@ -9,10 +9,10 @@ static struct playlist *pl_system_lookup(const gchar *);
|
|||
static struct playlist *pl_system_get(unsigned int);
|
||||
static void pl_system_save();
|
||||
|
||||
static struct file sys_file = DATA_FILE_INIT("playlist.db", 0);
|
||||
static struct file sys_deck_f = DATA_FILE_INIT("deck", 1);
|
||||
static struct file sys_collection_f = DATA_FILE_INIT("library.q", 0);
|
||||
static struct file sys_pl_file = DATA_FILE_INIT("playlist.system", 0);
|
||||
static struct file sys_file = FILE_INIT_DATA("", "playlist.db", 0);
|
||||
static struct file sys_deck_f = FILE_INIT_DATA("", "deck", 1);
|
||||
static struct file sys_collection_f = FILE_INIT_DATA("", "library.q", 0);
|
||||
static struct file sys_pl_file = FILE_INIT_DATA("", "playlist.system", 0);
|
||||
|
||||
|
||||
/*
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
#include <core/settings.h>
|
||||
|
||||
static GHashTable *gui_settings = NULL;
|
||||
static struct file gui_settings_file = DATA_FILE_INIT("settings", 0);
|
||||
static struct file gui_settings_file = FILE_INIT_DATA("", "settings", 0);
|
||||
|
||||
|
||||
static void __settings_save_item(gpointer key, gpointer value, gpointer data)
|
||||
|
|
|
@ -32,7 +32,7 @@ struct album_cache_file *__album_alloc_file(struct album *al)
|
|||
|
||||
ret->ac_subdir = g_strdup_printf("%d", al->al_year);
|
||||
ret->ac_name = g_strdup_printf("%s.jpg", name);
|
||||
cache_file_init(&ret->ac_file, ret->ac_subdir, ret->ac_name);
|
||||
file_init_cache(&ret->ac_file, ret->ac_subdir, ret->ac_name);
|
||||
|
||||
g_free(name);
|
||||
return ret;
|
||||
|
|
|
@ -78,7 +78,7 @@ struct database {
|
|||
{ \
|
||||
.db_size = 0, \
|
||||
.db_autosave = autosave, \
|
||||
.db_file = DATA_FILE_INIT(fname, fmin), \
|
||||
.db_file = FILE_INIT_DATA("", fname, fmin), \
|
||||
.db_entries = g_ptr_array_new(), \
|
||||
.db_keys = g_hash_table_new(g_str_hash, g_str_equal), \
|
||||
.db_ops = ops, \
|
||||
|
|
|
@ -41,23 +41,19 @@ struct data_file {
|
|||
unsigned int f_min; /* The file's minimum data version. */
|
||||
};
|
||||
|
||||
struct cache_file {
|
||||
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;
|
||||
};
|
||||
const gchar *f_subdir; /* The file's subdirectory. */
|
||||
|
||||
struct data_file f_data;
|
||||
};
|
||||
|
||||
#define DATA_FILE_INIT(fname, min) \
|
||||
#define FILE_INIT_DATA(fdir, fname, min) \
|
||||
{ \
|
||||
.f_file = NULL, \
|
||||
.f_name = fname, \
|
||||
.f_subdir = fdir, \
|
||||
.f_data = { \
|
||||
.f_mode = OPEN_READ, \
|
||||
.f_version = OCARINA_MINOR_VERSION, \
|
||||
|
@ -67,8 +63,8 @@ struct file {
|
|||
}
|
||||
|
||||
/* Initialize a file object. */
|
||||
void data_file_init(struct file *, const gchar *, unsigned int);
|
||||
void cache_file_init(struct file *, const gchar *, const gchar *);
|
||||
void file_init_data(struct file *, const gchar *, const gchar *, unsigned int);
|
||||
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.
|
||||
|
|
|
@ -67,8 +67,8 @@ static const struct db_ops int_ops = {
|
|||
|
||||
static void test_db_entry()
|
||||
{
|
||||
struct file f = FILE_INIT_DATA("", "test_db_entry", 0);
|
||||
struct int_entry *ent;
|
||||
struct file f;
|
||||
|
||||
ent = INT_ENTRY(int_ops.dbe_alloc("1", 0));
|
||||
g_assert_cmpuint(ent->ie_dbe.dbe_index, ==, 0);
|
||||
|
@ -76,7 +76,6 @@ static void test_db_entry()
|
|||
g_assert_cmpuint(ent->ie_val, ==, 1);
|
||||
g_assert_cmpstr_free(int_ops.dbe_key(&ent->ie_dbe), ==, "1");
|
||||
|
||||
data_file_init(&f, "test_db_entry", 0);
|
||||
data_file_open(&f, OPEN_WRITE);
|
||||
int_ops.dbe_write(&f, &ent->ie_dbe);
|
||||
data_file_close(&f);
|
||||
|
|
|
@ -16,7 +16,7 @@ void test_date()
|
|||
.d_month = 0,
|
||||
.d_day = 0,
|
||||
};
|
||||
struct file f = DATA_FILE_INIT("date", 0);
|
||||
struct file f = FILE_INIT_DATA("", "date", 0);
|
||||
|
||||
date_today(NULL);
|
||||
date_set(NULL, 0, 0, 0);
|
||||
|
|
|
@ -20,7 +20,7 @@ static void test_invalid_file(gconstpointer path)
|
|||
{
|
||||
struct file file;
|
||||
|
||||
data_file_init(&file, (gchar *)path, 0);
|
||||
file_init_data(&file, "", (gchar *)path, 0);
|
||||
test_verify_constructor(&file, "", "");
|
||||
|
||||
g_assert_false(data_file_open(&file, OPEN_READ));
|
||||
|
@ -34,7 +34,7 @@ static void test_invalid_file(gconstpointer path)
|
|||
|
||||
static void __test_file_subprocess()
|
||||
{
|
||||
struct file file = DATA_FILE_INIT("file.txt", 0);
|
||||
struct file file = FILE_INIT_DATA("", "file.txt", 0);
|
||||
gchar *basepath, *filepath, *realpath;
|
||||
|
||||
basepath = g_strjoin("/", g_get_user_data_dir(), OCARINA_NAME, NULL);
|
||||
|
@ -90,8 +90,8 @@ static void test_file()
|
|||
|
||||
static void test_io()
|
||||
{
|
||||
struct file fout = DATA_FILE_INIT("file.txt", 0);
|
||||
struct file fin = DATA_FILE_INIT("file.txt", 1);
|
||||
struct file fout = FILE_INIT_DATA("", "file.txt", 0);
|
||||
struct file fin = FILE_INIT_DATA("", "file.txt", 1);
|
||||
char *res = NULL;
|
||||
unsigned int i;
|
||||
|
||||
|
@ -130,8 +130,8 @@ static void test_io()
|
|||
|
||||
static void __test_versioning_subprocess(unsigned int out, unsigned int in)
|
||||
{
|
||||
struct file fout = DATA_FILE_INIT("file.txt", out);
|
||||
struct file fin = DATA_FILE_INIT("file.txt", in);
|
||||
struct file fout = FILE_INIT_DATA("", "file.txt", out);
|
||||
struct file fin = FILE_INIT_DATA("", "file.txt", in);
|
||||
|
||||
fout.f_data.f_version = out;
|
||||
fin.f_data.f_version = in;
|
||||
|
@ -174,8 +174,8 @@ static void test_cache()
|
|||
gchar *basepath, *filepath, *writepath;
|
||||
struct file file, copy;
|
||||
|
||||
cache_file_init(&file, "dir", "file.txt");
|
||||
cache_file_init(©, "dir", "copy.txt");
|
||||
file_init_cache(&file, "dir", "file.txt");
|
||||
file_init_cache(©, "dir", "copy.txt");
|
||||
|
||||
basepath = g_strjoin("/", g_get_user_cache_dir(), OCARINA_NAME, NULL);
|
||||
filepath = g_strjoin("/", basepath, "dir", "file.txt", NULL);
|
||||
|
@ -183,7 +183,7 @@ static void test_cache()
|
|||
|
||||
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(file.f_subdir, ==, "dir");
|
||||
|
||||
g_assert_cmpstr_free(cache_file_path(&file), ==, filepath);
|
||||
g_assert_cmpstr_free(cache_file_write_path(&file), ==, writepath);
|
||||
|
|
|
@ -329,7 +329,7 @@ static void test_save_load()
|
|||
struct playlist q = DEFINE_PLAYLIST(PL_MAX_TYPE, "Test 2", 0, NULL);
|
||||
struct playlist r = DEFINE_PLAYLIST(PL_MAX_TYPE, "Test", 0, &test_ops);
|
||||
struct playlist s = DEFINE_PLAYLIST(PL_MAX_TYPE, "Test 2", 0, NULL);
|
||||
struct file f = DATA_FILE_INIT("test.playlist", 0);
|
||||
struct file f = FILE_INIT_DATA("", "test.playlist", 0);
|
||||
unsigned int i;
|
||||
|
||||
for (i = 0; i < 13; i++) {
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
static void test_settings()
|
||||
{
|
||||
struct file f = DATA_FILE_INIT("settings", 0);
|
||||
struct file f = FILE_INIT_DATA("", "settings", 0);
|
||||
|
||||
settings_set(NULL, 0);
|
||||
g_assert_cmpuint(settings_get(NULL), ==, 0);
|
||||
|
|
|
@ -46,7 +46,7 @@ static void test_album()
|
|||
struct album *album;
|
||||
struct artist *koji;
|
||||
struct genre *genre;
|
||||
struct file f;
|
||||
struct file f = FILE_INIT_DATA("", "album_tag", 1);
|
||||
|
||||
idle_init(IDLE_SYNC);
|
||||
|
||||
|
@ -58,7 +58,6 @@ static void test_album()
|
|||
g_assert_true( album_match_token(album, "symphony"));
|
||||
g_assert_false(album_match_token(album, "skyward"));
|
||||
|
||||
data_file_init(&f, "album_tag", 1);
|
||||
data_file_open(&f, OPEN_WRITE);
|
||||
data_file_writef(&f, "0 0 0 \n");
|
||||
album_ops->dbe_write(&f, &album->al_dbe);
|
||||
|
|
|
@ -28,10 +28,10 @@ static void test_verify_koji(struct artist *artist)
|
|||
|
||||
static void test_artist()
|
||||
{
|
||||
struct file f = FILE_INIT_DATA("", "artist_tag", 0);
|
||||
const struct db_ops *artist_ops = test_artist_ops();
|
||||
struct artist *artist;
|
||||
unsigned int i;
|
||||
struct file f;
|
||||
|
||||
artist = ARTIST(artist_ops->dbe_alloc("Koji Kondo", 0));
|
||||
|
||||
|
@ -40,7 +40,6 @@ static void test_artist()
|
|||
g_assert_true( artist_match_token(artist, "kondo"));
|
||||
g_assert_false(artist_match_token(artist, "hajime"));
|
||||
|
||||
data_file_init(&f, "artist_tag", 0);
|
||||
data_file_open(&f, OPEN_WRITE);
|
||||
data_file_writef(&f, "1 \n2 ");
|
||||
artist_ops->dbe_write(&f, &artist->ar_dbe);
|
||||
|
|
|
@ -27,10 +27,10 @@ static void test_verify_vg(struct genre *genre)
|
|||
|
||||
static void test_genre()
|
||||
{
|
||||
struct file f = FILE_INIT_DATA("", "genre_tag", 0);
|
||||
const struct db_ops *genre_ops = test_genre_ops();
|
||||
struct genre *genre;
|
||||
unsigned int i;
|
||||
struct file f;
|
||||
|
||||
genre = GENRE(genre_ops->dbe_alloc("Video Game Music", 0));
|
||||
test_verify_vg(genre);
|
||||
|
@ -38,7 +38,6 @@ static void test_genre()
|
|||
g_assert_true( genre_match_token(genre, "music"));
|
||||
g_assert_false(genre_match_token(genre, "rock"));
|
||||
|
||||
data_file_init(&f, "genre_tag", 0);
|
||||
data_file_open(&f, OPEN_WRITE);
|
||||
data_file_writef(&f, "1 \n1 ");
|
||||
genre_ops->dbe_write(&f, &genre->ge_dbe);
|
||||
|
|
|
@ -20,9 +20,9 @@ static void test_verify_link(struct library *library)
|
|||
|
||||
static void test_library()
|
||||
{
|
||||
struct file f = FILE_INIT_DATA("", "library_tag", 0);
|
||||
const struct db_ops *library_ops = test_library_ops();
|
||||
struct library *link, *zelda, *library;
|
||||
struct file f;
|
||||
|
||||
link = LIBRARY(library_ops->dbe_alloc("/home/Link/Music", 0));
|
||||
zelda = LIBRARY(library_ops->dbe_alloc("/home/Zelda/Music", 0));
|
||||
|
@ -30,7 +30,6 @@ static void test_library()
|
|||
test_verify_link(link);
|
||||
test_verify_zelda(zelda);
|
||||
|
||||
data_file_init(&f, "library_tag", 0);
|
||||
data_file_open(&f, OPEN_WRITE);
|
||||
library_ops->dbe_write(&f, &link->li_dbe);
|
||||
data_file_writef(&f, "\n");
|
||||
|
|
|
@ -83,10 +83,9 @@ static void test_track()
|
|||
time_t rawtime = time(NULL);
|
||||
struct tm *now = localtime(&rawtime);
|
||||
struct track *track;
|
||||
struct file f;
|
||||
struct file f = FILE_INIT_DATA("", "track_tag", 1);
|
||||
gchar *date;
|
||||
|
||||
data_file_init(&f, "track_tag", 1);
|
||||
g_assert_nonnull(library_find("tests/Music"));
|
||||
date = string_tm2str(now);
|
||||
|
||||
|
|
Loading…
Reference in New Issue