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 <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2018-02-16 16:51:41 -05:00
parent 3fdf89c75e
commit 3736b6cf3b
6 changed files with 28 additions and 38 deletions

View File

@ -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");

View File

@ -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);

View File

@ -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

View File

@ -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(&copy));
g_assert_false(file_exists(&copy));
g_assert_false(cache_file_import(&copy, filepath));
g_assert_false(cache_file_exists(&copy));
g_assert_false(file_exists(&copy));
g_assert_true(cache_file_open(&copy, OPEN_WRITE));
g_assert_false(cache_file_import(&copy, NULL));
g_assert_true(cache_file_import(&copy, filepath));
g_assert_false(cache_file_exists(&copy));
g_assert_false(file_exists(&copy));
cache_file_close(&copy);
g_assert_true(cache_file_exists(&copy));
g_assert_true(file_exists(&copy));
}
int main(int argc, char **argv)

View File

@ -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);

View File

@ -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());