2001-08-17 05:46:24 +00:00
|
|
|
/*****************************************************************************
|
|
|
|
*
|
2002-10-18 18:23:35 +00:00
|
|
|
* $Id$
|
2001-08-17 05:46:24 +00:00
|
|
|
* Purpose ...............: Dupe checking.
|
|
|
|
*
|
|
|
|
*****************************************************************************
|
2005-10-11 20:49:41 +00:00
|
|
|
* Copyright (C) 1997-2005
|
2001-08-17 05:46:24 +00:00
|
|
|
*
|
|
|
|
* Michiel Broek FIDO: 2:280/2802
|
|
|
|
* Beekmansbos 10
|
|
|
|
* 1971 BV IJmuiden
|
|
|
|
* the Netherlands
|
|
|
|
*
|
|
|
|
* This file is part of MBSE BBS.
|
|
|
|
*
|
|
|
|
* This BBS is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2, or (at your option) any
|
|
|
|
* later version.
|
|
|
|
*
|
|
|
|
* MBSE BBS is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with MBSE BBS; see the file COPYING. If not, write to the Free
|
2003-08-15 20:05:34 +00:00
|
|
|
* Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
2001-08-17 05:46:24 +00:00
|
|
|
*****************************************************************************/
|
|
|
|
|
2002-06-30 12:48:44 +00:00
|
|
|
#include "../config.h"
|
2004-02-21 14:24:03 +00:00
|
|
|
#include "mbselib.h"
|
|
|
|
#include "mbse.h"
|
|
|
|
#include "users.h"
|
|
|
|
#include "mbsedb.h"
|
2001-08-17 05:46:24 +00:00
|
|
|
|
|
|
|
|
2002-10-20 20:58:55 +00:00
|
|
|
|
2001-08-17 05:46:24 +00:00
|
|
|
typedef struct _dupesrec {
|
2005-10-11 20:49:41 +00:00
|
|
|
unsigned int *crcs;
|
2001-08-17 05:46:24 +00:00
|
|
|
int loaded;
|
|
|
|
int changed;
|
|
|
|
int count;
|
|
|
|
int max;
|
|
|
|
int peak;
|
|
|
|
} dupesrec;
|
|
|
|
|
|
|
|
|
|
|
|
dupesrec dupes[3];
|
|
|
|
static char *files[] = {(char *)"echomail", (char *)"fileecho", (char *)"news"};
|
|
|
|
void CloseDdb(int);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void InitDupes()
|
|
|
|
{
|
2002-09-10 11:03:16 +00:00
|
|
|
int i;
|
|
|
|
|
2003-12-07 13:18:09 +00:00
|
|
|
Syslog('n', "Init Dupes");
|
|
|
|
memset(dupes, 0, sizeof(dupes));
|
|
|
|
|
2002-09-10 11:03:16 +00:00
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
dupes[i].crcs= NULL;
|
|
|
|
dupes[i].loaded = FALSE;
|
|
|
|
dupes[i].changed = FALSE;
|
|
|
|
dupes[i].count = 0;
|
|
|
|
dupes[i].max = 0;
|
|
|
|
}
|
2001-08-17 05:46:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-10-11 20:49:41 +00:00
|
|
|
int CheckDupe(unsigned int crc, int idx, int max)
|
2001-08-17 05:46:24 +00:00
|
|
|
{
|
2002-09-10 11:03:16 +00:00
|
|
|
char *dfile;
|
|
|
|
FILE *fil;
|
2005-10-11 20:49:41 +00:00
|
|
|
unsigned int test;
|
2002-09-10 11:03:16 +00:00
|
|
|
int i, size = 0;
|
|
|
|
|
|
|
|
if (!dupes[idx].loaded) {
|
|
|
|
dfile = calloc(PATH_MAX, sizeof(char));
|
2005-08-28 10:30:41 +00:00
|
|
|
snprintf(dfile, PATH_MAX -1, "%s/etc/%s.dupe", getenv("MBSE_ROOT"), files[idx]);
|
2002-09-10 11:03:16 +00:00
|
|
|
if ((fil = fopen(dfile, "r+")) == NULL) {
|
|
|
|
/*
|
|
|
|
* Dupe database doesn't exist yet.
|
|
|
|
*/
|
|
|
|
if ((fil = fopen(dfile, "w")) == NULL) {
|
|
|
|
WriteError("$PANIC: dbdupe.c, can't create %s", dfile);
|
2001-08-17 05:46:24 +00:00
|
|
|
free(dfile);
|
2002-10-20 20:58:55 +00:00
|
|
|
exit(MBERR_INIT_ERROR);
|
2002-09-10 11:03:16 +00:00
|
|
|
}
|
|
|
|
fclose(fil);
|
|
|
|
fil = fopen(dfile, "r+");
|
|
|
|
} else {
|
|
|
|
fseek(fil, 0L, SEEK_END);
|
2005-10-11 20:49:41 +00:00
|
|
|
size = ftell(fil) / sizeof(unsigned int);
|
2002-09-10 11:03:16 +00:00
|
|
|
fseek(fil, 0L, SEEK_SET);
|
2001-08-17 05:46:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2002-09-10 11:03:16 +00:00
|
|
|
* Reserve some extra memory and record howmuch.
|
2001-08-17 05:46:24 +00:00
|
|
|
*/
|
2002-09-10 11:03:16 +00:00
|
|
|
if (size > max)
|
|
|
|
dupes[idx].peak = size + 5000;
|
|
|
|
else
|
|
|
|
dupes[idx].peak = max + 5000;
|
2005-10-11 20:49:41 +00:00
|
|
|
dupes[idx].crcs = (unsigned int *)malloc(dupes[idx].peak * sizeof(unsigned int));
|
|
|
|
memset(dupes[idx].crcs, 0, dupes[idx].peak * sizeof(unsigned int));
|
2001-08-17 05:46:24 +00:00
|
|
|
|
|
|
|
/*
|
2002-09-10 11:03:16 +00:00
|
|
|
* Load dupe records
|
2001-08-17 05:46:24 +00:00
|
|
|
*/
|
2002-09-10 11:03:16 +00:00
|
|
|
while (fread(&test, sizeof(test), 1, fil) == 1) {
|
|
|
|
dupes[idx].crcs[dupes[idx].count] = test;
|
|
|
|
dupes[idx].count++;
|
|
|
|
}
|
|
|
|
fclose(fil);
|
|
|
|
free(dfile);
|
|
|
|
dupes[idx].loaded = TRUE;
|
|
|
|
dupes[idx].max = max;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < dupes[idx].count; i++) {
|
|
|
|
if (dupes[idx].crcs[i] == crc) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Not a dupe, append new crc value
|
|
|
|
*/
|
|
|
|
dupes[idx].crcs[dupes[idx].count] = crc;
|
|
|
|
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;
|
2001-08-17 05:46:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void CloseDdb(int idx)
|
|
|
|
{
|
2002-09-10 11:03:16 +00:00
|
|
|
int j, start;
|
|
|
|
char *dfile;
|
|
|
|
FILE *fil;
|
|
|
|
|
|
|
|
dfile = calloc(PATH_MAX, sizeof(char));
|
|
|
|
if (dupes[idx].loaded) {
|
|
|
|
if (dupes[idx].changed) {
|
|
|
|
if (dupes[idx].count > dupes[idx].max)
|
|
|
|
start = dupes[idx].count - dupes[idx].max;
|
|
|
|
else
|
|
|
|
start = 0;
|
2005-08-28 10:30:41 +00:00
|
|
|
snprintf(dfile, PATH_MAX -1, "%s/etc/%s.dupe", getenv("MBSE_ROOT"), files[idx]);
|
2002-09-10 11:03:16 +00:00
|
|
|
if ((fil = fopen(dfile, "w"))) {
|
|
|
|
for (j = start; j < dupes[idx].count; j++)
|
2005-10-11 20:49:41 +00:00
|
|
|
fwrite(&dupes[idx].crcs[j], sizeof(unsigned int), 1, fil);
|
2002-09-10 11:03:16 +00:00
|
|
|
fclose(fil);
|
|
|
|
} else {
|
|
|
|
WriteError("$Can't write %s", dfile);
|
|
|
|
}
|
2001-08-17 05:46:24 +00:00
|
|
|
}
|
2002-09-10 11:03:16 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
}
|
|
|
|
free(dfile);
|
2001-08-17 05:46:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void CloseDupes()
|
|
|
|
{
|
2002-09-10 11:03:16 +00:00
|
|
|
int i;
|
2001-08-17 05:46:24 +00:00
|
|
|
|
2002-09-10 11:03:16 +00:00
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
CloseDdb(i);
|
2001-08-17 05:46:24 +00:00
|
|
|
}
|
|
|
|
|