core/file: Add file_remove() function

I plan to use this for upgrades to remove obsolete files.

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2016-04-01 08:15:07 -04:00
parent 0344a2088d
commit a6bfbbb1c6
3 changed files with 22 additions and 0 deletions

View File

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

View File

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

View File

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