core/file: Make a common file_remove() function

This change means removing cache files is now supported.  As a bonus,
we try to remove empty subdirectories to keep down the clutter.

Signed-off-by: Anna Schumaker <Anna@NoWheyCreamery.com>
This commit is contained in:
Anna Schumaker 2018-02-20 10:09:06 -05:00
parent 84a1022bdf
commit 8f13765b08
5 changed files with 28 additions and 14 deletions

View File

@ -134,7 +134,7 @@ static bool __audio_init_idle(void *data)
} else if (data_file_open(&audio_file, OPEN_READ)) {
data_file_readf(&audio_file, "%u", &track);
file_close(&audio_file);
data_file_remove(&audio_file);
file_remove(&audio_file);
__audio_load(track_get(track), LOAD_HISTORY);
}

View File

@ -260,15 +260,19 @@ bool cache_file_import(struct file *cache, const gchar *srcpath)
return true;
}
bool data_file_remove(struct file *data)
bool file_remove(struct file *file)
{
gchar *path, *dir;
int ret = -1;
gchar *path;
if (!data->f_file) {
path = file_path(data);
if (!file->f_file) {
path = file_path(file);
ret = g_unlink(path);
dir = g_path_get_dirname(path);
if (string_length(file->f_subdir) > 0)
g_rmdir(dir);
g_free(path);
g_free(dir);
}
return ret == 0;

View File

@ -157,7 +157,7 @@ static bool sys_pl_queued_load()
}
file_close(&sys_deck_f);
data_file_remove(&sys_deck_f);
file_remove(&sys_deck_f);
return true;
}
@ -196,7 +196,7 @@ static bool sys_pl_collection_load()
if (data_file_open(&sys_collection_f, OPEN_READ)) {
playlist_generic_load(playlist, &sys_collection_f, PL_SAVE_FLAGS);
file_close(&sys_collection_f);
data_file_remove(&sys_collection_f);
file_remove(&sys_collection_f);
}
return true;
@ -279,7 +279,7 @@ static bool __sys_pl_load()
}
file_close(&sys_file);
data_file_remove(&sys_file);
file_remove(&sys_file);
return true;
}

View File

@ -142,6 +142,6 @@ int cache_file_write(struct file *, const void *, size_t);
bool cache_file_import(struct file *, const gchar *);
/* Removes a closed file from disk. */
bool data_file_remove(struct file *);
bool file_remove(struct file *);
#endif /* OCARINA_CORE_FILE_H */

View File

@ -69,11 +69,11 @@ static void __test_file_subprocess()
g_assert_cmpuint(file.f_mode, ==, OPEN_READ);
g_assert_false(data_file_open(&file, OPEN_READ));
g_assert_false(data_file_remove(&file));
g_assert_false(file_remove(&file));
g_assert_true(file_exists(&file));
file_close(&file);
g_assert_cmpuint(file.f_mode, ==, CLOSED);
g_assert_true(data_file_remove(&file));
g_assert_true(file_remove(&file));
g_assert_false(file_exists(&file));
g_free(filepath);
@ -180,9 +180,9 @@ static void test_cache()
file_init_cache(&file, "dir", "file.txt");
file_init_cache(&copy, "dir", "copy.txt");
basepath = g_strjoin("/", g_get_user_cache_dir(), OCARINA_NAME, NULL);
filepath = g_strjoin("/", basepath, "dir", "file.txt", NULL);
writepath = g_strjoin("/", basepath, "dir", ".file.txt.tmp", NULL);
basepath = g_strjoin("/", g_get_user_cache_dir(), OCARINA_NAME, "dir", NULL);
filepath = g_strjoin("/", basepath, "file.txt", NULL);
writepath = g_strjoin("/", basepath, ".file.txt.tmp", NULL);
g_assert_null(file.f_file);
g_assert_cmpstr(file.f_name, ==, "file.txt");
@ -219,6 +219,16 @@ static void test_cache()
g_assert_false(file_exists(&copy));
file_close(&copy);
g_assert_true(file_exists(&copy));
/* Test removing cache files. */
g_assert_true(file_remove(&copy));
g_assert_false(file_exists(&copy));
g_assert_true(file_exists(&file));
g_assert_true(g_file_test(basepath, G_FILE_TEST_EXISTS));
g_assert_true(file_remove(&file));
g_assert_false(file_exists(&file));
g_assert_false(g_file_test(basepath, G_FILE_TEST_EXISTS));
}
int main(int argc, char **argv)