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/mbfido/mbfkill.c

238 lines
6.7 KiB
C
Raw Normal View History

2001-11-18 23:19:08 +00:00
/*****************************************************************************
*
* $Id$
* Purpose: File Database Maintenance, kill or move old files
*
*****************************************************************************
2004-02-21 17:22:00 +00:00
* Copyright (C) 1997-2004
2001-11-18 23:19:08 +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-11-18 23:19:08 +00:00
*****************************************************************************/
2002-06-30 12:48:44 +00:00
#include "../config.h"
2004-02-21 17:22:00 +00:00
#include "../lib/mbselib.h"
2002-01-07 19:16:03 +00:00
#include "../lib/users.h"
2004-02-21 17:22:00 +00:00
#include "../lib/mbsedb.h"
2001-11-18 23:19:08 +00:00
#include "mbfkill.h"
#include "mbfutil.h"
extern int do_quiet; /* Suppress screen output */
2001-11-18 23:19:08 +00:00
extern int do_pack; /* Perform pack */
/*
* Check files for age, and not downloaded for x days. If they match
* one of these criteria (setable in areas setup), the file will be
* move to some retire area or deleted, depending on the setup.
* If they are moved, the upload date is reset to the current date,
* so you can set new removal criteria again.
*/
void Kill(void)
{
2004-05-05 20:32:39 +00:00
FILE *pAreas;
2004-03-06 21:48:41 +00:00
int i, iAreas, iAreasNew = 0, iTotal = 0, iKilled = 0, iMoved = 0, rc, Killit;
2004-05-05 20:32:39 +00:00
char *sAreas, *newdir = NULL, *sTemp, from[PATH_MAX], to[PATH_MAX];
2003-03-13 19:27:48 +00:00
time_t Now;
struct fileareas darea;
2004-05-05 20:32:39 +00:00
struct _fdbarea *fdb_area = NULL, *dst_area = NULL;
sAreas = calloc(PATH_MAX, sizeof(char));
2003-03-13 19:27:48 +00:00
sTemp = calloc(PATH_MAX, sizeof(char));
2001-11-18 23:19:08 +00:00
2003-03-13 19:27:48 +00:00
IsDoing("Kill files");
if (!do_quiet) {
colour(3, 0);
printf("Kill/move files...\n");
}
2001-11-18 23:19:08 +00:00
2003-03-13 19:27:48 +00:00
sprintf(sAreas, "%s/etc/fareas.data", getenv("MBSE_ROOT"));
if ((pAreas = fopen (sAreas, "r")) == NULL) {
WriteError("Can't open %s", sAreas);
die(MBERR_INIT_ERROR);
}
fread(&areahdr, sizeof(areahdr), 1, pAreas);
fseek(pAreas, 0, SEEK_END);
iAreas = (ftell(pAreas) - areahdr.hdrsize) / areahdr.recsize;
Now = time(NULL);
2001-11-18 23:19:08 +00:00
2003-03-13 19:27:48 +00:00
for (i = 1; i <= iAreas; i++) {
fseek(pAreas, ((i-1) * areahdr.recsize) + areahdr.hdrsize, SEEK_SET);
fread(&area, areahdr.recsize, 1, pAreas);
if ((area.Available) && (area.DLdays || area.FDdays) && (!area.CDrom)) {
2004-03-21 12:55:45 +00:00
if (enoughspace(CFG.freespace) == 0)
2003-03-13 19:27:48 +00:00
die(MBERR_DISK_FULL);
if (!do_quiet) {
printf("\r%4d => %-44s \b\b\b\b", i, area.Name);
fflush(stdout);
}
/*
* Check if download directory exists, if not, create the directory.
*/
if (access(area.Path, R_OK) == -1) {
Syslog('!', "Create dir: %s", area.Path);
newdir = xstrcpy(area.Path);
newdir = xstrcat(newdir, (char *)"/");
mkdirs(newdir, 0755);
free(newdir);
newdir = NULL;
}
2004-05-05 20:32:39 +00:00
if ((fdb_area = mbsedb_OpenFDB(i, 30)) == NULL)
die(MBERR_GENERAL);
2003-03-13 19:27:48 +00:00
/*
* Now start checking the files in the filedatabase
* against the contents of the directory.
*/
2004-05-05 20:32:39 +00:00
while (fread(&fdb, fdbhdr.recsize, 1, fdb_area->fp) == 1) {
2003-03-13 19:27:48 +00:00
iTotal++;
Marker();
Killit = FALSE;
2004-03-06 21:48:41 +00:00
if (!fdb.UploadDate)
Syslog('!', "Warning: file %s in area %d has no upload date", fdb.Name, i);
2003-03-13 19:27:48 +00:00
if (area.DLdays) {
/*
* Test last download date or never downloaded and the
* file is more then n days available for download.
*/
2004-03-06 21:48:41 +00:00
if ((fdb.LastDL) && (((Now - fdb.LastDL) / 84400) > area.DLdays)) {
2003-03-13 19:27:48 +00:00
Killit = TRUE;
}
2004-03-06 21:48:41 +00:00
if ((!fdb.LastDL) && fdb.UploadDate && (((Now - fdb.UploadDate) / 84400) > area.DLdays)) {
2003-03-13 19:27:48 +00:00
Killit = TRUE;
}
}
if (area.FDdays) {
/*
* Check filedate
*/
2004-03-06 21:48:41 +00:00
if (fdb.UploadDate && (((Now - fdb.UploadDate) / 84400) > area.FDdays)) {
2003-03-13 19:27:48 +00:00
Killit = TRUE;
}
}
if (Killit) {
do_pack = TRUE;
if (area.MoveArea) {
fseek(pAreas, ((area.MoveArea -1) * areahdr.recsize) + areahdr.hdrsize, SEEK_SET);
fread(&darea, areahdr.recsize, 1, pAreas);
2004-03-06 21:48:41 +00:00
sprintf(from, "%s/%s", area.Path, fdb.Name);
sprintf(to, "%s/%s", darea.Path, fdb.Name);
2003-03-13 19:27:48 +00:00
if ((rc = file_mv(from, to)) == 0) {
2004-03-06 21:48:41 +00:00
Syslog('+', "Move %s, area %d => %d", fdb.Name, i, area.MoveArea);
2004-05-05 20:32:39 +00:00
if ((dst_area = mbsedb_OpenFDB(area.MoveArea, 30))) {
fdb.UploadDate = time(NULL);
fdb.LastDL = time(NULL);
mbsedb_InsertFDB(dst_area, fdb, FALSE);
mbsedb_CloseFDB(dst_area);
}
2003-03-13 19:27:48 +00:00
/*
* Now again if there is a dotted version (thumbnail) of this file.
*/
2004-03-06 21:48:41 +00:00
sprintf(from, "%s/.%s", area.Path, fdb.Name);
sprintf(to, "%s/.%s", darea.Path, fdb.Name);
2003-03-13 19:27:48 +00:00
if (file_exist(from, R_OK) == 0)
file_mv(from, to);
/*
* Unlink the old symbolic link
*/
2004-03-06 21:48:41 +00:00
sprintf(from, "%s/%s", area.Path, fdb.LName);
2003-03-13 19:27:48 +00:00
unlink(from);
/*
* Create the new symbolic link
*/
2004-03-06 21:48:41 +00:00
sprintf(from, "%s/%s", darea.Path, fdb.Name);
sprintf(to, "%s/%s", darea.Path, fdb.LName);
2003-03-13 19:27:48 +00:00
symlink(from, to);
2004-03-06 21:48:41 +00:00
fdb.Deleted = TRUE;
2004-05-05 20:32:39 +00:00
if (mbsedb_LockFDB(fdb_area, 30)) {
fseek(fdb_area->fp, - fdbhdr.recsize, SEEK_CUR);
fwrite(&fdb, fdbhdr.recsize, 1, fdb_area->fp);
mbsedb_UnlockFDB(fdb_area);
}
2003-03-13 19:27:48 +00:00
iMoved++;
} else {
2004-03-06 21:48:41 +00:00
WriteError("Move %s to area %d failed, %s", fdb.Name, area.MoveArea, strerror(rc));
2003-03-13 19:27:48 +00:00
}
} else {
2004-03-06 21:48:41 +00:00
Syslog('+', "Delete %s, area %d", fdb.LName, i);
fdb.Deleted = TRUE;
2004-05-05 20:32:39 +00:00
if (mbsedb_LockFDB(fdb_area, 30)) {
fseek(fdb_area->fp, - fdbhdr.recsize, SEEK_CUR);
fwrite(&fdb, fdbhdr.recsize, 1, fdb_area->fp);
mbsedb_UnlockFDB(fdb_area);
}
2003-03-13 19:27:48 +00:00
iKilled++;
2004-03-06 21:48:41 +00:00
sprintf(from, "%s/%s", area.Path, fdb.LName);
unlink(from);
sprintf(from, "%s/%s", area.Path, fdb.Name);
2003-03-13 19:27:48 +00:00
unlink(from);
2004-03-06 21:48:41 +00:00
sprintf(from, "%s/.%s", area.Path, fdb.Name);
2003-03-13 19:27:48 +00:00
unlink(from);
}
}
}
/*
* Now we must pack this area database otherwise
* we run into trouble later on.
*/
2004-05-05 20:32:39 +00:00
mbsedb_PackFDB(fdb_area);
mbsedb_CloseFDB(fdb_area);
2003-03-13 19:27:48 +00:00
iAreasNew++;
} /* if area.Available */
}
fclose(pAreas);
Syslog('+', "Kill Areas [%5d] Files [%5d] Deleted [%5d] Moved [%5d]", iAreasNew, iTotal, iKilled, iMoved);
if (!do_quiet) {
printf("\r \r");
fflush(stdout);
}
free(sTemp);
free(sAreas);
2001-11-18 23:19:08 +00:00
}