core/file: Create a single file_close() function

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2018-02-20 09:53:53 -05:00
parent 659aaff6a1
commit 84a1022bdf
18 changed files with 59 additions and 71 deletions

View File

@ -133,7 +133,7 @@ static bool __audio_init_idle(void *data)
__audio_load(track_get(track), LOAD_HISTORY);
} else if (data_file_open(&audio_file, OPEN_READ)) {
data_file_readf(&audio_file, "%u", &track);
data_file_close(&audio_file);
file_close(&audio_file);
data_file_remove(&audio_file);
__audio_load(track_get(track), LOAD_HISTORY);
}

View File

@ -101,7 +101,7 @@ void db_save(struct database *db)
for (unsigned int i = 0; i < db_actual_size(db); i++)
__dbe_write(db, DB_ENTRY_AT(db, i));
data_file_close(&db->db_file);
file_close(&db->db_file);
}
void db_autosave(struct database *db)
@ -126,7 +126,7 @@ void db_load(struct database *db)
}
save = data_file_version(&db->db_file) != OCARINA_MINOR_VERSION;
data_file_close(&db->db_file);
file_close(&db->db_file);
if (save)
db_autosave(db);

View File

@ -38,18 +38,6 @@ static FILE *__file_open(gchar *path, const gchar *mode)
return ret;
}
static void __file_close(FILE *file, gchar *path, gchar *tmp)
{
if (file) {
fclose(file);
if (path && tmp)
g_rename(tmp, path);
}
g_free(path);
g_free(tmp);
}
static bool __file_mkdir(const gchar *basedir, const gchar *subdir)
{
gchar *dir = __file_path(basedir, subdir, NULL);
@ -143,12 +131,12 @@ static bool __file_open_read(struct file *data)
return false;
if (file->f_prev < file->f_min) {
REPORT_ERROR(data->f_name, "File too old to be upgraded.");
data_file_close(data);
file_close(data);
exit(1);
}
if (file->f_prev > file->f_version) {
REPORT_ERROR(data->f_name, "File too new to be opened.");
data_file_close(data);
file_close(data);
exit(1);
}
return true;
@ -196,22 +184,22 @@ bool cache_file_open(struct file *cache, enum open_mode mode)
return cache->f_file != NULL;
}
void data_file_close(struct file *data)
void file_close(struct file *file)
{
__file_close(data->f_file,
data->f_mode == OPEN_WRITE ? file_path(data) : NULL,
data->f_mode == OPEN_WRITE ? file_write_path(data) : NULL);
data->f_file = NULL;
data->f_mode = CLOSED;
}
gchar *path = file_path(file);
gchar *tmp = file_write_path(file);
void cache_file_close(struct file *cache)
{
__file_close(cache->f_file,
file_path(cache),
file_write_path(cache));
cache->f_file = NULL;
cache->f_mode = CLOSED;
if (file->f_file) {
fclose(file->f_file);
if (file->f_mode == OPEN_WRITE)
g_rename(tmp, path);
}
file->f_file = NULL;
file->f_mode = CLOSED;
g_free(path);
g_free(tmp);
}
int data_file_readf(struct file *data, const char *fmt, ...)

View File

@ -60,7 +60,7 @@ static bool __artist_pl_load(void *data)
g_free(name);
}
data_file_close(&artist_file);
file_close(&artist_file);
return true;
}
@ -80,7 +80,7 @@ static void pl_artist_save(void)
playlist_generic_save(playlist, &artist_file, PL_SAVE_METADATA);
}
data_file_close(&artist_file);
file_close(&artist_file);
}
static struct playlist *pl_artist_lookup(const gchar *name)

View File

@ -65,7 +65,7 @@ static bool __lib_pl_load(void *data)
g_free(name);
}
data_file_close(&lib_file);
file_close(&lib_file);
return true;
}
@ -197,7 +197,7 @@ static void pl_library_save(void)
playlist_generic_save(playlist, &lib_file, PL_SAVE_METADATA);
}
data_file_close(&lib_file);
file_close(&lib_file);
}
static struct playlist *pl_library_lookup(const gchar *name)

