src/mod/filesys.mod/dbcompat.c File Reference

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

static int convert_old_files (char *path, char *newfiledb)
static void convert_version1 (FILE *fdb_s, FILE *fdb_t)
static void convert_version2 (FILE *fdb_s, FILE *fdb_t)
static int convert_old_db (FILE **fdb_s, char *filedb)

Function Documentation

static int convert_old_db ( FILE **  fdb_s,
char *  filedb 
) [static]

Definition at line 230 of file dbcompat.c.

References convert_version1(), convert_version2(), filedb_initdb(), filedb_readtop(), FILEDB_VERSION1, FILEDB_VERSION3, lockfile(), LOG_MISC, movefile, my_free, nmalloc, putlog, simple_sprintf, unlockfile(), and filedb_top::version.

Referenced by filedb_open().

00231 {
00232   filedb_top fdbt;
00233   FILE *fdb_t;
00234   int ret = 0;                  /* Default to 'failure' */
00235 
00236   filedb_readtop(*fdb_s, &fdbt);
00237   /* Old DB version? */
00238   if (fdbt.version > 0 && fdbt.version < FILEDB_VERSION3) {
00239     char *tempdb;
00240 
00241     putlog(LOG_MISC, "*", "Converting old filedb %s to newest format.",
00242            filedb);
00243     /* Create temp DB name */
00244     tempdb = nmalloc(strlen(filedb) + 5);
00245     simple_sprintf(tempdb, "%s-tmp", filedb);
00246 
00247     fdb_t = fopen(tempdb, "w+b");       /* Open temp DB         */
00248     if (fdb_t) {
00249       filedb_initdb(fdb_t);     /* Initialise new DB    */
00250 
00251       /* Convert old database to new one, saving
00252        * in temporary db file
00253        */
00254       if (fdbt.version == FILEDB_VERSION1)
00255         convert_version1(*fdb_s, fdb_t);        /* v1 -> v3             */
00256       else
00257         convert_version2(*fdb_s, fdb_t);        /* v2 -> v3             */
00258 
00259       unlockfile(*fdb_s);
00260       fclose(fdb_t);
00261       fclose(*fdb_s);
00262 
00263       /* Move over db to new location */
00264       if (movefile(tempdb, filedb))
00265         putlog(LOG_MISC, "*", "(!) Moving file db from %s to %s failed.",
00266                tempdb, filedb);
00267 
00268       *fdb_s = fopen(filedb, "r+b");    /* Reopen new db        */
00269       if (*fdb_s) {
00270         lockfile(*fdb_s);
00271         /* Now we should have recreated the original situation,
00272          * with the file pointer just pointing to the new version
00273          * of the DB instead of the original one.
00274          */
00275         ret = 1;
00276       } else
00277         putlog(LOG_MISC, "*", "(!) Reopening db %s failed.", filedb);
00278     }
00279     my_free(tempdb);
00280     /* Database already at the newest version? */
00281   } else if (fdbt.version == FILEDB_VERSION3)
00282     ret = 1;
00283   else
00284     putlog(LOG_MISC, "*", "(!) Unknown db version: %d", fdbt.version);
00285   if (!ret)
00286     putlog(LOG_MISC, "*", "Conversion of filedb %s failed.", filedb);
00287   return ret;
00288 }

Here is the call graph for this function:

Here is the caller graph for this function:

static int convert_old_files ( char *  path,
char *  newfiledb 
) [static]

Definition at line 38 of file dbcompat.c.

References break_down_flags, build_flags, filedb_entry::desc, FILE_DIR, filedb_addfile, filedb_initdb(), filedb_entry::filename, FILES_CONVERT, filedb_entry::flags_req, FR_GLOBAL, free_fdbe(), filedb_entry::gots, lockfile(), LOG_FILES, LOG_MISC, malloc_fdbe, malloc_strcpy, malloc_strcpy_nocheck, my_free, newsplit, nmalloc, nrealloc, NULL, putlog, rmspace, S_ISDIR, filedb_entry::size, filedb_entry::stat, unlockfile(), filedb_entry::uploaded, and filedb_entry::uploader.

Referenced by filedb_open().

