core/file: File version is OCARINA_MINOR_VERSION

This makes file versioning way easier, since every file will have the
same version.  I'll still need to manage minimum-supported versions, but
that shouldn't be too difficult going forward.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2016-08-17 14:17:52 -04:00
parent 43a0ffd54c
commit b39c4c9ba4
25 changed files with 62 additions and 64 deletions

View File

@ -8,7 +8,7 @@
static const char *SETTINGS_TRACK = "core.audio.cur";
static const char *SETTINGS_VOLUME = "core.audio.volume";
static struct file audio_file = FILE_INIT("cur_track", 0, 0);
static struct file audio_file = FILE_INIT("cur_track", 0);
static struct track *audio_track = NULL;
static GstElement *audio_player = NULL;
static struct audio_ops *audio_ops = NULL;

View File

@ -71,14 +71,14 @@ static void __dbe_write(struct database *db, struct db_entry *dbe)
}
void db_init(struct database *db, const char *filepath, bool autosave,
const struct db_ops *ops, unsigned int fmin, unsigned int fcur)
const struct db_ops *ops, unsigned int fmin)
{
db->db_ops = ops;
db->db_size = 0;
db->db_autosave = autosave;
db->db_entries = g_ptr_array_new();
db->db_keys = g_hash_table_new(g_str_hash, g_str_equal);
file_init(&db->db_file, filepath, fcur, fmin);
file_init(&db->db_file, filepath, fmin);
}
void db_deinit(struct database *db)

View File