View File

@ -156,7 +156,7 @@ static bool sys_pl_queued_load()
playlist_generic_load(playlist, &sys_deck_f, PL_SAVE_TRACKS);
}
data_file_close(&sys_deck_f);
file_close(&sys_deck_f);
data_file_remove(&sys_deck_f);
return true;
}
@ -195,7 +195,7 @@ static bool sys_pl_collection_load()
if (data_file_open(&sys_collection_f, OPEN_READ)) {
playlist_generic_load(playlist, &sys_collection_f, PL_SAVE_FLAGS);
data_file_close(&sys_collection_f);
file_close(&sys_collection_f);
data_file_remove(&sys_collection_f);
}
@ -278,7 +278,7 @@ static bool __sys_pl_load()
playlist_generic_load(playlist, &sys_file, PL_SAVE_TRACKS);
}
data_file_close(&sys_file);
file_close(&sys_file);
data_file_remove(&sys_file);
return true;
}
@ -320,7 +320,7 @@ static bool __sys_pl_load_new()
playlist_generic_load(playlist, &sys_pl_file, load);
}
data_file_close(&sys_pl_file);
file_close(&sys_pl_file);
return true;
}
@ -350,7 +350,7 @@ static void pl_system_save(void)
}
data_file_close(&sys_pl_file);
file_close(&sys_pl_file);
}
static struct playlist *pl_system_lookup(const gchar *name)

View File

@ -21,7 +21,7 @@ static void __settings_save()
data_file_writef(&gui_settings_file, "%u\n", g_hash_table_size(gui_settings));
g_hash_table_foreach(gui_settings, __settings_save_item, NULL);
data_file_close(&gui_settings_file);
file_close(&gui_settings_file);
}
static void __settings_read()
@ -44,7 +44,7 @@ void settings_init()
if (data_file_open(&gui_settings_file, OPEN_READ))
__settings_read();
data_file_close(&gui_settings_file);
file_close(&gui_settings_file);
}
void settings_deinit()

View File

@ -67,7 +67,7 @@ static bool __album_fetch_cover(struct album *album, gchar *releaseid)
if (cache_file_open(&file->ac_file, OPEN_WRITE)) {
cache_file_write(&file->ac_file, caa_imagedata_data(image),
caa_imagedata_size(image));
cache_file_close(&file->ac_file);
file_close(&file->ac_file);
}
__album_free_file(file);
@ -393,7 +393,7 @@ bool album_artwork_import(struct album *album, gchar *path)
file = __album_alloc_file(album);
if (path && cache_file_open(&file->ac_file, OPEN_WRITE)) {
ret = cache_file_import(&file->ac_file, path);
cache_file_close(&file->ac_file);
file_close(&file->ac_file);
}
__album_free_file(file);

View File

