Added more dupes database debug logging

This commit is contained in:
Michiel Broek 2002-09-10 11:03:16 +00:00
parent 92d5e5464b
commit 7e1b714687

View File

@ -5,7 +5,7 @@
* Last modification date : 25-May-2001 * Last modification date : 25-May-2001
* *
***************************************************************************** *****************************************************************************
* Copyright (C) 1997-2001 * Copyright (C) 1997-2002
* *
* Michiel Broek FIDO: 2:280/2802 * Michiel Broek FIDO: 2:280/2802
* Beekmansbos 10 * Beekmansbos 10
@ -55,134 +55,145 @@ void CloseDdb(int);
void InitDupes() void InitDupes()
{ {
int i; int i;
for (i = 0; i < 3; i++) { Syslog('n', "Init Dupes");
dupes[i].crcs= NULL; for (i = 0; i < 3; i++) {
dupes[i].loaded = FALSE; dupes[i].crcs= NULL;
dupes[i].changed = FALSE; dupes[i].loaded = FALSE;
dupes[i].count = 0; dupes[i].changed = FALSE;
dupes[i].max = 0; dupes[i].count = 0;
} dupes[i].max = 0;
}
} }
int CheckDupe(unsigned long crc, int idx, int max) int CheckDupe(unsigned long crc, int idx, int max)
{ {
char *dfile; char *dfile;
FILE *fil; FILE *fil;
unsigned long test; unsigned long test;
int i, size = 0; int i, size = 0;
if (!dupes[idx].loaded) { if (!dupes[idx].loaded) {
dfile = calloc(PATH_MAX, sizeof(char)); dfile = calloc(PATH_MAX, sizeof(char));
sprintf(dfile, "%s/etc/%s.dupe", getenv("MBSE_ROOT"), files[idx]); sprintf(dfile, "%s/etc/%s.dupe", getenv("MBSE_ROOT"), files[idx]);
if ((fil = fopen(dfile, "r+")) == NULL) { if ((fil = fopen(dfile, "r+")) == NULL) {
/* /*
* Dupe database doesn't exist yet. * Dupe database doesn't exist yet.
*/ */
if ((fil = fopen(dfile, "w")) == NULL) { if ((fil = fopen(dfile, "w")) == NULL) {
WriteError("$PANIC: dbdupe.c, can't create %s", dfile); WriteError("$PANIC: dbdupe.c, can't create %s", dfile);
free(dfile);
exit(1);
}
fclose(fil);
fil = fopen(dfile, "r+");
} else {
fseek(fil, 0L, SEEK_END);
size = ftell(fil) / sizeof(unsigned long);
fseek(fil, 0L, SEEK_SET);
}
/*
* Reserve some extra memeory and record howmuch.
*/
if (size > max)
dupes[idx].peak = size + 5000;
else
dupes[idx].peak = max + 5000;
dupes[idx].crcs = (unsigned long *)malloc(dupes[idx].peak * sizeof(unsigned long));
/*
* Load dupe records
*/
while (fread(&test, sizeof(test), 1, fil) == 1) {
dupes[idx].crcs[dupes[idx].count] = test;
dupes[idx].count++;
}
fclose(fil);
free(dfile); free(dfile);
dupes[idx].loaded = TRUE; exit(1);
dupes[idx].max = max; }
fclose(fil);
fil = fopen(dfile, "r+");
} else {
fseek(fil, 0L, SEEK_END);
size = ftell(fil) / sizeof(unsigned long);
fseek(fil, 0L, SEEK_SET);
} }
Syslog('n', "dupetest %08x %d %d", crc, idx, max); /*
* Reserve some extra memory and record howmuch.
*/
if (size > max)
dupes[idx].peak = size + 5000;
else
dupes[idx].peak = max + 5000;
dupes[idx].crcs = (unsigned long *)malloc(dupes[idx].peak * sizeof(unsigned long));
for (i = 0; i < dupes[idx].count; i++) { /*
if (dupes[idx].crcs[i] == crc) { * Load dupe records
Syslog('n', "dupe at %d", i); */
return TRUE; while (fread(&test, sizeof(test), 1, fil) == 1) {
} dupes[idx].crcs[dupes[idx].count] = test;
dupes[idx].count++;
} }
/* Syslog('n', "Loaded %d dupe records in %s", dupes[idx].count++, files[idx]);
* Not a dupe, append new crc value fclose(fil);
*/ free(dfile);
dupes[idx].crcs[dupes[idx].count] = crc; dupes[idx].loaded = TRUE;
dupes[idx].count++; dupes[idx].max = max;
dupes[idx].changed = TRUE; }
/* Syslog('n', "dupetest %08x %s %d", crc, files[idx], max);
* If we reach the high limit, flush the current dupelist.
*/ for (i = 0; i < dupes[idx].count; i++) {
if (dupes[idx].count >= dupes[idx].peak) if (dupes[idx].crcs[i] == crc) {
CloseDdb(idx); Syslog('n', "dupe at %d", i);
return FALSE; return TRUE;
}
}
/*
* Not a dupe, append new crc value
*/
dupes[idx].crcs[dupes[idx].count] = crc;
Syslog('n', "Added new dupe at %d", dupes[idx].count);
dupes[idx].count++;
dupes[idx].changed = TRUE;
/*
* If we reach the high limit, flush the current dupelist.
*/
if (dupes[idx].count >= dupes[idx].peak)
CloseDdb(idx);
return FALSE;
} }
void CloseDdb(int idx) void CloseDdb(int idx)
{ {
int j, start; int j, start;
char *dfile; char *dfile;
FILE *fil; FILE *fil;
dfile = calloc(PATH_MAX, sizeof(char)); dfile = calloc(PATH_MAX, sizeof(char));
if (dupes[idx].loaded) { Syslog('n', "Checking %s.dupe", files[idx]);
if (dupes[idx].changed) { if (dupes[idx].loaded) {
if (dupes[idx].count > dupes[idx].max) if (dupes[idx].changed) {
start = dupes[idx].count - dupes[idx].max; if (dupes[idx].count > dupes[idx].max)
else start = dupes[idx].count - dupes[idx].max;
start = 0; else
sprintf(dfile, "%s/etc/%s.dupe", getenv("MBSE_ROOT"), files[idx]); start = 0;
if ((fil = fopen(dfile, "w"))) { sprintf(dfile, "%s/etc/%s.dupe", getenv("MBSE_ROOT"), files[idx]);
for (j = start; j < dupes[idx].count; j++) if ((fil = fopen(dfile, "w"))) {
fwrite(&dupes[idx].crcs[j], sizeof(unsigned long), 1, fil); Syslog('n', "Writing dupes %d to %d", start, dupes[idx].count);
fclose(fil); for (j = start; j < dupes[idx].count; j++)
} else { fwrite(&dupes[idx].crcs[j], sizeof(unsigned long), 1, fil);
WriteError("$Can't write %s", dfile); fclose(fil);
} } else {
} WriteError("$Can't write %s", dfile);
}
dupes[idx].changed = FALSE; } else {
dupes[idx].loaded = FALSE; Syslog('n', "Not changed so not saved");
dupes[idx].count = 0;
dupes[idx].max = 0;
dupes[idx].peak = 0;
free(dupes[idx].crcs);
dupes[idx].crcs = NULL;
} }
free(dfile);
dupes[idx].changed = FALSE;
dupes[idx].loaded = FALSE;
dupes[idx].count = 0;
dupes[idx].max = 0;
dupes[idx].peak = 0;
free(dupes[idx].crcs);
dupes[idx].crcs = NULL;
} else {
Syslog('n', "Not loaded");
}
free(dfile);
} }
void CloseDupes() void CloseDupes()
{ {
int i; int i;
for (i = 0; i < 3; i++) Syslog('n', "Closing dupes databases");
CloseDdb(i); for (i = 0; i < 3; i++)
CloseDdb(i);
} }