Updated fix for FreeBSD lastread pointers

This commit is contained in:
Michiel Broek 2002-05-26 14:08:29 +00:00
parent 0a70980608
commit 91f59e902c
2 changed files with 165 additions and 166 deletions

View File

@ -4631,6 +4631,7 @@ v0.33.20 10-Feb-2002
msgbase.a: msgbase.a:
Improved logging for opening message bases. Improved logging for opening message bases.
Added function to delete a JAM message base. Added function to delete a JAM message base.
Fix for corrupting LastRead pointers on FreeBSD systems.
mbsetup: mbsetup:
In message groups added default settings for auto area In message groups added default settings for auto area

View File

@ -590,187 +590,185 @@ int JAM_Open(char *Msgbase)
*/ */
void JAM_Pack(void) void JAM_Pack(void)
{ {
int fdnHdr, fdnJdx, fdnJdt, fdnJlr; int fdnHdr, fdnJdx, fdnJdt, fdnJlr;
int ToRead, Readed, i, count; int ToRead, Readed, i, count;
char *File, *New, *Subfield, *Temp; char *File, *New, *Subfield, *Temp;
JAMIDXREC jamIdx; JAMIDXREC jamIdx;
unsigned long NewNumber = 0, RefNumber = 0, Written = 0; unsigned long NewNumber = 0, RefNumber = 0, Written = 0;
lastread LR; lastread LR;
File = calloc(PATH_MAX, sizeof(char)); File = calloc(PATH_MAX, sizeof(char));
New = calloc(PATH_MAX, sizeof(char)); New = calloc(PATH_MAX, sizeof(char));
sprintf(File, "%s%s", BaseName, ".$dr"); sprintf(File, "%s%s", BaseName, ".$dr");
fdnHdr = open(File, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); fdnHdr = open(File, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
sprintf(File, "%s%s", BaseName, ".$dt"); sprintf(File, "%s%s", BaseName, ".$dt");
fdnJdt = open(File, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); fdnJdt = open(File, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
sprintf(File, "%s%s", BaseName, ".$dx"); sprintf(File, "%s%s", BaseName, ".$dx");
fdnJdx = open(File, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); fdnJdx = open(File, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
sprintf(File, "%s%s", BaseName, ".$lr"); sprintf(File, "%s%s", BaseName, ".$lr");
fdnJlr = open(File, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); fdnJlr = open(File, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
/* /*
* Get the number of LastRead records, this number is needed to prevent * Get the number of LastRead records, this number is needed to prevent
* that FreeBSD makes garbage of the LastRead pointers for some reason. * that FreeBSD makes garbage of the LastRead pointers for some reason.
*/ */
count = lseek(fdJlr, 0, SEEK_END) / sizeof(lastread); count = lseek(fdJlr, 0, SEEK_END) / sizeof(lastread);
if (fdnHdr != -1 && fdnJdt != -1 && fdnJdx != -1 && fdnJlr != -1) { if (fdnHdr != -1 && fdnJdt != -1 && fdnJdx != -1 && fdnJlr != -1) {
lseek(fdHdr, 0L, SEEK_SET); lseek(fdHdr, 0L, SEEK_SET);
if (read(fdHdr, &jamHdrInfo, sizeof(JAMHDRINFO)) == sizeof(JAMHDRINFO)) { if (read(fdHdr, &jamHdrInfo, sizeof(JAMHDRINFO)) == sizeof(JAMHDRINFO)) {
write(fdnHdr, &jamHdrInfo, sizeof(JAMHDRINFO)); write(fdnHdr, &jamHdrInfo, sizeof(JAMHDRINFO));
while (read(fdHdr, &jamHdr, sizeof(JAMHDR)) == sizeof(JAMHDR)) { while (read(fdHdr, &jamHdr, sizeof(JAMHDR)) == sizeof(JAMHDR)) {
RefNumber++; RefNumber++;
if (strncmp(jamHdr.Signature, "JAM", 3)) { if (strncmp(jamHdr.Signature, "JAM", 3)) {
WriteError("jamPack: %s headerfile corrupt", BaseName); WriteError("jamPack: %s headerfile corrupt", BaseName);
lseek(fdJdx, (RefNumber -1) * sizeof(JAMIDXREC), SEEK_SET); lseek(fdJdx, (RefNumber -1) * sizeof(JAMIDXREC), SEEK_SET);
read(fdJdx, &jamIdx, sizeof(JAMIDXREC)); read(fdJdx, &jamIdx, sizeof(JAMIDXREC));
lseek(fdHdr, jamIdx.HdrOffset, SEEK_SET); lseek(fdHdr, jamIdx.HdrOffset, SEEK_SET);
read(fdHdr, &jamHdr, sizeof(JAMHDR)); read(fdHdr, &jamHdr, sizeof(JAMHDR));
if ((strncmp(jamHdr.Signature, "JAM", 3) == 0) && (jamHdr.MsgNum == RefNumber)) if ((strncmp(jamHdr.Signature, "JAM", 3) == 0) && (jamHdr.MsgNum == RefNumber))
WriteError("jamPack: corrected the problem"); WriteError("jamPack: corrected the problem");
else { else {
WriteError("jamPack: PANIC, problem cannot be solved, skipping this area"); WriteError("jamPack: PANIC, problem cannot be solved, skipping this area");
Written = 0; Written = 0;
break; break;
} }
}
if (jamHdr.Attribute & MSG_DELETED) {
if (jamHdr.SubfieldLen > 0L)
lseek (fdHdr, jamHdr.SubfieldLen, SEEK_CUR);
} else {
jamIdx.UserCRC = 0;
jamIdx.HdrOffset = tell(fdnHdr);
write(fdnJdx, &jamIdx, sizeof(JAMIDXREC));
lseek(fdJdt, jamHdr.TxtOffset, SEEK_SET);
jamHdr.TxtOffset = tell(fdnJdt);
NewNumber++;
Written++;
lseek(fdJlr, 0, SEEK_SET);
for (i = 0; i < count; i++) {
if ((read(fdJlr, &LR, sizeof(lastread)) == sizeof(lastread))) {
/*
* Test if one of the lastread pointer is the current
* old message number.
*/
if ((LR.LastReadMsg == jamHdr.MsgNum) || (LR.HighReadMsg == jamHdr.MsgNum)) {
/*
* Adjust the matching numbers
*/
if (LR.LastReadMsg == jamHdr.MsgNum)
LR.LastReadMsg = NewNumber;
if (LR.HighReadMsg == jamHdr.MsgNum)
LR.HighReadMsg = NewNumber;
lseek(fdJlr, - sizeof(lastread), SEEK_CUR);
write(fdJlr, &LR, sizeof(lastread));
}
}
}
jamHdr.MsgNum = NewNumber;
write(fdnHdr, &jamHdr, sizeof(JAMHDR));
if (jamHdr.SubfieldLen > 0L) {
if ((Subfield = (char *)malloc ((size_t)(jamHdr.SubfieldLen + 1))) != NULL) {
read (fdHdr, Subfield, (size_t)jamHdr.SubfieldLen);
write (fdnHdr, Subfield, (size_t)jamHdr.SubfieldLen);
free(Subfield);
}
}
if ((Temp = (char *)malloc (MAX_TEXT)) != NULL) {
do {
if ((ToRead = MAX_TEXT) > jamHdr.TxtLen)
ToRead = (int)jamHdr.TxtLen;
Readed = (int)read (fdJdt, Temp, ToRead);
write (fdnJdt, Temp, Readed);
jamHdr.TxtLen -= Readed;
} while (jamHdr.TxtLen > 0);
free(Temp);
}
}
}
} }
if (jamHdr.Attribute & MSG_DELETED) {
if (jamHdr.SubfieldLen > 0L)
lseek (fdHdr, jamHdr.SubfieldLen, SEEK_CUR);
} else {
jamIdx.UserCRC = 0;
jamIdx.HdrOffset = tell(fdnHdr);
write(fdnJdx, &jamIdx, sizeof(JAMIDXREC));
/* lseek(fdJdt, jamHdr.TxtOffset, SEEK_SET);
* Correct any errors in the header jamHdr.TxtOffset = tell(fdnJdt);
*/ NewNumber++;
if (Written) { Written++;
lseek(fdnHdr, 0, SEEK_SET);
if (read(fdnHdr, &jamHdrInfo, sizeof(JAMHDRINFO)) == sizeof(JAMHDRINFO)) { lseek(fdJlr, 0, SEEK_SET);
if (jamHdrInfo.ActiveMsgs != Written) { for (i = 0; i < count; i++) {
WriteError("jamPack: repair msgs %lu to %lu area %s", if ((read(fdJlr, &LR, sizeof(lastread)) == sizeof(lastread))) {
jamHdrInfo.ActiveMsgs, Written, BaseName); /*
jamHdrInfo.ActiveMsgs = Written; * Test if one of the lastread pointer is the current
lseek(fdnHdr, 0, SEEK_SET); * old message number.
write(fdnHdr, &jamHdrInfo, sizeof(JAMHDRINFO)); */
} if ((LR.LastReadMsg == jamHdr.MsgNum) || (LR.HighReadMsg == jamHdr.MsgNum)) {
/*
* Adjust the matching numbers
*/
if (LR.LastReadMsg == jamHdr.MsgNum)
LR.LastReadMsg = NewNumber;
if (LR.HighReadMsg == jamHdr.MsgNum)
LR.HighReadMsg = NewNumber;
lseek(fdJlr, - sizeof(lastread), SEEK_CUR);
write(fdJlr, &LR, sizeof(lastread));
}
} }
} }
jamHdr.MsgNum = NewNumber;
write(fdnHdr, &jamHdr, sizeof(JAMHDR));
/* if (jamHdr.SubfieldLen > 0L) {
* Now copy the lastread file if ((Subfield = (char *)malloc ((size_t)(jamHdr.SubfieldLen + 1))) != NULL) {
*/ read (fdHdr, Subfield, (size_t)jamHdr.SubfieldLen);
lseek(fdJlr, 0, SEEK_SET); write (fdnHdr, Subfield, (size_t)jamHdr.SubfieldLen);
for (i = 0; i < count; i++) { free(Subfield);
if (read(fdJlr, &LR, sizeof(lastread)) == sizeof(lastread)) {
write(fdnJlr, &LR, sizeof(lastread));
} }
}
if ((Temp = (char *)malloc (MAX_TEXT)) != NULL) {
do {
if ((ToRead = MAX_TEXT) > jamHdr.TxtLen)
ToRead = (int)jamHdr.TxtLen;
Readed = (int)read (fdJdt, Temp, ToRead);
write (fdnJdt, Temp, Readed);
jamHdr.TxtLen -= Readed;
} while (jamHdr.TxtLen > 0);
free(Temp);
}
} }
}
/*
* Close all files
*/
close(fdnHdr);
close(fdnJdt);
close(fdnJdx);
close(fdnJlr);
fdnHdr = fdnJdt = fdnJdx = fdnJlr = -1;
close(fdHdr);
close(fdJdt);
close(fdJdx);
close(fdJlr);
fdHdr = fdJdt = fdJdx = fdJlr = -1;
sprintf(File, "%s%s", BaseName, ".$dr");
sprintf(New, "%s%s", BaseName, EXT_HDRFILE);
unlink(New);
rename(File, New);
sprintf(File, "%s%s", BaseName, ".$dt");
sprintf(New, "%s%s", BaseName, EXT_TXTFILE);
unlink(New);
rename(File, New);
sprintf(File, "%s%s", BaseName, ".$dx");
sprintf(New, "%s%s", BaseName, EXT_IDXFILE);
unlink(New);
rename(File, New);
sprintf(File, "%s%s", BaseName, ".$lr");
sprintf(New, "%s%s", BaseName, EXT_LRDFILE);
unlink(New);
rename(File, New);
JAM_Open(BaseName);
} }
if (fdnHdr != -1) /*
close(fdnHdr); * Correct any errors in the header
*/
if (Written) {
lseek(fdnHdr, 0, SEEK_SET);
if (read(fdnHdr, &jamHdrInfo, sizeof(JAMHDRINFO)) == sizeof(JAMHDRINFO)) {
if (jamHdrInfo.ActiveMsgs != Written) {
WriteError("jamPack: repair msgs %lu to %lu area %s", jamHdrInfo.ActiveMsgs, Written, BaseName);
jamHdrInfo.ActiveMsgs = Written;
lseek(fdnHdr, 0, SEEK_SET);
write(fdnHdr, &jamHdrInfo, sizeof(JAMHDRINFO));
}
}
}
/*
* Now copy the lastread file
*/
lseek(fdJlr, 0, SEEK_SET);
for (i = 0; i < count; i++) {
if (read(fdJlr, &LR, sizeof(lastread)) == sizeof(lastread))
write(fdnJlr, &LR, sizeof(lastread));
}
/*
* Close all files
*/
close(fdnHdr);
close(fdnJdt);
close(fdnJdx);
close(fdnJlr);
fdnHdr = fdnJdt = fdnJdx = fdnJlr = -1;
close(fdHdr);
close(fdJdt);
close(fdJdx);
close(fdJlr);
fdHdr = fdJdt = fdJdx = fdJlr = -1;
sprintf(File, "%s%s", BaseName, ".$dr"); sprintf(File, "%s%s", BaseName, ".$dr");
unlink(File); sprintf(New, "%s%s", BaseName, EXT_HDRFILE);
if (fdnJdt != -1) unlink(New);
close(fdnJdt); rename(File, New);
sprintf(File, "%s%s", BaseName, ".$dt"); sprintf(File, "%s%s", BaseName, ".$dt");
unlink(File); sprintf(New, "%s%s", BaseName, EXT_TXTFILE);
if (fdnJdx != -1) unlink(New);
close(fdnJdx); rename(File, New);
sprintf(File, "%s%s", BaseName, ".$dx"); sprintf(File, "%s%s", BaseName, ".$dx");
unlink(File); sprintf(New, "%s%s", BaseName, EXT_IDXFILE);
if (fdnJlr != -1) unlink(New);
close(fdnJlr); rename(File, New);
sprintf(File, "%s%s", BaseName, ".$lr"); sprintf(File, "%s%s", BaseName, ".$lr");
unlink(File); sprintf(New, "%s%s", BaseName, EXT_LRDFILE);
free(File); unlink(New);
free(New); rename(File, New);
JAM_Open(BaseName);
}
if (fdnHdr != -1)
close(fdnHdr);
sprintf(File, "%s%s", BaseName, ".$dr");
unlink(File);
if (fdnJdt != -1)
close(fdnJdt);
sprintf(File, "%s%s", BaseName, ".$dt");
unlink(File);
if (fdnJdx != -1)
close(fdnJdx);
sprintf(File, "%s%s", BaseName, ".$dx");
unlink(File);
if (fdnJlr != -1)
close(fdnJlr);
sprintf(File, "%s%s", BaseName, ".$lr");
unlink(File);
free(File);
free(New);
} }