Skip to content
Permalink
Browse files

filedb3 patch

  • Loading branch information
fknittel committed Nov 14, 1999
1 parent 3b0085e commit ed3a0485e641c57f6f4ec8365235b6fbad4fb103
@@ -52,6 +52,13 @@ As soon as we start getting the translations in there will be a directory
on raistlin.toledolink.com that will contain ready to use language files.
Look for it real soon.

FILESYS
The filesys module has changed it's db format to add flexibility and remove
the limits imposed by the old format. There's nothing special for you to
do as the old db is automatically converted to the new format when you first
access it. You won't be able to downgrade to older versions after this
though, so maybe you'll want to make a backup first.

3. MISC stuff

COMMANDS
@@ -5,6 +5,8 @@ Changes in eggdrop: (since v1.5.0)

1.5.0
Foundby Fixedby What....
Fabian Rewritten filesys.mod to allow unlimited filename,
description, etc. lengths.
Wiktor Fabian block_dns_ipbyhost crashed on illegal hostnames/ips
Cybah Fixed tcl_console getting confused between flags and +chans.
Cybah Userfile functions need to use chan->dname.
@@ -30,4 +30,5 @@ clean:
../../lang.h ../../eggdrop.h ../../flags.h ../../proto.h \
../../../lush.h ../../cmdt.h ../../tclegg.h ../../tclhash.h \
../../chan.h ../../users.h ../modvals.h ../../tandem.h filesys.h \
../transfer.mod/transfer.h files.h filedb.c files.c tclfiles.c
../transfer.mod/transfer.h files.h filedb3.c filedb3.h files.c \
tclfiles.c dbcompat.c filelist.c filelist.h
@@ -0,0 +1,300 @@
/*
* dbcompat.c - Compability functions to convert older DBs to
* the newest version.
*/
/*
* This file is part of the eggdrop source code.
*
* Copyright (C) 1999 Eggheads
* Copyright (C) 1997 Robey Pointer
* Written for filedb3 by Fabian Knittel <fknittel@gmx.de>
*
* This program 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 of the License, or
* (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

/* Convert '.files' db to newest db. Returns 1 if a valid file is
* found and could be converted, 0 in all other cases.
*
* '.files' is a text file which contains file records built up in the
* following way:
* '<filename> <nick> <tm> <gots>\n'
* '- <comment>\n'
* '- <comment>\n'
* ...
*/
static int convert_old_files(char *path, char *newfiledb)
{
FILE *f, *fdb;
char *s, *fn, *nick, *tm, *s1;
filedb_entry *fdbe = NULL;
int in_file = 0, i;
struct stat st;

context;
s = nmalloc(strlen(path) + 8);
sprintf(s, "%s/.files", path);
f = fopen(s, "r");
nfree(s);
if (f == NULL)
return 0;

context;
fdb = fopen(newfiledb, "w+b");
if (!fdb) {
putlog(LOG_MISC, "(!) Can't create filedb in %s", newfiledb);
fclose(f);
return 0;
}
lockfile(fdb);
lockfile(f);
filedb_initdb(fdb);

putlog(LOG_FILES, "*", FILES_CONVERT, path);
/* scan contents of .files and painstakingly create .filedb entries */
while (!feof(f)) {
s = nmalloc(121);
s1 = s;
fgets(s, 120, f);
if (s[strlen(s) - 1] == '\n')
s[strlen(s) - 1] = 0;
if (!feof(f)) {
fn = newsplit(&s1);
rmspace(fn);
if ((fn[0]) && (fn[0] != ';') && (fn[0] != '#')) {
/* not comment */
if (fn[0] == '-') {
/* adjust comment for current file */
if (in_file && fdbe) {
rmspace(s);
if (fdbe->desc) {
fdbe->desc = nrealloc(fdbe->desc,
strlen(fdbe->desc) + strlen(s) + 2);
strcat(fdbe->desc, "\n");
} else
fdbe->desc = nmalloc(strlen(s) + 2);
strcat(fdbe->desc, s);
}
} else {
if (fdbe) {
/* File pending. Write to DB */
context;
filedb_addfile(fdb, POS_NEW, fdbe);
free_fdbe(fdbe);
}
fdbe = malloc_fdbe();
in_file = 1;
nick = newsplit(&s1);
rmspace(nick);
tm = newsplit(&s1);
rmspace(tm);
rmspace(s1);
i = strlen(fn) - 1;
if (fn[i] == '/')
fn[i] = 0;
malloc_strcpy(fdbe->filename, fn);
malloc_strcpy(fdbe->uploader, nick);
fdbe->gots = atoi(s1);
fdbe->uploaded = atoi(tm);
sprintf(s, "%s/%s", path, fn);
if (stat(s, &st) == 0) {
/* file is okay */
if (S_ISDIR(st.st_mode)) {
fdbe->stat |= FILE_DIR;
if (nick[0] == '+') {
char x[100];
/* only do global flags, it's an old one */
struct flag_record fr = {FR_GLOBAL, 0, 0, 0, 0, 0};

break_down_flags(nick + 1, &fr, NULL);
build_flags(x, &fr, NULL);
/* we only want valid flags */
malloc_strcpy(fdbe->flags_req, x);
}
}
fdbe->size = st.st_size;
} else
in_file = 0; /* skip */
}
}
}
nfree(s);
}
if (fdbe) {
/* File pending. Write to DB */
context;
filedb_addfile(fdb, POS_NEW, fdbe);
free_fdbe(fdbe);
}
fseek(fdb, 0, SEEK_END);
unlockfile(f);
unlockfile(fdb);
fclose(fdb);
fclose(f);
context;
return 1;
}

