tests: exercise tests new "==" operator
[coreutils.git] / src / cp-hash.c
blob31bee713f4cc5625ac6dfcd8dc5e56f7ff2437c7
1 /* cp-hash.c -- file copying (hash search routines)
2 Copyright (C) 1989-1991, 1995-2011 Free Software Foundation, Inc.
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 3 of the License, or
7 (at your option) any later version.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 Written by Torbjorn Granlund, Sweden (tege@sics.se).
18 Rewritten to use lib/hash.c by Jim Meyering. */
20 #include <config.h>
22 #include <sys/types.h>
23 #include "system.h"
25 #include "same.h"
26 #include "hash.h"
27 #include "cp-hash.h"
29 /* Use ST_DEV and ST_INO as the key, FILENAME as the value.
30 These are used e.g., in copy.c to associate the destination name with
31 the source device/inode pair so that if we encounter a matching dev/ino
32 pair in the source tree we can arrange to create a hard link between
33 the corresponding names in the destination tree. */
34 struct Src_to_dest
36 ino_t st_ino;
37 dev_t st_dev;
38 /* Destination file name (of non-directory or pre-existing directory)
39 corresponding to the dev/ino of a copied file, or the destination file
40 name corresponding to a dev/ino pair for a newly-created directory. */
41 char *name;
44 /* This table maps source dev/ino to destination file name.
45 We use it to preserve hard links when copying. */
46 static Hash_table *src_to_dest;
48 /* Initial size of the above hash table. */
49 #define INITIAL_TABLE_SIZE 103
51 static size_t
52 src_to_dest_hash (void const *x, size_t table_size)
54 struct Src_to_dest const *p = x;
56 /* Ignoring the device number here should be fine. */
57 /* The cast to uintmax_t prevents negative remainders
58 if st_ino is negative. */
59 return (uintmax_t) p->st_ino % table_size;
62 /* Compare two Src_to_dest entries.
63 Return true if their keys are judged `equal'. */
64 static bool
65 src_to_dest_compare (void const *x, void const *y)
67 struct Src_to_dest const *a = x;
68 struct Src_to_dest const *b = y;
69 return SAME_INODE (*a, *b) ? true : false;
72 static void
73 src_to_dest_free (void *x)
75 struct Src_to_dest *a = x;
76 free (a->name);
77 free (x);
80 /* Remove the entry matching INO/DEV from the table
81 that maps source ino/dev to destination file name. */
82 extern void
83 forget_created (ino_t ino, dev_t dev)
85 struct Src_to_dest probe;
86 struct Src_to_dest *ent;
88 probe.st_ino = ino;
89 probe.st_dev = dev;
90 probe.name = NULL;
92 ent = hash_delete (src_to_dest, &probe);
93 if (ent)
94 src_to_dest_free (ent);
97 /* If INO/DEV correspond to an already-copied source file, return the
98 name of the corresponding destination file. Otherwise, return NULL. */
100 extern char *
101 src_to_dest_lookup (ino_t ino, dev_t dev)
103 struct Src_to_dest ent;
104 struct Src_to_dest const *e;
105 ent.st_ino = ino;
106 ent.st_dev = dev;
107 e = hash_lookup (src_to_dest, &ent);
108 return e ? e->name : NULL;
111 /* Add file NAME, copied from inode number INO and device number DEV,
112 to the list of files we have copied.
113 Return NULL if inserted, otherwise non-NULL. */
115 extern char *
116 remember_copied (const char *name, ino_t ino, dev_t dev)
118 struct Src_to_dest *ent;
119 struct Src_to_dest *ent_from_table;
121 ent = xmalloc (sizeof *ent);
122 ent->name = xstrdup (name);
123 ent->st_ino = ino;
124 ent->st_dev = dev;
126 ent_from_table = hash_insert (src_to_dest, ent);
127 if (ent_from_table == NULL)
129 /* Insertion failed due to lack of memory. */
130 xalloc_die ();
133 /* Determine whether there was already an entry in the table
134 with a matching key. If so, free ENT (it wasn't inserted) and
135 return the `name' from the table entry. */
136 if (ent_from_table != ent)
138 src_to_dest_free (ent);
139 return (char *) ent_from_table->name;
142 /* New key; insertion succeeded. */
143 return NULL;
146 /* Initialize the hash table. */
147 extern void
148 hash_init (void)
150 src_to_dest = hash_initialize (INITIAL_TABLE_SIZE, NULL,
151 src_to_dest_hash,
152 src_to_dest_compare,
153 src_to_dest_free);
154 if (src_to_dest == NULL)
155 xalloc_die ();
158 /* Reset the hash structure in the global variable `htab' to
159 contain no entries. */
161 extern void
162 forget_all (void)
164 hash_free (src_to_dest);