2015-10-09 13:55:34 -04:00
|
|
|
/*
|
2013-08-07 21:00:09 -04:00
|
|
|
* Copyright 2013 (c) Anna Schumaker.
|
2013-07-27 11:40:16 -04:00
|
|
|
*/
|
2014-06-05 10:19:22 -04:00
|
|
|
#include <core/file.h>
|
2015-10-08 09:41:51 -04:00
|
|
|
#include <core/string.h>
|
2016-02-05 07:57:22 -05:00
|
|
|
#include <core/version.h>
|
2015-10-08 09:41:51 -04:00
|
|
|
#include <errno.h>
|
2016-08-28 10:05:51 -04:00
|
|
|
#include <stdlib.h>
|
2016-03-30 10:11:02 -04:00
|
|
|
#include <unistd.h>
|
2013-07-27 11:40:16 -04:00
|
|
|
|
2016-03-30 14:17:12 -04:00
|
|
|
#define REPORT_ERROR(fname, error) \
|
|
|
|
g_printerr("%s (%s:%d): %s: %s\n", __func__, __FILE__, __LINE__, fname, error)
|
|
|
|
#define REPORT_ERRNO(fname) REPORT_ERROR(fname, strerror(errno))
|
2015-10-08 09:41:51 -04:00
|
|
|
|
2016-03-31 08:52:59 -04:00
|
|
|
static gchar *__file_path(const gchar *base, const gchar *dir,
|
|
|
|
const gchar *name)
|
2013-07-28 19:57:07 -04:00
|
|
|
{
|
2016-03-31 08:52:59 -04:00
|
|
|
return g_build_filename(base, OCARINA_NAME, dir ? dir : "", name, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gchar *__file_build_path(const gchar *base, const gchar *dir,
|
|
|
|
const gchar *name)
|
|
|
|
{
|
|
|
|
if (string_length(name) == 0)
|
|
|
|
return g_strdup("");
|
|
|
|
return __file_path(base, dir, name);
|
2013-07-27 11:40:16 -04:00
|
|
|
}
|
|
|
|
|
2016-03-31 09:25:49 -04:00
|
|
|
static gchar *__file_build_tmp(const gchar *base, const gchar *dir,
|
|
|
|
const gchar *name)
|
|
|
|
{
|
|
|
|
gchar *tmp, *res;
|
|
|
|
|
|
|
|
if (string_length(name) == 0)
|
|
|
|
return g_strdup("");
|
|
|
|
|
|
|
|
tmp = g_strdup_printf(".%s.tmp", name);
|
|
|
|
res = __file_path(base, dir, tmp);
|
|
|
|
g_free(tmp);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-03-31 09:31:55 -04:00
|
|
|
static bool __file_exists(gchar *path)
|
|
|
|
{
|
|
|
|
bool ret = g_file_test(path, G_FILE_TEST_EXISTS);
|
|
|
|
g_free(path);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-03-30 10:17:42 -04:00
|
|
|
static FILE *__file_open(gchar *path, const gchar *mode)
|
|
|
|
{
|
|
|
|
FILE *ret = g_fopen(path, mode);
|
|
|
|
|
|
|
|
if (!ret)
|
2016-03-30 14:17:12 -04:00
|
|
|
REPORT_ERRNO(path);
|
2016-03-30 10:17:42 -04:00
|
|
|
g_free(path);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-03-31 10:15:17 -04:00
|
|
|
static void __file_close(FILE *file, gchar *path, gchar *tmp)
|
|
|
|
{
|
|
|
|
if (file) {
|
|
|
|
fclose(file);
|
|
|
|
if (path && tmp)
|
|
|
|
g_rename(tmp, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_free(path);
|
|
|
|
g_free(tmp);
|
|
|
|
}
|
|
|
|
|
2016-03-31 09:57:24 -04:00
|
|
|
static bool __file_mkdir(const gchar *basedir, const gchar *subdir)
|
2015-09-10 09:20:43 -04:00
|
|
|
{
|
2016-03-31 09:57:24 -04:00
|
|
|
gchar *dir = __file_path(basedir, subdir, NULL);
|
2015-10-09 10:07:08 -04:00
|
|
|
int ret = g_mkdir_with_parents(dir, 0755);
|
|
|
|
|
|
|
|
if (ret != 0)
|
2016-03-30 14:17:12 -04:00
|
|
|
REPORT_ERRNO(dir);
|
2016-03-31 14:20:44 -04:00
|
|
|
g_free(dir);
|
2015-10-09 10:07:08 -04:00
|
|
|
return ret == 0;
|
2015-09-10 09:20:43 -04:00
|
|
|
}
|
|
|
|
|
2016-03-30 10:11:02 -04:00
|
|
|
static bool __file_can_write(struct file *file)
|
|
|
|
{
|
|
|
|
gchar *path = file_path(file);
|
|
|
|
bool ret = true;
|
|
|
|
|
|
|
|
if (g_access(path, F_OK) == 0 && g_access(path, W_OK) < 0)
|
|
|
|
ret = false;
|
|
|
|
|
|
|
|
g_free(path);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-11-01 21:30:06 -04:00
|
|
|
|
2016-08-17 14:17:52 -04:00
|
|
|
void file_init(struct file *file, const gchar *name, unsigned int min)
|
2013-07-28 19:57:07 -04:00
|
|
|
{
|
2015-10-09 08:10:18 -04:00
|
|
|
file->f_mode = OPEN_READ;
|
2016-08-17 14:17:52 -04:00
|
|
|
file->f_version = OCARINA_MINOR_VERSION;
|
2015-09-10 10:33:24 -04:00
|
|
|
file->f_prev = 0;
|
2016-08-12 11:30:56 -04:00
|
|
|
file->f_min = min;
|
2015-10-08 09:41:51 -04:00
|
|
|
file->f_file = NULL;
|
2016-03-29 08:13:56 -04:00
|
|
|
file->f_name = name;
|
2013-07-28 19:57:07 -04:00
|
|
|
}
|
|
|
|
|
2016-03-31 10:55:42 -04:00
|
|
|
void cache_file_init(struct cache_file *file, gchar *subdir, gchar *name)
|
|
|
|
{
|
|
|
|
file->cf_file = NULL;
|
|
|
|
file->cf_name = name;
|
|
|
|
file->cf_subdir = subdir;
|
|
|
|
}
|
|
|
|
|
2015-10-09 10:07:08 -04:00
|
|
|
gchar *file_path(struct file *file)
|
2013-07-27 11:40:16 -04:00
|
|
|
{
|
2016-03-31 08:52:59 -04:00
|
|
|
return __file_build_path(g_get_user_data_dir(), NULL, file->f_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
gchar *cache_file_path(struct cache_file *file)
|
|
|
|
{
|
|
|
|
return __file_build_path(g_get_user_cache_dir(), file->cf_subdir,
|
|
|
|
file->cf_name);
|
2013-07-27 11:40:16 -04:00
|
|
|
}
|
2013-07-28 19:57:07 -04:00
|
|
|
|
2016-03-30 10:11:02 -04:00
|
|
|
gchar *file_write_path(struct file *file)
|
|
|
|
{
|
2016-03-31 09:25:49 -04:00
|
|
|
return __file_build_tmp(g_get_user_data_dir(), NULL, file->f_name);
|
|
|
|
}
|
2016-03-30 10:11:02 -04:00
|
|
|
|
2016-03-31 09:25:49 -04:00
|
|
|
gchar *cache_file_write_path(struct cache_file *file)
|
|
|
|
{
|
|
|
|
return __file_build_tmp(g_get_user_cache_dir(), file->cf_subdir,
|
|
|
|
file->cf_name);
|
2016-03-30 10:11:02 -04:00
|
|
|
}
|
|
|
|
|
2015-09-10 08:10:38 -04:00
|
|
|
const unsigned int file_version(struct file *file)
|
2013-07-28 22:33:40 -04:00
|
|
|
{
|
2015-10-09 08:10:18 -04:00
|
|
|
if (file->f_file && (file->f_mode == OPEN_READ))
|
2015-09-10 08:10:38 -04:00
|
|
|
return file->f_prev;
|
|
|
|
return file->f_version;
|
2013-07-28 22:33:40 -04:00
|
|
|
}
|
|
|
|
|
2015-09-10 08:23:09 -04:00
|
|
|
bool file_exists(struct file *file)
|
2013-07-28 19:57:07 -04:00
|
|
|
{
|
2016-03-31 09:31:55 -04:00
|
|
|
return __file_exists(file_path(file));
|
|
|
|
}
|
2015-10-09 10:07:08 -04:00
|
|
|
|
2016-03-31 09:31:55 -04:00
|
|
|
bool cache_file_exists(struct cache_file *file)
|
|
|
|
{
|
|
|
|
return __file_exists(cache_file_path(file));
|
2014-03-02 14:44:24 -05:00
|
|
|
}
|
|
|
|
|
2015-09-10 09:20:43 -04:00
|
|
|
static bool __file_open_read(struct file *file)
|
|
|
|
{
|
|
|
|
if (!file_exists(file))
|
|
|
|
return false;
|
2016-03-30 10:17:42 -04:00
|
|
|
|
|
|
|
file->f_file = __file_open(file_path(file), "r");
|
|
|
|
if (!file->f_file)
|
2014-03-09 10:34:06 -04:00
|
|
|
return false;
|
2016-03-30 10:17:42 -04:00
|
|
|
|
|
|
|
file->f_mode = OPEN_READ;
|
2016-03-30 11:25:14 -04:00
|
|
|
if (file_readf(file, "%u\n", &file->f_prev) != 1)
|
|
|
|
return false;
|
|
|
|
if (file->f_prev < file->f_min) {
|
|
|
|
REPORT_ERROR(file->f_name, "File too old to be upgraded.");
|
|
|
|
file_close(file);
|
2016-08-28 10:05:51 -04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (file->f_prev > file->f_version) {
|
|
|
|
REPORT_ERROR(file->f_name, "File too new to be opened.");
|
|
|
|
file_close(file);
|
|
|
|
exit(1);
|
2016-03-30 11:25:14 -04:00
|
|
|
}
|
|
|
|
return true;
|
2013-07-28 19:57:07 -04:00
|
|
|
}
|
|
|
|
|
2015-09-10 09:20:43 -04:00
|
|
|
static bool __file_open_write(struct file *file)
|
2013-07-28 19:57:07 -04:00
|
|
|
{
|
2016-03-31 09:57:24 -04:00
|
|
|
if (!__file_mkdir(g_get_user_data_dir(), NULL))
|
2014-03-09 10:34:06 -04:00
|
|
|
return false;
|
2016-03-30 10:11:02 -04:00
|
|
|
if (!__file_can_write(file))
|
|
|
|
return false;
|
2016-03-30 10:17:42 -04:00
|
|
|
|
2016-03-30 10:11:02 -04:00
|
|
|
file->f_file = __file_open(file_write_path(file), "w");
|
2016-03-30 10:17:42 -04:00
|
|
|
if (!file->f_file)
|
2014-03-09 10:34:06 -04:00
|
|
|
return false;
|
2016-03-30 10:17:42 -04:00
|
|
|
|
|
|
|
file->f_mode = OPEN_WRITE;
|
2015-10-08 09:41:51 -04:00
|
|
|
return file_writef(file, "%d\n", file->f_version) > 0;
|
2013-07-28 19:57:07 -04:00
|
|
|
}
|
|
|
|
|
2015-10-09 13:55:34 -04:00
|
|
|
bool file_open(struct file *file, enum open_mode mode)
|
2013-07-28 19:57:07 -04:00
|
|
|
{
|
2016-03-29 08:13:56 -04:00
|
|
|
if ((string_length(file->f_name) == 0) || (file->f_file != NULL))
|
2014-03-09 10:34:06 -04:00
|
|
|
return false;
|
2013-07-28 19:57:07 -04:00
|
|
|
|
2015-09-10 09:20:43 -04:00
|
|
|
if (mode == OPEN_READ)
|
2015-09-10 09:46:33 -04:00
|
|
|
return __file_open_read(file);
|
|
|
|
return __file_open_write(file);
|
2013-07-28 19:57:07 -04:00
|
|
|
}
|
|
|
|
|
2016-03-31 09:57:24 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-09-10 09:12:48 -04:00
|
|
|
void file_close(struct file *file)
|
2013-07-28 19:57:07 -04:00
|
|
|
{
|
2016-03-31 10:15:17 -04:00
|
|
|
__file_close(file->f_file,
|
|
|
|
file->f_mode == OPEN_WRITE ? file_path(file) : NULL,
|
|
|
|
file->f_mode == OPEN_WRITE ? file_write_path(file) : NULL);
|
2015-10-09 08:10:18 -04:00
|
|
|
file->f_file = NULL;
|
2013-07-28 22:33:40 -04:00
|
|
|
}
|
|
|
|
|
2016-03-31 10:15:17 -04:00
|
|
|
void cache_file_close(struct cache_file *file)
|
|
|
|
{
|
|
|
|
__file_close(file->cf_file,
|
|
|
|
cache_file_path(file),
|
|
|
|
cache_file_write_path(file));
|
|
|
|
file->cf_file = NULL;
|
|
|
|
}
|
|
|
|
|
2015-10-08 11:16:38 -04:00
|
|
|
int file_readf(struct file *file, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list argp;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
va_start(argp, fmt);
|
|
|
|
ret = vfscanf(file->f_file, fmt, argp);
|
|
|
|
va_end(argp);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-10-09 13:09:47 -04:00
|
|
|
gchar *file_readl(struct file *file)
|
2013-07-28 22:33:40 -04:00
|
|
|
{
|
2015-10-09 13:09:47 -04:00
|
|
|
gchar *res;
|
2014-01-22 19:21:11 -05:00
|
|
|
|
2015-10-09 13:09:47 -04:00
|
|
|
if (file_readf(file, "%m[^\n]\n", &res) == 0)
|
|
|
|
return g_strdup("");
|
2014-01-22 19:21:11 -05:00
|
|
|
|
2015-10-09 13:09:47 -04:00
|
|
|
g_strstrip(res);
|
2013-07-28 22:33:40 -04:00
|
|
|
return res;
|
2013-07-28 19:57:07 -04:00
|
|
|
}
|
2015-10-08 09:41:51 -04:00
|
|
|
|
|
|
|
int file_writef(struct file *file, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list argp;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
va_start(argp, fmt);
|
|
|
|
ret = g_vfprintf(file->f_file, fmt, argp);
|
|
|
|
va_end(argp);
|
|
|
|
|
|
|
|
if (ret < 0)
|
2016-03-30 14:17:12 -04:00
|
|
|
REPORT_ERRNO(file->f_name);
|
2015-10-08 09:41:51 -04:00
|
|
|
return ret;
|
|
|
|
}
|
2016-03-31 10:32:38 -04:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2016-04-01 08:15:07 -04:00
|
|
|
|
2016-07-12 09:08:45 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-04-01 08:15:07 -04:00
|
|
|
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;
|
|
|
|
}
|