From 3736b6cf3bccc5cecd8cd5746b6224bd65443804 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Fri, 16 Feb 2018 16:51:41 -0500 Subject: [PATCH] core/file: Create a single file_exists() function We can use the single file_path() function to check if files exist, too. Signed-off-by: Anna Schumaker --- core/file.c | 21 ++++++--------------- core/tags/album.c | 4 ++-- include/core/file.h | 3 +-- tests/core/file.c | 30 +++++++++++++++--------------- tests/core/playlist.c | 2 +- tests/core/settings.c | 6 +++--- 6 files changed, 28 insertions(+), 38 deletions(-) diff --git a/core/file.c b/core/file.c index 71e1f6aa..befa0c76 100644 --- a/core/file.c +++ b/core/file.c @@ -27,13 +27,6 @@ static gchar *__file_path(const gchar *base, const gchar *dir, return g_build_filename(base, OCARINA_NAME, dir ? dir : "", name, NULL); } -static bool __file_exists(gchar *path) -{ - bool ret = g_file_test(path, G_FILE_TEST_EXISTS); - g_free(path); - return ret; -} - static FILE *__file_open(gchar *path, const gchar *mode) { FILE *ret = g_fopen(path, mode); @@ -127,20 +120,18 @@ const unsigned int data_file_version(struct file *data) return file->f_version; } -bool data_file_exists(struct file *file) +bool file_exists(struct file *file) { - return __file_exists(file_path(file)); -} - -bool cache_file_exists(struct file *file) -{ - return __file_exists(file_path(file)); + gchar *path = file_path(file); + bool ret = g_file_test(path, G_FILE_TEST_EXISTS); + g_free(path); + return ret; } static bool __file_open_read(struct file *data) { struct data_file *file = &data->f_data; - if (!data_file_exists(data)) + if (!file_exists(data)) return false; data->f_file = __file_open(file_path(data), "r"); diff --git a/core/tags/album.c b/core/tags/album.c index 6eed6268..a3877ace 100644 --- a/core/tags/album.c +++ b/core/tags/album.c @@ -366,7 +366,7 @@ bool album_artwork_exists(struct album *album) bool ret; file = __album_alloc_file(album); - ret = cache_file_exists(&file->ac_file); + ret = file_exists(&file->ac_file); __album_free_file(file); return ret; @@ -378,7 +378,7 @@ gchar *album_artwork_path(struct album *album) gchar *ret = NULL; file = __album_alloc_file(album); - if (cache_file_exists(&file->ac_file)) + if (file_exists(&file->ac_file)) ret = file_path(&file->ac_file); __album_free_file(file); diff --git a/include/core/file.h b/include/core/file.h index 27f3fe9e..856e4e7c 100644 --- a/include/core/file.h +++ b/include/core/file.h @@ -84,8 +84,7 @@ gchar *file_write_path(struct file *); const unsigned int data_file_version(struct file *); /* Returns true if the file exists on disk and false otherwise. */ -bool data_file_exists(struct file *); -bool cache_file_exists(struct file *); +bool file_exists(struct file *); /* * Call to open a file for either reading or writing. Callers diff --git a/tests/core/file.c b/tests/core/file.c index 1bedc0de..02a232a6 100644 --- a/tests/core/file.c +++ b/tests/core/file.c @@ -29,7 +29,7 @@ static void test_invalid_file(gconstpointer path) g_assert_null(file.f_file); g_assert_cmpuint(file.f_data.f_mode, ==, OPEN_READ); - g_assert_false(data_file_exists(&file)); + g_assert_false(file_exists(&file)); } static void __test_file_subprocess() @@ -42,7 +42,7 @@ static void __test_file_subprocess() realpath = g_strjoin("/", basepath, ".file.txt.tmp", NULL); test_verify_constructor(&file, filepath, realpath); - g_assert_false(data_file_exists(&file)); + g_assert_false(file_exists(&file)); g_assert_false(data_file_open(&file, OPEN_READ)); g_assert_true(data_file_open(&file, OPEN_WRITE)); @@ -50,11 +50,11 @@ static void __test_file_subprocess() g_assert_cmpuint(file.f_data.f_mode, ==, OPEN_WRITE); g_assert_false(data_file_open(&file, OPEN_WRITE)); - g_assert_false(data_file_exists(&file)); + g_assert_false(file_exists(&file)); data_file_close(&file); g_assert_null(file.f_file); g_assert_cmpuint(file.f_data.f_mode, ==, OPEN_WRITE); - g_assert_true(data_file_exists(&file)); + g_assert_true(file_exists(&file)); g_chmod(filepath, 0444); g_assert_false(data_file_open(&file, OPEN_WRITE)); @@ -68,10 +68,10 @@ static void __test_file_subprocess() g_assert_false(data_file_open(&file, OPEN_READ)); g_assert_false(data_file_remove(&file)); - g_assert_true(data_file_exists(&file)); + g_assert_true(file_exists(&file)); data_file_close(&file); g_assert_true(data_file_remove(&file)); - g_assert_false(data_file_exists(&file)); + g_assert_false(file_exists(&file)); g_free(filepath); } @@ -102,7 +102,7 @@ static void test_io() data_file_writef(&fout, "3 \n"); data_file_writef(&fout, "4 5 PQRST\n"); data_file_close(&fout); - g_assert_true(data_file_exists(&fout)); + g_assert_true(file_exists(&fout)); g_assert_true(data_file_open(&fin, OPEN_READ)); g_assert_cmpuint(data_file_version(&fin), ==, 1); @@ -139,7 +139,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); - g_assert_true(data_file_exists(&fout)); + g_assert_true(file_exists(&fout)); g_assert_false(data_file_open(&fin, OPEN_READ)); g_assert_null(fin.f_file); @@ -189,28 +189,28 @@ static void test_cache() g_assert_cmpstr_free(file_write_path(&file), ==, writepath); /* Test writing data to a cache file. */ - g_assert_false(cache_file_exists(&file)); + g_assert_false(file_exists(&file)); g_assert_false(cache_file_open(&file, OPEN_READ)); g_assert_true(cache_file_open(&file, OPEN_WRITE)); g_assert_nonnull(file.f_file); g_assert_false(cache_file_open(&file, OPEN_WRITE)); - g_assert_false(cache_file_exists(&file)); + g_assert_false(file_exists(&file)); g_assert_cmpuint(cache_file_write(&file, "abcde", 5), ==, 5); cache_file_close(&file); g_assert_null(file.f_file); - g_assert_true(cache_file_exists(&file)); + g_assert_true(file_exists(&file)); /* Test importing a file into the cache. */ - g_assert_false(cache_file_exists(©)); + g_assert_false(file_exists(©)); g_assert_false(cache_file_import(©, filepath)); - g_assert_false(cache_file_exists(©)); + g_assert_false(file_exists(©)); g_assert_true(cache_file_open(©, OPEN_WRITE)); g_assert_false(cache_file_import(©, NULL)); g_assert_true(cache_file_import(©, filepath)); - g_assert_false(cache_file_exists(©)); + g_assert_false(file_exists(©)); cache_file_close(©); - g_assert_true(cache_file_exists(©)); + g_assert_true(file_exists(©)); } int main(int argc, char **argv) diff --git a/tests/core/playlist.c b/tests/core/playlist.c index 17662ea1..fd12861e 100644 --- a/tests/core/playlist.c +++ b/tests/core/playlist.c @@ -343,7 +343,7 @@ static void test_save_load() playlist_current_set(&p, 3); playlist_current_set(&q, 4); - g_assert_false(data_file_exists(&f)); + g_assert_false(file_exists(&f)); 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); diff --git a/tests/core/settings.c b/tests/core/settings.c index 60e11005..14de75a2 100644 --- a/tests/core/settings.c +++ b/tests/core/settings.c @@ -19,7 +19,7 @@ static void test_settings() g_assert_false(settings_has("test.value1")); g_assert_cmpuint(settings_get("test.value1"), ==, 0); g_assert_cmpuint(settings_get("test.value2"), ==, 0); - g_assert_false(data_file_exists(&f)); + g_assert_false(file_exists(&f)); settings_init(); g_assert_nonnull(test_get_settings()); @@ -27,7 +27,7 @@ static void test_settings() settings_set("test.value1", 42); g_assert_true(settings_has("test.value1")); g_assert_false(settings_has("test.value2")); - g_assert_true(data_file_exists(&f)); + g_assert_true(file_exists(&f)); settings_set("test.value2", 84); g_assert_true(settings_has("test.value2")); g_assert_cmpuint(settings_get("test.value1"), ==, 42); @@ -41,7 +41,7 @@ static void test_settings() g_assert_cmpuint(settings_get("test.value2"), ==, 0); g_assert_false(settings_has("test.value1")); g_assert_false(settings_has("test.value2")); - g_assert_true(data_file_exists(&f)); + g_assert_true(file_exists(&f)); settings_init(); g_assert_nonnull(test_get_settings());