home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 9 / FreshFishVol9-CD2.bin / bbs / gnu / fileutils-3.12-src.lha / fileutils-3.12 / src / cp-hash.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-02  |  5.5 KB  |  220 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. #include <config.h>
  21. #include <stdio.h>
  22. #include "cp.h"
  23. #include "safe-stat.h"
  24.  
  25. char *hash_insert ();
  26. char *hash_insert2 ();
  27.  
  28. struct htab *htab;
  29. char new_file;
  30.  
  31. /* Add PATH to the list of files that we have created.
  32.    Return 0 if successful, 1 if not. */
  33.  
  34. int
  35. remember_created (path)
  36.      char *path;
  37. {
  38.   struct stat sb;
  39.  
  40.   if (SAFE_STAT (path, &sb) < 0)
  41.     {
  42.       error (0, errno, "%s", path);
  43.       return 1;
  44.     }
  45.  
  46.   hash_insert (sb.st_ino, sb.st_dev, &new_file);
  47.   return 0;
  48. }
  49.  
  50. /* Add path NODE, copied from inode number INO and device number DEV,
  51.    to the list of files we have copied.
  52.    Return NULL if inserted, otherwise non-NULL. */
  53.  
  54. char *
  55. remember_copied (node, ino, dev)
  56.      char *node;
  57.      ino_t ino;
  58.      dev_t dev;
  59. {
  60.   return hash_insert (ino, dev, node);
  61. }
  62.  
  63. /* Allocate space for the hash structures, and set the global
  64.    variable `htab' to point to it.  The initial hash module is specified in
  65.    MODULUS, and the number of entries are specified in ENTRY_TAB_SIZE.  (The
  66.    hash structure will be rebuilt when ENTRY_TAB_SIZE entries have been
  67.    inserted, and MODULUS and ENTRY_TAB_SIZE in the global `htab' will be
  68.    doubled.)  */
  69.  
  70. void
  71. hash_init (modulus, entry_tab_size)
  72.      unsigned modulus;
  73.      unsigned entry_tab_size;
  74. {
  75.   struct htab *htab_r;
  76.  
  77.   htab_r = (struct htab *)
  78.     xmalloc (sizeof (struct htab) + sizeof (struct entry *) * modulus);
  79.  
  80.   htab_r->entry_tab = (struct entry *)
  81.     xmalloc (sizeof (struct entry) * entry_tab_size);
  82.  
  83.   htab_r->modulus = modulus;
  84.   htab_r->entry_tab_size = entry_tab_size;
  85.   htab = htab_r;
  86.  
  87.   forget_all ();
  88. }
  89.  
  90. /* Reset the hash structure in the global variable `htab' to
  91.    contain no entries.  */
  92.  
  93. void
  94. forget_all ()
  95. {
  96.   int i;
  97.   struct entry **p;
  98.  
  99.   htab->first_free_entry = 0;
  100.  
  101.   p = htab->hash;
  102.   for (i = htab->modulus; i > 0; i--)
  103.     *p++ = NULL;
  104. }
  105.  
  106. /* Insert path NODE, copied from inode number INO and device number DEV,
  107.    into the hash structure in the global variable `htab', if an entry with
  108.    the same inode and device was not found already.
  109.    Return NULL if inserted, otherwise non-NULL. */
  110.  
  111. char *
  112. hash_insert (ino, dev, node)
  113.      ino_t ino;
  114.      dev_t dev;
  115.      char *node;
  116. {
  117.   struct htab *htab_r = htab;
  118.  
  119.   if (htab_r->first_free_entry >= htab_r->entry_tab_size)
  120.     {
  121.       int i;
  122.       struct entry *ep;
  123.       unsigned modulus;
  124.       unsigned entry_tab_size;
  125.  
  126.       /* Increase the number of hash entries, and re-hash the data.
  127.      The method of shrinking and increasing is made to compactify
  128.      the heap.  If twice as much data would be allocated
  129.      straightforwardly, we would never re-use a byte of memory.  */
  130.  
  131.       /* Let htab shrink.  Keep only the header, not the pointer vector.  */
  132.  
  133.       htab_r = (struct htab *)
  134.     xrealloc ((char *) htab_r, sizeof (struct htab));
  135.  
  136.       modulus = 2 * htab_r->modulus;
  137.       entry_tab_size = 2 * htab_r->entry_tab_size;
  138.  
  139.       /* Increase the number of possible entries.  */
  140.  
  141.       htab_r->entry_tab = (struct entry *)
  142.     xrealloc ((char *) htab_r->entry_tab,
  143.           sizeof (struct entry) * entry_tab_size);
  144.  
  145.       /* Increase the size of htab again.  */
  146.  
  147.       htab_r = (struct htab *)
  148.     xrealloc ((char *) htab_r,
  149.           sizeof (struct htab) + sizeof (struct entry *) * modulus);
  150.  
  151.       htab_r->modulus = modulus;
  152.       htab_r->entry_tab_size = entry_tab_size;
  153.       htab = htab_r;
  154.  
  155.       i = htab_r->first_free_entry;
  156.  
  157.       /* Make the increased hash table empty.  The entries are still
  158.      available in htab->entry_tab.  */
  159.  
  160.       forget_all ();
  161.  
  162.       /* Go through the entries and install them in the pointer vector
  163.      htab->hash.  The items are actually inserted in htab->entry_tab at
  164.      the position where they already are.  The htab->coll_link need
  165.      however be updated.  Could be made a little more efficient.  */
  166.  
  167.       for (ep = htab_r->entry_tab; i > 0; i--)
  168.     {
  169.       hash_insert2 (htab_r, ep->ino, ep->dev, ep->node);
  170.       ep++;
  171.     }
  172.     }
  173.  
  174.   return hash_insert2 (htab_r, ino, dev, node);
  175. }
  176.  
  177. /* Insert path NODE, copied from inode number INO and device number DEV,
  178.    into the hash structure HTAB, if not already present.
  179.    Return NULL if inserted, otherwise non-NULL. */
  180.  
  181. char *
  182. hash_insert2 (htab, ino, dev, node)
  183.      struct htab *htab;
  184.      ino_t ino;
  185.      dev_t dev;
  186.      char *node;
  187. {
  188.   struct entry **hp, *ep2, *ep;
  189.   hp = &htab->hash[ino % htab->modulus];
  190.   ep2 = *hp;
  191.  
  192.   /* Collision?  */
  193.  
  194.   if (ep2 != NULL)
  195.     {
  196.       ep = ep2;
  197.  
  198.       /* Search for an entry with the same data.  */
  199.  
  200.       do
  201.     {
  202.       if (ep->ino == ino && ep->dev == dev)
  203.         return ep->node;    /* Found an entry with the same data.  */
  204.       ep = ep->coll_link;
  205.     }
  206.       while (ep != NULL);
  207.  
  208.       /* Did not find it.  */
  209.  
  210.     }
  211.  
  212.   ep = *hp = &htab->entry_tab[htab->first_free_entry++];
  213.   ep->ino = ino;
  214.   ep->dev = dev;
  215.   ep->node = node;
  216.   ep->coll_link = ep2;        /* ep2 is NULL if not collision.  */
  217.  
  218.   return NULL;
  219. }
  220.