diff --git a/core/file.c b/core/file.c index 6786a3f9..e86eee75 100644 --- a/core/file.c +++ b/core/file.c @@ -267,3 +267,17 @@ int cache_file_write(struct cache_file *file, const void *data, size_t len) return len; return -1; } + +bool file_remove(struct file *file) +{ + int ret = -1; + gchar *path; + + if (!file->f_file) { + path = file_path(file); + ret = g_unlink(path); + g_free(path); + } + + return ret == 0; +} diff --git a/include/core/file.h b/include/core/file.h index c9a9976e..9516283b 100644 --- a/include/core/file.h +++ b/include/core/file.h @@ -141,4 +141,7 @@ int file_writef(struct file *, const char *, ...); */ int cache_file_write(struct cache_file *, const void *, size_t); +/* Removes a closed file from disk. */ +bool file_remove(struct file *); + #endif /* OCARINA_CORE_FILE_H */ diff --git a/tests/core/file.c b/tests/core/file.c index 648c9678..e282c878 100644 --- a/tests/core/file.c +++ b/tests/core/file.c @@ -81,7 +81,12 @@ static void test_file() test_not_equal((void *)file.f_file, NULL); test_equal(file.f_mode, OPEN_READ); test_equal(file_open(&file, OPEN_READ), (bool)false); + + test_equal(file_remove(&file), (bool)false); + test_equal(file_exists(&file), (bool)true); file_close(&file); + test_equal(file_remove(&file), (bool)true); + test_equal(file_exists(&file), (bool)false); g_free(filepath); }