@ -94,11 +94,10 @@ static bool __file_can_write(struct file *file)
}
void file_init(struct file *file, const gchar *name,
unsigned int version, unsigned int min)
void file_init(struct file *file, const gchar *name, unsigned int min)
{
file->f_mode = OPEN_READ;
file->f_version = version;
file->f_version = OCARINA_MINOR_VERSION;
file->f_prev = 0;
file->f_min = min;
file->f_file = NULL;

View File

@ -6,7 +6,7 @@
#include <core/string.h>
static struct queue_ops *artist_ops = NULL;
static struct file artist_file = FILE_INIT("playlist.artist", 0, 0);
static struct file artist_file = FILE_INIT("playlist.artist", 0);
static struct playlist *__artist_pl_alloc(gchar *name)

View File

@ -14,7 +14,7 @@ struct scan_data {
static bool __lib_pl_scan_dir(void *);
static struct queue_ops *lib_ops = NULL;
static struct file lib_file = FILE_INIT("playlist.library", 0, 0);
static struct file lib_file = FILE_INIT("playlist.library", 0);
static struct playlist *__lib_pl_alloc(struct library *library)

View File

@ -12,10 +12,10 @@ static inline struct queue *__sys_pl_queue(enum sys_playlist_t);
static void __sys_pl_save();
static bool __sys_pl_load();
static struct file sys_file = FILE_INIT("playlist.db", 0, 0);
static struct file sys_deck_f = FILE_INIT("deck", 1, 1);
static struct file sys_collection_f = FILE_INIT("library.q", 0, 0);
static struct file sys_pl_file = FILE_INIT("playlist.system", 0, 0);
static struct file sys_file = FILE_INIT("playlist.db", 0);
static struct file sys_deck_f = FILE_INIT("deck", 1);
static struct file sys_collection_f = FILE_INIT("library.q", 0);
static struct file sys_pl_file = FILE_INIT("playlist.system", 0);
static struct sys_playlist *sys_playlists[SYS_PL_NUM_PLAYLISTS];

View File

@ -195,7 +195,7 @@ struct playlist_type pl_user = {
void pl_user_init(struct queue_ops *ops)
{
user_pl_ops = ops;
db_init(&user_db, "playlist.user", true, &user_db_ops, 0, 0);
db_init(&user_db, "playlist.user", true, &user_db_ops, 0);
db_load_idle(&user_db);
}

View File

@ -5,7 +5,7 @@
#include <core/settings.h>
static GHashTable *gui_settings = NULL;
static struct file gui_settings_file = FILE_INIT("settings", 0, 0);
static struct file gui_settings_file = FILE_INIT("settings", 0);
static void __settings_save_item(gpointer key, gpointer value, gpointer data)

View File

@ -16,7 +16,6 @@
#endif
#define ALBUM_DB_MIN 0 /* Ocarina 6.0 */
#define ALBUM_DB_CUR 1 /* Ocarina 6.5 */
static struct database album_db;
static bool album_db_upgraded = false;
@ -290,8 +289,7 @@ static const struct db_ops album_ops = {
void album_db_init()
{
db_init(&album_db, "album.db", true, &album_ops, ALBUM_DB_MIN,
ALBUM_DB_CUR);
db_init(&album_db, "album.db", true, &album_ops, ALBUM_DB_MIN);
db_load_idle(&album_db);
}

View File

@ -60,7 +60,7 @@ static const struct db_ops artist_ops = {
void artist_db_init()
{
db_init(&artist_db, "artist.db", true, &artist_ops, 0, 0);
db_init(&artist_db, "artist.db", true, &artist_ops, 0);
db_load_idle(&artist_db);
}

View File

@ -58,7 +58,7 @@ static const struct db_ops genre_ops = {
void genre_db_init()
{
db_init(&genre_db, "genre.db", true, &genre_ops, 0, 0);
db_init(&genre_db, "genre.db", true, &genre_ops, 0);
db_load_idle(&genre_db);
}

View File

@ -4,7 +4,6 @@
#include <core/tags/library.h>
#define LIBRARY_DB_MIN 0 /* Ocarina 6.0 */
#define LIBRARY_DB_CUR 1 /* Ocarina 6.5 */
static struct database library_db;
static struct library *__library_alloc(gchar *path)
@ -63,8 +62,7 @@ static const struct db_ops library_ops = {
void library_db_init()
{
db_init(&library_db, "library.db", true, &library_ops, LIBRARY_DB_MIN,
LIBRARY_DB_CUR);
db_init(&library_db, "library.db", true, &library_ops, LIBRARY_DB_MIN);
db_load_idle(&library_db);
}

View File

@ -8,7 +8,6 @@
#include <taglib/tag_c.h>
#define TRACK_DB_MIN 0 /* Ocarina 6.0 */
#define TRACK_DB_CUR 1 /* Ocarina 6.5 */
static struct database track_db;
static unsigned int unplayed_count = 0;
static unsigned int play_count = 0;
@ -197,8 +196,7 @@ static const struct db_ops track_ops = {
void track_db_init()
{
db_init(&track_db, "track.db", false, &track_ops, TRACK_DB_MIN,
TRACK_DB_CUR);
db_init(&track_db, "track.db", false, &track_ops, TRACK_DB_MIN);
db_load_idle(&track_db);
}

View File

@ -77,11 +77,11 @@ struct database {
const struct db_ops *db_ops; /* The database's operations vector. */
};
#define DB_INIT(fname, autosave, ops, fmin, fcur) \
#define DB_INIT(fname, autosave, ops, fmin) \
{ \
.db_size = 0, \
.db_autosave = autosave, \
.db_file = FILE_INIT(fname, fcur, fmin), \
.db_file = FILE_INIT(fname, fmin), \
.db_entries = g_ptr_array_new(), \
.db_keys = g_hash_table_new(g_str_hash, g_str_equal), \
.db_ops = ops, \
@ -93,7 +93,7 @@ struct database {
* and autosave as a hint for if this database should be automatically saved.
*/
void db_init(struct database *, const char *, bool, const struct db_ops *,
unsigned int, unsigned int);
unsigned int);
/* Called to prevent memory leaks by freeing all remaining database entries. */
void db_deinit(struct database *);

View File

@ -23,6 +23,7 @@
#ifndef OCARINA_CORE_FILE_H
#define OCARINA_CORE_FILE_H
#include <core/version.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <stdbool.h>
@ -42,14 +43,14 @@ struct file {
const gchar *f_name; /* The file's basename. */
};
#define FILE_INIT(fname, version, min) \
{ \
.f_mode = OPEN_READ, \
.f_version = version, \
.f_prev = 0, \
.f_min = min, \
.f_file = NULL, \
.f_name = fname, \
#define FILE_INIT(fname, min) \
{ \
.f_mode = OPEN_READ, \
.f_version = OCARINA_MINOR_VERSION, \
.f_prev = 0, \
.f_min = min, \
.f_file = NULL, \
.f_name = fname, \
}
@ -67,7 +68,7 @@ struct cache_file {
}
/* Initialize a new file object. */
void file_init(struct file *, const gchar *, unsigned int, unsigned int);
void file_init(struct file *, const gchar *, unsigned int);
void cache_file_init(struct cache_file *, gchar *, gchar *);
/*

View File

@ -83,7 +83,7 @@ static void test_db_entry()
g_assert_cmpuint(ent->ie_val, ==, 1);
g_assert_cmpstr_free(int_ops.dbe_key(&ent->ie_dbe), ==, "1");
file_init(&f, "test_db_entry", 0, 0);
file_init(&f, "test_db_entry", 0);
file_open(&f, OPEN_WRITE);
int_ops.dbe_write(&f, &ent->ie_dbe);
file_close(&f);
@ -107,14 +107,14 @@ static void test_db_entry()
static void test_init()
{
struct database db = DB_INIT("init.db", false, &int_ops, 0, 0);
struct database db = DB_INIT("init.db", false, &int_ops, 0);
/* Check initial sizes. */
g_assert_cmpuint(db.db_entries->len, ==, 0);
g_assert_cmpuint(g_hash_table_size(db.db_keys), ==, 0);
g_assert_cmpuint(db.db_size, ==, 0);
g_assert_false(db.db_autosave);
g_assert_cmpuint(db.db_file.f_version, ==, 0);
g_assert_cmpuint(db.db_file.f_version, ==, OCARINA_MINOR_VERSION);
g_assert_cmpstr(db.db_file.f_name, ==, "init.db");
db_deinit(&db);
@ -123,7 +123,7 @@ static void test_init()
static void test_database(gconstpointer arg)
{
unsigned int N = GPOINTER_TO_UINT(arg);
struct database db = DB_INIT("stress.db", false, &int_ops, 0, 0);
struct database db = DB_INIT("stress.db", false, &int_ops, 0);
struct db_entry *dbe, *next;
struct int_entry rmv;
unsigned int i;
@ -241,8 +241,8 @@ static void test_database(gconstpointer arg)
static void test_save_load()
{
struct database db1 = DB_INIT("save_load.db", true, &int_ops, 0, 0);
struct database db2 = DB_INIT("save_load.db", false, &int_ops, 0, 0);
struct database db1 = DB_INIT("save_load.db", true, &int_ops, 0);
struct database db2 = DB_INIT("save_load.db", false, &int_ops, 0);
struct db_entry *dbe, *next;
const unsigned int N = 10;
unsigned int i;

View File

@ -16,7 +16,7 @@ void test_date()
.d_month = 0,
.d_day = 0,
};
struct file f = FILE_INIT("date", 0, 0);
struct file f = FILE_INIT("date", 0);
date_today(NULL);
date_set(NULL, 0, 0, 0);

View File

@ -10,7 +10,7 @@
static void test_verify_constructor(struct file *file, gchar *fpath, gchar *ftmp)
{
g_assert_null(file->f_file);
g_assert_cmpuint(file_version(file), ==, 0);
g_assert_cmpuint(file_version(file), ==, OCARINA_MINOR_VERSION);
g_assert_cmpuint(file->f_mode, ==, OPEN_READ);
g_assert_cmpstr_free(file_path(file), ==, fpath);
g_assert_cmpstr_free(file_write_path(file), ==, ftmp);
@ -20,7 +20,7 @@ static void test_invalid_file(gconstpointer path)
{
struct file file;
file_init(&file, (gchar *)path, 0, 0);
file_init(&file, (gchar *)path, 0);
test_verify_constructor(&file, "", "");
g_assert_false(file_open(&file, OPEN_READ));
@ -34,7 +34,7 @@ static void test_invalid_file(gconstpointer path)
static void __test_file_subprocess()
{
struct file file = FILE_INIT("file.txt", 0, 0);
struct file file = FILE_INIT("file.txt", 0);
gchar *basepath, *filepath, *realpath;
basepath = g_strjoin("/", g_get_user_data_dir(), OCARINA_NAME, NULL);
@ -90,11 +90,12 @@ static void test_file()
static void test_io()
{
struct file fout = FILE_INIT("file.txt", 1, 1);
struct file fin = FILE_INIT("file.txt", 2, 1);
struct file fout = FILE_INIT("file.txt", 0);
struct file fin = FILE_INIT("file.txt", 1);
char *res = NULL;
unsigned int i;
fout.f_version = 1;
g_assert_true(file_open(&fout, OPEN_WRITE));
file_writef(&fout, "1 ABCDE\n");
file_writef(&fout, "2 FGHIJ KLMNO\n");
@ -124,13 +125,16 @@ static void test_io()
g_assert_cmpstr_free(res, ==, "5 PQRST");
file_close(&fin);
g_assert_cmpuint(file_version(&fin), ==, 2);
g_assert_cmpuint(file_version(&fin), ==, OCARINA_MINOR_VERSION);
}
static void __test_versioning_subprocess(unsigned int out, unsigned int in)
{
struct file fout = FILE_INIT("file.txt", out, out);
struct file fin = FILE_INIT("file.txt", in, in);
struct file fout = FILE_INIT("file.txt", out);
struct file fin = FILE_INIT("file.txt", in);
fout.f_version = out;
fin.f_version = in;
g_assert_true(file_open(&fout, OPEN_WRITE));
file_writef(&fout, "abcdefghijklmnopqrstuvwxyz");

View File

@ -455,7 +455,7 @@ static void test_sorting()
static void test_save_load()
{
struct file f = FILE_INIT("queue.q", 0, 0);
struct file f = FILE_INIT("queue.q", 0);
struct queue q, r;
unsigned int i;

View File

@ -7,7 +7,7 @@
static void test_settings()
{
struct file f = FILE_INIT("settings", 0, 0);
struct file f = FILE_INIT("settings", 0);
g_assert_null(test_get_settings());
settings_set("test.value1", 42);

View File

@ -58,7 +58,7 @@ static void test_album()
g_assert_true( album_match_token(album, "symphony"));
g_assert_false(album_match_token(album, "skyward"));
file_init(&f, "album_tag", 1, 1);
file_init(&f, "album_tag", 1);
file_open(&f, OPEN_WRITE);
file_writef(&f, "0 0 0 \n");
album_ops->dbe_write(&f, &album->al_dbe);
@ -117,7 +117,7 @@ static void test_album_db()
g_assert(album_get(0) == album);
g_assert_null(album_get(1));
db_init(&album_db, "album.db", false, album_ops, 1, 1);
db_init(&album_db, "album.db", false, album_ops, 0);
db_load(&album_db);
g_assert_cmpuint(album_db.db_size, ==, 1);

View File

@ -40,7 +40,7 @@ static void test_artist()
g_assert_true( artist_match_token(artist, "kondo"));
g_assert_false(artist_match_token(artist, "hajime"));
file_init(&f, "artist_tag", 0, 0);
file_init(&f, "artist_tag", 0);
file_open(&f, OPEN_WRITE);
file_writef(&f, "1 \n2 ");
artist_ops->dbe_write(&f, &artist->ar_dbe);
@ -102,7 +102,7 @@ static void test_artist_db()
g_assert(artist_get(0) == artist);
g_assert_null(artist_get(1));
db_init(&artist_db, "artist.db", false, artist_ops, 0, 0);
db_init(&artist_db, "artist.db", false, artist_ops, 0);
db_load(&artist_db);
g_assert_cmpuint(artist_db.db_size, ==, 1);

View File

@ -38,7 +38,7 @@ static void test_genre()
g_assert_true( genre_match_token(genre, "music"));
g_assert_false(genre_match_token(genre, "rock"));
file_init(&f, "genre_tag", 0, 0);
file_init(&f, "genre_tag", 0);
file_open(&f, OPEN_WRITE);
file_writef(&f, "1 \n1 ");
genre_ops->dbe_write(&f, &genre->ge_dbe);
@ -92,7 +92,7 @@ static void test_genere_db()
g_assert(genre_get(0) == genre);
g_assert_null(genre_get(1));
db_init(&genre_db, "genre.db", false, genre_ops, 0, 0);
db_init(&genre_db, "genre.db", false, genre_ops, 0);
db_load(&genre_db);
g_assert_cmpint(genre_db.db_size, ==, 1);

View File

@ -30,7 +30,7 @@ static void test_library()
test_verify_link(link);
test_verify_zelda(zelda);
file_init(&f, "library_tag", 0, 0);
file_init(&f, "library_tag", 0);
file_open(&f, OPEN_WRITE);
library_ops->dbe_write(&f, &link->li_dbe);
file_writef(&f, "\n");
@ -80,7 +80,7 @@ static void test_library_db()
g_assert(library_get(0) == library);
g_assert_null(library_get(1));
db_init(&library_db, "library.db", false, library_ops, 1, 1);
db_init(&library_db, "library.db", false, library_ops, 1);
db_load(&library_db);
g_assert_cmpuint(library_db.db_size, ==, 1);
g_assert_cmpuint(db_actual_size(&library_db), ==, 1);

View File

@ -75,7 +75,7 @@ static void test_track()
struct file f;
gchar *date;
file_init(&f, "track_tag", 1, 1);
file_init(&f, "track_tag", 1);
g_assert_nonnull(library_find("tests/Music"));
date = string_tm2str(now);
@ -222,7 +222,7 @@ static void __test_track_db_subprocess()
g_assert_cmpuint(track_db_get()->db_size, ==, 1);
g_assert_cmpuint(track_db_count_unplayed(), ==, 1);
db_init(&track_db, "track.db", false, track_ops, 1, 1);
db_init(&track_db, "track.db", false, track_ops, 0);
db_load(&track_db);
g_assert_cmpuint(track_db.db_size, ==, 0);