core/file: Add cache_file_exists() function
Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
79ecaa11fb
commit
3723dff87d
16
core/file.c
16
core/file.c
|
@ -40,6 +40,13 @@ static gchar *__file_build_tmp(const gchar *base, const gchar *dir,
|
||||||
return res;
|
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)
|
static FILE *__file_open(gchar *path, const gchar *mode)
|
||||||
{
|
{
|
||||||
FILE *ret = g_fopen(path, 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)
|
bool file_exists(struct file *file)
|
||||||
{
|
{
|
||||||
gchar *path = file_path(file);
|
return __file_exists(file_path(file));
|
||||||
bool ret = g_file_test(path, G_FILE_TEST_EXISTS);
|
}
|
||||||
|
|
||||||
g_free(path);
|
bool cache_file_exists(struct cache_file *file)
|
||||||
return ret;
|
{
|
||||||
|
return __file_exists(cache_file_path(file));
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool __file_open_read(struct file *file)
|
static bool __file_open_read(struct file *file)
|
||||||
|
|
|
@ -89,6 +89,7 @@ const unsigned int 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 file_exists(struct file *);
|
bool file_exists(struct file *);
|
||||||
|
bool cache_file_exists(struct cache_file *);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Call to open a file for either reading or writing. Callers
|
* Call to open a file for either reading or writing. Callers
|
||||||
|
|
|
@ -158,6 +158,8 @@ static void test_cache()
|
||||||
|
|
||||||
test_str_equal(cache_file_path(&file), filepath);
|
test_str_equal(cache_file_path(&file), filepath);
|
||||||
test_str_equal(cache_file_write_path(&file), writepath);
|
test_str_equal(cache_file_write_path(&file), writepath);
|
||||||
|
|
||||||
|
test_equal(cache_file_exists(&file), (bool)false);
|
||||||
}
|
}
|
||||||
|
|
||||||
DECLARE_UNIT_TESTS(
|
DECLARE_UNIT_TESTS(
|
||||||
|
|
30
tests/test.c
30
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);
|
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)
|
bool test_data_file_exists(const gchar *name)
|
||||||
{
|
{
|
||||||
GFileTest test = G_FILE_TEST_EXISTS;
|
GFileTest test = G_FILE_TEST_EXISTS;
|
||||||
|
@ -123,25 +128,36 @@ bool test_data_file_exists(const gchar *name)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_rm_data_dir()
|
static void test_rm_dir(const gchar *path)
|
||||||
{
|
{
|
||||||
const gchar *file = NULL;
|
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)
|
if (!dir)
|
||||||
goto out;
|
return;
|
||||||
|
|
||||||
while ((file = g_dir_read_name(dir)) != NULL) {
|
while ((file = g_dir_read_name(dir)) != NULL) {
|
||||||
gchar *fullpath = g_strjoin("/", path, file, 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_remove(fullpath);
|
||||||
g_free(fullpath);
|
g_free(fullpath);
|
||||||
}
|
}
|
||||||
|
|
||||||
g_dir_close(dir);
|
g_dir_close(dir);
|
||||||
g_rmdir(path);
|
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)
|
int main(int argc, char **argv)
|
||||||
|
@ -149,7 +165,7 @@ int main(int argc, char **argv)
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
|
|
||||||
if (test_data_file_exists(NULL))
|
if (test_data_file_exists(NULL))
|
||||||
test_rm_data_dir();
|
test_rm_dirs();
|
||||||
|
|
||||||
for (i = 0; i < unit_tests_size; i++) {
|
for (i = 0; i < unit_tests_size; i++) {
|
||||||
run_test(&unit_tests[i]);
|
run_test(&unit_tests[i]);
|
||||||
|
|
Loading…
Reference in New Issue