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)
|
||||
{
|
||||
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;
|
||||
|
||||
data = gamedata_open(filename, type);
|
||||
|
|
|
@ -708,6 +708,20 @@ void set_reportpath(const char *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)
|
||||
{
|
||||
const char *str = get_param(p, key);
|
||||
|
|
|
@ -148,6 +148,8 @@ struct param;
|
|||
const char *reportpath(void);
|
||||
void set_reportpath(const char *);
|
||||
|
||||
int create_directories(void);
|
||||
|
||||
void kernel_init(void);
|
||||
void kernel_done(void);
|
||||
|
||||
|
|
|
@ -23,7 +23,7 @@ static void test_group_readwrite(CuTest * tc)
|
|||
FILE *F;
|
||||
stream strm;
|
||||
|
||||
F = fopen("test.dat", "wb");
|
||||
F = fopen("test.dat", "w");
|
||||
fstream_init(&strm, F);
|
||||
binstore_init(&store, &strm);
|
||||
test_cleanup();
|
||||
|
@ -36,7 +36,7 @@ static void test_group_readwrite(CuTest * tc)
|
|||
binstore_done(&store);
|
||||
fstream_done(&strm);
|
||||
|
||||
F = fopen("test.dat", "rb");
|
||||
F = fopen("test.dat", "r");
|
||||
fstream_init(&strm, F);
|
||||
binstore_init(&store, &strm);
|
||||
f->groups = 0;
|
||||
|
|
|
@ -256,7 +256,7 @@ int readorders(const char *filename)
|
|||
int nfactions = 0;
|
||||
struct faction *f = NULL;
|
||||
|
||||
F = fopen(filename, "rb");
|
||||
F = fopen(filename, "r");
|
||||
if (!F) {
|
||||
perror(filename);
|
||||
return -1;
|
||||
|
@ -1407,7 +1407,7 @@ int readgame(const char *filename, bool backup)
|
|||
create_backup(path);
|
||||
}
|
||||
|
||||
F = fopen(path, "rb");
|
||||
F = fopen(path, "r");
|
||||
if (!F) {
|
||||
perror(path);
|
||||
return -1;
|
||||
|
@ -1747,21 +1747,16 @@ int writegame(const char *filename)
|
|||
stream strm;
|
||||
FILE *F;
|
||||
|
||||
sprintf(path, "%s/%s", datapath(), filename);
|
||||
create_directories();
|
||||
join_path(datapath(), filename, path, sizeof(path));
|
||||
#ifdef HAVE_UNISTD_H
|
||||
/* make sure we don't overwrite an existing file (hard links) */
|
||||
unlink(path);
|
||||
#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) {
|
||||
perror(path);
|
||||
return -1;
|
||||
}
|
||||
perror(path);
|
||||
return -1;
|
||||
}
|
||||
|
||||
gdata.store = &store;
|
||||
|
|
|
@ -34,15 +34,15 @@ static void test_readwrite_unit(CuTest * tc)
|
|||
int fno;
|
||||
/* FIXME: at some point during this test, errno is set to 17 (File exists), why? */
|
||||
|
||||
create_directories();
|
||||
test_cleanup();
|
||||
r = test_create_region(0, 0, 0);
|
||||
f = test_create_faction(0);
|
||||
fno = f->no;
|
||||
u = test_create_unit(f, r);
|
||||
_mkdir(datapath());
|
||||
join_path(datapath(), filename, path, sizeof(path));
|
||||
|
||||
data = gamedata_open(path, "wb");
|
||||
data = gamedata_open(path, "w");
|
||||
CuAssertPtrNotNull(tc, data); // TODO: intermittent test
|
||||
write_unit(data, u);
|
||||
gamedata_close(data);
|
||||
|
@ -50,7 +50,7 @@ static void test_readwrite_unit(CuTest * tc)
|
|||
free_gamedata();
|
||||
f = test_create_faction(0);
|
||||
renumber_faction(f, fno);
|
||||
data = gamedata_open(path, "rb");
|
||||
data = gamedata_open(path, "r");
|
||||
u = read_unit(data);
|
||||
gamedata_close(data);
|
||||
|
||||
|
|
|
@ -211,7 +211,7 @@ static int parse_args(int argc, char **argv, int *exitcode)
|
|||
}
|
||||
|
||||
if (log_stderr) {
|
||||
log_to_file(log_stderr, stderr);
|
||||
log_to_file(log_stderr | LOG_FLUSH | LOG_BRIEF, stderr);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
|
@ -1540,16 +1540,6 @@ static void prepare_report(struct report_context *ctx, faction *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)
|
||||
{
|
||||
unsigned int backup = 1, maxbackup = 128 * 1000;
|
||||
|
@ -1557,14 +1547,12 @@ int write_reports(faction * f, time_t ltime)
|
|||
struct report_context ctx;
|
||||
const char *encoding = "UTF-8";
|
||||
report_type *rtype;
|
||||
const char *path = reportpath();
|
||||
|
||||
if (noreports) {
|
||||
return false;
|
||||
}
|
||||
prepare_report(&ctx, f);
|
||||
get_addresses(&ctx);
|
||||
mkreportdir(path); // FIXME: too many mkdir calls! init_reports is enough
|
||||
log_debug("Reports for %s:", factionname(f));
|
||||
for (rtype = report_types; rtype != NULL; rtype = rtype->next) {
|
||||
if (f->options & rtype->flag) {
|
||||
|
@ -1635,14 +1623,8 @@ static void check_messages_exist(void) {
|
|||
int init_reports(void)
|
||||
{
|
||||
check_messages_exist();
|
||||
|
||||
create_directories();
|
||||
prepare_reports();
|
||||
{
|
||||
if (_access(reportpath(), 0) != 0) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
mkreportdir(reportpath());
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -1659,8 +1641,7 @@ int reports(void)
|
|||
report_donations();
|
||||
remove_empty_units();
|
||||
|
||||
mkreportdir(rpath); // FIXME: init_reports already does this?
|
||||
sprintf(path, "%s/reports.txt", rpath);
|
||||
join_path(rpath, "reports.txt", path, sizeof(path));
|
||||
mailit = fopen(path, "w");
|
||||
if (mailit == NULL) {
|
||||
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