core/file: Add support for importing a file into the cache
This will be needed to allow the user to manually set album art from a downloaded file. Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
99b51f5257
commit
80d921343d
14
core/file.c
14
core/file.c
|
@ -268,6 +268,20 @@ int cache_file_write(struct cache_file *file, const void *data, size_t len)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool cache_file_import(struct cache_file *file, const gchar *srcpath)
|
||||||
|
{
|
||||||
|
gchar *contents = NULL;
|
||||||
|
gsize length = 0;
|
||||||
|
|
||||||
|
if (!file->cf_file || !srcpath)
|
||||||
|
return false;
|
||||||
|
if (!g_file_get_contents(srcpath, &contents, &length, NULL))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
cache_file_write(file, contents, length);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
bool file_remove(struct file *file)
|
bool file_remove(struct file *file)
|
||||||
{
|
{
|
||||||
int ret = -1;
|
int ret = -1;
|
||||||
|
|
|
@ -141,6 +141,9 @@ 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);
|
||||||
|
|
||||||
|
/* Import a file into the cache. */
|
||||||
|
bool cache_file_import(struct cache_file *, const gchar *);
|
||||||
|
|
||||||
/* Removes a closed file from disk. */
|
/* Removes a closed file from disk. */
|
||||||
bool file_remove(struct file *);
|
bool file_remove(struct file *);
|
||||||
|
|
||||||
|
|
|
@ -151,6 +151,7 @@ static void test_versioning()
|
||||||
static void test_cache()
|
static void test_cache()
|
||||||
{
|
{
|
||||||
struct cache_file file = CACHE_FILE_INIT("dir", "file.txt");
|
struct cache_file file = CACHE_FILE_INIT("dir", "file.txt");
|
||||||
|
struct cache_file copy = CACHE_FILE_INIT("dir", "copy.txt");
|
||||||
gchar *basepath, *filepath, *writepath;
|
gchar *basepath, *filepath, *writepath;
|
||||||
|
|
||||||
basepath = g_strjoin("/", g_get_user_cache_dir(), OCARINA_NAME, NULL);
|
basepath = g_strjoin("/", g_get_user_cache_dir(), OCARINA_NAME, NULL);
|
||||||
|
@ -164,6 +165,7 @@ static void test_cache()
|
||||||
test_str_equal(cache_file_path(&file), filepath);
|
test_str_equal(cache_file_path(&file), filepath);
|
||||||
test_str_equal(cache_file_write_path(&file), writepath);
|
test_str_equal(cache_file_write_path(&file), writepath);
|
||||||
|
|
||||||
|
/* Test writing data to a cache file. */
|
||||||
test_equal(cache_file_exists(&file), (bool)false);
|
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_READ), (bool)false);
|
||||||
test_equal(cache_file_open(&file, OPEN_WRITE), (bool)true);
|
test_equal(cache_file_open(&file, OPEN_WRITE), (bool)true);
|
||||||
|
@ -175,6 +177,17 @@ static void test_cache()
|
||||||
cache_file_close(&file);
|
cache_file_close(&file);
|
||||||
test_equal((void *)file.cf_file, NULL);
|
test_equal((void *)file.cf_file, NULL);
|
||||||
test_equal(cache_file_exists(&file), (bool)true);
|
test_equal(cache_file_exists(&file), (bool)true);
|
||||||
|
|
||||||
|
/* Test importing a file into the cache. */
|
||||||
|
test_equal(cache_file_exists(©), (bool)false);
|
||||||
|
test_equal(cache_file_import(©, filepath), (bool)false);
|
||||||
|
test_equal(cache_file_exists(©), (bool)false);
|
||||||
|
test_equal(cache_file_open(©, OPEN_WRITE), (bool)true);
|
||||||
|
test_equal(cache_file_import(©, NULL), (bool)false);
|
||||||
|
test_equal(cache_file_import(©, filepath), (bool)true);
|
||||||
|
test_equal(cache_file_exists(©), (bool)false);
|
||||||
|
cache_file_close(©);
|
||||||
|
test_equal(cache_file_exists(©), (bool)true);
|
||||||
}
|
}
|
||||||
|
|
||||||
DECLARE_UNIT_TESTS(
|
DECLARE_UNIT_TESTS(
|
||||||
|
|
Loading…
Reference in New Issue