core/file: Add cache_file_write() function

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2016-03-31 10:32:38 -04:00
parent 9db9df619f
commit 0344a2088d
3 changed files with 14 additions and 0 deletions

View File

@ -260,3 +260,10 @@ int file_writef(struct file *file, const char *fmt, ...)
REPORT_ERRNO(file->f_name);
return ret;
}
int cache_file_write(struct cache_file *file, const void *data, size_t len)
{
if (fwrite(data, len, 1, file->cf_file) == 1)
return len;
return -1;
}

View File

@ -135,4 +135,10 @@ int file_readf(struct file *, const char *, ...);
*/
int file_writef(struct file *, const char *, ...);
/*
* Write binary data to a cache file similar to fwrite(3).
* Returns the number of bytes successfully written.
*/
int cache_file_write(struct cache_file *, const void *, size_t);
#endif /* OCARINA_CORE_FILE_H */

View File

@ -166,6 +166,7 @@ static void test_cache()
test_equal(cache_file_open(&file, OPEN_WRITE), (bool)false);
test_equal(cache_file_exists(&file), (bool)false);
test_equal(cache_file_write(&file, "abcde", 5), 5);
cache_file_close(&file);
test_equal((void *)file.cf_file, NULL);
test_equal(cache_file_exists(&file), (bool)true);