home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 2 / FFMCD02.bin / useful / dist / gnu / fileutils / fileutils-3.9-amiga / src / cp-hash.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-06  |  5.8 KB  |  229 lines

  1. /* cp-hash.c  -- file copying (hash search routines)
  2.    Copyright (C) 1989, 1990, 1991 Free Software Foundation.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18.    Written by Torbjorn Granlund, Sweden (tege@sics.se). */
  19.  
  20. #ifdef HAVE_CONFIG_H
  21. #if defined (CONFIG_BROKETS)
  22. /* We use <config.h> instead of "config.h" so that a compilation
  23.    using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
  24.    (which it would do because it found this file in $srcdir).  */
  25. #include <config.h>
  26. #else
  27. #include "config.h"
  28. #endif
  29. #endif
  30.  
  31. #include <stdio.h>
  32. #include "cp.h"
  33.  
  34. char *hash_insert ();
  35. char *hash_insert2 ();
  36.  
  37. struct htab *htab;
  38. char new_file;
  39.  
  40. /* Add PATH to the list of files that we have created.
  41.    Return 0 if successful, 1 if not. */
  42.  
  43. int
  44. remember_created (path)
  45.      char *path;
  46. {
  47.   struct stat sb;
  48.  
  49.   if (stat (path, &sb) < 0)
  50.     {
  51.       error (0, errno, "%s", path);
  52.       return 1;
  53.     }
  54.  
  55.   hash_insert (sb.st_ino, sb.st_dev, &new_file);
  56.   return 0;
  57. }
  58.  
  59. /* Add path NODE, copied from inode number INO and device number DEV,
  60.    to the list of files we have copied.
  61.    Return NULL if inserted, otherwise non-NULL. */
  62.  
  63. char *
  64. remember_copied (node, ino, dev)
  65.      char *node;
  66.      ino_t ino;
  67.      dev_t dev;
  68. {
  69.   return hash_insert (ino, dev, node);
  70. }
  71.  
  72. /* Allocate space for the hash structures, and set the global
  73.    variable `htab' to point to it.  The initial hash module is specified in
  74.    MODULUS, and the number of entries are specified in ENTRY_TAB_SIZE.  (The
  75.    hash structure will be rebuilt when ENTRY_TAB_SIZE entries have been
  76.    inserted, and MODULUS and ENTRY_TAB_SIZE in the global `htab' will be
  77.    doubled.)  */
  78.  
  79. void
  80. hash_init (modulus, entry_tab_size)
  81.      unsigned modulus;
  82.      unsigned entry_tab_size;
  83. {
  84.   struct htab *htab_r;
  85.  
  86.   htab_r = (struct htab *)
  87.     xmalloc (sizeof (struct htab) + sizeof (struct entry *) * modulus);
  88.  
  89.   htab_r->entry_tab = (struct entry *)
  90.     xmalloc (sizeof (struct entry) * entry_tab_size);
  91.  
  92.   htab_r->modulus = modulus;
  93.   htab_r->entry_tab_size = entry_tab_size;
  94.   htab = htab_r;
  95.  
  96.   forget_all ();
  97. }
  98.  
  99. /* Reset the hash structure in the global variable `htab' to
  100.    contain no entries.  */
  101.  
  102. void
  103. forget_all ()
  104. {
  105.   int i;
  106.   struct entry **p;
  107.  
  108.   htab->first_free_entry = 0;
  109.  
  110.   p = htab->hash;
  111.   for (i = htab->modulus; i > 0; i--)
  112.     *p++ = NULL;
  113. }
  114.  
  115. /* Insert path NODE, copied from inode number INO and device number DEV,
  116.    into the hash structure in the global variable `htab', if an entry with
  117.    the same inode and device was not found already.
  118.    Return NULL if inserted, otherwise non-NULL. */
  119.  
  120. char *
  121. hash_insert (ino, dev, node)
  122.      ino_t ino;
  123.      dev_t dev;
  124.      char *node;
  125. {
  126.   struct htab *htab_r = htab;
  127.  
  128.   if (htab_r->first_free_entry >= htab_r->entry_tab_size)
  129.     {
  130.       int i;
  131.       struct entry *ep;
  132.       unsigned modulus;
  133.       unsigned entry_tab_size;
  134.  
  135.       /* Increase the number of hash entries, and re-hash the data.
  136.      The method of shrinking and increasing is made to compactify
  137.      the heap.  If twice as much data would be allocated
  138.      straightforwardly, we would never re-use a byte of memory.  */
  139.  
  140.       /* Let htab shrink.  Keep only the header, not the pointer vector.  */
  141.  
  142.       htab_r = (struct htab *)
  143.     xrealloc ((char *) htab_r, sizeof (struct htab));
  144.  
  145.       modulus = 2 * htab_r->modulus;
  146.       entry_tab_size = 2 * htab_r->entry_tab_size;
  147.  
  148.       /* Increase the number of possible entries.  */
  149.  
  150.       htab_r->entry_tab = (struct entry *)
  151.     xrealloc ((char *) htab_r->entry_tab,
  152.           sizeof (struct entry) * entry_tab_size);
  153.  
  154.       /* Increase the size of htab again.  */
  155.  
  156.       htab_r = (struct htab *)
  157.     xrealloc ((char *) htab_r,
  158.           sizeof (struct htab) + sizeof (struct entry *) * modulus);
  159.  
  160.       htab_r->modulus = modulus;
  161.       htab_r->entry_tab_size = entry_tab_size;
  162.       htab = htab_r;
  163.  
  164.       i = htab_r->first_free_entry;
  165.  
  166.       /* Make the increased hash table empty.  The entries are still
  167.      available in htab->entry_tab.  */
  168.  
  169.       forget_all ();
  170.  
  171.       /* Go through the entries and install them in the pointer vector
  172.      htab->hash.  The items are actually inserted in htab->entry_tab at
  173.      the position where they already are.  The htab->coll_link need
  174.      however be updated.  Could be made a little more efficient.  */
  175.  
  176.       for (ep = htab_r->entry_tab; i > 0; i--)
  177.     {
  178.       hash_insert2 (htab_r, ep->ino, ep->dev, ep->node);
  179.       ep++;
  180.     }
  181.     }
  182.  
  183.   return hash_insert2 (htab_r, ino, dev, node);
  184. }
  185.  
  186. /* Insert path NODE, copied from inode number INO and device number DEV,
  187.    into the hash structure HTAB, if not already present.
  188.    Return NULL if inserted, otherwise non-NULL. */
  189.  
  190. char *
  191. hash_insert2 (htab, ino, dev, node)
  192.      struct htab *htab;
  193.      ino_t ino;
  194.      dev_t dev;
  195.      char *node;
  196. {
  197.   struct entry **hp, *ep2, *ep;
  198.   hp = &htab->hash[ino % htab->modulus];
  199.   ep2 = *hp;
  200.  
  201.   /* Collision?  */
  202.  
  203.   if (ep2 != NULL)
  204.     {
  205.       ep = ep2;
  206.  
  207.       /* Search for an entry with the same data.  */
  208.  
  209.       do
  210.     {
  211.       if (ep->ino == ino && ep->dev == dev)
  212.         return ep->node;    /* Found an entry with the same data.  */
  213.       ep = ep->coll_link;
  214.     }
  215.       while (ep != NULL);
  216.  
  217.       /* Did not find it.  */
  218.  
  219.     }
  220.  
  221.   ep = *hp = &htab->entry_tab[htab->first_free_entry++];
  222.   ep->ino = ino;
  223.   ep->dev = dev;
  224.   ep->node = node;
  225.   ep->coll_link = ep2;        /* ep2 is NULL if not collision.  */
  226.  
  227.   return NULL;
  228. }
  229.