Possible fix for not closing the tempfile

This commit is contained in:
Michiel Broek 2004-09-18 10:50:21 +00:00
parent 370ae9d149
commit 05c0803b9e
2 changed files with 42 additions and 49 deletions

View File

@ -32,6 +32,8 @@ v0.61.4 11-Aug-2004
The files database is moved from ~/fdb to ~/var/fdb. The files database is moved from ~/fdb to ~/var/fdb.
Added debug logging to dbfdb to find the problem with not Added debug logging to dbfdb to find the problem with not
cleaned magic aliases. cleaned magic aliases.
Possible fix for not closing the temp file with several
functions. Under test!
mbcico: mbcico:
Removed a little debug logging. Removed a little debug logging.

View File

@ -187,10 +187,39 @@ int mbsedb_UnlockFDB(struct _fdbarea *fdb_area)
void mbsedb_Temp2Data(unsigned long);
void mbsedb_Temp2Data(unsigned long fdb_area)
{
char *temp1, *temp2;
temp1 = calloc(PATH_MAX, sizeof(char));
temp2 = calloc(PATH_MAX, sizeof(char));
/*
* Now the trick, some might be waiting for a lock on the original file,
* we will give that a new name on disk. Then we move the temp in place.
* Finaly remove the old (still locked) original file.
*/
sprintf(temp2, "%s/var/fdb/file%ld.data", getenv("MBSE_ROOT"), fdb_area);
sprintf(temp1, "%s/var/fdb/file%ld.xxxx", getenv("MBSE_ROOT"), fdb_area);
rename(temp2, temp1);
sprintf(temp1, "%s/var/fdb/file%ld.temp", getenv("MBSE_ROOT"), fdb_area);
rename(temp1, temp2);
sprintf(temp1, "%s/var/fdb/file%ld.xxxx", getenv("MBSE_ROOT"), fdb_area);
unlink(temp1);
free(temp1);
free(temp2);
return;
}
int mbsedb_InsertFDB(struct _fdbarea *fdb_area, struct FILE_record frec, int AddAlpha) int mbsedb_InsertFDB(struct _fdbarea *fdb_area, struct FILE_record frec, int AddAlpha)
{ {
char *temp, *temp2; char *temp;
int i, Insert, Done = FALSE, Found = FALSE, rc; int i, Insert, Done = FALSE, Found = FALSE;
FILE *fp; FILE *fp;
Syslog('f', "mbsedb_InsertFDB: \"%s\", magic \"%s\"", frec.LName, frec.Magic); Syslog('f', "mbsedb_InsertFDB: \"%s\", magic \"%s\"", frec.LName, frec.Magic);
@ -212,7 +241,6 @@ int mbsedb_InsertFDB(struct _fdbarea *fdb_area, struct FILE_record frec, int Add
* There are files, search the insert point. * There are files, search the insert point.
*/ */
temp = calloc(PATH_MAX, sizeof(char)); temp = calloc(PATH_MAX, sizeof(char));
temp2 = calloc(PATH_MAX, sizeof(char));
sprintf(temp, "%s/var/fdb/file%ld.temp", getenv("MBSE_ROOT"), fdb_area->area); sprintf(temp, "%s/var/fdb/file%ld.temp", getenv("MBSE_ROOT"), fdb_area->area);
fseek(fdb_area->fp, fdbhdr.hdrsize, SEEK_SET); fseek(fdb_area->fp, fdbhdr.hdrsize, SEEK_SET);
Insert = 0; Insert = 0;
@ -301,19 +329,8 @@ int mbsedb_InsertFDB(struct _fdbarea *fdb_area, struct FILE_record frec, int Add
fwrite(&frec, fdbhdr.recsize, 1, fp); fwrite(&frec, fdbhdr.recsize, 1, fp);
} }
/* fclose(fdb_area->fp);
* Now the trick, some might be waiting for a lock on the original file, mbsedb_Temp2Data(fdb_area->area);
* we will give that a new name on disk. Then we move the temp in place.
* Finaly remove the old (still locked) original file.
*/
sprintf(temp2, "%s/var/fdb/file%ld.data", getenv("MBSE_ROOT"), fdb_area->area);
sprintf(temp, "%s/var/fdb/file%ld.xxxx", getenv("MBSE_ROOT"), fdb_area->area);
rc = rename(temp2, temp);
sprintf(temp, "%s/var/fdb/file%ld.temp", getenv("MBSE_ROOT"), fdb_area->area);
rc = rename(temp, temp2);
sprintf(temp, "%s/var/fdb/file%ld.xxxx", getenv("MBSE_ROOT"), fdb_area->area);
rc = unlink(temp);
fdb_area->fp = fp; fdb_area->fp = fp;
fdb_area->locked = 0; fdb_area->locked = 0;
} else { } else {
@ -325,7 +342,6 @@ int mbsedb_InsertFDB(struct _fdbarea *fdb_area, struct FILE_record frec, int Add
} }
free(temp); free(temp);
free(temp2);
return TRUE; return TRUE;
} }
@ -337,7 +353,7 @@ int mbsedb_InsertFDB(struct _fdbarea *fdb_area, struct FILE_record frec, int Add
*/ */
int mbsedb_PackFDB(struct _fdbarea *fdb_area) int mbsedb_PackFDB(struct _fdbarea *fdb_area)
{ {
char *temp, *temp2; char *temp;
FILE *fp; FILE *fp;
int count = 0; int count = 0;
@ -353,11 +369,11 @@ int mbsedb_PackFDB(struct _fdbarea *fdb_area)
* There are files, copy the remaining entries * There are files, copy the remaining entries
*/ */
temp = calloc(PATH_MAX, sizeof(char)); temp = calloc(PATH_MAX, sizeof(char));
temp2 = calloc(PATH_MAX, sizeof(char));
sprintf(temp, "%s/var/fdb/file%ld.temp", getenv("MBSE_ROOT"), fdb_area->area); sprintf(temp, "%s/var/fdb/file%ld.temp", getenv("MBSE_ROOT"), fdb_area->area);
if ((fp = fopen(temp, "a+")) == NULL) { if ((fp = fopen(temp, "a+")) == NULL) {
WriteError("$Can't create %s", temp); WriteError("$Can't create %s", temp);
mbsedb_UnlockFDB(fdb_area); mbsedb_UnlockFDB(fdb_area);
free(temp);
return -1; return -1;
} }
fwrite(&fdbhdr, fdbhdr.hdrsize, 1, fp); fwrite(&fdbhdr, fdbhdr.hdrsize, 1, fp);
@ -370,24 +386,12 @@ int mbsedb_PackFDB(struct _fdbarea *fdb_area)
count++; count++;
} }
/* fclose(fdb_area->fp);
* Now the trick, some might be waiting for a lock on the original file, mbsedb_Temp2Data(fdb_area->area);
* we will give that a new name on disk. Then we move the temp in place.
* Finaly remove the old (still locked) original file.
*/
sprintf(temp2, "%s/var/fdb/file%ld.data", getenv("MBSE_ROOT"), fdb_area->area);
sprintf(temp, "%s/var/fdb/file%ld.xxxx", getenv("MBSE_ROOT"), fdb_area->area);
rename(temp2, temp);
sprintf(temp, "%s/var/fdb/file%ld.temp", getenv("MBSE_ROOT"), fdb_area->area);
rename(temp, temp2);
sprintf(temp, "%s/var/fdb/file%ld.xxxx", getenv("MBSE_ROOT"), fdb_area->area);
unlink(temp);
fdb_area->fp = fp; fdb_area->fp = fp;
fdb_area->locked = 0; fdb_area->locked = 0;
free(temp); free(temp);
free(temp2);
return count; return count;
} }
@ -476,7 +480,7 @@ int comp_fdbs(fdbs **fap1, fdbs **fap2)
int mbsedb_SortFDB(struct _fdbarea *fdb_area) int mbsedb_SortFDB(struct _fdbarea *fdb_area)
{ {
fdbs *fdx = NULL, *tmp; fdbs *fdx = NULL, *tmp;
char *temp, *temp2; char *temp;
FILE *fp; FILE *fp;
int count = 0; int count = 0;
@ -522,25 +526,12 @@ int mbsedb_SortFDB(struct _fdbarea *fdb_area)
} }
tidy_fdbs(&fdx); tidy_fdbs(&fdx);
/* fclose(fdb_area->fp);
* Now the trick, some might be waiting for a lock on the original file, mbsedb_Temp2Data(fdb_area->area);
* we will give that a new name on disk. Then we move the temp in place.
* Finaly remove the old (still locked) original file.
*/
temp2 = calloc(PATH_MAX, sizeof(char));
sprintf(temp2, "%s/var/fdb/file%ld.data", getenv("MBSE_ROOT"), fdb_area->area);
sprintf(temp, "%s/var/fdb/file%ld.xxxx", getenv("MBSE_ROOT"), fdb_area->area);
rename(temp2, temp);
sprintf(temp, "%s/var/fdb/file%ld.temp", getenv("MBSE_ROOT"), fdb_area->area);
rename(temp, temp2);
sprintf(temp, "%s/var/fdb/file%ld.xxxx", getenv("MBSE_ROOT"), fdb_area->area);
unlink(temp);
fdb_area->fp = fp; fdb_area->fp = fp;
fdb_area->locked = 0; fdb_area->locked = 0;
free(temp); free(temp);
free(temp2);
return count; return count;
} }