From b6d45e666e8d1f870adfda9151df258e5055fe4c Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Fri, 16 Feb 2018 15:50:46 -0500 Subject: [PATCH] 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 --- core/audio.c | 2 +- core/database.c | 2 +- core/file.c | 41 ++++++++++++++++++++------------------- core/playlists/artist.c | 2 +- core/playlists/library.c | 2 +- core/playlists/system.c | 8 ++++---- core/settings.c | 2 +- core/tags/album.c | 2 +- include/core/database.h | 2 +- include/core/file.h | 18 +++++++---------- tests/core/database.c | 3 +-- tests/core/date.c | 2 +- tests/core/file.c | 18 ++++++++--------- tests/core/playlist.c | 2 +- tests/core/settings.c | 2 +- tests/core/tags/album.c | 3 +-- tests/core/tags/artist.c | 3 +-- tests/core/tags/genre.c | 3 +-- tests/core/tags/library.c | 3 +-- tests/core/tags/track.c | 3 +-- 20 files changed, 57 insertions(+), 66 deletions(-) diff --git a/core/audio.c b/core/audio.c index 9bbf4c35..2bba7cf1 100644 --- a/core/audio.c +++ b/core/audio.c @@ -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; diff --git a/core/database.c b/core/database.c index 5de53321..90230cb6 100644 --- a/core/database.c +++ b/core/database.c @@ -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) diff --git a/core/file.c b/core/file.c index 209c3311..3b80a951 100644 --- a/core/file.c +++ b/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"); diff --git a/core/playlists/artist.c b/core/playlists/artist.c index 2786a7f3..94821210 100644 --- a/core/playlists/artist.c +++ b/core/playlists/artist.c @@ -5,7 +5,7 @@ #include #include -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, diff --git a/core/playlists/library.c b/core/playlists/library.c index db4a6414..9d862d72 100644 --- a/core/playlists/library.c +++ b/core/playlists/library.c @@ -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; diff --git a/core/playlists/system.c b/core/playlists/system.c index bc6a04ab..b3104754 100644 --- a/core/playlists/system.c +++ b/core/playlists/system.c @@ -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); /* diff --git a/core/settings.c b/core/settings.c index bb7e20d3..415d8660 100644 --- a/core/settings.c +++ b/core/settings.c @@ -5,7 +5,7 @@ #include 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) diff --git a/core/tags/album.c b/core/tags/album.c index cd7c6088..128dfeac 100644 --- a/core/tags/album.c +++ b/core/tags/album.c @@ -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; diff --git a/include/core/database.h b/include/core/database.h index 2f788af9..d5184cf6 100644 --- a/include/core/database.h +++ b/include/core/database.h @@ -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, \ diff --git a/include/core/file.h b/include/core/file.h index ced223a7..1dea7403 100644 --- a/include/core/file.h +++ b/include/core/file.h @@ -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. diff --git a/tests/core/database.c b/tests/core/database.c index c6df8e6d..441e93ef 100644 --- a/tests/core/database.c +++ b/tests/core/database.c @@ -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); diff --git a/tests/core/date.c b/tests/core/date.c index ca3581df..336a682f 100644 --- a/tests/core/date.c +++ b/tests/core/date.c @@ -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); diff --git a/tests/core/file.c b/tests/core/file.c index a995779b..9d1a93cc 100644 --- a/tests/core/file.c +++ b/tests/core/file.c @@ -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); diff --git a/tests/core/playlist.c b/tests/core/playlist.c index ad467fe6..17662ea1 100644 --- a/tests/core/playlist.c +++ b/tests/core/playlist.c @@ -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++) { diff --git a/tests/core/settings.c b/tests/core/settings.c index bab43854..60e11005 100644 --- a/tests/core/settings.c +++ b/tests/core/settings.c @@ -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); diff --git a/tests/core/tags/album.c b/tests/core/tags/album.c index 5c8d9ba2..06f75124 100644 --- a/tests/core/tags/album.c +++ b/tests/core/tags/album.c @@ -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); diff --git a/tests/core/tags/artist.c b/tests/core/tags/artist.c index 851f4dd2..bcd31248 100644 --- a/tests/core/tags/artist.c +++ b/tests/core/tags/artist.c @@ -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); diff --git a/tests/core/tags/genre.c b/tests/core/tags/genre.c index 357c6673..da6dbdd7 100644 --- a/tests/core/tags/genre.c +++ b/tests/core/tags/genre.c @@ -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); diff --git a/tests/core/tags/library.c b/tests/core/tags/library.c index 39283939..557a793d 100644 --- a/tests/core/tags/library.c +++ b/tests/core/tags/library.c @@ -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"); diff --git a/tests/core/tags/track.c b/tests/core/tags/track.c index 583eb19b..0f0d16cd 100644 --- a/tests/core/tags/track.c +++ b/tests/core/tags/track.c @@ -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);