core/file: Add cache_file_open() function
Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
3723dff87d
commit
e137eb0108
19
core/file.c
19
core/file.c
|
@ -57,9 +57,9 @@ static FILE *__file_open(gchar *path, const gchar *mode)
|
|||
return ret;
|
||||
}
|
||||
|
||||
static bool __file_mkdir(const gchar *basedir)
|
||||
static bool __file_mkdir(const gchar *basedir, const gchar *subdir)
|
||||
{
|
||||
gchar *dir = __file_path(basedir, NULL, NULL);
|
||||
gchar *dir = __file_path(basedir, subdir, NULL);
|
||||
int ret = g_mkdir_with_parents(dir, 0755);
|
||||
|
||||
if (ret != 0)
|
||||
|
@ -171,7 +171,7 @@ static bool __file_open_read(struct file *file)
|
|||
|
||||
static bool __file_open_write(struct file *file)
|
||||
{
|
||||
if (!__file_mkdir(g_get_user_data_dir()))
|
||||
if (!__file_mkdir(g_get_user_data_dir(), NULL))
|
||||
return false;
|
||||
if (!__file_can_write(file))
|
||||
return false;
|
||||
|
@ -194,6 +194,19 @@ bool file_open(struct file *file, enum open_mode mode)
|
|||
return __file_open_write(file);
|
||||
}
|
||||
|
||||
bool cache_file_open(struct cache_file *file, enum open_mode mode)
|
||||
{
|
||||
if (mode == OPEN_READ)
|
||||
return false;
|
||||
if ((string_length(file->cf_name) == 0) || (file->cf_file != NULL))
|
||||
return false;
|
||||
if (!__file_mkdir(g_get_user_cache_dir(), file->cf_subdir))
|
||||
return false;
|
||||
|
||||
file->cf_file = __file_open(cache_file_write_path(file), "wb");
|
||||
return file->cf_file != NULL;
|
||||
}
|
||||
|
||||
void file_close(struct file *file)
|
||||
{
|
||||
if (file->f_file) {
|
||||
|
|
|
@ -102,11 +102,13 @@ bool cache_file_exists(struct cache_file *);
|
|||
* When opening a file for writing (OPEN_WRITE):
|
||||
* - Create missing directories as needed.
|
||||
* - Open a temporary file to protect data if Ocarina crashes.
|
||||
* - Write file->_version to the start of the file.
|
||||
* - Write file->_version to the start of the file (data files only).
|
||||
*
|
||||
* Returns true if the open was successful and false otherwise.
|
||||
* Oening a cache file with OPEN_READ is currently unsupported.
|
||||
*/
|
||||
bool file_open(struct file *, enum open_mode);
|
||||
bool cache_file_open(struct cache_file *, enum open_mode);
|
||||
|
||||
/*
|
||||
* Closes an open file, setting file->f_file to NULL. If the file was opened
|
||||
|
|
|
@ -159,6 +159,12 @@ static void test_cache()
|
|||
test_str_equal(cache_file_path(&file), filepath);
|
||||
test_str_equal(cache_file_write_path(&file), writepath);
|
||||
|
||||
test_equal(cache_file_exists(&file), (bool)false);
|
||||
test_equal(cache_file_open(&file, OPEN_READ), (bool)false);
|
||||
test_equal(cache_file_open(&file, OPEN_WRITE), (bool)true);
|
||||
test_not_equal((void *)file.cf_file, NULL);
|
||||
test_equal(cache_file_open(&file, OPEN_WRITE), (bool)false);
|
||||
|
||||
test_equal(cache_file_exists(&file), (bool)false);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue