This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
deb-mbse/lib/dbfdb.c

361 lines
8.9 KiB
C
Raw Normal View History

2004-05-03 12:35:58 +00:00
/*****************************************************************************
*
* $Id$
* Purpose ...............: Files database functions
*
*****************************************************************************
* Copyright (C) 1997-2004
*
* 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
* Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*****************************************************************************/
#include "../config.h"
#include "mbselib.h"
#include "users.h"
#include "mbsedb.h"
2004-05-03 19:35:53 +00:00
#ifdef USE_EXPERIMENT
2004-05-03 19:11:04 +00:00
/*
2004-05-05 11:40:10 +00:00
* Open files database Area number. Do some checks and abort
2004-05-03 19:11:04 +00:00
* if they fail.
*/
2004-05-04 12:31:23 +00:00
struct _fdbarea *mbsedb_OpenFDB(long Area, int Timeout)
2004-05-03 19:11:04 +00:00
{
2004-05-04 12:31:23 +00:00
char *temp;
struct _fdbarea *fdb_area = NULL;
int Tries = 0;
2004-05-04 14:31:25 +00:00
FILE *fp;
2004-05-04 12:31:23 +00:00
Syslog('f', "OpenFDB area %ld, timeout %d", Area, Timeout);
2004-05-03 19:11:04 +00:00
temp = calloc(PATH_MAX, sizeof(char));
2004-05-04 14:31:25 +00:00
fdb_area = malloc(sizeof(struct _fdbarea)); /* Will be freed by CloseFDB */
2004-05-03 19:35:53 +00:00
sprintf(temp, "%s/fdb/file%ld.data", getenv("MBSE_ROOT"), Area);
2004-05-03 19:11:04 +00:00
/*
* Open the file database, if it's locked, just wait.
*/
2004-05-04 14:31:25 +00:00
while (((fp = fopen(temp, "r+")) == NULL) && ((errno == EACCES) || (errno == EAGAIN))) {
2004-05-04 12:31:23 +00:00
if (++Tries >= (Timeout * 4)) {
2004-05-03 19:11:04 +00:00
WriteError("Can't open file area %ld, timeout", Area);
free(temp);
return NULL;
}
msleep(250);
Syslog('f', "Open file area %ld, try %d", Area, Tries);
}
2004-05-05 11:40:10 +00:00
if (fp == NULL) {
if (errno == ENOENT) {
Syslog('+', "Create empty FDB for area %ld", Area);
fdbhdr.hdrsize = sizeof(fdbhdr);
fdbhdr.recsize = sizeof(fdb);
if ((fp = fopen(temp, "w+"))) {
fwrite(&fdbhdr, sizeof(fdbhdr), 1, fp);
}
}
} else {
fread(&fdbhdr, sizeof(fdbhdr), 1, fp);
}
/*
* If still not open, it's fatal.
*/
2004-05-04 14:31:25 +00:00
if (fp == NULL) {
2004-05-03 19:11:04 +00:00
WriteError("$Can't open %s", temp);
free(temp);
return NULL;
}
/*
* Fix attributes if needed
*/
chmod(temp, 0660);
free(temp);
if ((fdbhdr.hdrsize != sizeof(fdbhdr)) || (fdbhdr.recsize != sizeof(fdb))) {
WriteError("Files database header in area %d is corrupt (%d:%d)", Area, fdbhdr.hdrsize, fdbhdr.recsize);
2004-05-04 12:31:23 +00:00
fclose(fdb_area->fp);
2004-05-03 19:11:04 +00:00
return NULL;
}
2004-05-04 14:31:25 +00:00
fseek(fp, 0, SEEK_END);
if ((ftell(fp) - fdbhdr.hdrsize) % fdbhdr.recsize) {
2004-05-03 19:11:04 +00:00
WriteError("Files database area %ld is corrupt, unalligned records", Area);
2004-05-04 14:31:25 +00:00
fclose(fp);
2004-05-03 19:11:04 +00:00
return NULL;
}
/*
* Point to the first record
*/
2004-05-04 14:31:25 +00:00
fseek(fp, fdbhdr.hdrsize, SEEK_SET);
fdb_area->fp = fp;
2004-05-04 12:31:23 +00:00
fdb_area->locked = 0;
fdb_area->area = Area;
Syslog('f', "OpenFDB success");
return fdb_area;
2004-05-03 19:11:04 +00:00
}
/*
* Close current open file area
*/
2004-05-04 12:31:23 +00:00
int mbsedb_CloseFDB(struct _fdbarea *fdb_area)
2004-05-03 19:11:04 +00:00
{
2004-05-04 12:31:23 +00:00
Syslog('f', "CloseFDB %ld", fdb_area->area);
if (fdb_area->locked) {
2004-05-03 19:11:04 +00:00
/*
* Unlock first
*/
2004-05-04 12:31:23 +00:00
mbsedb_UnlockFDB(fdb_area);
}
fclose(fdb_area->fp);
2004-05-04 14:31:25 +00:00
free(fdb_area);
2004-05-04 12:31:23 +00:00
return TRUE;
}
2004-05-05 13:20:07 +00:00
/*
* Lock Files DataBase
*/
2004-05-04 12:31:23 +00:00
int mbsedb_LockFDB(struct _fdbarea *fdb_area, int Timeout)
{
int rc, Tries = 0;
struct flock fl;
Syslog('f', "LockFDB %ld", fdb_area->area);
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0L;
fl.l_len = 1L;
fl.l_pid = getpid();
while ((rc = fcntl(fileno(fdb_area->fp), F_SETLK, &fl)) && ((errno == EACCES) || (errno == EAGAIN))) {
if (++Tries >= (Timeout * 4)) {
fcntl(fileno(fdb_area->fp), F_GETLK, &fl);
WriteError("FDB %ld is locked by pid %d", fdb_area->area, fl.l_pid);
return FALSE;
}
msleep(250);
Syslog('f', "FDB lock attempt %d", Tries);
}
if (rc) {
WriteError("$FDB %ld lock error", fdb_area->area);
return FALSE;
}
Syslog('f', "LockFDB success");
fdb_area->locked = 1;
return TRUE;
}
2004-05-05 13:20:07 +00:00
/*
* Unlock Files DataBase
*/
2004-05-04 12:31:23 +00:00
int mbsedb_UnlockFDB(struct _fdbarea *fdb_area)
{
struct flock fl;
Syslog('f', "UnlockFDB %ld", fdb_area->area);
fl.l_type = F_UNLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 0L;
fl.l_len = 1L;
fl.l_pid = getpid();
if (fcntl(fileno(fdb_area->fp), F_SETLK, &fl)) {
WriteError("$Can't unlock FDB area %ld", fdb_area->area);
2004-05-03 19:11:04 +00:00
}
2004-05-04 12:31:23 +00:00
fdb_area->locked = 0;
2004-05-03 19:35:53 +00:00
return TRUE;
2004-05-03 19:11:04 +00:00
}
2004-05-05 13:20:07 +00:00
int mbsedb_InsertFDB(struct _fdbarea *fdb_area, struct FILE_record frec, int AddAlpha)
{
char *temp, *temp2;
int i, Insert, Done = FALSE, Found = FALSE, rc;
FILE *fp;
Syslog('f', "InsertFDB %ld %s", fdb_area->area, frec.LName);
if (mbsedb_LockFDB(fdb_area, 30) == FALSE)
return FALSE;
fseek(fdb_area->fp, 0, SEEK_END);
if (ftell(fdb_area->fp) == fdbhdr.hdrsize) {
/*
* No records yet, simply append this first record.
*/
Syslog('f', "append first record");
2004-05-05 14:21:15 +00:00
fwrite(&frec, fdbhdr.recsize, 1, fdb_area->fp);
2004-05-05 13:20:07 +00:00
mbsedb_UnlockFDB(fdb_area);
return TRUE;
}
/*
* There are files, search the insert point.
*/
temp = calloc(PATH_MAX, sizeof(char));
temp2 = calloc(PATH_MAX, sizeof(char));
sprintf(temp, "%s/fdb/file%ld.temp", getenv("MBSE_ROOT"), fdb_area->area);
fseek(fdb_area->fp, fdbhdr.hdrsize, SEEK_SET);
Insert = 0;
do {
if (fread(&fdb, fdbhdr.recsize, 1, fdb_area->fp) != 1)
Done = TRUE;
if (!Done) {
if (strcmp(frec.LName, fdb.LName) == 0) {
Found = TRUE;
Insert++;
} else if (strcmp(frec.LName, fdb.LName) < 0)
Found = TRUE;
else
Insert++;
}
} while ((!Found) && (!Done));
if (Found) {
if ((fp = fopen(temp, "a+")) == NULL) {
WriteError("$Can't create %s", temp);
mbsedb_UnlockFDB(fdb_area);
free(temp);
return FALSE;
}
fwrite(&fdbhdr, sizeof(fdbhdr), 1, fp);
fseek(fdb_area->fp, fdbhdr.hdrsize, SEEK_SET);
/*
* Copy entries untill the insert point.
*/
for (i = 0; i < Insert; i++) {
2004-05-05 14:21:15 +00:00
fread(&fdb, fdbhdr.recsize, 1, fdb_area->fp);
2004-05-05 13:20:07 +00:00
/*
* If we see a magic that is the new magic, remove
* the old one.
*/
if (strlen(frec.Magic) && (strcmp(fdb.Magic, frec.Magic) == 0)) {
Syslog('f', "InsertFDB: remove magic from %s (%s)", fdb.Name, fdb.LName);
memset(&fdb.Magic, 0, sizeof(fdb.Magic));
}
/*
* Check if we are importing a file with the same
* name, if so, don't copy the original database
* record. The file is also overwritten.
*/
if (strcmp(fdb.LName, frec.LName) != 0)
fwrite(&fdb, fdbhdr.recsize, 1, fp);
}
if (AddAlpha) {
/*
* Insert new entry
*/
fwrite(&frec, fdbhdr.recsize, 1, fp);
}
/*
* Append the rest of the entries
*/
while (fread(&fdb, fdbhdr.recsize, 1, fdb_area->fp) == 1) {
/*
* If we see a magic that is the new magic, remove
* the old one.
*/
if (strlen(frec.Magic) && (strcmp(fdb.Magic, frec.Magic) == 0)) {
Syslog('f', "InsertFDB: remove magic from %s (%s)", fdb.Name, fdb.LName);
memset(&fdb.Magic, 0, sizeof(fdb.Magic));
}
/*
* Check if we are importing a file with the same
* name, if so, don't copy the original database
* record. The file is also overwritten.
*/
if (strcmp(fdb.LName, frec.LName) != 0)
fwrite(&fdb, fdbhdr.recsize, 1, fp);
}
if (! AddAlpha) {
/*
* Append
*/
fwrite(&frec, fdbhdr.recsize, 1, fp);
}
/*
* 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/fdb/file%ld.data", getenv("MBSE_ROOT"), fdb_area->area);
sprintf(temp, "%s/fdb/file%ld.xxxx", getenv("MBSE_ROOT"), fdb_area->area);
rc = rename(temp2, temp);
Syslog('f', "rename %s %s rc=%d", temp2, temp, rc);
sprintf(temp, "%s/fdb/file%ld.temp", getenv("MBSE_ROOT"), fdb_area->area);
rc = rename(temp, temp2);
Syslog('f', "rename %s %s rc=%d", temp, temp2, rc);
sprintf(temp, "%s/fdb/file%ld.xxxx", getenv("MBSE_ROOT"), fdb_area->area);
rc = unlink(temp);
Syslog('f', "unlink %s rc=%d", temp, rc);
fdb_area->fp = fp;
fdb_area->locked = 0;
} else {
/*
* Append new entry
*/
fseek(fdb_area->fp, 0, SEEK_END);
fwrite(&frec, fdbhdr.recsize, 1, fdb_area->fp);
}
free(temp);
free(temp2);
Syslog('f', "success");
return TRUE;
}
2004-05-03 19:11:04 +00:00
// mbsedb_SearchFileFDB
// mbsedb_UpdateFileFDB
// mbsedb_InsertFileFDB
// mbsedb_DeleteFileFDB
// mbsedb_SortFDB
2004-05-03 13:23:02 +00:00
#endif