home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / linux / backup / star-1.3.1.tar.gz / star-1.3.1.tar / star-1.3.1 / star / append.c next >
C/C++ Source or Header  |  2001-04-07  |  4KB  |  182 lines

  1. /* @(#)append.c    1.12 01/04/07 Copyright 1992 J. Schilling */
  2. #ifndef lint
  3. static    char sccsid[] =
  4.     "@(#)append.c    1.12 01/04/07 Copyright 1992 J. Schilling";
  5. #endif
  6. /*
  7.  *    Routines used to append files to an existing 
  8.  *    tape archive
  9.  *
  10.  *    Copyright (c) 1992 J. Schilling
  11.  */
  12. /*
  13.  * This program is free software; you can redistribute it and/or modify
  14.  * it under the terms of the GNU General Public License as published by
  15.  * the Free Software Foundation; either version 2, or (at your option)
  16.  * any later version.
  17.  *
  18.  * This program is distributed in the hope that it will be useful,
  19.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.  * GNU General Public License for more details.
  22.  *
  23.  * You should have received a copy of the GNU General Public License
  24.  * along with this program; see the file COPYING.  If not, write to
  25.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  26.  */
  27.  
  28. #include <mconfig.h>
  29. #include <stdio.h>
  30. #include <standard.h>
  31. #include <schily.h>
  32. #include "star.h"
  33. #include "starsubs.h"
  34.  
  35. extern    BOOL    debug;
  36. extern    BOOL    cflag;
  37. extern    BOOL    uflag;
  38. extern    BOOL    rflag;
  39.  
  40. /*
  41.  * XXX We should try to share the hash code with lhash.c
  42.  */
  43. static struct h_elem {
  44.     struct h_elem *h_next;
  45.     Ulong        h_time;
  46.     short        h_len;
  47.     char             h_data[1];            /* Variable size. */
  48. } **h_tab;
  49.  
  50. static    unsigned    h_size;
  51. LOCAL    int        cachesize;
  52.  
  53. EXPORT    void    skipall        __PR((void));
  54. LOCAL    void    hash_new    __PR((unsigned size));
  55. LOCAL    struct h_elem *    uhash_lookup    __PR((FINFO *info));
  56. LOCAL    void    hash_add    __PR((FINFO *info));
  57. EXPORT    BOOL    update_newer    __PR((FINFO *info));
  58. LOCAL    int    hashval        __PR((Uchar *str, Uint maxsize));
  59.  
  60. EXPORT void
  61. skipall()
  62. {
  63.         FINFO    finfo;
  64.         TCB    tb;
  65.         char    name[PATH_MAX+1];
  66.     register TCB     *ptb = &tb;
  67.  
  68.     if (uflag)
  69.         hash_new(100);
  70.  
  71.     fillbytes((char *)&finfo, sizeof(finfo), '\0');
  72.  
  73.     finfo.f_tcb = ptb;
  74.     for (;;) {
  75.         if (get_tcb(ptb) == EOF) {
  76.             if (debug)
  77.                 error("used %d bytes for update cache.\n",
  78.                             cachesize);
  79.             return;
  80.         }
  81.         finfo.f_name = name;
  82.         if (tcb_to_info(ptb, &finfo) == EOF)
  83.             return;
  84.  
  85.         if (debug)
  86.             printf("R %s\n", finfo.f_name);
  87.         if (uflag)
  88.             hash_add(&finfo);
  89.  
  90.         void_file(&finfo);
  91.     }
  92. }
  93.  
  94. #include <strdefs.h>
  95.  
  96. LOCAL void
  97. hash_new(size)
  98.     unsigned    size;
  99. {
  100.     register    int    i;
  101.  
  102.     h_size = size;
  103.     h_tab = (struct h_elem **)__malloc(size * sizeof (struct h_elem *));
  104.     for (i=0; i<size; i++) h_tab[i] = 0;
  105.  
  106.     cachesize += size * sizeof (struct h_elem *);
  107. }
  108.  
  109. LOCAL struct h_elem *
  110. uhash_lookup(info)
  111.     FINFO    *info;
  112. {
  113.     register char        *name = info->f_name;
  114.     register struct h_elem *hp;
  115.     register int        hv;
  116.  
  117.     hv = hashval((unsigned char *)name, h_size);
  118.     for (hp = h_tab[hv]; hp; hp=hp->h_next) {
  119.         if (streql(name, hp->h_data))
  120.             return (hp);
  121.     }
  122.     return (0);
  123. }
  124.  
  125. LOCAL void
  126. hash_add(info)
  127.     FINFO    *info;
  128. {
  129.     register    int    len;
  130.     register struct h_elem    *hp;
  131.     register    int    hv;
  132.  
  133.     /*
  134.      * XXX nsec beachten wenn im Archiv!
  135.      */
  136.     if ((hp = uhash_lookup(info)) != 0) {
  137.         if (hp->h_time < info->f_mtime)
  138.             hp->h_time = info->f_mtime;
  139.         return;
  140.     }
  141.  
  142.     len = strlen(info->f_name);
  143.     hp = __malloc((unsigned)len + sizeof (struct h_elem));
  144.     cachesize += len + sizeof (struct h_elem);
  145.     strcpy(hp->h_data, info->f_name);
  146.     hp->h_time = info->f_mtime;
  147.     hv = hashval((unsigned char *)info->f_name, h_size);
  148.     hp->h_next = h_tab[hv];
  149.     h_tab[hv] = hp;
  150. }
  151.  
  152. EXPORT BOOL
  153. update_newer(info)
  154.     FINFO    *info;
  155. {
  156.     register struct h_elem *hp;
  157.  
  158.     /*
  159.      * XXX nsec beachten wenn im Archiv!
  160.      */
  161.     if ((hp = uhash_lookup(info)) != 0) {
  162.         if (info->f_mtime > hp->h_time)
  163.             return (TRUE);
  164.         return (FALSE);
  165.     }
  166.     return (TRUE);
  167. }
  168.  
  169. LOCAL int
  170. hashval(str, maxsize)
  171.     register Uchar *str;
  172.          Uint    maxsize;
  173. {
  174.     register int    sum = 0;
  175.     register int    i;
  176.     register int    c;
  177.  
  178.     for (i=0; (c = *str++) != '\0'; i++)
  179.         sum ^= (c << (i&7));
  180.     return sum % maxsize;
  181. }
  182.