diff --git a/core/file.c b/core/file.c index 8978c0e5..6786a3f9 100644 --- a/core/file.c +++ b/core/file.c @@ -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; +} diff --git a/include/core/file.h b/include/core/file.h index ad10d297..c9a9976e 100644 --- a/include/core/file.h +++ b/include/core/file.h @@ -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 */ diff --git a/tests/core/file.c b/tests/core/file.c index 6d172f78..648c9678 100644 --- a/tests/core/file.c +++ b/tests/core/file.c @@ -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);