diff --git a/mbfido/createf.c b/mbfido/createf.c index 6b640aed..9031e145 100644 --- a/mbfido/createf.c +++ b/mbfido/createf.c @@ -230,9 +230,25 @@ int CheckTicGroup(char *Area, int SendUplink, faddr *f) fread(&areahdr, sizeof(areahdr), 1, fp); Syslog('f', "File area is open"); + /* + * Verify the file is large enough + */ + fseek(fp, 0, SEEK_END); offset = areahdr.hdrsize + ((fgroup.StartArea -1) * (areahdr.recsize)); + Syslog('+', "file end at %ld, offset needed %ld", ftell(fp), offset); + + if (ftell(fp) < offset) { + Syslog('f', "Database too small, expanding..."); + memset(&area, 0, sizeof(area)); + while (TRUE) { + fwrite(&area, sizeof(area), 1, fp); + if (ftell(fp) >= areahdr.hdrsize + ((fgroup.StartArea -1) * (areahdr.recsize))) + break; + } + } + if (fseek(fp, offset, SEEK_SET)) { - WriteError("$Can't seek in %s", temp); + WriteError("$Can't seek in %s to position %ld", temp, offset); fclose(ap); fclose(mp); fclose(fp); diff --git a/mbfido/createm.c b/mbfido/createm.c index 1c7ba8d5..6eeb7431 100644 --- a/mbfido/createm.c +++ b/mbfido/createm.c @@ -140,6 +140,24 @@ int CheckEchoGroup(char *Area, int SendUplink, faddr *f) } fread(&msgshdr, sizeof(msgshdr), 1, mp); offset = msgshdr.hdrsize + ((mgroup.StartArea -1) * (msgshdr.recsize + msgshdr.syssize)); + Syslog('+', "file end at %ld, offset needed %ld", ftell(mp), offset); + + /* + * Verify the file is large enough + */ + if (ftell(mp) < offset) { + Syslog('m', "Database too small, expanding..."); + memset(&msgs, 0, sizeof(msgs)); + memset(&System, 0, sizeof(System)); + while (TRUE) { + fwrite(&msgs, sizeof(msgs), 1, mp); + for (i = 0; i < (msgshdr.syssize / sizeof(System)); i++) + fwrite(&System, sizeof(System), 1, mp); + if (ftell(mp) >= msgshdr.hdrsize + ((mgroup.StartArea -1) * (msgshdr.recsize + msgshdr.syssize))) + break; + } + } + if (fseek(mp, offset, SEEK_SET)) { WriteError("$Can't seek in %s", temp); fclose(ap);