From 10d5f0d58afd158599a0903567b14a4f801d4313 Mon Sep 17 00:00:00 2001 From: Michiel Broek Date: Mon, 3 May 2004 13:23:02 +0000 Subject: [PATCH] Updates for experiment --- lib/Makefile | 3 +- lib/dbfdb.c | 246 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 248 insertions(+), 1 deletion(-) diff --git a/lib/Makefile b/lib/Makefile index 67333617..c586026d 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -22,7 +22,7 @@ NODELIST_SRCS = nodelist.c NODELIST_OBJS = nodelist.o NODELIST_HDRS = nodelist.h DBASE_SRCS = dbcfg.c dbdupe.c dbftn.c dbmsgs.c dbnode.c dbtic.c dbuser.c dbfdb.c -DBASE_OBJS = dbcfg.o dbdupe.o dbftn.o dbmsgs.o dbnode.o dbtic.o dbuser.o dbfdb.h +DBASE_OBJS = dbcfg.o dbdupe.o dbftn.o dbmsgs.o dbnode.o dbtic.o dbuser.o dbfdb.o DBASE_HDRS = mbsedb.h MSGBASE_SRCS = jammsg.c msg.c msgtext.c MSGBASE_OBJS = jammsg.o msg.o msgtext.o @@ -173,6 +173,7 @@ dbmsgs.o: ../config.h mbselib.h users.h mbsedb.h dbnode.o: ../config.h mbselib.h users.h mbsedb.h dbtic.o: ../config.h mbselib.h users.h mbsedb.h dbuser.o: ../config.h mbselib.h users.h mbsedb.h +dbfdb.o: ../config.h mbselib.h users.h mbsedb.h jammsg.o: ../config.h mbselib.h msgtext.h msg.h jam.h jammsg.h msg.o: ../config.h mbselib.h msgtext.h msg.h jammsg.h msgtext.o: ../config.h mbselib.h msgtext.h msg.h diff --git a/lib/dbfdb.c b/lib/dbfdb.c index aaf43da2..7240f715 100644 --- a/lib/dbfdb.c +++ b/lib/dbfdb.c @@ -33,4 +33,250 @@ #include "users.h" #include "mbsedb.h" +#ifdef USE_EXPERIMEMT + +/* + * Add file to the BBS. The file must in the current directory which may not be + * the destination directory. + * The f_db record already has all needed information. + * AllowReplace should be set by mbfido on ticfiles import to allow updates of + * files like allfiles.zip + */ +int AddFile(struct FILE_record f_db, int Area, char *DestPath, char *FromPath, char *LinkPath, int AllowReplace) +{ + char *temp1, *temp2; + FILE *fp1, *fp2; + int i, rc, Insert, Done = FALSE, Found = FALSE; + + if (AllowReplace) { + /* + * First check for an existing record with the same filename, + * if it exists, update the record and we are ready. This will + * prevent for example allfiles.zip to get a new record everytime + * and thus the download counters will be reset after a new update. + */ + sprintf(fdbname, "%s/fdb/file%ld.data", getenv("MBSE_ROOT"), tic.FileArea); + if ((fp = fopen(fdbname, "r+")) != NULL) { + fread(&frechdr, sizeof(frechdr), 1, fp); + while (fread(&frec, frechdr.recsize, 1, fp) == 1) { + if (strcmp(frec.Name, TIC.NewFile) == 0) { + sprintf(temp1, "%s/%s", TIC.Inbound, TIC.NewFile); + sprintf(temp2, "%s/%s", TIC.BBSpath, TIC.NewFile); + mkdirs(temp2, 0755); + if ((rc = file_cp(temp1, temp2))) { + WriteError("Copy to %s failed: %s", temp2, strerror(rc)); + fclose(fp); + return FALSE; + } + chmod(temp2, 0644); + strncpy(frec.TicArea, TIC.TicIn.Area, sizeof(frec.TicArea) -1); + frec.Size = TIC.FileSize; + frec.Crc32 = TIC.Crc_Int; + frec.Announced = TRUE; + frec.FileDate = TIC.FileDate; + frec.UploadDate = time(NULL); + for (i = 0; i <= TIC.File_Id_Ct; i++) { + strcpy(frec.Desc[i], TIC.File_Id[i]); + if (i == 24) + break; + } + fseek(fp, 0 - sizeof(frec), SEEK_CUR); + fwrite(&frec, sizeof(frec), 1, fp); + fclose(fp); + tic_imp++; + if ((i = file_rm(temp1))) + WriteError("file_rm(%s): %s", temp1, strerror(i)); + return TRUE; + } + } + fclose(fp); + } + } + + + /* + * Copy file to the final destination and make a hard link with the + * 8.3 filename to the long filename. + */ + mkdirs(DestPath, 0775); + + if (file_exist(DestPath, F_OK) == 0) { + WriteError("File %s already exists in area %d", f_db.Name, Area); + if (!do_quiet) + printf("\nFile %s already exists in area %d\n", f_db.Name, Area); + return FALSE; + } + + if ((rc = file_cp(FromPath, DestPath))) { + WriteError("Can't copy file in place"); + if (!do_quiet) + printf("\nCan't copy file to %s, %s\n", DestPath, strerror(rc)); + return FALSE; + } + chmod(DestPath, 0644); + if (LinkPath) { + if ((rc = symlink(DestPath, LinkPath))) { + WriteError("Can't create symbolic link %s", LinkPath); + if (!do_quiet) + printf("\nCan't create symbolic link %s, %s\n", LinkPath, strerror(rc)); + unlink(DestPath); + return FALSE; + } + } + + temp1 = calloc(PATH_MAX, sizeof(char)); + temp2 = calloc(PATH_MAX, sizeof(char)); + sprintf(temp1, "%s/fdb/file%d.data", getenv("MBSE_ROOT"), Area); + sprintf(temp2, "%s/fdb/file%d.temp", getenv("MBSE_ROOT"), Area); + + fp1 = fopen(temp1, "r+"); + fread(&fdbhdr, sizeof(fdbhdr.hdrsize), 1, fp1); + fseek(fp1, 0, SEEK_END); + if (ftell(fp1) == fdbhdr.hdrsize) { + /* + * No records yet + */ + fwrite(&f_db, fdbhdr.recsize, 1, fp1); + fclose(fp1); + } else { + /* + * Files are already there. Find the right spot. + */ + fseek(fp1, fdbhdr.hdrsize, SEEK_SET); + + Insert = 0; + do { + if (fread(&fdb, fdbhdr.recsize, 1, fp1) != 1) + Done = TRUE; + if (!Done) { + if (strcmp(f_db.LName, fdb.LName) == 0) { + Found = TRUE; + Insert++; + } else { + if (strcmp(f_db.LName, fdb.LName) < 0) + Found = TRUE; + else + Insert++; + } + } + } while ((!Found) && (!Done)); + + if (Found) { + if ((fp2 = fopen(temp2, "a+")) == NULL) { + WriteError("Can't create %s", temp2); + return FALSE; + } + fwrite(&fdbhdr, fdbhdr.hdrsize, 1, fp2); + fseek(fp1, fdbhdr.hdrsize, SEEK_SET); + + /* + * Copy until the insert point + */ + for (i = 0; i < Insert; i++) { + fread(&fdb, fdbhdr.recsize, 1, fp1); + /* + * If we are importing a file with the same name, + * skip the original record and put the new one in place. + */ + if (strcmp(fdb.LName, f_db.LName) != 0) + fwrite(&fdb, fdbhdr.recsize, 1, fp2); + } + + if (area.AddAlpha) + fwrite(&f_db, fdbhdr.recsize, 1, fp2); + + /* + * Append the rest of the records + */ + while (fread(&fdb, fdbhdr.recsize, 1, fp1) == 1) { + if (strcmp(fdb.LName, f_db.LName) != 0) + fwrite(&fdb, fdbhdr.recsize, 1, fp2); + } + if (!area.AddAlpha) + fwrite(&f_db, fdbhdr.recsize, 1, fp2); + fclose(fp1); + fclose(fp2); + + if (unlink(temp1) == 0) { + rename(temp2, temp1); + chmod(temp1, 0660); + } else { + WriteError("$Can't unlink %s", temp1); + unlink(temp2); + return FALSE; + } + } else { /* if (Found) */ + /* + * Append file record + */ + fseek(fp1, 0, SEEK_END); + fwrite(&f_db, fdbhdr.recsize, 1, fp1); + fclose(fp1); + } + } + + free(temp1); + free(temp2); + return TRUE; +} + + + +/* + * Check files database, create if it doesn't exist. + */ +int mbsedb_CheckFDB(int Area, char *Path) +{ + char *temp; + FILE *fp; + int rc = FALSE; + + temp = calloc(PATH_MAX, sizeof(char)); + sprintf(temp, "%s/fdb/file%d.data", getenv("MBSE_ROOT"), Area); + + /* + * Open the file database, create new one if it doesn't excist. + */ + if ((fp = fopen(temp, "r+")) == NULL) { + Syslog('!', "Creating new %s", temp); + if ((fp = fopen(temp, "a+")) == NULL) { + WriteError("$Can't create %s", temp); + rc = TRUE; + } else { + fdbhdr.hdrsize = sizeof(fdbhdr); + fdbhdr.recsize = sizeof(fdb); + fwrite(&fdbhdr, sizeof(fdbhdr), 1, fp); + fclose(fp); + } + } else { + fread(&fdbhdr, sizeof(fdbhdr), 1, fp); + fclose(fp); + } + + /* + * Set the right attributes + */ + chmod(temp, 0660); + + /* + * Now check the download directory + */ + if (access(Path, W_OK) == -1) { + sprintf(temp, "%s/foobar", Path); + if (mkdirs(temp, 0755)) + Syslog('+', "Created directory %s", Path); + } + + free(temp); + + if ((fdbhdr.hdrsize != sizeof(fdbhdr)) || (fdbhdr.recsize != sizeof(fdb))) { + WriteError("Files database header in area %d is corrupt", Area); + rc = TRUE; + } + + return rc; +} + + +#endif