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

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

Go to the source code of this file.

Defines

#define filedb_read(fdb, entry, len)

Functions

static void free_fdbe (filedb_entry **fdbe)
static filedb_entry_malloc_fdbe (char *file, int line)
static void lockfile (FILE *fdb)
static void unlockfile (FILE *f)
static int filedb_readtop (FILE *fdb, filedb_top *fdbt)
static int filedb_writetop (FILE *fdb, filedb_top *fdbt)
static int filedb_delfile (FILE *fdb, long pos)
static filedb_entryfiledb_findempty (FILE *fdb, int tot)
static int _filedb_updatefile (FILE *fdb, long pos, filedb_entry *fdbe, int update, char *file, int line)
static int _filedb_movefile (FILE *fdb, long pos, filedb_entry *fdbe, char *file, int line)
static int _filedb_addfile (FILE *fdb, filedb_entry *fdbe, char *file, int line)
static filedb_entry_filedb_getfile (FILE *fdb, long pos, int get, char *file, int line)
static filedb_entry_filedb_matchfile (FILE *fdb, long pos, char *match, char *file, int line)
static void filedb_cleanup (FILE *fdb)
static void filedb_mergeempty (FILE *fdb)
static filedb_entryfiledb_getentry (char *dir, char *fn)
static void filedb_initdb (FILE *fdb)
static void filedb_timestamp (FILE *fdb)
static void filedb_update (char *path, FILE *fdb, int sort)
static char * make_point_path (char *path)
static FILE * filedb_open (char *path, int sort)
static void filedb_close (FILE *fdb)
static void filedb_add (FILE *fdb, char *filename, char *nick)
static void filedb_ls (FILE *fdb, int idx, char *mask, int showall)
static void remote_filereq (int idx, char *from, char *file)
static void filedb_getdesc (char *dir, char *fn, char **desc)
static void filedb_getowner (char *dir, char *fn, char **owner)
static int filedb_getgots (char *dir, char *fn)
static void filedb_setdesc (char *dir, char *fn, char *desc)
static void filedb_setowner (char *dir, char *fn, char *owner)
static void filedb_setlink (char *dir, char *fn, char *link)
static void filedb_getlink (char *dir, char *fn, char **link)
static void filedb_getfiles (Tcl_Interp *irp, char *dir)
static void filedb_getdirs (Tcl_Interp *irp, char *dir)
static void filedb_change (char *dir, char *fn, int what)

Variables

static int count = 0

Define Documentation

#define filedb_read ( fdb,
entry,
len   ) 
Value:
{                                       \
  if ((len) > 0) {                      \
    (entry) = nmalloc((len));           \
    fread((entry), 1, (len), (fdb));    \
  }                                     \
}

Definition at line 414 of file filedb3.c.

Referenced by _filedb_getfile().


Function Documentation

static int _filedb_addfile ( FILE *  fdb,
filedb_entry fdbe,
char *  file,
int  line 
) [static]

Definition at line 404 of file filedb3.c.

References _filedb_updatefile(), filedb_entry::_type, POS_NEW, TYPE_NEW, and UPDATE_ALL.

00405 {
00406   fdbe->_type = TYPE_NEW;
00407   _filedb_updatefile(fdb, POS_NEW, fdbe, UPDATE_ALL, file, line);
00408   return 0;
00409 }

Here is the call graph for this function:

static filedb_entry* _filedb_getfile ( FILE *  fdb,
long  pos,
int  get,
char *  file,
int  line 
) [static]

Definition at line 427 of file filedb3.c.

References _malloc_fdbe(), filedb_entry::_type, filedb_entry::buf_len, filedb_header::buffer_len, filedb_entry::chan, filedb_header::chan_len, filedb_entry::desc, filedb_header::desc_len, filedb_entry::dyn_len, FILE_ISLINK, FILE_UNUSED, filedb_read, filedb_tot_dynspace, filedb_entry::filename, filedb_header::filename_len, filedb_entry::flags_req, filedb_header::flags_req_len, GET_FILENAME, GET_FULL, filedb_header::gots, filedb_entry::gots, NULL, filedb_entry::pos, filedb_entry::sharelink, filedb_header::sharelink_len, filedb_header::size, filedb_entry::size, filedb_header::stat, filedb_entry::stat, TYPE_EXIST, filedb_header::uploaded, filedb_entry::uploaded, filedb_entry::uploader, and filedb_header::uploader_len.

Referenced by _filedb_matchfile().

00429 {
00430   filedb_entry *fdbe;
00431   filedb_header fdh;
00432 
00433   /* Read header */
00434   fseek(fdb, pos, SEEK_SET);
00435   fread(&fdh, 1, sizeof(filedb_header), fdb);
00436   if (feof(fdb))
00437     return NULL;
00438 
00439   /* Allocate memory for file db entry */
00440   fdbe = _malloc_fdbe(file, line);
00441 
00442   /* Optimisation: maybe use memcpy here? */
00443   fdbe->uploaded = fdh.uploaded;
00444   fdbe->size = fdh.size;
00445   fdbe->stat = fdh.stat;
00446   fdbe->gots = fdh.gots;
00447 
00448   fdbe->buf_len = fdh.buffer_len;
00449   fdbe->dyn_len = filedb_tot_dynspace(fdh);
00450   fdbe->pos = pos;              /* Save position                */
00451   fdbe->_type = TYPE_EXIST;     /* Entry exists in DB           */
00452 
00453   /* This is useful for cases where we don't read the rest of the
00454    * data, but need to know whether the file is a link.
00455    */
00456   if (fdh.sharelink_len > 0)
00457     fdbe->stat |= FILE_ISLINK;
00458   else
00459     fdbe->stat &= ~FILE_ISLINK;
00460 
00461   /* Read additional data from db */
00462   if (get >= GET_FILENAME) {
00463     filedb_read(fdb, fdbe->filename, fdh.filename_len);
00464   } else
00465     fseek(fdb, fdh.filename_len, SEEK_CUR);
00466   if (get < GET_FULL || (fdh.stat & FILE_UNUSED))
00467     fseek(fdb, filedb_tot_dynspace(fdh) - fdh.filename_len, SEEK_CUR);
00468   else if (get == GET_FULL) {
00469     filedb_read(fdb, fdbe->desc, fdh.desc_len);
00470     filedb_read(fdb, fdbe->chan, fdh.chan_len);
00471     filedb_read(fdb, fdbe->uploader, fdh.uploader_len);
00472     filedb_read(fdb, fdbe->flags_req, fdh.flags_req_len);
00473     filedb_read(fdb, fdbe->sharelink, fdh.sharelink_len);
00474   }
00475   fseek(fdb, fdh.buffer_len, SEEK_CUR); /* Skip buffer                  */
00476   return fdbe;                  /* Return the ready structure   */
00477 }

Here is the call graph for this function:

Here is the caller graph for this function:

static filedb_entry* _filedb_matchfile ( FILE *  fdb,
long  pos,
char *  match,
char *  file,
int  line 
) [static]

Definition at line 482 of file filedb3.c.

References _filedb_getfile(), FILE_UNUSED, filedb_getfile, filedb_entry::filename, free_fdbe(), GET_FILENAME, GET_FULL, NULL, filedb_entry::stat, and wild_match_file().

00484 {
00485   filedb_entry *fdbe = NULL;
00486 
00487   fseek(fdb, pos, SEEK_SET);
00488   while (!feof(fdb)) {
00489     pos = ftell(fdb);
00490     fdbe = filedb_getfile(fdb, pos, GET_FILENAME);
00491     if (fdbe) {
00492       if (!(fdbe->stat & FILE_UNUSED) &&        /* Not unused?         */
00493           wild_match_file(match, fdbe->filename)) {     /* Matches our mask?   */
00494         free_fdbe(&fdbe);
00495         fdbe = _filedb_getfile(fdb, pos, GET_FULL, file, line); /* Save all data now   */
00496         return fdbe;
00497       }
00498       free_fdbe(&fdbe);
00499     }
00500   }
00501   return NULL;
00502 }

Here is the call graph for this function:

static int _filedb_movefile ( FILE *  fdb,
long  pos,
filedb_entry fdbe,
char *  file,
int  line 
) [static]

Definition at line 394 of file filedb3.c.

References _filedb_updatefile(), filedb_entry::_type, TYPE_EXIST, and UPDATE_ALL.

00396 {
00397   fdbe->_type = TYPE_EXIST;
00398   _filedb_updatefile(fdb, pos, fdbe, UPDATE_ALL, file, line);
00399   return 0;
00400 }

Here is the call graph for this function:

static int _filedb_updatefile ( FILE *  fdb,
long  pos,
filedb_entry fdbe,
int  update,
char *  file,
int  line 
) [static]

Definition at line 274 of file filedb3.c.

References filedb_entry::_type, filedb_entry::buf_len, filedb_header::buffer_len, filedb_entry::chan, filedb_header::chan_len, filedb_entry::desc, filedb_header::desc_len, filedb_entry::dyn_len, egg_bzero, filedb_delfile(), filedb_findempty(), filedb_tot_dynspace, filedb_entry::filename, filedb_header::filename_len, filedb_entry::flags_req, filedb_header::flags_req_len, free_fdbe(), filedb_entry::gots, filedb_header::gots, filedb_entry::pos, POS_NEW, filedb_entry::sharelink, filedb_header::sharelink_len, filedb_entry::size, filedb_header::size, filedb_entry::stat, filedb_header::stat, TYPE_EXIST, UPDATE_ALL, UPDATE_SIZE, filedb_entry::uploaded, filedb_header::uploaded, filedb_entry::uploader, and filedb_header::uploader_len.

Referenced by _filedb_addfile(), and _filedb_movefile().

00276 {
00277   filedb_header fdh;
00278   int reposition = 0;
00279   int ndyntot, odyntot, nbuftot, obuftot;
00280 
00281   egg_bzero(&fdh, sizeof(filedb_header));
00282   fdh.uploaded = fdbe->uploaded;
00283   fdh.size = fdbe->size;
00284   fdh.stat = fdbe->stat;
00285   fdh.gots = fdbe->gots;
00286 
00287   /* Only add the buffer length if the buffer is not empty. Otherwise it
00288    * would result in lots of 1 byte entries which actually don't contain
00289    * any data.
00290    */
00291   if (fdbe->filename)
00292     fdh.filename_len = strlen(fdbe->filename) + 1;
00293   if (fdbe->desc)
00294     fdh.desc_len = strlen(fdbe->desc) + 1;
00295   if (fdbe->chan)
00296     fdh.chan_len = strlen(fdbe->chan) + 1;
00297   if (fdbe->uploader)
00298     fdh.uploader_len = strlen(fdbe->uploader) + 1;
00299   if (fdbe->flags_req)
00300     fdh.flags_req_len = strlen(fdbe->flags_req) + 1;
00301   if (fdbe->sharelink)
00302     fdh.sharelink_len = strlen(fdbe->sharelink) + 1;
00303 
00304   odyntot = fdbe->dyn_len;      /* Old length of dynamic data   */
00305   obuftot = fdbe->buf_len;      /* Old length of spare space    */
00306   ndyntot = filedb_tot_dynspace(fdh);   /* New length of dynamic data   */
00307   nbuftot = obuftot;
00308 
00309   if (fdbe->_type == TYPE_EXIST) {
00310     /* If we only update the header, we don't need to worry about
00311      * sizes and just use the old place (i.e. the place pointed
00312      * to by pos).
00313      */
00314     if (update < UPDATE_ALL) {
00315       /* Unless forced to it, we ignore new buffer sizes if we do not
00316        * run in UPDATE_ALL mode.
00317        */
00318       if (update != UPDATE_SIZE) {
00319         ndyntot = odyntot;
00320         nbuftot = obuftot;
00321       }
00322     } else {
00323       /* If we have a given/preferred position */
00324       if ((pos != POS_NEW) &&
00325           /* and if our new size is smaller than the old size, we
00326            * just adjust the buffer length and still use the same
00327            * position
00328            */
00329           (ndyntot <= (odyntot + obuftot))) {
00330         nbuftot = (odyntot + obuftot) - ndyntot;
00331       } else {
00332         /* If we have an existing position, but the new entry doesn't
00333          * fit into it's old home, we need to delete it before
00334          * repositioning.
00335          */
00336         if (pos != POS_NEW)
00337           filedb_delfile(fdb, pos);
00338         reposition = 1;
00339       }
00340     }
00341   } else {
00342     fdbe->_type = TYPE_EXIST;   /* Update type                  */
00343     reposition = 1;
00344   }
00345 
00346   /* Search for a new home */
00347   if (reposition) {
00348     filedb_entry *n_fdbe;
00349 
00350     n_fdbe = filedb_findempty(fdb, filedb_tot_dynspace(fdh));
00351     fdbe->pos = pos = n_fdbe->pos;
00352     /* If we create a new entry (instead of using an existing one),
00353      * buf_len is zero
00354      */
00355     if (n_fdbe->buf_len > 0)
00356       /* Note: empty entries have dyn_len set to zero, so we only
00357        *       need to consider buf_len.
00358        */
00359       nbuftot = n_fdbe->buf_len - ndyntot;
00360     else
00361       nbuftot = 0;
00362     free_fdbe(&n_fdbe);
00363   }
00364 
00365   /* Set length of dynamic data and buffer */
00366   fdbe->dyn_len = ndyntot;
00367   fdbe->buf_len = fdh.buffer_len = nbuftot;
00368 
00369   /* Write header */
00370   fseek(fdb, pos, SEEK_SET);
00371   fwrite(&fdh, 1, sizeof(filedb_header), fdb);
00372   /* Write dynamic data */
00373   if (update == UPDATE_ALL) {
00374     if (fdbe->filename)
00375       fwrite(fdbe->filename, 1, fdh.filename_len, fdb);
00376     if (fdbe->desc)
00377       fwrite(fdbe->desc, 1, fdh.desc_len, fdb);
00378     if (fdbe->chan)
00379       fwrite(fdbe->chan, 1, fdh.chan_len, fdb);
00380     if (fdbe->uploader)
00381       fwrite(fdbe->uploader, 1, fdh.uploader_len, fdb);
00382     if (fdbe->flags_req)
00383       fwrite(fdbe->flags_req, 1, fdh.flags_req_len, fdb);
00384     if (fdbe->sharelink)
00385       fwrite(fdbe->sharelink, 1, fdh.sharelink_len, fdb);
00386   } else
00387     fseek(fdb, ndyntot, SEEK_CUR);      /* Skip over dynamic data */
00388   fseek(fdb, nbuftot, SEEK_CUR);        /* Skip over buffer       */
00389   return 0;
00390 }

Here is the call graph for this function:

Here is the caller graph for this function:

static filedb_entry* _malloc_fdbe ( char *  file,
int  line 
) [static]

Definition at line 100 of file filedb3.c.

References filedb_entry::_type, egg_bzero, global, MODULE_NAME, nmalloc, NULL, and TYPE_NEW.

Referenced by _filedb_getfile().

00101 {
00102   filedb_entry *fdbe = NULL;
00103 
00104 #ifdef DEBUG_MEM
00105   /* This is a hack to access the nmalloc function with
00106    * special file and line information
00107    */
00108   fdbe = (((void *(*)())global[0])(sizeof(filedb_entry),MODULE_NAME,file,line));
00109 #else
00110   fdbe = nmalloc(sizeof(filedb_entry));
00111 #endif
00112   egg_bzero(fdbe, sizeof(filedb_entry));
00113 
00114   /* Mark as new, will be overwritten if necessary. */
00115   fdbe->_type = TYPE_NEW;
00116   return fdbe;
00117 }

Here is the caller graph for this function:

static void filedb_add ( FILE *  fdb,
char *  filename,
char *  nick 
) [static]

Definition at line 848 of file filedb3.c.

References filedb_matchfile, filedb_readtop(), filedb_updatefile, free_fdbe(), malloc_strcpy, my_free, now, NULL, filedb_entry::pos, UPDATE_ALL, filedb_entry::uploaded, and filedb_entry::uploader.

Referenced by add_file().

00849 {
00850   filedb_entry *fdbe = NULL;
00851 
00852   filedb_readtop(fdb, NULL);
00853   /* When the filedb was opened, a record was already created. */
00854   fdbe = filedb_matchfile(fdb, ftell(fdb), filename);
00855   if (!fdbe)
00856     return;
00857   my_free(fdbe->uploader);
00858   malloc_strcpy(fdbe->uploader, nick);
00859   fdbe->uploaded = now;
00860   filedb_updatefile(fdb, fdbe->pos, fdbe, UPDATE_ALL);
00861   free_fdbe(&fdbe);
00862 }

Here is the call graph for this function:

Here is the caller graph for this function:

static void filedb_change ( char *  dir,
char *  fn,
int  what 
) [static]

Definition at line 1268 of file filedb3.c.

References FILE_DIR, FILE_HIDDEN, FILE_SHARE, filedb_close(), FILEDB_HIDE, filedb_matchfile, filedb_open(), filedb_readtop(), FILEDB_SHARE, FILEDB_UNHIDE, FILEDB_UNSHARE, filedb_updatefile, free_fdbe(), NULL, filedb_entry::pos, filedb_entry::stat, and UPDATE_HEADER.

01269 {
01270   FILE *fdb;
01271   filedb_entry *fdbe;
01272   int changed = 0;
01273 
01274   fdb = filedb_open(dir, 0);
01275   if (!fdb)
01276     return;
01277   filedb_readtop(fdb, NULL);
01278   fdbe = filedb_matchfile(fdb, ftell(fdb), fn);
01279   if (fdbe) {
01280     if (!(fdbe->stat & FILE_DIR)) {
01281       switch (what) {
01282       case FILEDB_SHARE:
01283         fdbe->stat |= FILE_SHARE;
01284         break;
01285       case FILEDB_UNSHARE:
01286         fdbe->stat &= ~FILE_SHARE;
01287         break;
01288       }
01289       changed = 1;
01290     }
01291     switch (what) {
01292     case FILEDB_HIDE:
01293       fdbe->stat |= FILE_HIDDEN;
01294       changed = 1;
01295       break;
01296     case FILEDB_UNHIDE:
01297       fdbe->stat &= ~FILE_HIDDEN;
01298       changed = 1;
01299       break;
01300     }
01301     if (changed)
01302       filedb_updatefile(fdb, fdbe->pos, fdbe, UPDATE_HEADER);
01303     free_fdbe(&fdbe);
01304   }
01305   filedb_close(fdb);
01306 }

Here is the call graph for this function:

static void filedb_cleanup ( FILE *  fdb  )  [static]

Definition at line 507 of file filedb3.c.

References FILE_UNUSED, filedb_getfile, filedb_movefile, filedb_readtop(), free_fdbe(), GET_FULL, GET_HEADER, NULL, and filedb_entry::stat.

Referenced by filedb_update().

00508 {
00509   long oldpos, newpos, temppos;
00510   filedb_entry *fdbe = NULL;
00511 
00512   filedb_readtop(fdb, NULL);    /* Skip DB header  */
00513   newpos = temppos = oldpos = ftell(fdb);
00514   fseek(fdb, oldpos, SEEK_SET); /* Go to beginning */
00515   while (!feof(fdb)) {          /* Loop until EOF  */
00516     fdbe = filedb_getfile(fdb, oldpos, GET_HEADER);     /* Read header     */
00517     if (fdbe) {
00518       if (fdbe->stat & FILE_UNUSED) {   /* Found dirt!     */
00519         free_fdbe(&fdbe);
00520         while (!feof(fdb)) {    /* Loop until EOF  */
00521           newpos = ftell(fdb);
00522           fdbe = filedb_getfile(fdb, newpos, GET_FULL); /* Read next entry */
00523           if (!fdbe)
00524             break;
00525           if (!(fdbe->stat & FILE_UNUSED)) {    /* Not unused?     */
00526             temppos = ftell(fdb);
00527             filedb_movefile(fdb, oldpos, fdbe); /* Move to top     */
00528             oldpos = ftell(fdb);
00529             fseek(fdb, temppos, SEEK_SET);
00530           }
00531           free_fdbe(&fdbe);
00532         }
00533       } else {
00534         free_fdbe(&fdbe);
00535         oldpos = ftell(fdb);
00536       }
00537     }
00538   }
00539   ftruncate(fileno(fdb), oldpos);       /* Shorten file    */
00540 }

Here is the call graph for this function:

Here is the caller graph for this function:

static void filedb_close ( FILE *  fdb  )  [static]

Definition at line 835 of file filedb3.c.

References count, filedb_timestamp(), and unlockfile().

Referenced by add_file(), cmd_desc(), cmd_hide(), cmd_ln(), cmd_mkdir(), cmd_mv_cp(), cmd_optimize(), cmd_reget_get(), cmd_rm(), cmd_rmdir(), cmd_share(), cmd_unhide(), cmd_unshare(), filedb_change(), filedb_getdirs(), filedb_getentry(), filedb_getfiles(), filedb_setdesc(), filedb_setlink(), filedb_setowner(), files_ls(), files_reget(), incr_file_gots(), remote_filereq(), resolve_dir(), and welcome_to_files().

00836 {
00837   filedb_timestamp(fdb);
00838   fseek(fdb, 0L, SEEK_END);
00839   count--;
00840   unlockfile(fdb);
00841   fclose(fdb);
00842 }

Here is the call graph for this function:

Here is the caller graph for this function:

static int filedb_delfile ( FILE *  fdb,
long  pos 
) [static]

Definition at line 185 of file filedb3.c.

References filedb_header::buffer_len, FILE_UNUSED, filedb_tot_dynspace, filedb_zero_dynspace, and filedb_header::stat.

Referenced by _filedb_updatefile(), cmd_mv_cp(), cmd_rm(), cmd_rmdir(), filedb_setlink(), and filedb_update().

00186 {
00187   filedb_header fdh;
00188 
00189   fseek(fdb, pos, SEEK_SET);    /* Go to start of entry */
00190   if (feof(fdb))
00191     return 0;
00192   fread(&fdh, 1, sizeof(filedb_header), fdb);   /* Read header          */
00193   fdh.stat = FILE_UNUSED;
00194 
00195   /* Assign all available space to buffer. Simplifies
00196    * space calculation later on.
00197    */
00198   fdh.buffer_len += filedb_tot_dynspace(fdh);
00199   filedb_zero_dynspace(fdh);
00200 
00201   fseek(fdb, pos, SEEK_SET);    /* Go back to start     */
00202   fwrite(&fdh, 1, sizeof(filedb_header), fdb);  /* Write new header     */
00203   return 1;
00204 }

Here is the caller graph for this function:

static filedb_entry* filedb_findempty ( FILE *  fdb,
int  tot 
) [static]

Definition at line 218 of file filedb3.c.

References filedb_entry::buf_len, FILE_UNUSED, FILEDB_ESTDYN, filedb_getfile, filedb_movefile, filedb_readtop(), free_fdbe(), GET_HEADER, malloc_fdbe, NULL, filedb_entry::pos, and filedb_entry::stat.

Referenced by _filedb_updatefile().

00219 {
00220   filedb_entry *fdbe;
00221 
00222   filedb_readtop(fdb, NULL);
00223   fdbe = filedb_getfile(fdb, ftell(fdb), GET_HEADER);
00224   while (fdbe) {
00225     /* Found an existing, empty entry? */
00226     if ((fdbe->stat & FILE_UNUSED) && (fdbe->buf_len >= tot)) {
00227       /* Do we have enough space to split up the entry to form
00228        * another empty entry? That way we would use the space
00229        * more efficiently.
00230        */
00231       if (fdbe->buf_len > (tot + sizeof(filedb_header) + FILEDB_ESTDYN)) {
00232         filedb_entry *fdbe_oe;
00233 
00234         /* Create new entry containing the additional space */
00235         fdbe_oe = malloc_fdbe();
00236         fdbe_oe->stat = FILE_UNUSED;
00237         fdbe_oe->pos = fdbe->pos + sizeof(filedb_header) + tot;
00238         fdbe_oe->buf_len = fdbe->buf_len - tot - sizeof(filedb_header);
00239         filedb_movefile(fdb, fdbe_oe->pos, fdbe_oe);
00240         free_fdbe(&fdbe_oe);
00241 
00242         /* Cut down buf_len of entry as the rest is now used in the new
00243          * entry.
00244          */
00245         fdbe->buf_len = tot;
00246       }
00247       return fdbe;
00248     }
00249     free_fdbe(&fdbe);
00250     fdbe = filedb_getfile(fdb, ftell(fdb), GET_HEADER);
00251   }
00252 
00253   /* No existing entries, so create new entry at end of DB instead. */
00254   fdbe = malloc_fdbe();
00255   fseek(fdb, 0L, SEEK_END);
00256   fdbe->pos = ftell(fdb);
00257   return fdbe;
00258 }

Here is the call graph for this function:

Here is the caller graph for this function:

static void filedb_getdesc ( char *  dir,
char *  fn,
char **  desc 
) [static]

Definition at line 1100 of file filedb3.c.

References filedb_entry::desc, filedb_getentry(), free_fdbe(), nmalloc, and NULL.

01101 {
01102   filedb_entry *fdbe = NULL;
01103 
01104   fdbe = filedb_getentry(dir, fn);
01105   if (fdbe) {
01106     if (fdbe->desc) {
01107       *desc = nmalloc(strlen(fdbe->desc) + 1);
01108       strcpy(*desc, fdbe->desc);
01109     }
01110     free_fdbe(&fdbe);
01111   } else
01112     *desc = NULL;
01113 }

Here is the call graph for this function:

static void filedb_getdirs ( Tcl_Interp irp,
char *  dir 
) [static]

Definition at line 1248 of file filedb3.c.

References FILE_DIR, FILE_UNUSED, filedb_close(), filedb_getfile, filedb_open(), filedb_readtop(), filedb_entry::filename, free_fdbe(), GET_FILENAME, NULL, and filedb_entry::stat.

01249 {
01250   FILE *fdb;
01251   filedb_entry *fdbe;
01252 
01253   fdb = filedb_open(dir, 0);
01254   if (!fdb)
01255     return;
01256   filedb_readtop(fdb, NULL);
01257   while (!feof(fdb)) {
01258     fdbe = filedb_getfile(fdb, ftell(fdb), GET_FILENAME);
01259     if (fdbe) {
01260       if ((!(fdbe->stat & FILE_UNUSED)) && (fdbe->stat & FILE_DIR))
01261         Tcl_AppendElement(irp, fdbe->filename);
01262       free_fdbe(&fdbe);
01263     }
01264   }
01265   filedb_close(fdb);
01266 }

Here is the call graph for this function:

static filedb_entry* filedb_getentry ( char *  dir,
char *  fn 
) [static]

Definition at line 596 of file filedb3.c.

References filedb_close(), filedb_matchfile, filedb_open(), filedb_readtop(), and NULL.

Referenced by filedb_getdesc(), filedb_getgots(), filedb_getlink(), and filedb_getowner().

00597 {
00598   FILE *fdb;
00599   filedb_entry *fdbe = NULL;
00600 
00601   fdb = filedb_open(dir, 0);
00602   if (fdb) {
00603     filedb_readtop(fdb, NULL);
00604     fdbe = filedb_matchfile(fdb, ftell(fdb), fn);
00605     filedb_close(fdb);
00606   }
00607   return fdbe;
00608 }

Here is the call graph for this function:

Here is the caller graph for this function:

static void filedb_getfiles ( Tcl_Interp irp,
char *  dir 
) [static]

Definition at line 1228 of file filedb3.c.

References FILE_DIR, FILE_UNUSED, filedb_close(), filedb_getfile, filedb_open(), filedb_readtop(), filedb_entry::filename, free_fdbe(), GET_FILENAME, NULL, and filedb_entry::stat.

01229 {
01230   FILE *fdb;
01231   filedb_entry *fdbe;
01232 
01233   fdb = filedb_open(dir, 0);
01234   if (!fdb)
01235     return;
01236   filedb_readtop(fdb, NULL);
01237   while (!feof(fdb)) {
01238     fdbe = filedb_getfile(fdb, ftell(fdb), GET_FILENAME);
01239     if (fdbe) {
01240       if (!(fdbe->stat & (FILE_DIR | FILE_UNUSED)))
01241         Tcl_AppendElement(irp, fdbe->filename);
01242       free_fdbe(&fdbe);
01243     }
01244   }
01245   filedb_close(fdb);
01246 }

Here is the call graph for this function:

static int filedb_getgots ( char *  dir,
char *  fn 
) [static]

Definition at line 1128 of file filedb3.c.

References filedb_getentry(), free_fdbe(), filedb_entry::gots, and NULL.

01129 {
01130   filedb_entry *fdbe = NULL;
01131   int gots = 0;
01132 
01133   fdbe = filedb_getentry(dir, fn);
01134   if (fdbe) {
01135     gots = fdbe->gots;
01136     free_fdbe(&fdbe);
01137   }
01138   return gots;
01139 }

Here is the call graph for this function:

static void filedb_getlink ( char *  dir,
char *  fn,
char **  link 
) [static]

Definition at line 1214 of file filedb3.c.

References FILE_DIR, filedb_getentry(), free_fdbe(), malloc_strcpy, NULL, filedb_entry::sharelink, and filedb_entry::stat.

01215 {
01216   filedb_entry *fdbe = NULL;
01217 
01218   fdbe = filedb_getentry(dir, fn);
01219   if (fdbe && (!(fdbe->stat & FILE_DIR))) {
01220     malloc_strcpy(*link, fdbe->sharelink);
01221   } else
01222     *link = NULL;
01223   if (fdbe)
01224     free_fdbe(&fdbe);
01225   return;
01226 }

Here is the call graph for this function:

static void filedb_getowner ( char *  dir,
char *  fn,
char **  owner 
) [static]

Definition at line 1115 of file filedb3.c.

References filedb_getentry(), free_fdbe(), nmalloc, NULL, and filedb_entry::uploader.

01116 {
01117   filedb_entry *fdbe = NULL;
01118 
01119   fdbe = filedb_getentry(dir, fn);
01120   if (fdbe) {
01121     *owner = nmalloc(strlen(fdbe->uploader) + 1);
01122     strcpy(*owner, fdbe->uploader);
01123     free_fdbe(&fdbe);
01124   } else
01125     *owner = NULL;
01126 }

Here is the call graph for this function:

static void filedb_initdb ( FILE *  fdb  )  [static]

Definition at line 612 of file filedb3.c.

References FILEDB_NEWEST_VER, filedb_writetop(), now, filedb_top::timestamp, and filedb_top::version.

Referenced by convert_old_db(), and convert_old_files().

00613 {
00614   filedb_top fdbt;
00615 
00616   fdbt.version = FILEDB_NEWEST_VER;
00617   fdbt.timestamp = now;
00618   filedb_writetop(fdb, &fdbt);
00619 }

Here is the call graph for this function:

Here is the caller graph for this function:

static void filedb_ls ( FILE *  fdb,
int  idx,
char *  mask,
int  showall 
) [static]

Definition at line 867 of file filedb3.c.

References break_down_flags, filedb_entry::chan, dcc, filedb_entry::desc, dprintf, egg_strftime(), FILE_DIR, FILE_HIDDEN, FILE_SHARE, FILE_UNUSED, filedb_getfile, filedb_readtop(), filelist_add(), filelist_addout(), filelist_free(), filelist_idxshow(), filelist_new(), filelist_sort(), filedb_entry::filename, FILES_LSHEAD1, FILES_LSHEAD2, FILES_NOFILES, FILES_NOMATCH, FILES_REQUIRES, flagrec_ok, filedb_entry::flags_req, FR_CHAN, FR_GLOBAL, free_fdbe(), GET_FULL, get_user_flagrec, flag_record::global, filedb_entry::gots, malloc_strcpy, my_free, nmalloc, NULL, filedb_entry::sharelink, filedb_entry::size, filedb_entry::stat, filedb_entry::uploaded, filedb_entry::uploader, USER_JANITOR, USER_MASTER, and wild_match_file().

Referenced by files_ls().

00868 {
00869   int ok = 0, cnt = 0, is = 0;
00870   char s1[81], *p = NULL;
00871   struct flag_record user = { FR_GLOBAL | FR_CHAN, 0, 0, 0, 0, 0 };
00872   filedb_entry *fdbe = NULL;
00873   filelist_t *flist = NULL;
00874 
00875   flist = filelist_new();
00876   filedb_readtop(fdb, NULL);
00877   fdbe = filedb_getfile(fdb, ftell(fdb), GET_FULL);
00878   while (fdbe) {
00879     ok = 1;
00880     if (fdbe->stat & FILE_UNUSED)
00881       ok = 0;
00882     if (ok && (fdbe->stat & FILE_DIR) && fdbe->flags_req) {
00883       /* Check permissions */
00884       struct flag_record req = { FR_GLOBAL | FR_CHAN, 0, 0, 0, 0, 0 };
00885 
00886       break_down_flags(fdbe->flags_req, &req, NULL);
00887       get_user_flagrec(dcc[idx].user, &user, dcc[idx].u.file->chat->con_chan);
00888       if (!flagrec_ok(&req, &user)) {
00889         ok = 0;
00890       }
00891     }
00892     if (ok)
00893       is = 1;
00894     if (ok && !wild_match_file(mask, fdbe->filename))
00895       ok = 0;
00896     if (ok && (fdbe->stat & FILE_HIDDEN) && !(showall))
00897       ok = 0;
00898     if (ok) {
00899       /* Display it! */
00900       if (cnt == 0) {
00901         dprintf(idx, FILES_LSHEAD1);
00902         dprintf(idx, FILES_LSHEAD2);
00903       }
00904       filelist_add(flist, fdbe->filename);
00905       if (fdbe->stat & FILE_DIR) {
00906         char *s2 = NULL, *s3 = NULL;
00907 
00908         /* Too long? */
00909         if (strlen(fdbe->filename) > 45) {
00910           /* Display the filename on its own line. */
00911           s2 = nmalloc(strlen(fdbe->filename) + 3);
00912           sprintf(s2, "%s/\n", fdbe->filename);
00913           filelist_addout(flist, s2);
00914           my_free(s2);
00915         } else {
00916           s2 = nmalloc(strlen(fdbe->filename) + 2);
00917           sprintf(s2, "%s/", fdbe->filename);
00918         }
00919         /* Note: You have to keep the sprintf and the nmalloc statements
00920          *       in sync, i.e. always check that you allocate enough
00921          *       memory.
00922          */
00923         if ((fdbe->flags_req) && (user.global &(USER_MASTER | USER_JANITOR))) {
00924           s3 = nmalloc(42 + strlen(s2 ? s2 : "") + 6 +
00925                        strlen(FILES_REQUIRES) + strlen(fdbe->flags_req) + 1 +
00926                        strlen(fdbe->chan ? fdbe->chan : "") + 1);
00927           sprintf(s3, "%-30s <DIR%s>  (%s %s%s%s)\n", s2,
00928                   fdbe->stat & FILE_SHARE ?
00929                   " SHARE" : "", FILES_REQUIRES, fdbe->flags_req,
00930                   fdbe->chan ? " " : "", fdbe->chan ? fdbe->chan : "");
00931         } else {
00932           s3 = nmalloc(38 + strlen(s2 ? s2 : ""));
00933           sprintf(s3, "%-30s <DIR>\n", s2 ? s2 : "");
00934         }
00935         if (s2)
00936           my_free(s2);
00937         filelist_addout(flist, s3);
00938         my_free(s3);
00939       } else {
00940         char s2[41], t[50], *s3 = NULL, *s4;
00941 
00942         s2[0] = 0;
00943         if (showall) {
00944           if (fdbe->stat & FILE_SHARE)
00945             strcat(s2, " (shr)");
00946           if (fdbe->stat & FILE_HIDDEN)
00947             strcat(s2, " (hid)");
00948         }
00949         egg_strftime(t, 10, "%d%b%Y", localtime(&fdbe->uploaded));
00950         if (fdbe->size < 1024)
00951           sprintf(s1, "%5d", fdbe->size);
00952         else
00953           sprintf(s1, "%4dk", (int) (fdbe->size / 1024));
00954         if (fdbe->sharelink)
00955           strcpy(s1, "     ");
00956         /* Too long? */
00957         if (strlen(fdbe->filename) > 30) {
00958           s3 = nmalloc(strlen(fdbe->filename) + 2);
00959           sprintf(s3, "%s\n", fdbe->filename);
00960           filelist_addout(flist, s3);
00961           my_free(s3);
00962           /* Causes filename to be displayed on its own line */
00963         } else
00964           malloc_strcpy(s3, fdbe->filename);
00965         s4 = nmalloc(69 + strlen(s3 ? s3 : "") + strlen(s1) +
00966                      strlen(fdbe->uploader) + strlen(t) + strlen(s2));
00967         sprintf(s4, "%-30s %s  %-9s (%s)  %6d%s\n", s3 ? s3 : "", s1,
00968                 fdbe->uploader, t, fdbe->gots, s2);
00969         if (s3)
00970           my_free(s3);
00971         filelist_addout(flist, s4);
00972         my_free(s4);
00973         if (fdbe->sharelink) {
00974           s4 = nmalloc(9 + strlen(fdbe->sharelink));
00975           sprintf(s4, "   --> %s\n", fdbe->sharelink);
00976           filelist_addout(flist, s4);
00977           my_free(s4);
00978         }
00979       }
00980       if (fdbe->desc) {
00981         p = strchr(fdbe->desc, '\n');
00982         while (p != NULL) {
00983           *p = 0;
00984           if ((fdbe->desc)[0]) {
00985             char *sd;
00986 
00987             sd = nmalloc(strlen(fdbe->desc) + 5);
00988             sprintf(sd, "   %s\n", fdbe->desc);
00989             filelist_addout(flist, sd);
00990             my_free(sd);
00991           }
00992           strcpy(fdbe->desc, p + 1);
00993           p = strchr(fdbe->desc, '\n');
00994         }
00995         if ((fdbe->desc)[0]) {
00996           char *sd;
00997 
00998           sd = nmalloc(strlen(fdbe->desc) + 5);
00999           sprintf(sd, "   %s\n", fdbe->desc);
01000           filelist_addout(flist, sd);
01001           my_free(sd);
01002         }
01003       }
01004       cnt++;
01005     }
01006     free_fdbe(&fdbe);
01007     fdbe = filedb_getfile(fdb, ftell(fdb), GET_FULL);
01008   }
01009   if (is == 0)
01010     dprintf(idx, FILES_NOFILES);
01011   else if (cnt == 0)
01012     dprintf(idx, FILES_NOMATCH);
01013   else {
01014     filelist_sort(flist);
01015     filelist_idxshow(flist, idx);
01016     dprintf(idx, "--- %d file%s.\n", cnt, cnt != 1 ? "s" : "");
01017   }
01018   filelist_free(flist);
01019 }

Here is the call graph for this function:

Here is the caller graph for this function:

static void filedb_mergeempty ( FILE *  fdb  )  [static]

Definition at line 547 of file filedb3.c.

References filedb_entry::buf_len, FILE_UNUSED, filedb_getfile, filedb_readtop(), filedb_updatefile, free_fdbe(), GET_HEADER, NULL, filedb_entry::pos, filedb_entry::stat, and UPDATE_SIZE.

Referenced by filedb_open().

00548 {
00549   filedb_entry *fdbe_t, *fdbe_i;
00550   int modified;
00551 
00552   filedb_readtop(fdb, NULL);
00553   while (!feof(fdb)) {
00554     fdbe_t = filedb_getfile(fdb, ftell(fdb), GET_HEADER);
00555     if (fdbe_t) {
00556       if (fdbe_t->stat & FILE_UNUSED) {
00557         modified = 0;
00558         fdbe_i = filedb_getfile(fdb, ftell(fdb), GET_HEADER);
00559         while (fdbe_i) {
00560           /* Is this entry in use? */
00561           if (!(fdbe_i->stat & FILE_UNUSED))
00562             break;              /* It is, exit loop. */
00563 
00564           /* Woohoo, found an empty entry. Append it's space to
00565            * our target entry's buffer space.
00566            */
00567           fdbe_t->buf_len += sizeof(filedb_header) + fdbe_i->buf_len;
00568           modified++;
00569           free_fdbe(&fdbe_i);
00570           /* Get next file entry */
00571           fdbe_i = filedb_getfile(fdb, ftell(fdb), GET_HEADER);
00572         }
00573 
00574         /* Did we exit the loop because of a used entry? */
00575         if (fdbe_i) {
00576           free_fdbe(&fdbe_i);
00577           /* Did we find any empty entries before? */
00578           if (modified)
00579             filedb_updatefile(fdb, fdbe_t->pos, fdbe_t, UPDATE_SIZE);
00580           /* ... or because we hit EOF? */
00581         } else {
00582           /* Truncate trailing empty entries and exit. */
00583           ftruncate(fileno(fdb), fdbe_t->pos);
00584           free_fdbe(&fdbe_t);
00585           return;
00586         }
00587       }
00588       free_fdbe(&fdbe_t);
00589     }
00590   }
00591 }

Here is the call graph for this function:

Here is the caller graph for this function:

static FILE* filedb_open ( char *  path,
int  sort 
) [static]

Definition at line 736 of file filedb3.c.

References convert_old_db(), convert_old_files(), count, dccdir, filedb_mergeempty(), FILEDB_NEWEST_VER, filedb_path, filedb_readtop(), filedb_update(), filedb_writetop(), FILES_NOCONVERT, lockfile(), LOG_MISC, make_point_path(), my_free, nmalloc, now, NULL, putlog, simple_sprintf, filedb_top::timestamp, unlockfile(), and filedb_top::version.

Referenced by add_file(), cmd_desc(), cmd_hide(), cmd_ln(), cmd_mkdir(), cmd_mv_cp(), cmd_optimize(), cmd_reget_get(), cmd_rm(), cmd_rmdir(), cmd_share(), cmd_unhide(), cmd_unshare(), filedb_change(), filedb_getdirs(), filedb_getentry(), filedb_getfiles(), filedb_setdesc(), filedb_setlink(), filedb_setowner(), files_ls(), files_reget(), incr_file_gots(), remote_filereq(), resolve_dir(), and welcome_to_files().

00737 {
00738   char *s, *npath;
00739   FILE *fdb;
00740   filedb_top fdbt;
00741   struct stat st;
00742 
00743   if (count >= 2)
00744     putlog(LOG_MISC, "*", "(@) warning: %d open filedb's", count);
00745   npath = nmalloc(strlen(dccdir) + strlen(path) + 1);
00746   simple_sprintf(npath, "%s%s", dccdir, path);
00747   /* Use alternate filename if requested */
00748   if (filedb_path[0]) {
00749     char *s2;
00750 
00751     s2 = make_point_path(path);
00752     s = nmalloc(strlen(filedb_path) + strlen(s2) + 8);
00753     simple_sprintf(s, "%sfiledb.%s", filedb_path, s2);
00754     my_free(s2);
00755   } else {
00756     s = nmalloc(strlen(npath) + 10);
00757     simple_sprintf(s, "%s/.filedb", npath);
00758   }
00759   fdb = fopen(s, "r+b");
00760   if (!fdb) {
00761     if (convert_old_files(npath, s)) {
00762       fdb = fopen(s, "r+b");
00763       if (fdb == NULL) {
00764         putlog(LOG_MISC, "*", FILES_NOCONVERT, npath);
00765         my_free(s);
00766         my_free(npath);
00767         return NULL;
00768       }
00769       lockfile(fdb);
00770       filedb_update(npath, fdb, sort);
00771       count++;
00772       my_free(s);
00773       my_free(npath);
00774       return fdb;
00775     } else {
00776       filedb_top fdbt;
00777 
00778       /* Create new database and fix it up */
00779       fdb = fopen(s, "w+b");
00780       if (!fdb) {
00781         my_free(s);
00782         my_free(npath);
00783         return NULL;
00784       }
00785       lockfile(fdb);
00786       fdbt.version = FILEDB_NEWEST_VER;
00787       fdbt.timestamp = now;
00788       filedb_writetop(fdb, &fdbt);
00789       filedb_update(npath, fdb, sort);
00790       count++;
00791       my_free(s);
00792       my_free(npath);
00793       return fdb;
00794     }
00795   }
00796 
00797   lockfile(fdb);                /* Lock it from other bots */
00798   filedb_readtop(fdb, &fdbt);
00799   if (fdbt.version < FILEDB_NEWEST_VER) {
00800     if (!convert_old_db(&fdb, s)) {
00801       /* Conversion failed. Unlock file again and error out.
00802        * (convert_old_db() could have modified fdb, so check
00803        * for fdb != NULL.)
00804        */
00805       if (fdb)
00806         unlockfile(fdb);
00807       my_free(npath);
00808       my_free(s);
00809       return NULL;
00810     }
00811     filedb_update(npath, fdb, sort);
00812   }
00813   stat(npath, &st);
00814   /* Update filedb if:
00815    * + it's been 6 hours since it was last updated
00816    * + the directory has been visibly modified since then
00817    * (6 hours may be a bit often)
00818    */
00819   if (sort || ((now - fdbt.timestamp) > (6 * 3600)) ||
00820       (fdbt.timestamp < st.st_mtime) || (fdbt.timestamp < st.st_ctime))
00821     /* File database isn't up-to-date! */
00822     filedb_update(npath, fdb, sort & 1);
00823   else if ((now - fdbt.timestamp) > 300)
00824     filedb_mergeempty(fdb);
00825 
00826   count++;
00827   my_free(npath);
00828   my_free(s);
00829   return fdb;
00830 }

Here is the call graph for this function:

Here is the caller graph for this function:

static int filedb_readtop ( FILE *  fdb,
filedb_top fdbt 
) [static]

Definition at line 159 of file filedb3.c.

Referenced by cmd_desc(), cmd_hide(), cmd_ln(), cmd_mkdir(), cmd_mv_cp(), cmd_reget_get(), cmd_rm(), cmd_rmdir(), cmd_share(), cmd_unhide(), cmd_unshare(), convert_old_db(), filedb_add(), filedb_change(), filedb_cleanup(), filedb_findempty(), filedb_getdirs(), filedb_getentry(), filedb_getfiles(), filedb_ls(), filedb_mergeempty(), filedb_open(), filedb_setdesc(), filedb_setlink(), filedb_setowner(), filedb_timestamp(), filedb_update(), files_reget(), incr_file_gots(), remote_filereq(), and resolve_dir().

00160 {
00161   if (fdbt) {
00162     /* Read header */
00163     fseek(fdb, 0L, SEEK_SET);
00164     if (feof(fdb))
00165       return 0;
00166     fread(fdbt, 1, sizeof(filedb_top), fdb);
00167   } else
00168     fseek(fdb, sizeof(filedb_top), SEEK_SET);
00169   return 1;
00170 }

Here is the caller graph for this function:

static void filedb_setdesc ( char *  dir,
char *  fn,
char *  desc 
) [static]

Definition at line 1141 of file filedb3.c.

References filedb_entry::desc, filedb_close(), filedb_matchfile, filedb_open(), filedb_readtop(), filedb_updatefile, free_fdbe(), malloc_strcpy, my_free, NULL, filedb_entry::pos, and UPDATE_ALL.

01142 {
01143   filedb_entry *fdbe = NULL;
01144   FILE *fdb = NULL;
01145 
01146   fdb = filedb_open(dir, 0);
01147   if (!fdb)
01148     return;
01149   filedb_readtop(fdb, NULL);
01150   fdbe = filedb_matchfile(fdb, ftell(fdb), fn);
01151   if (fdbe) {
01152     my_free(fdbe->desc);
01153     malloc_strcpy(fdbe->desc, desc);
01154     filedb_updatefile(fdb, fdbe->pos, fdbe, UPDATE_ALL);
01155     free_fdbe(&fdbe);
01156   }
01157   filedb_close(fdb);
01158 }

Here is the call graph for this function:

static void filedb_setlink ( char *  dir,
char *  fn,
char *  link 
) [static]

Definition at line 1179 of file filedb3.c.

References botnetnick, FILE_DIR, filedb_addfile, filedb_close(), filedb_delfile(), filedb_matchfile, filedb_open(), filedb_readtop(), filedb_updatefile, filedb_entry::filename, free_fdbe(), malloc_fdbe, malloc_strcpy, my_free, now, NULL, filedb_entry::pos, filedb_entry::sharelink, filedb_entry::stat, UPDATE_ALL, filedb_entry::uploaded, and filedb_entry::uploader.

01180 {
01181   filedb_entry *fdbe = NULL;
01182   FILE *fdb = NULL;
01183 
01184   fdb = filedb_open(dir, 0);
01185   if (!fdb)
01186     return;
01187   filedb_readtop(fdb, NULL);
01188   fdbe = filedb_matchfile(fdb, ftell(fdb), fn);
01189   if (fdbe) {
01190     /* Change existing one? */
01191     if ((fdbe->stat & FILE_DIR) || !fdbe->sharelink)
01192       return;
01193     if (!link || !link[0])
01194       filedb_delfile(fdb, fdbe->pos);
01195     else {
01196       my_free(fdbe->sharelink);
01197       malloc_strcpy(fdbe->sharelink, link);
01198       filedb_updatefile(fdb, fdbe->pos, fdbe, UPDATE_ALL);
01199     }
01200     free_fdbe(&fdbe);
01201     return;
01202   }
01203 
01204   fdbe = malloc_fdbe();
01205   malloc_strcpy(fdbe->uploader, botnetnick);
01206   malloc_strcpy(fdbe->filename, fn);
01207   malloc_strcpy(fdbe->sharelink, link);
01208   fdbe->uploaded = now;
01209   filedb_addfile(fdb, fdbe);
01210   free_fdbe(&fdbe);
01211   filedb_close(fdb);
01212 }

Here is the call graph for this function:

static void filedb_setowner ( char *  dir,
char *  fn,
char *  owner 
) [static]

Definition at line 1160 of file filedb3.c.

References filedb_close(), filedb_matchfile, filedb_open(), filedb_readtop(), filedb_updatefile, free_fdbe(), malloc_strcpy, my_free, NULL, filedb_entry::pos, UPDATE_ALL, and filedb_entry::uploader.

01161 {
01162   filedb_entry *fdbe = NULL;
01163   FILE *fdb = NULL;
01164 
01165   fdb = filedb_open(dir, 0);
01166   if (!fdb)
01167     return;
01168   filedb_readtop(fdb, NULL);
01169   fdbe = filedb_matchfile(fdb, ftell(fdb), fn);
01170   if (fdbe) {
01171     my_free(fdbe->uploader);
01172     malloc_strcpy(fdbe->uploader, owner);
01173     filedb_updatefile(fdb, fdbe->pos, fdbe, UPDATE_ALL);
01174     free_fdbe(&fdbe);
01175   }
01176   filedb_close(fdb);
01177 }

Here is the call graph for this function:

static void filedb_timestamp ( FILE *  fdb  )  [static]

Definition at line 621 of file filedb3.c.

References filedb_readtop(), filedb_writetop(), NULL, and filedb_top::timestamp.

Referenced by filedb_close(), and filedb_update().

00622 {
00623   filedb_top fdbt;
00624 
00625   filedb_readtop(fdb, &fdbt);
00626   fdbt.timestamp = time(NULL);
00627   filedb_writetop(fdb, &fdbt);
00628 }

Here is the call graph for this function:

Here is the caller graph for this function:

static void filedb_update ( char *  path,
FILE *  fdb,
int  sort 
) [static]

Definition at line 636 of file filedb3.c.

References botnetnick, dirent, FILE_DIR, FILE_ISLINK, FILE_UNUSED, filedb_addfile, filedb_cleanup(), filedb_delfile(), filedb_getfile, filedb_matchfile, filedb_readtop(), filedb_timestamp(), filedb_updatefile, FILES_NOUPDATE, free_fdbe(), GET_FILENAME, LOG_MISC, malloc_fdbe, malloc_strcpy, my_free, nmalloc, now, NULL, putlog, S_ISDIR, and UPDATE_HEADER.

Referenced by filedb_open().

00637 {
00638   struct dirent *dd = NULL;
00639   struct stat st;
00640   filedb_entry *fdbe = NULL;
00641   DIR *dir = NULL;
00642   long where = 0;
00643   char *name = NULL, *s = NULL;
00644 
00645   /*
00646    * FIRST: make sure every real file is in the database
00647    */
00648   dir = opendir(path);
00649   if (dir == NULL) {
00650     putlog(LOG_MISC, "*", FILES_NOUPDATE);
00651     return;
00652   }
00653   dd = readdir(dir);
00654   while (dd != NULL) {
00655     malloc_strcpy(name, dd->d_name);
00656     if (name[0] != '.') {
00657       s = nmalloc(strlen(path) + strlen(name) + 2);
00658       sprintf(s, "%s/%s", path, name);
00659       stat(s, &st);
00660       my_free(s);
00661       filedb_readtop(fdb, NULL);
00662       fdbe = filedb_matchfile(fdb, ftell(fdb), name);
00663       if (!fdbe) {
00664         /* new file! */
00665         fdbe = malloc_fdbe();
00666         malloc_strcpy(fdbe->filename, name);
00667         malloc_strcpy(fdbe->uploader, botnetnick);
00668         fdbe->uploaded = now;
00669         fdbe->size = st.st_size;
00670         if (S_ISDIR(st.st_mode))
00671           fdbe->stat |= FILE_DIR;
00672         filedb_addfile(fdb, fdbe);
00673       } else if (fdbe->size != st.st_size) {
00674         /* update size if needed */
00675         fdbe->size = st.st_size;
00676         filedb_updatefile(fdb, fdbe->pos, fdbe, UPDATE_HEADER);
00677       }
00678       free_fdbe(&fdbe);
00679     }
00680     dd = readdir(dir);
00681   }
00682   if (name)
00683     my_free(name);
00684   closedir(dir);
00685 
00686   /*
00687    * SECOND: make sure every db file is real
00688    */
00689   filedb_readtop(fdb, NULL);
00690   fdbe = filedb_getfile(fdb, ftell(fdb), GET_FILENAME);
00691   while (fdbe) {
00692     where = ftell(fdb);
00693     if (!(fdbe->stat & FILE_UNUSED) && !(fdbe->stat & FILE_ISLINK) &&
00694         fdbe->filename) {
00695       s = nmalloc(strlen(path) + 1 + strlen(fdbe->filename) + 1);
00696       sprintf(s, "%s/%s", path, fdbe->filename);
00697       if (stat(s, &st) != 0)
00698         /* gone file */
00699         filedb_delfile(fdb, fdbe->pos);
00700       my_free(s);
00701     }
00702     free_fdbe(&fdbe);
00703     fdbe = filedb_getfile(fdb, where, GET_FILENAME);
00704   }
00705 
00706   /*
00707    * THIRD: optimise database
00708    *
00709    * Instead of sorting, we only clean up the db, because sorting is now
00710    * done on-the-fly when we display the file list.
00711    */
00712   if (sort)
00713     filedb_cleanup(fdb);        /* Cleanup DB           */
00714   filedb_timestamp(fdb);        /* Write new timestamp  */
00715 }

Here is the call graph for this function:

Here is the caller graph for this function:

static int filedb_writetop ( FILE *  fdb,
filedb_top fdbt 
) [static]

Definition at line 174 of file filedb3.c.

Referenced by filedb_initdb(), filedb_open(), and filedb_timestamp().

00175 {
00176   fseek(fdb, 0L, SEEK_SET);
00177   fwrite(fdbt, 1, sizeof(filedb_top), fdb);
00178   return 1;
00179 }

Here is the caller graph for this function:

static void free_fdbe ( filedb_entry **  fdbe  )  [static]

Definition at line 79 of file filedb3.c.

References my_free.

Referenced by _filedb_matchfile(), _filedb_updatefile(), cmd_desc(), cmd_hide(), cmd_ln(), cmd_mkdir(), cmd_mv_cp(), cmd_reget_get(), cmd_rm(), cmd_rmdir(), cmd_share(), cmd_unhide(), cmd_unshare(), convert_old_files(), convert_version1(), convert_version2(), filedb_add(), filedb_change(), filedb_cleanup(), filedb_findempty(), filedb_getdesc(), filedb_getdirs(), filedb_getfiles(), filedb_getgots(), filedb_getlink(), filedb_getowner(), filedb_ls(), filedb_mergeempty(), filedb_setdesc(), filedb_setlink(), filedb_setowner(), filedb_update(), files_reget(), incr_file_gots(), remote_filereq(), and resolve_dir().

00080 {
00081   if (!fdbe || !*fdbe)
00082     return;
00083   if ((*fdbe)->filename)
00084     my_free((*fdbe)->filename);
00085   if ((*fdbe)->desc)
00086     my_free((*fdbe)->desc);
00087   if ((*fdbe)->sharelink)
00088     my_free((*fdbe)->sharelink);
00089   if ((*fdbe)->chan)
00090     my_free((*fdbe)->chan);
00091   if ((*fdbe)->uploader)
00092     my_free((*fdbe)->uploader);
00093   if ((*fdbe)->flags_req)
00094     my_free((*fdbe)->flags_req);
00095   my_free(*fdbe);
00096 }

Here is the caller graph for this function:

static void lockfile ( FILE *  fdb  )  [static]

Definition at line 127 of file filedb3.c.

Referenced by convert_old_db(), convert_old_files(), and filedb_open().

00128 {
00129   struct flock fl;
00130 
00131   fl.l_type = F_WRLCK;
00132   fl.l_start = 0;
00133   fl.l_whence = SEEK_SET;
00134   fl.l_len = 0;
00135   fcntl(fileno(fdb), F_SETLKW, &fl);
00136 }

Here is the caller graph for this function:

static char* make_point_path ( char *  path  )  [static]

Definition at line 720 of file filedb3.c.

References malloc_strcpy, and NULL.

Referenced by filedb_open().

00721 {
00722   char *s2 = NULL, *p = NULL;
00723 
00724   malloc_strcpy(s2, path);
00725   if (s2[strlen(s2) - 1] == '/')
00726     s2[strlen(s2) - 1] = 0;     /* remove trailing '/' */
00727   p = s2;
00728   while (*p++)
00729     if (*p == '/')
00730       *p = '.';
00731   return s2;
00732 }

Here is the caller graph for this function:

static void remote_filereq ( int  idx,
char *  from,
char *  file 
) [static]

Definition at line 1021 of file filedb3.c.

References botnet_send_filereject, botnet_send_filesend, botnetnick, copy_to_tmp, copyfile, dcc, dcc_total, dccdir, FILE_DIR, FILE_HIDDEN, FILE_SHARE, filedb_close(), filedb_matchfile, filedb_open(), filedb_readtop(), FILES_DIRDNE, FILES_FILEDNE, FILES_NOSHARE, FILES_REMOTE, FILES_REMOTEREQ, FILES_SENDERR, free_fdbe(), getmyip, iptolong, LOG_FILES, malloc_strcpy, my_free, nmalloc, NULL, putlog, raw_dcc_send(), simple_sprintf, filedb_entry::stat, tempdir, and wipe_tmp_filename().

01022 {
01023   char *p = NULL, *what = NULL, *dir = NULL,
01024     *s1 = NULL, *reject = NULL, *s = NULL;
01025   FILE *fdb = NULL;
01026   int i = 0;
01027   filedb_entry *fdbe = NULL;
01028 
01029   malloc_strcpy(what, file);
01030   p = strrchr(what, '/');
01031   if (p) {
01032     *p = 0;
01033     malloc_strcpy(dir, what);
01034     strcpy(what, p + 1);
01035   } else {
01036     malloc_strcpy(dir, "");
01037   }
01038   fdb = filedb_open(dir, 0);
01039   if (!fdb) {
01040     reject = FILES_DIRDNE;
01041   } else {
01042     filedb_readtop(fdb, NULL);
01043     fdbe = filedb_matchfile(fdb, ftell(fdb), what);
01044     filedb_close(fdb);
01045     if (!fdbe) {
01046       reject = FILES_FILEDNE;
01047     } else {
01048       if ((!(fdbe->stat & FILE_SHARE)) ||
01049           (fdbe->stat & (FILE_HIDDEN | FILE_DIR)))
01050         reject = FILES_NOSHARE;
01051       else {
01052         s1 = nmalloc(strlen(dccdir) + strlen(dir) + strlen(what) + 2);
01053         /* Copy to /tmp if needed */
01054         sprintf(s1, "%s%s%s%s", dccdir, dir, dir[0] ? "/" : "", what);
01055         if (copy_to_tmp) {
01056           s = nmalloc(strlen(tempdir) + strlen(what) + 1);
01057           sprintf(s, "%s%s", tempdir, what);
01058           copyfile(s1, s);
01059         } else
01060           s = s1;
01061         i = raw_dcc_send(s, "*remote", FILES_REMOTE, s);
01062         if (i > 0) {
01063           wipe_tmp_filename(s, -1);
01064           reject = FILES_SENDERR;
01065         }
01066         if (s1 != s)
01067           my_free(s);
01068         my_free(s1);
01069       }
01070       free_fdbe(&fdbe);
01071     }
01072   }
01073   s1 = nmalloc(strlen(botnetnick) + strlen(dir) + strlen(what) + 3);
01074   simple_sprintf(s1, "%s:%s/%s", botnetnick, dir, what);
01075   if (reject) {
01076     botnet_send_filereject(idx, s1, from, reject);
01077     my_free(s1);
01078     my_free(what);
01079     my_free(dir);
01080     return;
01081   }
01082   /* Grab info from dcc struct and bounce real request across net */
01083   i = dcc_total - 1;
01084   s = nmalloc(40);              /* Enough? */
01085   simple_sprintf(s, "%d %u %d", iptolong(getmyip()), dcc[i].port,
01086                  dcc[i].u.xfer->length);
01087   botnet_send_filesend(idx, s1, from, s);
01088   putlog(LOG_FILES, "*", FILES_REMOTEREQ, dir, dir[0] ? "/" : "", what);
01089   my_free(s1);
01090   my_free(s);
01091   my_free(what);
01092   my_free(dir);
01093 }

Here is the call graph for this function:

static void unlockfile ( FILE *  f  )  [static]

Definition at line 140 of file filedb3.c.

Referenced by convert_old_db(), convert_old_files(), filedb_close(), and filedb_open().

00141 {
00142   struct flock fl;
00143 
00144   fl.l_type = F_UNLCK;
00145   fl.l_start = 0;
00146   fl.l_whence = SEEK_SET;
00147   fl.l_len = 0;
00148   fcntl(fileno(f), F_SETLKW, &fl);
00149 }

Here is the caller graph for this function:


Variable Documentation

int count = 0 [static]

Generated on 7 Sep 2016 for Eggdrop by  doxygen 1.6.1