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
|
|
|
|
2018-02-16 15:50:46 -05:00
|
|
|
static void __file_init_common(struct file *file, const gchar *subdir,
|
2018-02-20 10:35:34 -05:00
|
|
|
const gchar *name, unsigned int min,
|
|
|
|
const gchar *(*user_dir)(void))
|
2018-02-16 15:50:46 -05:00
|
|
|
{
|
2018-02-16 16:11:21 -05:00
|
|
|
file->f_file = NULL;
|
|
|
|
file->f_name = name;
|
|
|
|
file->f_subdir = subdir;
|
2018-02-20 09:31:40 -05:00
|
|
|
file->f_mode = CLOSED;
|
2018-02-20 10:35:34 -05:00
|
|
|
file->f_version = OCARINA_MINOR_VERSION;
|
|
|
|
file->f_prev = 0;
|
|
|
|
file->f_min = min;
|
2018-02-16 16:11:21 -05:00
|
|
|
file->f_user_dir = user_dir;
|
2018-02-16 15:50:46 -05:00
|
|
|
}
|
|
|
|
|
2018-02-20 14:26:47 -05:00
|
|
|
static bool __file_open(struct file *file, enum open_mode mode)
|
2016-03-30 10:17:42 -04:00
|
|
|
{
|
2018-02-20 14:26:47 -05:00
|
|
|
gchar *cmode, *path;
|
2016-03-30 10:17:42 -04:00
|
|
|
|
2018-02-20 14:26:47 -05:00
|
|
|
if (mode == OPEN_READ || mode == OPEN_READ_BINARY) {
|
|
|
|
cmode = "r";
|
|
|
|
path = file_path(file);
|
|
|
|
} else {
|
|
|
|
cmode = "w";
|
|
|
|
path = file_write_path(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
file->f_file = g_fopen(path, cmode);
|
|
|
|
if (!file->f_file)
|
2016-03-30 14:17:12 -04:00
|
|
|
REPORT_ERRNO(path);
|
2016-03-30 10:17:42 -04:00
|
|
|
g_free(path);
|
2018-02-20 14:26:47 -05:00
|
|
|
return file->f_file != NULL;
|
2016-03-30 10:17:42 -04:00
|
|
|
}
|
|
|
|
|
2018-02-20 15:38:28 -05:00
|
|
|
static bool __file_mkdir(struct file *file)
|
2015-09-10 09:20:43 -04:00
|
|
|
{
|
2018-02-20 15:38:28 -05:00
|
|
|
gchar *dir = g_build_filename(file->f_user_dir(), OCARINA_NAME,
|
|
|
|
file->f_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
|
|
|
}
|
|
|
|
|
2018-02-20 15:38:28 -05:00
|
|
|
static bool __file_can_write(struct file *file)
|
2016-03-30 10:11:02 -04:00
|
|
|
{
|
2018-02-20 15:38:28 -05:00
|
|
|
gchar *path = file_path(file);
|
2016-03-30 10:11:02 -04:00
|
|
|
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
|
|
|
|
2018-02-16 15:50:46 -05:00
|
|
|
void file_init_data(struct file *file, const gchar *subdir,
|
|
|
|
const gchar *name, unsigned int min)
|
2013-07-28 19:57:07 -04:00
|
|
|
{
|
2018-02-20 10:35:34 -05:00
|
|
|
__file_init_common(file, subdir, name, min, g_get_user_data_dir);
|
2013-07-28 19:57:07 -04:00
|
|
|
}
|
|
|
|
|
2018-02-16 15:50:46 -05:00
|
|
|
void file_init_cache(struct file *file, const gchar *subdir, const gchar *name)
|
2016-03-31 10:55:42 -04:00
|
|
|
{
|
2018-02-20 10:35:34 -05:00
|
|
|
__file_init_common(file, subdir, name, 0, g_get_user_cache_dir);
|
2016-03-31 10:55:42 -04:00
|
|
|
}
|
|
|
|
|
2018-02-16 16:11:21 -05:00
|
|
|
gchar *file_path(struct file *file)
|
2013-07-27 11:40:16 -04:00
|
|
|
{
|
2018-02-16 16:11:21 -05:00
|
|
|
if (string_length(file->f_name) == 0)
|
|
|
|
return g_strdup("");
|
|
|
|
return g_build_filename(file->f_user_dir(), OCARINA_NAME,
|
|
|
|
file->f_subdir, file->f_name, NULL);
|
2013-07-27 11:40:16 -04:00
|
|
|
}
|
2013-07-28 19:57:07 -04:00
|
|
|
|
2018-02-16 16:22:30 -05:00
|
|
|
gchar *file_write_path(struct file *file)
|
2016-03-30 10:11:02 -04:00
|
|
|
{
|
2018-02-16 16:22:30 -05:00
|
|
|
gchar *tmp, *res;
|
2016-03-30 10:11:02 -04:00
|
|
|
|
2018-02-16 16:22:30 -05:00
|
|
|
if (string_length(file->f_name) == 0)
|
|
|
|
return g_strdup("");
|
|
|
|
|
|
|
|
tmp = g_strdup_printf(".%s.tmp", file->f_name);
|
|
|
|
res = g_build_filename(file->f_user_dir(), OCARINA_NAME,
|
|
|
|
file->f_subdir, tmp, NULL);
|
|
|
|
g_free(tmp);
|
|
|
|
|
|
|
|
return res;
|
2016-03-30 10:11:02 -04:00
|
|
|
}
|
|
|
|
|
2018-02-20 15:43:56 -05:00
|
|
|
const unsigned int file_version(struct file *file)
|
2013-07-28 22:33:40 -04:00
|
|
|
{
|
2018-02-20 15:43:56 -05:00
|
|
|
if (file->f_file && (file->f_mode == OPEN_READ))
|
|
|
|
return file->f_prev;
|
|
|
|
return file->f_version;
|
2013-07-28 22:33:40 -04:00
|
|
|
}
|
|
|
|
|
2018-02-16 16:51:41 -05:00
|
|
|
bool file_exists(struct file *file)
|
2013-07-28 19:57:07 -04:00
|
|
|
{
|
2018-02-16 16:51:41 -05:00
|
|
|
gchar *path = file_path(file);
|
|
|
|
bool ret = g_file_test(path, G_FILE_TEST_EXISTS);
|
|
|
|
g_free(path);
|
|
|
|
return ret;
|
2014-03-02 14:44:24 -05:00
|
|
|
}
|
|
|
|
|
2018-02-20 14:26:47 -05:00
|
|
|
static bool __file_open_read(struct file *file, enum open_mode mode)
|
2015-09-10 09:20:43 -04:00
|
|
|
{
|
2018-02-20 14:26:47 -05:00
|
|
|
if (!file_exists(file))
|
2015-09-10 09:20:43 -04:00
|
|
|
return false;
|
2018-02-20 14:26:47 -05:00
|
|
|
if (!__file_open(file, mode))
|
2014-03-09 10:34:06 -04:00
|
|
|
return false;
|
2016-03-30 10:17:42 -04:00
|
|
|
|
2018-02-20 14:26:47 -05:00
|
|
|
file->f_mode = mode;
|
|
|
|
if (mode == OPEN_READ_BINARY)
|
|
|
|
return true;
|
|
|
|
|
2018-02-21 16:01:15 -05:00
|
|
|
file->f_prev = file_readu(file);
|
2018-02-20 14:26:47 -05:00
|
|
|
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);
|
|
|
|
}
|
2018-02-20 14:26:47 -05:00
|
|
|
if (file->f_prev > file->f_version) {
|
|
|
|
REPORT_ERROR(file->f_name, "File too new to be opened.");
|
|
|
|
file_close(file);
|
2016-08-28 10:05:51 -04:00
|
|
|
exit(1);
|
2016-03-30 11:25:14 -04:00
|
|
|
}
|
|
|
|
return true;
|
2013-07-28 19:57:07 -04:00
|
|
|
}
|
|
|
|
|
2018-02-20 15:38:28 -05:00
|
|
|
static bool __file_open_write(struct file *file, enum open_mode mode)
|
2013-07-28 19:57:07 -04:00
|
|
|
{
|
2018-02-20 15:38:28 -05:00
|
|
|
if (!__file_mkdir(file))
|
2014-03-09 10:34:06 -04:00
|
|
|
return false;
|
2018-02-20 15:38:28 -05:00
|
|
|
if (!__file_can_write(file))
|
2016-03-30 10:11:02 -04:00
|
|
|
return false;
|
2018-02-20 15:38:28 -05:00
|
|
|
if (!__file_open(file, OPEN_WRITE))
|
2014-03-09 10:34:06 -04:00
|
|
|
return false;
|
2016-03-30 10:17:42 -04:00
|
|
|
|
2018-02-20 15:38:28 -05:00
|
|
|
file->f_mode = mode;
|
|
|
|
if (mode == OPEN_WRITE_BINARY)
|
|
|
|
return true;
|
2018-02-21 08:19:19 -05:00
|
|
|
return file_writef(file, "%d\n", file->f_version) > 0;
|
2016-03-31 09:57:24 -04:00
|
|
|
}
|
|
|
|
|
2018-02-20 14:26:47 -05:00
|
|
|
bool file_open(struct file *file, enum open_mode mode)
|
|
|
|
{
|
|
|
|
if ((string_length(file->f_name) == 0) || (file->f_file != NULL))
|
|
|
|
return false;
|
|
|
|
if (mode == CLOSED)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (mode == OPEN_READ || mode == OPEN_READ_BINARY)
|
|
|
|
return __file_open_read(file, mode);
|
2018-02-20 15:38:28 -05:00
|
|
|
return __file_open_write(file, mode);
|
2018-02-20 14:26:47 -05:00
|
|
|
}
|
|
|
|
|
2018-02-20 09:53:53 -05:00
|
|
|
void file_close(struct file *file)
|
2013-07-28 19:57:07 -04:00
|
|
|
{
|
2018-02-20 09:53:53 -05:00
|
|
|
gchar *path = file_path(file);
|
|
|
|
gchar *tmp = file_write_path(file);
|
2013-07-28 22:33:40 -04:00
|
|
|
|
2018-02-20 09:53:53 -05:00
|
|
|
if (file->f_file) {
|
|
|
|
fclose(file->f_file);
|
2018-02-20 15:38:28 -05:00
|
|
|
if (file->f_mode == OPEN_WRITE || file->f_mode == OPEN_WRITE_BINARY)
|
2018-02-20 09:53:53 -05:00
|
|
|
g_rename(tmp, path);
|
|
|
|
}
|
|
|
|
|
|
|
|
file->f_file = NULL;
|
|
|
|
file->f_mode = CLOSED;
|
|
|
|
|
|
|
|
g_free(path);
|
|
|
|
g_free(tmp);
|
2016-03-31 10:15:17 -04:00
|
|
|
}
|
|
|
|
|
2018-02-21 16:01:15 -05:00
|
|
|
gchar *file_readw(struct file *file)
|
2015-10-08 11:16:38 -04:00
|
|
|
{
|
2018-02-21 16:01:15 -05:00
|
|
|
gchar *s;
|
|
|
|
return fscanf(file->f_file, "%ms%*c", &s) ? s : g_strdup("");
|
2015-10-08 11:16:38 -04:00
|
|
|
}
|
|
|
|
|
2018-02-21 16:01:15 -05:00
|
|
|
gchar *file_readl(struct file *file)
|
2013-07-28 22:33:40 -04:00
|
|
|
{
|
2018-02-21 16:01:15 -05:00
|
|
|
gchar *s = NULL;
|
|
|
|
size_t len = 0;
|
|
|
|
return getline(&s, &len, file->f_file) ? g_strchomp(s) : g_strdup("");
|
|
|
|
}
|
2014-01-22 19:21:11 -05:00
|
|
|
|
2018-02-21 16:01:15 -05:00
|
|
|
unsigned int file_readu(struct file *file)
|
|
|
|
{
|
|
|
|
unsigned int u;
|
|
|
|
return fscanf(file->f_file, "%u%*c", &u) ? u : 0;
|
2013-07-28 19:57:07 -04:00
|
|
|
}
|
2015-10-08 09:41:51 -04:00
|
|
|
|
2018-02-21 08:19:19 -05:00
|
|
|
int file_writef(struct file *file, const char *fmt, ...)
|
2015-10-08 09:41:51 -04:00
|
|
|
{
|
|
|
|
va_list argp;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
va_start(argp, fmt);
|
2018-02-21 08:19:19 -05:00
|
|
|
ret = g_vfprintf(file->f_file, fmt, argp);
|
2015-10-08 09:41:51 -04:00
|
|
|
va_end(argp);
|
|
|
|
|
|
|
|
if (ret < 0)
|
2018-02-21 08:19:19 -05:00
|
|
|
REPORT_ERRNO(file->f_name);
|
2015-10-08 09:41:51 -04:00
|
|
|
return ret;
|
|
|
|
}
|
2016-03-31 10:32:38 -04:00
|
|
|
|
2018-02-20 16:44:57 -05:00
|
|
|
gchar *file_read(struct file *file)
|
2016-03-31 10:32:38 -04:00
|
|
|
{
|
2018-02-20 16:44:57 -05:00
|
|
|
int fd = fileno(file->f_file);
|
|
|
|
struct stat st;
|
|
|
|
gchar *buf;
|
|
|
|
|
|
|
|
if (fstat(fd, &st) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
buf = g_malloc0(st.st_size + 1);
|
|
|
|
if (fread(buf, st.st_size, 1, file->f_file) == 1)
|
|
|
|
return buf;
|
|
|
|
|
|
|
|
g_free(buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int file_write(struct file *file, const void *data, size_t len)
|
|
|
|
{
|
|
|
|
if (fwrite(data, len, 1, file->f_file) == 1)
|
2016-03-31 10:32:38 -04:00
|
|
|
return len;
|
|
|
|
return -1;
|
|
|
|
}
|
2016-04-01 08:15:07 -04:00
|
|
|
|
2018-02-27 08:25:40 -05:00
|
|
|
bool file_import(struct file *file, const gchar *srcpath)
|
2016-07-12 09:08:45 -04:00
|
|
|
{
|
|
|
|
gchar *contents = NULL;
|
|
|
|
gsize length = 0;
|
|
|
|
|
2018-02-27 08:25:40 -05:00
|
|
|
if (!file->f_file || !srcpath)
|
2016-07-12 09:08:45 -04:00
|
|
|
return false;
|
|
|
|
if (!g_file_get_contents(srcpath, &contents, &length, NULL))
|
|
|
|
return false;
|
|
|
|
|
2018-02-27 08:25:40 -05:00
|
|
|
file_write(file, contents, length);
|
2016-07-12 09:08:45 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-02-20 10:09:06 -05:00
|
|
|
bool file_remove(struct file *file)
|
2016-04-01 08:15:07 -04:00
|
|
|
{
|
2018-02-20 10:09:06 -05:00
|
|
|
gchar *path, *dir;
|
2016-04-01 08:15:07 -04:00
|
|
|
int ret = -1;
|
|
|
|
|
2018-02-20 10:09:06 -05:00
|
|
|
if (!file->f_file) {
|
|
|
|
path = file_path(file);
|
2016-04-01 08:15:07 -04:00
|
|
|
ret = g_unlink(path);
|
2018-02-20 10:09:06 -05:00
|
|
|
dir = g_path_get_dirname(path);
|
|
|
|
if (string_length(file->f_subdir) > 0)
|
|
|
|
g_rmdir(dir);
|
2016-04-01 08:15:07 -04:00
|
|
|
g_free(path);
|
2018-02-20 10:09:06 -05:00
|
|
|
g_free(dir);
|
2016-04-01 08:15:07 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret == 0;
|
|
|
|
}
|