From 84a1022bdf6d35c9b10917a55c5ad1f18e8da233 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Tue, 20 Feb 2018 09:53:53 -0500 Subject: [PATCH] core/file: Create a single file_close() function Signed-off-by: Anna Schumaker --- core/audio.c | 2 +- core/database.c | 4 ++-- core/file.c | 44 ++++++++++++++------------------------- core/playlists/artist.c | 4 ++-- core/playlists/library.c | 4 ++-- core/playlists/system.c | 10 ++++----- core/settings.c | 4 ++-- core/tags/album.c | 4 ++-- include/core/file.h | 8 +++---- tests/core/database.c | 4 ++-- tests/core/date.c | 4 ++-- tests/core/file.c | 14 ++++++------- tests/core/playlist.c | 4 ++-- tests/core/tags/album.c | 4 ++-- tests/core/tags/artist.c | 4 ++-- tests/core/tags/genre.c | 4 ++-- tests/core/tags/library.c | 4 ++-- tests/core/tags/track.c | 4 ++-- 18 files changed, 59 insertions(+), 71 deletions(-) diff --git a/core/audio.c b/core/audio.c index 2bba7cf1..50e13d35 100644 --- a/core/audio.c +++ b/core/audio.c @@ -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); } diff --git a/core/database.c b/core/database.c index 90230cb6..71a02972 100644 --- a/core/database.c +++ b/core/database.c @@ -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); diff --git a/core/file.c b/core/file.c index af0d6d73..b44e6ecd 100644 --- a/core/file.c +++ b/core/file.c @@ -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, ...) diff --git a/core/playlists/artist.c b/core/playlists/artist.c index 94821210..efe257e0 100644 --- a/core/playlists/artist.c +++ b/core/playlists/artist.c @@ -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) diff --git a/core/playlists/library.c b/core/playlists/library.c index 9d862d72..2e73696e 100644 --- a/core/playlists/library.c +++ b/core/playlists/library.c @@ -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) diff --git a/core/playlists/system.c b/core/playlists/system.c index b3104754..8572d67b 100644 --- a/core/playlists/system.c +++ b/core/playlists/system.c @@ -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) diff --git a/core/settings.c b/core/settings.c index 415d8660..ca77db59 100644 --- a/core/settings.c +++ b/core/settings.c @@ -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() diff --git a/core/tags/album.c b/core/tags/album.c index a3877ace..125924f2 100644 --- a/core/tags/album.c +++ b/core/tags/album.c @@ -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); diff --git a/include/core/file.h b/include/core/file.h index aa44018d..d7d0ce12 100644 --- a/include/core/file.h +++ b/include/core/file.h @@ -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. diff --git a/tests/core/database.c b/tests/core/database.c index 441e93ef..cb99c443 100644 --- a/tests/core/database.c +++ b/tests/core/database.c @@ -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); diff --git a/tests/core/date.c b/tests/core/date.c index 336a682f..df6a118e 100644 --- a/tests/core/date.c +++ b/tests/core/date.c @@ -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() diff --git a/tests/core/file.c b/tests/core/file.c index f51ca5ac..e6327a4c 100644 --- a/tests/core/file.c +++ b/tests/core/file.c @@ -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(©, NULL)); g_assert_true(cache_file_import(©, filepath)); g_assert_false(file_exists(©)); - cache_file_close(©); + file_close(©); g_assert_true(file_exists(©)); } diff --git a/tests/core/playlist.c b/tests/core/playlist.c index fd12861e..41b511a2 100644 --- a/tests/core/playlist.c +++ b/tests/core/playlist.c @@ -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); diff --git a/tests/core/tags/album.c b/tests/core/tags/album.c index 06f75124..7d70994e 100644 --- a/tests/core/tags/album.c +++ b/tests/core/tags/album.c @@ -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); } diff --git a/tests/core/tags/artist.c b/tests/core/tags/artist.c index bcd31248..bd58bca4 100644 --- a/tests/core/tags/artist.c +++ b/tests/core/tags/artist.c @@ -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); diff --git a/tests/core/tags/genre.c b/tests/core/tags/genre.c index da6dbdd7..d8a53f85 100644 --- a/tests/core/tags/genre.c +++ b/tests/core/tags/genre.c @@ -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); diff --git a/tests/core/tags/library.c b/tests/core/tags/library.c index 557a793d..ea7cf89c 100644 --- a/tests/core/tags/library.c +++ b/tests/core/tags/library.c @@ -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"); diff --git a/tests/core/tags/track.c b/tests/core/tags/track.c index 0f0d16cd..529f03cc 100644 --- a/tests/core/tags/track.c +++ b/tests/core/tags/track.c @@ -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);