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/mbfimport.c

428 lines
10 KiB
C
Raw Normal View History

2001-11-25 12:25:59 +00:00
/*****************************************************************************
*
* $Id$
* Purpose: File Database Maintenance - Import files with files.bbs
*
*****************************************************************************
2002-01-07 19:16:03 +00:00
* Copyright (C) 1997-2002
2001-11-25 12:25:59 +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-25 12:25:59 +00:00
*****************************************************************************/
2002-06-30 12:48:44 +00:00
#include "../config.h"
2001-11-25 12:25:59 +00:00
#include "../lib/libs.h"
#include "../lib/structs.h"
2002-01-07 19:16:03 +00:00
#include "../lib/users.h"
2001-11-25 12:25:59 +00:00
#include "../lib/records.h"
#include "../lib/common.h"
#include "../lib/clcomm.h"
#include "../lib/dbcfg.h"
2002-10-20 20:58:55 +00:00
#include "../lib/mberrors.h"
2001-11-25 12:25:59 +00:00
#include "virscan.h"
#include "mbfutil.h"
#include "mbfimport.h"
extern int do_quiet; /* Suppress screen output */
extern int do_annon; /* Suppress announce files */
extern int do_novir; /* Suppress virus scanning */
2001-11-25 12:25:59 +00:00
void ImportFiles(int Area)
{
char *pwd, *temp, *temp2, *tmpdir, *String, *token, *dest, *unarc, *lname;
2001-12-02 20:49:36 +00:00
FILE *fbbs;
2003-01-19 14:43:23 +00:00
DIR *dp;
int Append = FALSE, Files = 0, rc, i, line = 0, pos, x, Doit;
int Imported = 0, Errors = 0, Present = FALSE;
2001-11-25 12:25:59 +00:00
struct FILERecord fdb;
struct stat statfile;
2003-01-19 14:43:23 +00:00
struct dirent *de;
2001-11-25 12:25:59 +00:00
Syslog('-', "Import(%d)", Area);
if (!do_quiet)
colour(CYAN, BLACK);
2001-12-02 20:49:36 +00:00
if (LoadAreaRec(Area) == FALSE)
2002-10-20 20:58:55 +00:00
die(MBERR_INIT_ERROR);
2001-11-25 12:25:59 +00:00
if (area.Available && !area.CDrom) {
temp = calloc(PATH_MAX, sizeof(char));
temp2 = calloc(PATH_MAX, sizeof(char));
pwd = calloc(PATH_MAX, sizeof(char));
tmpdir = calloc(PATH_MAX, sizeof(char));
String = calloc(4096, sizeof(char));
2001-11-25 12:25:59 +00:00
dest = calloc(PATH_MAX, sizeof(char));
lname = calloc(PATH_MAX, sizeof(char));
2001-11-25 12:25:59 +00:00
getcwd(pwd, PATH_MAX);
if (CheckFDB(Area, area.Path))
2002-10-20 20:58:55 +00:00
die(MBERR_GENERAL);
sprintf(tmpdir, "%s/tmp/arc", getenv("MBSE_ROOT"));
2001-11-25 12:25:59 +00:00
IsDoing("Import files");
/*
* Find files.bbs
*/
sprintf(temp, "FILES.BBS");
if ((fbbs = fopen(temp, "r")) == NULL) {
sprintf(temp, "files.bbs");
if ((fbbs = fopen(temp, "r")) == NULL) {
sprintf(temp, "%s", area.FilesBbs);
if ((fbbs = fopen(temp, "r")) == NULL) {
WriteError("Can't find files.bbs anywhere");
if (!do_quiet)
printf("Can't find files.bbs anywhere\n");
2002-10-20 20:58:55 +00:00
die(MBERR_INIT_ERROR);
2001-11-25 12:25:59 +00:00
}
}
}
while (fgets(String, 4095, fbbs) != NULL) {
2001-11-25 12:25:59 +00:00
if ((String[0] != ' ') && (String[0] != '\t')) {
2001-11-25 12:25:59 +00:00
/*
* New file entry, check if there has been a file that is not yet saved.
*/
if (Append && Present) {
2001-11-25 12:25:59 +00:00
Doit = TRUE;
if ((unarc = unpacker(temp)) == NULL) {
Syslog('+', "Unknown archive format %s", temp);
2003-01-19 14:43:23 +00:00
sprintf(temp2, "%s/tmp/arc/%s", getenv("MBSE_ROOT"), fdb.Name);
2002-01-19 13:04:55 +00:00
mkdirs(temp2, 0755);
2002-05-10 18:14:27 +00:00
if ((rc = file_cp(temp, temp2))) {
2002-05-18 12:57:00 +00:00
WriteError("Can't copy file to %s, %s", temp2, strerror(rc));
2002-05-10 18:14:27 +00:00
if (!do_quiet)
printf("Can't copy file to %s, %s\n", temp2, strerror(rc));
2001-11-25 12:25:59 +00:00
Doit = FALSE;
} else {
2002-06-15 14:00:49 +00:00
if (do_novir == FALSE) {
if (!do_quiet) {
printf("Virscan \b\b\b\b\b\b\b\b\b\b");
fflush(stdout);
}
if (VirScan(tmpdir)) {
Doit = FALSE;
}
2001-11-25 12:25:59 +00:00
}
}
} else {
if (!do_quiet) {
printf("Unpacking \b\b\b\b\b\b\b\b\b\b");
fflush(stdout);
}
if (UnpackFile(temp)) {
2002-06-15 14:00:49 +00:00
if (do_novir == FALSE) {
if (!do_quiet) {
printf("Virscan \b\b\b\b\b\b\b\b\b\b");
fflush(stdout);
}
if (VirScan(tmpdir)) {
Doit = FALSE;
}
2001-11-25 12:25:59 +00:00
}
} else {
Doit = FALSE;
}
}
DeleteVirusWork();
if (Doit) {
if (!do_quiet) {
printf("Adding \b\b\b\b\b\b\b\b\b\b");
fflush(stdout);
}
if (strcmp(fdb.Name, fdb.LName)) {
if (AddFile(fdb, Area, dest, temp, lname)) {
Imported++;
} else {
Errors++;
}
} else {
if (AddFile(fdb, Area, dest, temp, NULL)) {
Imported++;
} else {
Errors++;
}
}
2001-11-25 12:25:59 +00:00
} else {
Errors++;
}
Append = FALSE;
Present = FALSE;
2003-01-19 14:43:23 +00:00
line = 0;
pos = 0;
2001-11-25 12:25:59 +00:00
}
/*
* Check diskspace
*/
if (!diskfree(CFG.freespace))
2002-10-20 20:58:55 +00:00
die(MBERR_DISK_FULL);
2001-11-25 12:25:59 +00:00
Files++;
memset(&fdb, 0, sizeof(fdb));
Present = TRUE;
2001-11-25 12:25:59 +00:00
token = strtok(String, " \t");
2001-11-27 22:55:00 +00:00
/*
* Test filename against name on disk, first normal case,
* then lowercase and finally uppercase.
*/
2003-01-19 14:43:23 +00:00
if ((dp = opendir(pwd)) == NULL) {
WriteError("$Can't open directory %s", pwd);
die(MBERR_INIT_ERROR);
}
while ((de = readdir(dp))) {
if (strcasecmp(de->d_name, token) == 0) {
/*
* Found the right file.
*/
strncpy(fdb.LName, token, 80);
break;
2001-11-27 22:55:00 +00:00
}
}
2003-01-19 14:43:23 +00:00
closedir(dp);
2001-11-27 22:55:00 +00:00
2003-01-19 14:43:23 +00:00
if (strlen(fdb.LName) == 0) {
WriteError("Can't find file on disk, skipping: %s\n", token);
Append = FALSE;
Present = FALSE;
} else {
/*
* Create DOS 8.3 filename
*/
strcpy(temp2, fdb.LName);
name_mangle(temp2);
strcpy(fdb.Name, temp2);
2001-11-27 22:55:00 +00:00
2003-01-19 14:43:23 +00:00
if (strcmp(fdb.LName, fdb.Name) && (rename(fdb.LName, fdb.Name) == 0)) {
Syslog('+', "Renamed %s to %s", fdb.LName, fdb.Name);
}
sprintf(temp, "%s/%s", pwd, fdb.Name);
stat(temp, &statfile);
if (do_annon)
fdb.Announced = TRUE;
Syslog('f', "File: %s (%s)", fdb.Name, fdb.LName);
2001-11-25 12:25:59 +00:00
if (!do_quiet) {
printf("\rImport file: %s ", fdb.Name);
printf("Checking \b\b\b\b\b\b\b\b\b\b");
fflush(stdout);
}
2001-11-25 12:25:59 +00:00
IsDoing("Import %s", fdb.Name);
2001-11-25 12:25:59 +00:00
token = strtok(NULL, "\0");
i = strlen(token);
2003-01-19 14:43:23 +00:00
line = pos = 0;
for (x = 0; x < i; x++) {
if ((token[x] == '\n') || (token[x] == '\r'))
token[x] = '\0';
2001-11-25 12:25:59 +00:00
}
2003-01-19 14:43:23 +00:00
i = strlen(token);
Doit = FALSE;
for (x = 0; x < i; x++) {
if (!Doit) {
2003-01-19 14:43:23 +00:00
if (!iscntrl(token[x]) && !isblank(token[x]))
Doit = TRUE;
}
if (Doit) {
2003-01-19 14:43:23 +00:00
if (pos > 42) {
if (token[x] == ' ') {
2003-01-19 14:43:23 +00:00
fdb.Desc[line][pos] = '\0';
line++;
pos = 0;
} else {
2003-01-19 14:43:23 +00:00
if (pos == 49) {
fdb.Desc[line][pos] = '\0';
pos = 0;
line++;
}
2003-01-19 14:43:23 +00:00
fdb.Desc[line][pos] = token[x];
pos++;
2001-11-25 12:25:59 +00:00
}
} else {
2003-01-19 14:43:23 +00:00
fdb.Desc[line][pos] = token[x];
pos++;
2001-11-25 12:25:59 +00:00
}
2003-01-19 14:43:23 +00:00
if (line == 25)
break;
2001-11-25 12:25:59 +00:00
}
}
2003-01-19 14:43:23 +00:00
sprintf(dest, "%s/%s", area.Path, fdb.Name);
sprintf(lname, "%s/%s", area.Path, fdb.LName);
Append = TRUE;
fdb.Size = statfile.st_size;
fdb.FileDate = statfile.st_mtime;
fdb.Crc32 = file_crc(temp, FALSE);
strcpy(fdb.Uploader, CFG.sysop_name);
fdb.UploadDate = time(NULL);
}
} else if (Present) {
2001-11-25 12:25:59 +00:00
/*
* Add multiple description lines
*/
2003-01-19 14:43:23 +00:00
if (line < 25) {
token = strtok(String, "\0");
i = strlen(token);
line++;
pos = 0;
Doit = FALSE;
for (x = 0; x < i; x++) {
if ((token[x] == '\n') || (token[x] == '\r'))
token[x] = '\0';
}
for (x = 0; x < i; x++) {
if (Doit) {
if (pos > 42) {
if (token[x] == ' ') {
fdb.Desc[line][pos] = '\0';
line++;
pos = 0;
} else {
if (pos == 49) {
fdb.Desc[line][pos] = '\0';
pos = 0;
line++;
}
fdb.Desc[line][pos] = token[x];
pos++;
2001-11-25 12:25:59 +00:00
}
2003-01-19 14:43:23 +00:00
} else {
fdb.Desc[line][pos] = token[x];
pos++;
2001-11-25 12:25:59 +00:00
}
2003-01-19 14:43:23 +00:00
if (line == 25)
break;
2001-11-25 12:25:59 +00:00
} else {
2003-01-19 14:43:23 +00:00
/*
* Skip until + or | is found
*/
if ((token[x] == '+') || (token[x] == '|'))
Doit = TRUE;
2001-11-25 12:25:59 +00:00
}
}
}
} /* End if new file entry found */
2001-11-25 12:25:59 +00:00
} /* End of files.bbs */
fclose(fbbs);
/*
* Flush the last file to the database
*/
if (Append) {
Doit = TRUE;
if ((unarc = unpacker(temp)) == NULL) {
Syslog('+', "Unknown archive format %s", temp);
sprintf(temp2, "%s/tmp/arc/%s", getenv("MBSE_ROOT"), fdb.LName);
2002-01-19 13:04:55 +00:00
mkdirs(temp2, 0755);
2002-05-18 12:57:00 +00:00
if ((rc = file_cp(temp, temp2))) {
WriteError("Can't copy file to %s, %s", temp2, strerror(rc));
2001-11-25 12:25:59 +00:00
Doit = FALSE;
} else {
2002-06-15 14:00:49 +00:00
if (do_novir == FALSE) {
if (!do_quiet) {
printf("Virscan \b\b\b\b\b\b\b\b\b\b");
fflush(stdout);
}
if (VirScan(tmpdir)) {
Doit = FALSE;
}
2001-11-25 12:25:59 +00:00
}
}
} else {
if (!do_quiet) {
printf("Unpacking \b\b\b\b\b\b\b\b\b\b");
fflush(stdout);
}
if (UnpackFile(temp)) {
2002-06-15 14:00:49 +00:00
if (do_novir == FALSE) {
if (!do_quiet) {
printf("Virscan \b\b\b\b\b\b\b\b\b\b");
fflush(stdout);
}
if (VirScan(tmpdir)) {
Doit = FALSE;
}
2001-11-25 12:25:59 +00:00
}
} else {
Doit = FALSE;
}
}
DeleteVirusWork();
if (Doit) {
if (!do_quiet) {
printf("Adding \b\b\b\b\b\b\b\b\b\b");
fflush(stdout);
}
if (strcmp(fdb.Name, fdb.LName)) {
if (AddFile(fdb, Area, dest, temp, lname))
Imported++;
else
Errors++;
} else {
if (AddFile(fdb, Area, dest, temp, NULL))
Imported++;
else
Errors++;
}
2001-11-25 12:25:59 +00:00
} else {
Errors++;
}
}
free(lname);
2001-11-25 12:25:59 +00:00
free(dest);
free(String);
free(pwd);
free(temp2);
free(temp);
free(tmpdir);
2001-11-25 12:25:59 +00:00
} else {
if (!area.Available) {
WriteError("Area not available");
if (!do_quiet)
printf("Area not available\n");
}
if (area.CDrom) {
WriteError("Can't import on CD-ROM");
if (!do_quiet)
printf("Can't import on CD-ROM\n");
}
}
if (!do_quiet) {
printf("\r \r");
fflush(stdout);
}
Syslog('+', "Import Files [%5d] Imported [%5d] Errors [%5d]", Files, Imported, Errors);
}