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