diff --git a/core/file.c b/core/file.c index 10ad441e..26b2448c 100644 --- a/core/file.c +++ b/core/file.c @@ -40,6 +40,13 @@ static gchar *__file_build_tmp(const gchar *base, const gchar *dir, return res; } +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); @@ -134,11 +141,12 @@ const unsigned int file_version(struct file *file) bool file_exists(struct file *file) { - gchar *path = file_path(file); - bool ret = g_file_test(path, G_FILE_TEST_EXISTS); + return __file_exists(file_path(file)); +} - g_free(path); - return ret; +bool cache_file_exists(struct cache_file *file) +{ + return __file_exists(cache_file_path(file)); } static bool __file_open_read(struct file *file) diff --git a/include/core/file.h b/include/core/file.h index 2b02a0e0..15b12fff 100644 --- a/include/core/file.h +++ b/include/core/file.h @@ -89,6 +89,7 @@ const unsigned int file_version(struct file *); /* Returns true if the file exists on disk and false otherwise. */ bool file_exists(struct file *); +bool cache_file_exists(struct cache_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 af26267b..1e225666 100644 --- a/tests/core/file.c +++ b/tests/core/file.c @@ -158,6 +158,8 @@ static void test_cache() test_str_equal(cache_file_path(&file), filepath); test_str_equal(cache_file_write_path(&file), writepath); + + test_equal(cache_file_exists(&file), (bool)false); } DECLARE_UNIT_TESTS( diff --git a/tests/test.c b/tests/test.c index 7edb0589..db733d0f 100644 --- a/tests/test.c +++ b/tests/test.c @@ -108,6 +108,11 @@ gchar *test_data_file(const gchar *name) return g_strjoin("/", g_get_user_data_dir(), "ocarina-test", name, NULL); } +gchar *test_cache_file(const gchar *name) +{ + return g_strjoin("/", g_get_user_cache_dir(), "ocarina-test", name, NULL); +} + bool test_data_file_exists(const gchar *name) { GFileTest test = G_FILE_TEST_EXISTS; @@ -123,25 +128,36 @@ bool test_data_file_exists(const gchar *name) return ret; } -static void test_rm_data_dir() +static void test_rm_dir(const gchar *path) { const gchar *file = NULL; - gchar *path = test_data_file(NULL); - GDir *dir = g_dir_open(path, 0, NULL); + GDir *dir = g_dir_open(path, 0, NULL); if (!dir) - goto out; + return; while ((file = g_dir_read_name(dir)) != NULL) { gchar *fullpath = g_strjoin("/", path, file, NULL); + if (g_file_test(fullpath, G_FILE_TEST_IS_DIR)) + test_rm_dir(fullpath); g_remove(fullpath); g_free(fullpath); } g_dir_close(dir); g_rmdir(path); -out: - g_free(path); +} + +static void test_rm_dirs() +{ + gchar *data = test_data_file(NULL); + gchar *cache = test_cache_file(NULL); + + test_rm_dir(data); + test_rm_dir(cache); + + g_free(data); + g_free(cache); } int main(int argc, char **argv) @@ -149,7 +165,7 @@ int main(int argc, char **argv) unsigned int i; if (test_data_file_exists(NULL)) - test_rm_data_dir(); + test_rm_dirs(); for (i = 0; i < unit_tests_size; i++) { run_test(&unit_tests[i]);