forked from github/server
Merge pull request #469 from ennorehling/develop
file operations cleanup
This commit is contained in:
commit
ea8246f762
9 changed files with 33 additions and 41 deletions
|
@ -32,7 +32,7 @@ without prior permission by the authors of Eressea.
|
||||||
static int tolua_storage_create(lua_State * L)
|
static int tolua_storage_create(lua_State * L)
|
||||||
{
|
{
|
||||||
const char *filename = tolua_tostring(L, 1, 0);
|
const char *filename = tolua_tostring(L, 1, 0);
|
||||||
const char *type = tolua_tostring(L, 2, "rb");
|
const char *type = tolua_tostring(L, 2, "r");
|
||||||
gamedata *data;
|
gamedata *data;
|
||||||
|
|
||||||
data = gamedata_open(filename, type);
|
data = gamedata_open(filename, type);
|
||||||
|
|
|
@ -708,6 +708,20 @@ void set_reportpath(const char *path)
|
||||||
g_reportdir = path;
|
g_reportdir = path;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int create_directories(void) {
|
||||||
|
int err;
|
||||||
|
err = _mkdir(datapath());
|
||||||
|
if (err) {
|
||||||
|
if (errno == EEXIST) errno = 0;
|
||||||
|
else return err;
|
||||||
|
}
|
||||||
|
err = _mkdir(reportpath());
|
||||||
|
if (err && errno == EEXIST) {
|
||||||
|
errno = 0;
|
||||||
|
}
|
||||||
|
return err;
|
||||||
|
}
|
||||||
|
|
||||||
double get_param_flt(const struct param *p, const char *key, double def)
|
double get_param_flt(const struct param *p, const char *key, double def)
|
||||||
{
|
{
|
||||||
const char *str = get_param(p, key);
|
const char *str = get_param(p, key);
|
||||||
|
|
|
@ -148,6 +148,8 @@ struct param;
|
||||||
const char *reportpath(void);
|
const char *reportpath(void);
|
||||||
void set_reportpath(const char *);
|
void set_reportpath(const char *);
|
||||||
|
|
||||||
|
int create_directories(void);
|
||||||
|
|
||||||
void kernel_init(void);
|
void kernel_init(void);
|
||||||
void kernel_done(void);
|
void kernel_done(void);
|
||||||
|
|
||||||
|
|
|
@ -23,7 +23,7 @@ static void test_group_readwrite(CuTest * tc)
|
||||||
FILE *F;
|
FILE *F;
|
||||||
stream strm;
|
stream strm;
|
||||||
|
|
||||||
F = fopen("test.dat", "wb");
|
F = fopen("test.dat", "w");
|
||||||
fstream_init(&strm, F);
|
fstream_init(&strm, F);
|
||||||
binstore_init(&store, &strm);
|
binstore_init(&store, &strm);
|
||||||
test_cleanup();
|
test_cleanup();
|
||||||
|
@ -36,7 +36,7 @@ static void test_group_readwrite(CuTest * tc)
|
||||||
binstore_done(&store);
|
binstore_done(&store);
|
||||||
fstream_done(&strm);
|
fstream_done(&strm);
|
||||||
|
|
||||||
F = fopen("test.dat", "rb");
|
F = fopen("test.dat", "r");
|
||||||
fstream_init(&strm, F);
|
fstream_init(&strm, F);
|
||||||
binstore_init(&store, &strm);
|
binstore_init(&store, &strm);
|
||||||
f->groups = 0;
|
f->groups = 0;
|
||||||
|
|
|
@ -256,7 +256,7 @@ int readorders(const char *filename)
|
||||||
int nfactions = 0;
|
int nfactions = 0;
|
||||||
struct faction *f = NULL;
|
struct faction *f = NULL;
|
||||||
|
|
||||||
F = fopen(filename, "rb");
|
F = fopen(filename, "r");
|
||||||
if (!F) {
|
if (!F) {
|
||||||
perror(filename);
|
perror(filename);
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -1407,7 +1407,7 @@ int readgame(const char *filename, bool backup)
|
||||||
create_backup(path);
|
create_backup(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
F = fopen(path, "rb");
|
F = fopen(path, "r");
|
||||||
if (!F) {
|
if (!F) {
|
||||||
perror(path);
|
perror(path);
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -1747,22 +1747,17 @@ int writegame(const char *filename)
|
||||||
stream strm;
|
stream strm;
|
||||||
FILE *F;
|
FILE *F;
|
||||||
|
|
||||||
sprintf(path, "%s/%s", datapath(), filename);
|
create_directories();
|
||||||
|
join_path(datapath(), filename, path, sizeof(path));
|
||||||
#ifdef HAVE_UNISTD_H
|
#ifdef HAVE_UNISTD_H
|
||||||
/* make sure we don't overwrite an existing file (hard links) */
|
/* make sure we don't overwrite an existing file (hard links) */
|
||||||
unlink(path);
|
unlink(path);
|
||||||
#endif
|
#endif
|
||||||
F = fopen(path, "wb");
|
F = fopen(path, "w");
|
||||||
if (!F) {
|
|
||||||
/* we might be missing the directory, let's try creating it */
|
|
||||||
int err = _mkdir(datapath());
|
|
||||||
if (err) return err;
|
|
||||||
F = fopen(path, "wb");
|
|
||||||
if (!F) {
|
if (!F) {
|
||||||
perror(path);
|
perror(path);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
gdata.store = &store;
|
gdata.store = &store;
|
||||||
gdata.encoding = enc_gamedata;
|
gdata.encoding = enc_gamedata;
|
||||||
|
|
|
@ -34,15 +34,15 @@ static void test_readwrite_unit(CuTest * tc)
|
||||||
int fno;
|
int fno;
|
||||||
/* FIXME: at some point during this test, errno is set to 17 (File exists), why? */
|
/* FIXME: at some point during this test, errno is set to 17 (File exists), why? */
|
||||||
|
|
||||||
|
create_directories();
|
||||||
test_cleanup();
|
test_cleanup();
|
||||||
r = test_create_region(0, 0, 0);
|
r = test_create_region(0, 0, 0);
|
||||||
f = test_create_faction(0);
|
f = test_create_faction(0);
|
||||||
fno = f->no;
|
fno = f->no;
|
||||||
u = test_create_unit(f, r);
|
u = test_create_unit(f, r);
|
||||||
_mkdir(datapath());
|
|
||||||
join_path(datapath(), filename, path, sizeof(path));
|
join_path(datapath(), filename, path, sizeof(path));
|
||||||
|
|
||||||
data = gamedata_open(path, "wb");
|
data = gamedata_open(path, "w");
|
||||||
CuAssertPtrNotNull(tc, data); // TODO: intermittent test
|
CuAssertPtrNotNull(tc, data); // TODO: intermittent test
|
||||||
write_unit(data, u);
|
write_unit(data, u);
|
||||||
gamedata_close(data);
|
gamedata_close(data);
|
||||||
|
@ -50,7 +50,7 @@ static void test_readwrite_unit(CuTest * tc)
|
||||||
free_gamedata();
|
free_gamedata();
|
||||||
f = test_create_faction(0);
|
f = test_create_faction(0);
|
||||||
renumber_faction(f, fno);
|
renumber_faction(f, fno);
|
||||||
data = gamedata_open(path, "rb");
|
data = gamedata_open(path, "r");
|
||||||
u = read_unit(data);
|
u = read_unit(data);
|
||||||
gamedata_close(data);
|
gamedata_close(data);
|
||||||
|
|
||||||
|
|
|
@ -211,7 +211,7 @@ static int parse_args(int argc, char **argv, int *exitcode)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (log_stderr) {
|
if (log_stderr) {
|
||||||
log_to_file(log_stderr, stderr);
|
log_to_file(log_stderr | LOG_FLUSH | LOG_BRIEF, stderr);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -1540,16 +1540,6 @@ static void prepare_report(struct report_context *ctx, faction *f)
|
||||||
ctx->last = lastregion(f);
|
ctx->last = lastregion(f);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void mkreportdir(const char *rpath) {
|
|
||||||
if (_mkdir(rpath) != 0) {
|
|
||||||
if (_access(rpath, 0) < 0) {
|
|
||||||
log_error("could not create reports directory %s: %s", rpath, strerror(errno));
|
|
||||||
abort();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
errno = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int write_reports(faction * f, time_t ltime)
|
int write_reports(faction * f, time_t ltime)
|
||||||
{
|
{
|
||||||
unsigned int backup = 1, maxbackup = 128 * 1000;
|
unsigned int backup = 1, maxbackup = 128 * 1000;
|
||||||
|
@ -1557,14 +1547,12 @@ int write_reports(faction * f, time_t ltime)
|
||||||
struct report_context ctx;
|
struct report_context ctx;
|
||||||
const char *encoding = "UTF-8";
|
const char *encoding = "UTF-8";
|
||||||
report_type *rtype;
|
report_type *rtype;
|
||||||
const char *path = reportpath();
|
|
||||||
|
|
||||||
if (noreports) {
|
if (noreports) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
prepare_report(&ctx, f);
|
prepare_report(&ctx, f);
|
||||||
get_addresses(&ctx);
|
get_addresses(&ctx);
|
||||||
mkreportdir(path); // FIXME: too many mkdir calls! init_reports is enough
|
|
||||||
log_debug("Reports for %s:", factionname(f));
|
log_debug("Reports for %s:", factionname(f));
|
||||||
for (rtype = report_types; rtype != NULL; rtype = rtype->next) {
|
for (rtype = report_types; rtype != NULL; rtype = rtype->next) {
|
||||||
if (f->options & rtype->flag) {
|
if (f->options & rtype->flag) {
|
||||||
|
@ -1635,14 +1623,8 @@ static void check_messages_exist(void) {
|
||||||
int init_reports(void)
|
int init_reports(void)
|
||||||
{
|
{
|
||||||
check_messages_exist();
|
check_messages_exist();
|
||||||
|
create_directories();
|
||||||
prepare_reports();
|
prepare_reports();
|
||||||
{
|
|
||||||
if (_access(reportpath(), 0) != 0) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
mkreportdir(reportpath());
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1659,8 +1641,7 @@ int reports(void)
|
||||||
report_donations();
|
report_donations();
|
||||||
remove_empty_units();
|
remove_empty_units();
|
||||||
|
|
||||||
mkreportdir(rpath); // FIXME: init_reports already does this?
|
join_path(rpath, "reports.txt", path, sizeof(path));
|
||||||
sprintf(path, "%s/reports.txt", rpath);
|
|
||||||
mailit = fopen(path, "w");
|
mailit = fopen(path, "w");
|
||||||
if (mailit == NULL) {
|
if (mailit == NULL) {
|
||||||
log_error("%s could not be opened!\n", path);
|
log_error("%s could not be opened!\n", path);
|
||||||
|
|
2
storage
2
storage
|
@ -1 +1 @@
|
||||||
Subproject commit 89f3c1b01e41f2675fcbfd51fd8494894dc22d44
|
Subproject commit 18cc3bb8f8906237915eb31c9899f95340318087
|
Loading…
Reference in a new issue