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:
parent
3fdf89c75e
commit
3736b6cf3b
21
core/file.c
21
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);
|
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)
|
static FILE *__file_open(gchar *path, const gchar *mode)
|
||||||
{
|
{
|
||||||
FILE *ret = g_fopen(path, mode);
|
FILE *ret = g_fopen(path, mode);
|
||||||
|
@ -127,20 +120,18 @@ const unsigned int data_file_version(struct file *data)
|
||||||
return file->f_version;
|
return file->f_version;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool data_file_exists(struct file *file)
|
bool 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);
|
||||||
bool cache_file_exists(struct file *file)
|
return ret;
|
||||||
{
|
|
||||||
return __file_exists(file_path(file));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool __file_open_read(struct file *data)
|
static bool __file_open_read(struct file *data)
|
||||||
{
|
{
|
||||||
struct data_file *file = &data->f_data;
|
struct data_file *file = &data->f_data;
|
||||||
if (!data_file_exists(data))
|
if (!file_exists(data))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
data->f_file = __file_open(file_path(data), "r");
|
data->f_file = __file_open(file_path(data), "r");
|
||||||
|
|
|
@ -366,7 +366,7 @@ bool album_artwork_exists(struct album *album)
|
||||||
bool ret;
|
bool ret;
|
||||||
|
|
||||||
file = __album_alloc_file(album);
|
file = __album_alloc_file(album);
|
||||||
ret = cache_file_exists(&file->ac_file);
|
ret = file_exists(&file->ac_file);
|
||||||
__album_free_file(file);
|
__album_free_file(file);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -378,7 +378,7 @@ gchar *album_artwork_path(struct album *album)
|
||||||
gchar *ret = NULL;
|
gchar *ret = NULL;
|
||||||
|
|
||||||
file = __album_alloc_file(album);
|
file = __album_alloc_file(album);
|
||||||
if (cache_file_exists(&file->ac_file))
|
if (file_exists(&file->ac_file))
|
||||||
ret = file_path(&file->ac_file);
|
ret = file_path(&file->ac_file);
|
||||||
__album_free_file(file);
|
__album_free_file(file);
|
||||||
|
|
||||||
|
|
|
@ -84,8 +84,7 @@ gchar *file_write_path(struct file *);
|
||||||
const unsigned int data_file_version(struct file *);
|
const unsigned int data_file_version(struct file *);
|
||||||
|
|
||||||
/* Returns true if the file exists on disk and false otherwise. */
|
/* Returns true if the file exists on disk and false otherwise. */
|
||||||
bool data_file_exists(struct file *);
|
bool file_exists(struct file *);
|
||||||
bool cache_file_exists(struct file *);
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Call to open a file for either reading or writing. Callers
|
* Call to open a file for either reading or writing. Callers
|
||||||
|
|
|
@ -29,7 +29,7 @@ static void test_invalid_file(gconstpointer path)
|
||||||
g_assert_null(file.f_file);
|
g_assert_null(file.f_file);
|
||||||
g_assert_cmpuint(file.f_data.f_mode, ==, OPEN_READ);
|
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()
|
static void __test_file_subprocess()
|
||||||
|
@ -42,7 +42,7 @@ static void __test_file_subprocess()
|
||||||
realpath = g_strjoin("/", basepath, ".file.txt.tmp", NULL);
|
realpath = g_strjoin("/", basepath, ".file.txt.tmp", NULL);
|
||||||
|
|
||||||
test_verify_constructor(&file, filepath, realpath);
|
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_false(data_file_open(&file, OPEN_READ));
|
||||||
g_assert_true(data_file_open(&file, OPEN_WRITE));
|
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_cmpuint(file.f_data.f_mode, ==, OPEN_WRITE);
|
||||||
g_assert_false(data_file_open(&file, 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);
|
data_file_close(&file);
|
||||||
g_assert_null(file.f_file);
|
g_assert_null(file.f_file);
|
||||||
g_assert_cmpuint(file.f_data.f_mode, ==, OPEN_WRITE);
|
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_chmod(filepath, 0444);
|
||||||
g_assert_false(data_file_open(&file, OPEN_WRITE));
|
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_open(&file, OPEN_READ));
|
||||||
|
|
||||||
g_assert_false(data_file_remove(&file));
|
g_assert_false(data_file_remove(&file));
|
||||||
g_assert_true(data_file_exists(&file));
|
g_assert_true(file_exists(&file));
|
||||||
data_file_close(&file);
|
data_file_close(&file);
|
||||||
g_assert_true(data_file_remove(&file));
|
g_assert_true(data_file_remove(&file));
|
||||||
g_assert_false(data_file_exists(&file));
|
g_assert_false(file_exists(&file));
|
||||||
|
|
||||||
g_free(filepath);
|
g_free(filepath);
|
||||||
}
|
}
|
||||||
|
@ -102,7 +102,7 @@ static void test_io()
|
||||||
data_file_writef(&fout, "3 \n");
|
data_file_writef(&fout, "3 \n");
|
||||||
data_file_writef(&fout, "4 5 PQRST\n");
|
data_file_writef(&fout, "4 5 PQRST\n");
|
||||||
data_file_close(&fout);
|
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_true(data_file_open(&fin, OPEN_READ));
|
||||||
g_assert_cmpuint(data_file_version(&fin), ==, 1);
|
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));
|
g_assert_true(data_file_open(&fout, OPEN_WRITE));
|
||||||
data_file_writef(&fout, "abcdefghijklmnopqrstuvwxyz");
|
data_file_writef(&fout, "abcdefghijklmnopqrstuvwxyz");
|
||||||
data_file_close(&fout);
|
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_false(data_file_open(&fin, OPEN_READ));
|
||||||
g_assert_null(fin.f_file);
|
g_assert_null(fin.f_file);
|
||||||
|
@ -189,28 +189,28 @@ static void test_cache()
|
||||||
g_assert_cmpstr_free(file_write_path(&file), ==, writepath);
|
g_assert_cmpstr_free(file_write_path(&file), ==, writepath);
|
||||||
|
|
||||||
/* Test writing data to a cache file. */
|
/* 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_false(cache_file_open(&file, OPEN_READ));
|
||||||
g_assert_true(cache_file_open(&file, OPEN_WRITE));
|
g_assert_true(cache_file_open(&file, OPEN_WRITE));
|
||||||
g_assert_nonnull(file.f_file);
|
g_assert_nonnull(file.f_file);
|
||||||
g_assert_false(cache_file_open(&file, OPEN_WRITE));
|
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);
|
g_assert_cmpuint(cache_file_write(&file, "abcde", 5), ==, 5);
|
||||||
cache_file_close(&file);
|
cache_file_close(&file);
|
||||||
g_assert_null(file.f_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. */
|
/* 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_import(©, filepath));
|
||||||
g_assert_false(cache_file_exists(©));
|
g_assert_false(file_exists(©));
|
||||||
g_assert_true(cache_file_open(©, OPEN_WRITE));
|
g_assert_true(cache_file_open(©, OPEN_WRITE));
|
||||||
g_assert_false(cache_file_import(©, NULL));
|
g_assert_false(cache_file_import(©, NULL));
|
||||||
g_assert_true(cache_file_import(©, filepath));
|
g_assert_true(cache_file_import(©, filepath));
|
||||||
g_assert_false(cache_file_exists(©));
|
g_assert_false(file_exists(©));
|
||||||
cache_file_close(©);
|
cache_file_close(©);
|
||||||
g_assert_true(cache_file_exists(©));
|
g_assert_true(file_exists(©));
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
int main(int argc, char **argv)
|
||||||
|
|
|
@ -343,7 +343,7 @@ static void test_save_load()
|
||||||
playlist_current_set(&p, 3);
|
playlist_current_set(&p, 3);
|
||||||
playlist_current_set(&q, 4);
|
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));
|
g_assert_true( data_file_open(&f, OPEN_WRITE));
|
||||||
playlist_generic_save(&p, &f, PL_SAVE_METADATA);
|
playlist_generic_save(&p, &f, PL_SAVE_METADATA);
|
||||||
playlist_generic_save(&q, &f, PL_SAVE_ALL);
|
playlist_generic_save(&q, &f, PL_SAVE_ALL);
|
||||||
|
|
|
@ -19,7 +19,7 @@ static void test_settings()
|
||||||
g_assert_false(settings_has("test.value1"));
|
g_assert_false(settings_has("test.value1"));
|
||||||
g_assert_cmpuint(settings_get("test.value1"), ==, 0);
|
g_assert_cmpuint(settings_get("test.value1"), ==, 0);
|
||||||
g_assert_cmpuint(settings_get("test.value2"), ==, 0);
|
g_assert_cmpuint(settings_get("test.value2"), ==, 0);
|
||||||
g_assert_false(data_file_exists(&f));
|
g_assert_false(file_exists(&f));
|
||||||
|
|
||||||
settings_init();
|
settings_init();
|
||||||
g_assert_nonnull(test_get_settings());
|
g_assert_nonnull(test_get_settings());
|
||||||
|
@ -27,7 +27,7 @@ static void test_settings()
|
||||||
settings_set("test.value1", 42);
|
settings_set("test.value1", 42);
|
||||||
g_assert_true(settings_has("test.value1"));
|
g_assert_true(settings_has("test.value1"));
|
||||||
g_assert_false(settings_has("test.value2"));
|
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);
|
settings_set("test.value2", 84);
|
||||||
g_assert_true(settings_has("test.value2"));
|
g_assert_true(settings_has("test.value2"));
|
||||||
g_assert_cmpuint(settings_get("test.value1"), ==, 42);
|
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_cmpuint(settings_get("test.value2"), ==, 0);
|
||||||
g_assert_false(settings_has("test.value1"));
|
g_assert_false(settings_has("test.value1"));
|
||||||
g_assert_false(settings_has("test.value2"));
|
g_assert_false(settings_has("test.value2"));
|
||||||
g_assert_true(data_file_exists(&f));
|
g_assert_true(file_exists(&f));
|
||||||
|
|
||||||
settings_init();
|
settings_init();
|
||||||
g_assert_nonnull(test_get_settings());
|
g_assert_nonnull(test_get_settings());
|
||||||
|
|
Loading…
Reference in New Issue