do not write status file when corrupted

unfortunately this is only a guess
This commit is contained in:
2024-11-14 09:06:01 +01:00
parent 68f2042d8f
commit 1ab4723261

View File

@ -415,7 +415,10 @@ int WriteSicsStatus(SicsInterp * self, char *file, int iMot)
pIDrivable pDriv = NULL; pIDrivable pDriv = NULL;
void *pTest = NULL; void *pTest = NULL;
char tmpfile[PATH_MAX]; char tmpfile[PATH_MAX];
char buf[PATH_MAX];
int l; int l;
static int minStatusFileLength = 0;
static int firstbad = 1;
assert(self); assert(self);
assert(file); assert(file);
@ -466,6 +469,27 @@ int WriteSicsStatus(SicsInterp * self, char *file, int iMot)
if (iMot) { if (iMot) {
fprintf(fd, "Success \n"); fprintf(fd, "Success \n");
} }
l = ftell(fd);
if (l <= minStatusFileLength) {
/* do not rename file, if length is short (this seems to happen when the computer crashes, why?)*/
snprintf(buf, sizeof buf, "do not write status file with length only %d", l);
Log(FATAL,"sys", buf);
fclose(fd);
strcpy(buf, tmpfile);
l = strlen(buf);
if (firstbad) {
if (l < sizeof buf - 12) {
firstbad = 0;
snprintf(buf + l, 12, "%ld", time(NULL));
rename(tmpfile, buf);
}
} else {
buf[l-1] = '_';
rename(tmpfile, buf);
}
return 1;
}
minStatusFileLength = l / 2;
fclose(fd); fclose(fd);
/* rename temporary to final file (this is an atomic action) */ /* rename temporary to final file (this is an atomic action) */
if (0 > rename(tmpfile, file)) { if (0 > rename(tmpfile, file)) {