From 8f13765b08fa76cf00b2f955aeb0d8243ca4a709 Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Tue, 20 Feb 2018 10:09:06 -0500 Subject: [PATCH] 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 --- core/audio.c | 2 +- core/file.c | 12 ++++++++---- core/playlists/system.c | 6 +++--- include/core/file.h | 2 +- tests/core/file.c | 20 +++++++++++++++----- 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/core/audio.c b/core/audio.c index 50e13d35..55d1a30d 100644 --- a/core/audio.c +++ b/core/audio.c @@ -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); } diff --git a/core/file.c b/core/file.c index b44e6ecd..826e01b7 100644 --- a/core/file.c +++ b/core/file.c @@ -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; diff --git a/core/playlists/system.c b/core/playlists/system.c index 8572d67b..8029efe1 100644 --- a/core/playlists/system.c +++ b/core/playlists/system.c @@ -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; } diff --git a/include/core/file.h b/include/core/file.h index d7d0ce12..af34c325 100644 --- a/include/core/file.h +++ b/include/core/file.h @@ -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 */ diff --git a/tests/core/file.c b/tests/core/file.c index e6327a4c..58c341f8 100644 --- a/tests/core/file.c +++ b/tests/core/file.c @@ -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(©, "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(©)); file_close(©); g_assert_true(file_exists(©)); + + /* Test removing cache files. */ + g_assert_true(file_remove(©)); + g_assert_false(file_exists(©)); + 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)