00039 {
00040   FILE *f, *fdb;
00041   char *s, *fn, *nick, *tm, *s1;
00042   filedb_entry *fdbe = NULL;
00043   int in_file = 0, i;
00044   struct stat st;
00045 
00046   s = nmalloc(strlen(path) + 8);
00047   sprintf(s, "%s/.files", path);
00048   f = fopen(s, "r");
00049   my_free(s);
00050   if (f == NULL)
00051     return 0;
00052 
00053   fdb = fopen(newfiledb, "w+b");
00054   if (!fdb) {
00055     putlog(LOG_MISC, "(!) Can't create filedb in %s", newfiledb);
00056     fclose(f);
00057     return 0;
00058   }
00059   lockfile(fdb);
00060   lockfile(f);
00061   filedb_initdb(fdb);
00062 
00063   putlog(LOG_FILES, "*", FILES_CONVERT, path);
00064   /* Scan contents of .files and painstakingly create .filedb entries */
00065   while (!feof(f)) {
00066     s = nmalloc(121);
00067     s1 = s;
00068     fgets(s, 120, f);
00069     if (s[strlen(s) - 1] == '\n')
00070       s[strlen(s) - 1] = 0;
00071     if (!feof(f)) {
00072       fn = newsplit(&s1);
00073       rmspace(fn);
00074       if ((fn[0]) && (fn[0] != ';') && (fn[0] != '#')) {
00075         /* Not comment */
00076         if (fn[0] == '-') {
00077           /* Adjust comment for current file */
00078           if (in_file && fdbe) {
00079             rmspace(s);
00080             if (fdbe->desc) {
00081               fdbe->desc = nrealloc(fdbe->desc,
00082                                     strlen(fdbe->desc) + strlen(s) + 2);
00083               strcat(fdbe->desc, "\n");
00084             } else
00085               fdbe->desc = nmalloc(strlen(s) + 2);
00086             strcat(fdbe->desc, s);
00087           }
00088         } else {
00089           if (fdbe) {
00090             /* File pending. Write to DB */
00091             filedb_addfile(fdb, fdbe);
00092             free_fdbe(&fdbe);
00093           }
00094           fdbe = malloc_fdbe();
00095           in_file = 1;
00096           nick = newsplit(&s1);
00097           rmspace(nick);
00098           tm = newsplit(&s1);
00099           rmspace(tm);
00100           rmspace(s1);
00101           i = strlen(fn) - 1;
00102           if (fn[i] == '/')
00103             fn[i] = 0;
00104           malloc_strcpy(fdbe->filename, fn);
00105           malloc_strcpy(fdbe->uploader, nick);
00106           fdbe->gots = atoi(s1);
00107           fdbe->uploaded = atoi(tm);
00108           sprintf(s, "%s/%s", path, fn);
00109           if (stat(s, &st) == 0) {
00110             /* File is okay */
00111             if (S_ISDIR(st.st_mode)) {
00112               fdbe->stat |= FILE_DIR;
00113               if (nick[0] == '+') {
00114                 char x[100];
00115 
00116                 /* Only do global flags, it's an old one */
00117                 struct flag_record fr = { FR_GLOBAL, 0, 0, 0, 0, 0 };
00118 
00119                 break_down_flags(nick + 1, &fr, NULL);
00120                 build_flags(x, &fr, NULL);
00121                 /* We only want valid flags */
00122                 malloc_strcpy_nocheck(fdbe->flags_req, x);
00123               }
00124             }
00125             fdbe->size = st.st_size;
00126           } else
00127             in_file = 0;        /* skip */
00128         }
00129       }
00130     }
00131     my_free(s);
00132   }
00133   if (fdbe) {
00134     /* File pending. Write to DB */
00135     filedb_addfile(fdb, fdbe);
00136     free_fdbe(&fdbe);
00137   }
00138   fseek(fdb, 0L, SEEK_END);
00139   unlockfile(f);
00140   unlockfile(fdb);
00141   fclose(fdb);
00142   fclose(f);
00143   return 1;
00144 }

Here is the call graph for this function:

Here is the caller graph for this function:

static void convert_version1 ( FILE *  fdb_s,
FILE *  fdb_t 
) [static]

Definition at line 149 of file dbcompat.c.

