core/file: Add cache_file_exists() function

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2016-03-31 09:31:55 -04:00
parent 79ecaa11fb
commit 3723dff87d
4 changed files with 38 additions and 11 deletions

View File

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

View File

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

View File

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

View File

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