/* Reads file DB v1 entries from fdb_s and saves them to fdb_t in
* v3 format.
*/
static void convert_version1(FILE *fdb_s, FILE *fdb_t)
{
long where;
filedb1 fdb1;

fseek(fdb_s, 0, SEEK_SET);
while (!feof(fdb_s)) {
where = ftell(fdb_s);
fread(&fdb1, sizeof(filedb1), 1, fdb_s);
if (!feof(fdb_s)) {
if (!(fdb1.stat & FILE_UNUSED)) {
filedb_entry *fdbe = malloc_fdbe();

fdbe->stat = fdb1.stat;
if (fdb1.filename[0])
malloc_strcpy(fdbe->filename, fdb1.filename);
if (fdb1.desc[0])
malloc_strcpy(fdbe->desc, fdb1.desc);
if (fdb1.uploader[0])
malloc_strcpy(fdbe->uploader, fdb1.uploader);
if (fdb1.flags_req[0])
malloc_strcpy(fdbe->flags_req, fdb1.flags_req);
fdbe->uploaded = fdb1.uploaded;
fdbe->size = fdb1.size;
fdbe->gots = fdb1.gots;
if (fdb1.sharelink[0])
malloc_strcpy(fdbe->sharelink, fdb1.sharelink);
filedb_addfile(fdb_s, POS_NEW, fdbe);
free_fdbe(fdbe);
}
}
}
}

/* Reads file DB v2 entries from fdb_s and saves them to fdb_t in
* v3 format.
*/
static void convert_version2(FILE *fdb_s, FILE *fdb_t)
{
long where;
filedb2 fdb2;

fseek(fdb_s, 0, SEEK_SET);
while (!feof(fdb_s)) {
where = ftell(fdb_s);
fread(&fdb2, sizeof(filedb2), 1, fdb_s);
if (!feof(fdb_s)) {
if (!(fdb2.stat & FILE_UNUSED)) {
filedb_entry *fdbe = malloc_fdbe();

fdbe->stat = fdb2.stat;
if (fdb2.filename[0])
malloc_strcpy(fdbe->filename, fdb2.filename);
if (fdb2.desc[0])
malloc_strcpy(fdbe->desc, fdb2.desc);
if (fdb2.chname[0])
malloc_strcpy(fdbe->chan, fdb2.chname);
if (fdb2.uploader[0])
malloc_strcpy(fdbe->uploader, fdb2.uploader);
if (fdb2.flags_req[0])
malloc_strcpy(fdbe->flags_req, fdb2.flags_req);
fdbe->uploaded = fdb2.uploaded;
fdbe->size = fdb2.size;
fdbe->gots = fdb2.gots;
if (fdb2.sharelink[0])
malloc_strcpy(fdbe->sharelink, fdb2.sharelink);
filedb_addfile(fdb_t, POS_NEW, fdbe);
free_fdbe(fdbe);
}
}
}
}

/* Converts old versions of the filedb to the newest. Returns 1 if all went
* well and otherwise 0. The new db is first written to a temporary place
* and then moved over to the original db's position.
*
* Note: Unfortunately there is a small time-frame where aren't locking the
* DB, but want to replace it with a new one, using movefile().
* TODO: Copy old db to tmp file and then build the new db directly
* in the original file. This solves the tiny locking problem.
*
* Also remember to check the returned *fdb_s on failure, as it could be
* NULL.
*/
static int convert_old_db(FILE **fdb_s, char *filedb)
{
filedb_top fdbt;
FILE *fdb_t;
int ret = 0; /* Default to 'failure' */

context;
filedb_readtop(*fdb_s, &fdbt);
/* Old DB version? */
if ((fdbt.version > 0) && (fdbt.version < FILEDB_VERSION3)) {
char *tempdb;

context;
putlog(LOG_MISC, "*", "Converting old filedb %s to newest format.",
filedb);
/* Create temp DB name */
tempdb = nmalloc(strlen(filedb) + 5);
simple_sprintf(tempdb, "%s-tmp", filedb);

fdb_t = fopen(tempdb, "w+b"); /* Open temp DB */
if (fdb_t) {
filedb_initdb(fdb_t); /* Initialise new DB */

/* Convert old database to new one, saving
* in temporary db file */
if (fdbt.version == FILEDB_VERSION1)
convert_version1(*fdb_s, fdb_t); /* v1 -> v3 */
else
convert_version2(*fdb_s, fdb_t); /* v2 -> v3 */

unlockfile(*fdb_s);
fclose(fdb_t);
fclose(*fdb_s);

/* Move over db to new location */
if (movefile(tempdb, filedb))
putlog(LOG_MISC, "*", "(!) Moving file db from %s to %s failed.",
tempdb, filedb);

*fdb_s = fopen(filedb, "r+b"); /* Reopen new db */
if (*fdb_s) {
lockfile(*fdb_s);
/* Now we should have recreated the original situation,
* with the file pointer just pointing to the new version
* of the DB instead of the original one. */
ret = 1;
} else
putlog(LOG_MISC, "*", "(!) Reopening db %s failed.", filedb);
}
nfree(tempdb);
context;
/* Database already at the newest version? */
} else if (fdbt.version == FILEDB_VERSION3) {
context;
ret = 1; /* Always successfull */
/* Unknown version? */
} else {
context;
putlog(LOG_MISC, "*", "(!) Unknown db version: %d", fdbt.version);
}
if (!ret)
putlog(LOG_MISC, "*", "Conversion of filedb %s failed.", filedb);
return ret;
}

0 comments on commit ed3a048

Please sign in to comment.
You can’t perform that action at this time.