remove wild mkdir calls all over the code, catch EEXIST errors

use join_path more consistently
fix a test that's intermittent on windows because fopen(..., "wb") fails, why?
This commit is contained in:
Enno Rehling 2016-02-01 09:26:24 +01:00
parent 665a271972
commit 2f2bbb16ab
6 changed files with 25 additions and 33 deletions

View File

@ -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);

View File

@ -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);

View File

@ -1747,22 +1747,17 @@ 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");
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;
}
}
gdata.store = &store;
gdata.encoding = enc_gamedata;

View File

@ -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);

View File

@ -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;

View File

@ -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);