(forget_all): Use hash_free, not hash_clear.
[platform/upstream/coreutils.git] / src / cp-hash.c
1 /* cp-hash.c  -- file copying (hash search routines)
2    Copyright (C) 89, 90, 91, 1995-2001 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 Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18    Written by Torbjorn Granlund, Sweden (tege@sics.se).
19    Rewritten to use lib/hash.c by Jim Meyering.  */
20
21 #include <config.h>
22
23 #if HAVE_INTTYPES_H
24 # include <inttypes.h>
25 #endif
26 #include <stdio.h>
27 #include <sys/types.h>
28 #include "system.h"
29
30 #include "same.h"
31 #include "quote.h"
32 #include "hash.h"
33 #include "error.h"
34 #include "cp-hash.h"
35
36 /* Use ST_DEV and ST_INO as the key, FILENAME as the value.
37    These are used e.g., in copy.c to associate the destination path with
38    the source device/inode pair so that if we encounter a matching dev/ino
39    pair in the source tree we can arrange to create a hard link between
40    the corresponding names in the destination tree.  */
41 struct Src_to_dest
42 {
43   ino_t st_ino;
44   dev_t st_dev;
45   /* Destination path name (of non-directory or pre-existing directory)
46      corresponding to the dev/ino of a copied file, or the destination path
47      name corresponding to a dev/ino pair for a newly-created directory. */
48   char const* name;
49 };
50
51 /* This table maps source dev/ino to destination file name.
52    We use it to preserve hard links when copying.  */
53 static struct hash_table *src_to_dest;
54
55 /* Initial size of the above hash table.  */
56 #define INITIAL_TABLE_SIZE 103
57
58 static unsigned int
59 src_to_dest_hash (void const *x, unsigned int table_size)
60 {
61   struct Src_to_dest const *p = x;
62
63   /* Ignoring the device number here should be fine.  */
64   /* The cast to uintmax_t prevents negative remainders
65      if st_ino is negative.  */
66   return (uintmax_t) p->st_ino % table_size;
67 }
68
69 /* Compare two Src_to_dest entries.
70    Return true if their keys are judged `equal'.  */
71 static bool
72 src_to_dest_compare (void const *x, void const *y)
73 {
74   struct Src_to_dest const *a = x;
75   struct Src_to_dest const *b = y;
76   return SAME_INODE (*a, *b) ? true : false;
77 }
78
79 static void
80 src_to_dest_free (void *x)
81 {
82   struct Src_to_dest *a = x;
83   free ((char *) (a->name));
84   free (x);
85 }
86
87 /* Add PATH to the list of files that we have created.
88    Return 1 if we can't stat PATH, otherwise 0.  */
89
90 int
91 remember_created (const char *path)
92 {
93   struct stat sb;
94
95   if (stat (path, &sb) < 0)
96     {
97       error (0, errno, "%s", quote (path));
98       return 1;
99     }
100
101   remember_copied (path, sb.st_ino, sb.st_dev);
102   return 0;
103 }
104
105 /* Add path NAME, copied from inode number INO and device number DEV,
106    to the list of files we have copied.
107    Return NULL if inserted, otherwise non-NULL. */
108
109 char *
110 remember_copied (const char *name, ino_t ino, dev_t dev)
111 {
112   struct Src_to_dest *ent;
113   struct Src_to_dest *ent_from_table;
114
115   ent = (struct Src_to_dest *) xmalloc (sizeof *ent);
116   ent->name = xstrdup (name);
117   ent->st_ino = ino;
118   ent->st_dev = dev;
119
120   ent_from_table = hash_insert (src_to_dest, ent);
121   if (ent_from_table == NULL)
122     {
123       /* Insertion failed due to lack of memory.  */
124       xalloc_die ();
125     }
126
127   /* Determine whether there was already an entry in the table
128      with a matching key.  If so, free ENT (it wasn't inserted) and
129      return the `name' from the table entry.  */
130   if (ent_from_table != ent)
131     {
132       src_to_dest_free (ent);
133       return (char *) ent_from_table->name;
134     }
135
136   /* New key;  insertion succeeded.  */
137   return NULL;
138 }
139
140 /* Initialize the hash table.  */
141 void
142 hash_init (void)
143 {
144   src_to_dest = hash_initialize (INITIAL_TABLE_SIZE, NULL,
145                                  src_to_dest_hash,
146                                  src_to_dest_compare,
147                                  src_to_dest_free);
148   if (src_to_dest == NULL)
149     xalloc_die ();
150 }
151
152 /* Reset the hash structure in the global variable `htab' to
153    contain no entries.  */
154
155 void
156 forget_all (void)
157 {
158   hash_free (src_to_dest);
159 }