@ -108,11 +108,11 @@ bool data_file_open(struct file *, enum open_mode);
bool cache_file_open(struct file *, enum open_mode);
/*
* Closes an open file, setting file->{f|cf}_file to NULL. If the file was opened
* with OPEN_WRITE, then rename the temporary file to file_path().
* Closes an open file, setting file->f_file to NULL and file->f_mode
* to CLOSED. If the file was opened for writing, then rename the
* temporary file to file_path().
*/
void data_file_close(struct file *);
void cache_file_close(struct file *);
void file_close(struct file *);
/*
* Read an entire line from the file and return it to the caller.

View File

@ -78,14 +78,14 @@ static void test_db_entry()
data_file_open(&f, OPEN_WRITE);
int_ops.dbe_write(&f, &ent->ie_dbe);
data_file_close(&f);
file_close(&f);
int_ops.dbe_free(&ent->ie_dbe);
g_assert_cmpuint(test_free_count, ==, 1);
data_file_open(&f, OPEN_READ);
ent = INT_ENTRY(int_ops.dbe_read(&f, 0));
data_file_close(&f);
file_close(&f);
g_assert(ent->ie_dbe.dbe_data == ent);
g_assert_cmpuint(ent->ie_val, ==, 1);

View File

@ -41,7 +41,7 @@ void test_date()
g_assert_cmpuint(date.d_stamp, ==, 84543432);
date_write_stamp(&f, &date);
data_file_close(&f);
file_close(&f);
data_file_open(&f, OPEN_READ);
date_read(&f, &date);
@ -61,7 +61,7 @@ void test_date()
g_assert_cmpuint(date.d_year, ==, 1992);
g_assert_cmpuint(date.d_month, ==, 10);
g_assert_cmpuint(date.d_day, ==, 5);
data_file_close(&f);
file_close(&f);
}
void test_compare()

View File

@ -52,7 +52,7 @@ static void __test_file_subprocess()
g_assert_false(data_file_open(&file, OPEN_WRITE));
g_assert_false(file_exists(&file));
data_file_close(&file);
file_close(&file);
g_assert_null(file.f_file);
g_assert_cmpuint(file.f_mode, ==, CLOSED);
g_assert_true(file_exists(&file));
@ -71,7 +71,7 @@ static void __test_file_subprocess()
g_assert_false(data_file_remove(&file));
g_assert_true(file_exists(&file));
data_file_close(&file);
file_close(&file);
g_assert_cmpuint(file.f_mode, ==, CLOSED);
g_assert_true(data_file_remove(&file));
g_assert_false(file_exists(&file));
@ -104,7 +104,7 @@ static void test_io()
data_file_writef(&fout, "2 FGHIJ KLMNO\n");
data_file_writef(&fout, "3 \n");
data_file_writef(&fout, "4 5 PQRST\n");
data_file_close(&fout);
file_close(&fout);
g_assert_true(file_exists(&fout));
g_assert_true(data_file_open(&fin, OPEN_READ));
@ -127,7 +127,7 @@ static void test_io()
g_assert_cmpuint(i, ==, 4);
g_assert_cmpstr_free(res, ==, "5 PQRST");
data_file_close(&fin);
file_close(&fin);
g_assert_cmpuint(data_file_version(&fin), ==, OCARINA_MINOR_VERSION);
}
@ -141,7 +141,7 @@ static void __test_versioning_subprocess(unsigned int out, unsigned int in)
g_assert_true(data_file_open(&fout, OPEN_WRITE));
data_file_writef(&fout, "abcdefghijklmnopqrstuvwxyz");
data_file_close(&fout);
file_close(&fout);
g_assert_true(file_exists(&fout));
g_assert_false(data_file_open(&fin, OPEN_READ));
@ -203,7 +203,7 @@ static void test_cache()
g_assert_false(file_exists(&file));
g_assert_cmpuint(cache_file_write(&file, "abcde", 5), ==, 5);
cache_file_close(&file);
file_close(&file);
g_assert_null(file.f_file);
g_assert_cmpuint(file.f_mode, ==, CLOSED);
g_assert_true(file_exists(&file));
@ -217,7 +217,7 @@ static void test_cache()
g_assert_false(cache_file_import(&copy, NULL));
g_assert_true(cache_file_import(&copy, filepath));
g_assert_false(file_exists(&copy));
cache_file_close(&copy);
file_close(&copy);
g_assert_true(file_exists(&copy));
}

View File

@ -347,12 +347,12 @@ static void test_save_load()
g_assert_true( data_file_open(&f, OPEN_WRITE));
playlist_generic_save(&p, &f, PL_SAVE_METADATA);
playlist_generic_save(&q, &f, PL_SAVE_ALL);
data_file_close(&f);
file_close(&f);
g_assert_true(data_file_open(&f, OPEN_READ));
playlist_generic_load(&r, &f, PL_SAVE_METADATA);
playlist_generic_load(&s, &f, PL_SAVE_ALL);
data_file_close(&f);
file_close(&f);
g_assert_true(r.pl_random);
g_assert_cmpuint(playlist_current_index(&r), ==, 3);

View File

@ -61,7 +61,7 @@ static void test_album()
data_file_open(&f, OPEN_WRITE);
data_file_writef(&f, "0 0 0 \n");
album_ops->dbe_write(&f, &album->al_dbe);
data_file_close(&f);
file_close(&f);
album_ops->dbe_free(&album->al_dbe);
data_file_open(&f, OPEN_READ);
@ -70,7 +70,7 @@ static void test_album()
album_ops->dbe_free(&album->al_dbe);
album = ALBUM(album_ops->dbe_read(&f, 0));
data_file_close(&f);
file_close(&f);
test_verify_hyrule(album, koji, genre);
album_ops->dbe_free(&album->al_dbe);
}

View File

@ -43,7 +43,7 @@ static void test_artist()
data_file_open(&f, OPEN_WRITE);
data_file_writef(&f, "1 \n2 ");
artist_ops->dbe_write(&f, &artist->ar_dbe);
data_file_close(&f);
file_close(&f);
g_free(artist->ar_name);
artist_ops->dbe_free(&artist->ar_dbe);
@ -56,7 +56,7 @@ static void test_artist()
data_file_readf(&f, "%u", &i);
artist = ARTIST(artist_ops->dbe_read(&f, 0));
data_file_close(&f);
file_close(&f);
test_verify_koji(artist);
g_free(artist->ar_name);
artist_ops->dbe_free(&artist->ar_dbe);

View File

@ -41,7 +41,7 @@ static void test_genre()
data_file_open(&f, OPEN_WRITE);
data_file_writef(&f, "1 \n1 ");
genre_ops->dbe_write(&f, &genre->ge_dbe);
data_file_close(&f);
file_close(&f);
g_free(genre->ge_name);
genre_ops->dbe_free(&genre->ge_dbe);
@ -54,7 +54,7 @@ static void test_genre()
data_file_readf(&f, "%u", &i);
genre = GENRE(genre_ops->dbe_read(&f, 0));
data_file_close(&f);
file_close(&f);
test_verify_vg(genre);
g_free(genre->ge_name);
genre_ops->dbe_free(&genre->ge_dbe);

View File

@ -34,7 +34,7 @@ static void test_library()
library_ops->dbe_write(&f, &link->li_dbe);
data_file_writef(&f, "\n");
library_ops->dbe_write(&f, &zelda->li_dbe);
data_file_close(&f);
file_close(&f);
data_file_open(&f, OPEN_READ);
library = LIBRARY(library_ops->dbe_read(&f, 0));
@ -45,7 +45,7 @@ static void test_library()
library_ops->dbe_free(&library->li_dbe);
library = LIBRARY(library_ops->dbe_read(&f, 0));
data_file_close(&f);
file_close(&f);
test_verify_zelda(library);
g_assert_cmpstr_free(library_file(library, "impa.ogg"), ==,
"/home/Zelda/Music/impa.ogg");

View File

@ -102,7 +102,7 @@ static void test_track()
data_file_writef(&f, "0 0 0 0 0 0 \n");
data_file_writef(&f, "Hyrule Symphony/00 - No Track.ogg\n");
track_ops->dbe_write(&f, &track->tr_dbe);
data_file_close(&f);
file_close(&f);
g_free(track->tr_path);
track_ops->dbe_free(&track->tr_dbe);
@ -118,7 +118,7 @@ static void test_track()
track = TRACK(track_ops->dbe_read(&f, 0));
track->tr_dbe.dbe_index = 0;
test_verify_track(track, false);
data_file_close(&f);
file_close(&f);
track_played(track);
g_assert_cmpuint(track->tr_count, ==, 1);