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:
parent
0344a2088d
commit
a6bfbbb1c6
14
core/file.c
14
core/file.c
|
@ -267,3 +267,17 @@ int cache_file_write(struct cache_file *file, const void *data, size_t len)
|
||||||
return len;
|
return len;
|
||||||
return -1;
|
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;
|
||||||
|
}
|
||||||
|
|
|
@ -141,4 +141,7 @@ int file_writef(struct file *, const char *, ...);
|
||||||
*/
|
*/
|
||||||
int cache_file_write(struct cache_file *, const void *, size_t);
|
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 */
|
#endif /* OCARINA_CORE_FILE_H */
|
||||||
|
|
|
@ -81,7 +81,12 @@ static void test_file()
|
||||||
test_not_equal((void *)file.f_file, NULL);
|
test_not_equal((void *)file.f_file, NULL);
|
||||||
test_equal(file.f_mode, OPEN_READ);
|
test_equal(file.f_mode, OPEN_READ);
|
||||||
test_equal(file_open(&file, OPEN_READ), (bool)false);
|
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);
|
file_close(&file);
|
||||||
|
test_equal(file_remove(&file), (bool)true);
|
||||||
|
test_equal(file_exists(&file), (bool)false);
|
||||||
|
|
||||||
g_free(filepath);
|
g_free(filepath);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue