コード例 #1
0
ファイル: filedb3.c プロジェクト: Protospace/protobot
static void filedb_setlink(char *dir, char *fn, char *link)
{
    filedb_entry *fdbe = NULL;
    FILE *fdb = NULL;

    fdb = filedb_open(dir, 0);
    if (!fdb)
        return;
    filedb_readtop(fdb, NULL);
    fdbe = filedb_matchfile(fdb, ftell(fdb), fn);
    if (fdbe) {
        /* Change existing one? */
        if ((fdbe->stat & FILE_DIR) || !fdbe->sharelink)
            return;
        if (!link || !link[0])
            filedb_delfile(fdb, fdbe->pos);
        else {
            my_free(fdbe->sharelink);
            malloc_strcpy(fdbe->sharelink, link);
            filedb_updatefile(fdb, fdbe->pos, fdbe, UPDATE_ALL);
        }
        free_fdbe(&fdbe);
        return;
    }

    fdbe = malloc_fdbe();
    malloc_strcpy(fdbe->uploader, botnetnick);
    malloc_strcpy(fdbe->filename, fn);
    malloc_strcpy(fdbe->sharelink, link);
    fdbe->uploaded = now;
    filedb_addfile(fdb, fdbe);
    free_fdbe(&fdbe);
    filedb_close(fdb);
}
コード例 #2
0
ファイル: filedb3.c プロジェクト: Protospace/protobot
/* Updates the specified filedb in several ways:
 *
 * 1. Adds all new files from the directory to the db.
 * 2. Removes all stale entries from the db.
 * 3. Optimises the db.
 */
static void filedb_update(char *path, FILE *fdb, int sort)
{
    struct dirent *dd = NULL;
    struct stat st;
    filedb_entry *fdbe = NULL;
    DIR *dir = NULL;
    long where = 0;
    char *name = NULL, *s = NULL;

    /*
     * FIRST: make sure every real file is in the database
     */
    dir = opendir(path);
    if (dir == NULL) {
        putlog(LOG_MISC, "*", FILES_NOUPDATE);
        return;
    }
    dd = readdir(dir);
    while (dd != NULL) {
        malloc_strcpy(name, dd->d_name);
        if (name[0] != '.') {
            s = nmalloc(strlen(path) + strlen(name) + 2);
            sprintf(s, "%s/%s", path, name);
            stat(s, &st);
            my_free(s);
            filedb_readtop(fdb, NULL);
            fdbe = filedb_matchfile(fdb, ftell(fdb), name);
            if (!fdbe) {
                /* new file! */
                fdbe = malloc_fdbe();
                malloc_strcpy(fdbe->filename, name);
                malloc_strcpy(fdbe->uploader, botnetnick);
                fdbe->uploaded = now;
                fdbe->size = st.st_size;
                if (S_ISDIR(st.st_mode))
                    fdbe->stat |= FILE_DIR;
                filedb_addfile(fdb, fdbe);
            } else if (fdbe->size != st.st_size) {
                /* update size if needed */
                fdbe->size = st.st_size;
                filedb_updatefile(fdb, fdbe->pos, fdbe, UPDATE_HEADER);
            }
            free_fdbe(&fdbe);
        }
        dd = readdir(dir);
    }
    if (name)
        my_free(name);
    closedir(dir);

    /*
     * SECOND: make sure every db file is real
     */
    filedb_readtop(fdb, NULL);
    fdbe = filedb_getfile(fdb, ftell(fdb), GET_FILENAME);
    while (fdbe) {
        where = ftell(fdb);
        if (!(fdbe->stat & FILE_UNUSED) && !(fdbe->stat & FILE_ISLINK) &&
                fdbe->filename) {
            s = nmalloc(strlen(path) + 1 + strlen(fdbe->filename) + 1);
            sprintf(s, "%s/%s", path, fdbe->filename);
            if (stat(s, &st) != 0)
                /* gone file */
                filedb_delfile(fdb, fdbe->pos);
            my_free(s);
        }
        free_fdbe(&fdbe);
        fdbe = filedb_getfile(fdb, where, GET_FILENAME);
    }

    /*
     * THIRD: optimise database
     *
     * Instead of sorting, we only clean up the db, because sorting is now
     * done on-the-fly when we display the file list.
     */
    if (sort)
        filedb_cleanup(fdb);        /* Cleanup DB           */
    filedb_timestamp(fdb);        /* Write new timestamp  */
}
コード例 #3
0
ファイル: filedb3.c プロジェクト: Protospace/protobot
/* Updates or creates entries and information in the filedb.
 *
 *   * If the new entry is the same size or smaller than the original
 *     one, we use the old position and just adjust the buffer length
 *     apropriately.
 *   * If the entry is completely _new_ or if the entry's new size is
 *     _bigger_ than the old one, we search for a new position which
 *     suits our needs.
 *
 * Note that the available space also includes the buffer.
 *
 * The file pointer will _always_ position directly after the updated
 * entry.
 */
static int _filedb_updatefile(FILE *fdb, long pos, filedb_entry *fdbe,
                              int update, char *file, int line)
{
    filedb_header fdh;
    int reposition = 0;
    int ndyntot, odyntot, nbuftot, obuftot;

    egg_bzero(&fdh, sizeof(filedb_header));
    fdh.uploaded = fdbe->uploaded;
    fdh.size = fdbe->size;
    fdh.stat = fdbe->stat;
    fdh.gots = fdbe->gots;

    /* Only add the buffer length if the buffer is not empty. Otherwise it
     * would result in lots of 1 byte entries which actually don't contain
     * any data.
     */
    if (fdbe->filename)
        fdh.filename_len = strlen(fdbe->filename) + 1;
    if (fdbe->desc)
        fdh.desc_len = strlen(fdbe->desc) + 1;
    if (fdbe->chan)
        fdh.chan_len = strlen(fdbe->chan) + 1;
    if (fdbe->uploader)
        fdh.uploader_len = strlen(fdbe->uploader) + 1;
    if (fdbe->flags_req)
        fdh.flags_req_len = strlen(fdbe->flags_req) + 1;
    if (fdbe->sharelink)
        fdh.sharelink_len = strlen(fdbe->sharelink) + 1;

    odyntot = fdbe->dyn_len;      /* Old length of dynamic data   */
    obuftot = fdbe->buf_len;      /* Old length of spare space    */
    ndyntot = filedb_tot_dynspace(fdh);   /* New length of dynamic data   */
    nbuftot = obuftot;

    if (fdbe->_type == TYPE_EXIST) {
        /* If we only update the header, we don't need to worry about
         * sizes and just use the old place (i.e. the place pointed
         * to by pos).
         */
        if (update < UPDATE_ALL) {
            /* Unless forced to it, we ignore new buffer sizes if we do not
             * run in UPDATE_ALL mode.
             */
            if (update != UPDATE_SIZE) {
                ndyntot = odyntot;
                nbuftot = obuftot;
            }
        } else {
            /* If we have a given/preferred position */
            if ((pos != POS_NEW) &&
                    /* and if our new size is smaller than the old size, we
                     * just adjust the buffer length and still use the same
                     * position
                     */
                    (ndyntot <= (odyntot + obuftot))) {
                nbuftot = (odyntot + obuftot) - ndyntot;
            } else {
                /* If we have an existing position, but the new entry doesn't
                 * fit into it's old home, we need to delete it before
                 * repositioning.
                 */
                if (pos != POS_NEW)
                    filedb_delfile(fdb, pos);
                reposition = 1;
            }
        }
    } else {
        fdbe->_type = TYPE_EXIST;   /* Update type                  */
        reposition = 1;
    }

    /* Search for a new home */
    if (reposition) {
        filedb_entry *n_fdbe;

        n_fdbe = filedb_findempty(fdb, filedb_tot_dynspace(fdh));
        fdbe->pos = pos = n_fdbe->pos;
        /* If we create a new entry (instead of using an existing one),
         * buf_len is zero
         */
        if (n_fdbe->buf_len > 0)
            /* Note: empty entries have dyn_len set to zero, so we only
             *       need to consider buf_len.
             */
            nbuftot = n_fdbe->buf_len - ndyntot;
        else
            nbuftot = 0;
        free_fdbe(&n_fdbe);
    }

    /* Set length of dynamic data and buffer */
    fdbe->dyn_len = ndyntot;
    fdbe->buf_len = fdh.buffer_len = nbuftot;

    /* Write header */
    fseek(fdb, pos, SEEK_SET);
    fwrite(&fdh, 1, sizeof(filedb_header), fdb);
    /* Write dynamic data */
    if (update == UPDATE_ALL) {
        if (fdbe->filename)
            fwrite(fdbe->filename, 1, fdh.filename_len, fdb);
        if (fdbe->desc)
            fwrite(fdbe->desc, 1, fdh.desc_len, fdb);
        if (fdbe->chan)
            fwrite(fdbe->chan, 1, fdh.chan_len, fdb);
        if (fdbe->uploader)
            fwrite(fdbe->uploader, 1, fdh.uploader_len, fdb);
        if (fdbe->flags_req)
            fwrite(fdbe->flags_req, 1, fdh.flags_req_len, fdb);
        if (fdbe->sharelink)
            fwrite(fdbe->sharelink, 1, fdh.sharelink_len, fdb);
    } else
        fseek(fdb, ndyntot, SEEK_CUR);      /* Skip over dynamic data */
    fseek(fdb, nbuftot, SEEK_CUR);        /* Skip over buffer       */
    return 0;
}
コード例 #4
0
ファイル: tclfiles.c プロジェクト: ArNz8o8/Fr3shness
static int tcl_mv_cp(Tcl_Interp *irp, int argc, char **argv, int copy)
{
  char *p, *fn = NULL, *oldpath = NULL, *s = NULL, *s1 = NULL;
  char *newfn = NULL, *newpath = NULL;
  int ok = 0, only_first, skip_this;
  FILE *fdb_old, *fdb_new;
  filedb_entry *fdbe_old, *fdbe_new;
  long where;

  BADARGS(3, 3, " oldfilepath newfilepath");

  malloc_strcpy(fn, argv[1]);
  p = strrchr(fn, '/');
  if (p != NULL) {
    *p = 0;
    malloc_strcpy(s, fn);
    strcpy(fn, p + 1);
    if (!resolve_dir("/", s, &oldpath, -1)) {
      /* Tcl can do * anything */
      Tcl_AppendResult(irp, "-1", NULL);        /* Invalid source */
      my_free(fn);
      my_free(oldpath);
      return TCL_OK;
    }
    my_free(s);
  } else
    malloc_strcpy(oldpath, "/");
  malloc_strcpy(s, argv[2]);
  if (!resolve_dir("/", s, &newpath, -1)) {
    /* Destination is not just a directory */
    p = strrchr(s, '/');
    if (!p) {
      malloc_strcpy(newfn, s);
      s[0] = 0;
    } else {
      *p = 0;
      malloc_strcpy(newfn, p + 1);
    }
    my_free(newpath);
    if (!resolve_dir("/", s, &newpath, -1)) {
      Tcl_AppendResult(irp, "-2", NULL);        /* Invalid desto */
      my_free(newpath);
      my_free(s);
      my_free(newfn);
      return TCL_OK;
    }
  } else
    malloc_strcpy(newfn, "");
  my_free(s);

  /* Stupidness checks */
  if ((!strcmp(oldpath, newpath)) && (!newfn[0] || !strcmp(newfn, fn))) {
    my_free(newfn);
    my_free(fn);
    my_free(oldpath);
    my_free(newpath);
    Tcl_AppendResult(irp, "-3", NULL);  /* Stupid copy to self */
    return TCL_OK;
  }
  /* Be aware of 'cp * this.file' possibility: ONLY COPY FIRST ONE */
  if ((strchr(fn, '?') || strchr(fn, '*')) && newfn[0])
    only_first = 1;
  else
    only_first = 0;

  fdb_old = filedb_open(oldpath, 0);
  if (!strcmp(oldpath, newpath))
    fdb_new = fdb_old;
  else
    fdb_new = filedb_open(newpath, 0);
  if (!fdb_old || !fdb_new) {
    my_free(newfn);
    my_free(fn);
    my_free(oldpath);
    my_free(newpath);
    if (fdb_old)
      filedb_close(fdb_old);
    else if (fdb_new)
      filedb_close(fdb_new);
    Tcl_AppendResult(irp, "-5", NULL);  /* DB access failed */
    return -1;
  }

  filedb_readtop(fdb_old, NULL);
  fdbe_old = filedb_matchfile(fdb_old, ftell(fdb_old), fn);
  if (!fdbe_old) {
    my_free(newfn);
    my_free(fn);
    my_free(oldpath);
    my_free(newpath);
    if (fdb_new != fdb_old)
      filedb_close(fdb_new);
    filedb_close(fdb_old);
    Tcl_AppendResult(irp, "-4", NULL);  /* No match */
    return -2;
  }
  while (fdbe_old) {
    where = ftell(fdb_old);
    skip_this = 0;
    if (!(fdbe_old->stat & (FILE_HIDDEN | FILE_DIR))) {
      s = nmalloc(strlen(dccdir) + strlen(oldpath)
                  + strlen(fdbe_old->filename) + 2);
      s1 = nmalloc(strlen(dccdir) + strlen(newpath)
                   + strlen(newfn[0] ? newfn : fdbe_old->filename) + 2);
      sprintf(s, "%s%s%s%s", dccdir, oldpath,
              oldpath[0] ? "/" : "", fdbe_old->filename);
      sprintf(s1, "%s%s%s%s", dccdir, newpath,
              newpath[0] ? "/" : "", newfn[0] ? newfn : fdbe_old->filename);
      if (!strcmp(s, s1)) {
        Tcl_AppendResult(irp, "-3", NULL);      /* Stupid copy to self */
        skip_this = 1;
      }
      /* Check for existence of file with same name in new dir */
      filedb_readtop(fdb_new, NULL);
      fdbe_new = filedb_matchfile(fdb_new, ftell(fdb_new),
                                  newfn[0] ? newfn : fdbe_old->filename);
      if (fdbe_new) {
        /* It's ok if the entry in the new dir is a normal file (we'll
         * just scrap the old entry and overwrite the file) -- but if
         * it's a directory, this file has to be skipped.
         */
        if (fdbe_new->stat & FILE_DIR)
          skip_this = 1;
        else
          filedb_delfile(fdb_new, fdbe_new->pos);
        free_fdbe(&fdbe_new);
      }
      if (!skip_this) {
        if ((fdbe_old->sharelink) ||
            ((copy ? copyfile(s, s1) : movefile(s, s1)) == 0)) {
          /* Raw file moved okay: create new entry for it */
          ok++;
          fdbe_new = malloc_fdbe();
          fdbe_new->stat = fdbe_old->stat;
          /* We don't have to worry about any entries to be
           * NULL, because malloc_strcpy takes care of that.
           */
          malloc_strcpy(fdbe_new->flags_req, fdbe_old->flags_req);
          malloc_strcpy(fdbe_new->chan, fdbe_old->chan);
          malloc_strcpy(fdbe_new->filename, fdbe_old->filename);
          malloc_strcpy(fdbe_new->desc, fdbe_old->desc);
          if (newfn[0])
            malloc_strcpy(fdbe_new->filename, newfn);
          malloc_strcpy(fdbe_new->uploader, fdbe_old->uploader);
          fdbe_new->uploaded = fdbe_old->uploaded;
          fdbe_new->size = fdbe_old->size;
          fdbe_new->gots = fdbe_old->gots;
          malloc_strcpy(fdbe_new->sharelink, fdbe_old->sharelink);
          filedb_addfile(fdb_new, fdbe_new);
          if (!copy)
            filedb_delfile(fdb_old, fdbe_old->pos);
          free_fdbe(&fdbe_new);
        }
      }
      my_free(s);
      my_free(s1);
    }
    free_fdbe(&fdbe_old);
    fdbe_old = filedb_matchfile(fdb_old, where, fn);
    if (ok && only_first) {
      free_fdbe(&fdbe_old);
    }
  }
  if (fdb_old != fdb_new)
    filedb_close(fdb_new);
  filedb_close(fdb_old);
  if (!ok)
    Tcl_AppendResult(irp, "-4", NULL);  /* No match */
  else {
    char x[30];

    sprintf(x, "%d", ok);
    Tcl_AppendResult(irp, x, NULL);
  }
  my_free(newfn);
  my_free(fn);
  my_free(oldpath);
  my_free(newpath);
  return TCL_OK;
}
コード例 #5
0
ファイル: tclfiles.c プロジェクト: Estella/eggdrop-1.7
static int tcl_rmdir(ClientData cd, Tcl_Interp *irp,
                     int argc, char *argv[])
{
  FILE *fdb;
  filedb_entry *fdbe;
  char *s = NULL, *t, *d, *p;

  BADARGS(2, 2, " dir");

  malloc_strcpy(s, argv[1]);
  if (s[strlen(s) - 1] == '/')
    s[strlen(s) - 1] = 0;
  p = strrchr(s, '/');
  if (p == NULL) {
    p = s;
    d = "";
  } else {
    *p = 0;
    p++;
    d = s;
  }

  fdb = filedb_open(d, 0);
  if (!fdb) {
    Tcl_AppendResult(irp, "1", NULL);
    my_free(s);
    return TCL_OK;
  }
  filedb_readtop(fdb, NULL);
  fdbe = filedb_matchfile(fdb, ftell(fdb), p);

  if (!fdbe) {
    Tcl_AppendResult(irp, "1", NULL);
    filedb_close(fdb);
    my_free(s);
    return TCL_OK;
  }
  if (!(fdbe->stat & FILE_DIR)) {
    Tcl_AppendResult(irp, "1", NULL);
    filedb_close(fdb);
    free_fdbe(&fdbe);
    my_free(s);
    return TCL_OK;
  }
  /* Erase '.filedb' and '.files' if they exist */
  t = nmalloc(strlen(dccdir) + strlen(d) + strlen(p) + 11);
  sprintf(t, "%s%s/%s/.filedb", dccdir, d, p);
  unlink(t);
  sprintf(t, "%s%s/%s/.files", dccdir, d, p);
  unlink(t);
  sprintf(t, "%s%s/%s", dccdir, d, p);
  my_free(s);
  if (rmdir(t) == 0) {
    filedb_delfile(fdb, fdbe->pos);
    filedb_close(fdb);
    free_fdbe(&fdbe);
    my_free(t);
    Tcl_AppendResult(irp, "0", NULL);
    return TCL_OK;
  }
  my_free(t);
  free_fdbe(&fdbe);
  filedb_close(fdb);
  Tcl_AppendResult(irp, "1", NULL);
  return TCL_OK;
}