X-Git-Url: https://jfr.im/git/irc/evilnet/x3.git/blobdiff_plain/83ff05c356f6497be2475640d1cb2e3eb369831f..2dddcd742b32cc37e3f7b7040869004c67b3e061:/src/recdb.c diff --git a/src/recdb.c b/src/recdb.c index 41f82bd..4fc9e3c 100644 --- a/src/recdb.c +++ b/src/recdb.c @@ -3,9 +3,9 @@ * * This file is part of x3. * - * srvx is free software; you can redistribute it and/or modify + * x3 is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or + * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, @@ -93,7 +93,7 @@ typedef struct recdb_outfile { } RECDB_OUT; #ifdef HAVE_MMAP -static int mmap_error=0; +static int mmap_error; #endif #define EOL '\n' @@ -240,6 +240,10 @@ database_get_path(dict_t db, const char *path) void* database_get_data(dict_t db, const char *path, enum recdb_type type) { + assert(path != NULL); + if(!path) + log_module(MAIN_LOG, LOG_WARNING, "Null path in database_get_data()"); + /* log_module(MAIN_LOG, LOG_DEBUG, "Reading config option '%s'", path); */ struct record_data *rd = database_get_path(db, path); return (rd && rd->type == type) ? rd->d.whatever : NULL; } @@ -568,9 +572,16 @@ failure_reason(int code) void explain_failure(RECDB *recdb, int code) { - log_module(MAIN_LOG, LOG_ERROR, "%s (got '%c') at %s line %d column %d.", - failure_reason(code), code & 255, - recdb->source, recdb->ctx.line, recdb->ctx.col); + static char msg[1024]; + snprintf(msg, sizeof(msg), "%s (got '%c') at %s line %d column %d.", + failure_reason(code), code & 255, + recdb->source, recdb->ctx.line, recdb->ctx.col); + if (MAIN_LOG == NULL) { + fputs(msg, stderr); + fputc('\n', stderr); + fflush(stderr); + } else + log_module(MAIN_LOG, LOG_ERROR, "%s", msg); } const char * @@ -613,14 +624,19 @@ parse_database(const char *filename) if (fstat(fileno(recdb.f), &statinfo)) { log_module(MAIN_LOG, LOG_ERROR, "Unable to fstat database file '%s': %s", filename, strerror(errno)); + fclose(recdb.f); return NULL; } recdb.length = (size_t)statinfo.st_size; + if (recdb.length == 0) { + return alloc_database(); + } #ifdef HAVE_MMAP /* Try mmap */ if (!mmap_error && (recdb.s = mmap(NULL, recdb.length, PROT_READ|PROT_WRITE, MAP_PRIVATE, fileno(recdb.f), 0)) != MAP_FAILED) { recdb.type = RECDB_MMAP; + madvise(recdb.s, recdb.length, MADV_SEQUENTIAL); } else { /* Fall back to stdio */ if (!mmap_error) {