References filedb_entry::desc, filedb1::desc, FILE_UNUSED, filedb_addfile, filedb_entry::filename, filedb1::filename, filedb_entry::flags_req, filedb1::flags_req, free_fdbe(), filedb1::gots, filedb_entry::gots, malloc_fdbe, malloc_strcpy, filedb_entry::sharelink, filedb1::sharelink, filedb1::size, filedb_entry::size, filedb_entry::stat, filedb1::stat, filedb1::uploaded, filedb_entry::uploaded, filedb_entry::uploader, and filedb1::uploader.

Referenced by convert_old_db().

00150 {
00151   filedb1 fdb1;
00152 
00153   fseek(fdb_s, 0L, SEEK_SET);
00154   while (!feof(fdb_s)) {
00155     fread(&fdb1, sizeof(filedb1), 1, fdb_s);
00156     if (!feof(fdb_s)) {
00157       if (!(fdb1.stat & FILE_UNUSED)) {
00158         filedb_entry *fdbe = malloc_fdbe();
00159 
00160         fdbe->stat = fdb1.stat;
00161         if (fdb1.filename[0])
00162           malloc_strcpy(fdbe->filename, fdb1.filename);
00163         if (fdb1.desc[0])
00164           malloc_strcpy(fdbe->desc, fdb1.desc);
00165         if (fdb1.uploader[0])
00166           malloc_strcpy(fdbe->uploader, fdb1.uploader);
00167         if (fdb1.flags_req[0])
00168           malloc_strcpy(fdbe->flags_req, (char *) fdb1.flags_req);
00169         fdbe->uploaded = fdb1.uploaded;
00170         fdbe->size = fdb1.size;
00171         fdbe->gots = fdb1.gots;
00172         if (fdb1.sharelink[0])
00173           malloc_strcpy(fdbe->sharelink, fdb1.sharelink);
00174         filedb_addfile(fdb_s, fdbe);
00175         free_fdbe(&fdbe);
00176       }
00177     }
00178   }
00179 }

Here is the call graph for this function:

Here is the caller graph for this function:

static void convert_version2 ( FILE *  fdb_s,
FILE *  fdb_t 
) [static]

Definition at line 184 of file dbcompat.c.

References filedb_entry::chan, filedb2::chname, filedb_entry::desc, filedb2::desc, FILE_UNUSED, filedb_addfile, filedb_entry::filename, filedb2::filename, filedb_entry::flags_req, filedb2::flags_req, free_fdbe(), filedb2::gots, filedb_entry::gots, malloc_fdbe, malloc_strcpy, filedb_entry::sharelink, filedb2::sharelink, filedb2::size, filedb_entry::size, filedb_entry::stat, filedb2::stat, filedb2::uploaded, filedb_entry::uploaded, filedb_entry::uploader, and filedb2::uploader.

Referenced by convert_old_db().

00185 {
00186   filedb2 fdb2;
00187 
00188   fseek(fdb_s, 0L, SEEK_SET);
00189   while (!feof(fdb_s)) {
00190     fread(&fdb2, sizeof(filedb2), 1, fdb_s);
00191     if (!feof(fdb_s)) {
00192       if (!(fdb2.stat & FILE_UNUSED)) {
00193         filedb_entry *fdbe = malloc_fdbe();
00194 
00195         fdbe->stat = fdb2.stat;
00196         if (fdb2.filename[0])
00197           malloc_strcpy(fdbe->filename, fdb2.filename);
00198         if (fdb2.desc[0])
00199           malloc_strcpy(fdbe->desc, fdb2.desc);
00200         if (fdb2.chname[0])
00201           malloc_strcpy(fdbe->chan, fdb2.chname);
00202         if (fdb2.uploader[0])
00203           malloc_strcpy(fdbe->uploader, fdb2.uploader);
00204         if (fdb2.flags_req[0])
00205           malloc_strcpy(fdbe->flags_req, fdb2.flags_req);
00206         fdbe->uploaded = fdb2.uploaded;
00207         fdbe->size = fdb2.size;
00208         fdbe->gots = fdb2.gots;
00209         if (fdb2.sharelink[0])
00210           malloc_strcpy(fdbe->sharelink, fdb2.sharelink);
00211         filedb_addfile(fdb_t, fdbe);
00212         free_fdbe(&fdbe);
00213       }
00214     }
00215   }
00216 }

Here is the call graph for this function:

Here is the caller graph for this function:


Generated on 7 Sep 2016 for Eggdrop by  doxygen 1.6.1