1 /* cp-hash.c -- file copying (hash search routines)
2 Copyright (C) 89, 90, 91, 95, 96, 97, 1998 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)
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). */
23 # include <inttypes.h>
26 #include <sys/types.h>
35 char *node
; /* Path name, or &new_file for new inodes. */
36 struct entry
*coll_link
; /* 0 = entry not occupied. */
41 unsigned modulus
; /* Size of the `hash' pointer vector. */
42 struct entry
*entry_tab
; /* Pointer to dynamically growing vector. */
43 unsigned entry_tab_size
; /* Size of current `entry_tab' allocation. */
44 unsigned first_free_entry
; /* Index in `entry_tab'. */
45 struct entry
*hash
[1]; /* Vector of pointers in `entry_tab'. */
51 /* Add PATH to the list of files that we have created.
52 Return 0 if successful, 1 if not. */
55 remember_created (const char *path
)
59 if (stat (path
, &sb
) < 0)
61 error (0, errno
, "%s", path
);
65 hash_insert (sb
.st_ino
, sb
.st_dev
, &new_file
);
69 /* Add path NODE, copied from inode number INO and device number DEV,
70 to the list of files we have copied.
71 Return NULL if inserted, otherwise non-NULL. */
74 remember_copied (const char *node
, ino_t ino
, dev_t dev
)
76 return hash_insert (ino
, dev
, node
);
79 /* Allocate space for the hash structures, and set the global
80 variable `htab' to point to it. The initial hash module is specified in
81 MODULUS, and the number of entries are specified in ENTRY_TAB_SIZE. (The
82 hash structure will be rebuilt when ENTRY_TAB_SIZE entries have been
83 inserted, and MODULUS and ENTRY_TAB_SIZE in the global `htab' will be
87 hash_init (unsigned int modulus
, unsigned int entry_tab_size
)
91 htab_r
= (struct htab
*)
92 xmalloc (sizeof (struct htab
) + sizeof (struct entry
*) * modulus
);
94 htab_r
->entry_tab
= (struct entry
*)
95 xmalloc (sizeof (struct entry
) * entry_tab_size
);
97 htab_r
->modulus
= modulus
;
98 htab_r
->entry_tab_size
= entry_tab_size
;
104 /* Reset the hash structure in the global variable `htab' to
105 contain no entries. */
113 htab
->first_free_entry
= 0;
116 for (i
= htab
->modulus
; i
> 0; i
--)
120 /* Insert path NODE, copied from inode number INO and device number DEV,
121 into the hash structure HTAB, if not already present.
122 Return NULL if inserted, otherwise non-NULL. */
125 hash_insert2 (struct htab
*ht
, ino_t ino
, dev_t dev
, const char *node
)
127 struct entry
**hp
, *ep2
, *ep
;
129 /* The cast to uintmax_t prevents negative remainders if ino is negative. */
130 hp
= &ht
->hash
[(uintmax_t) ino
% ht
->modulus
];
140 /* Search for an entry with the same data. */
144 if (ep
->ino
== ino
&& ep
->dev
== dev
)
145 return ep
->node
; /* Found an entry with the same data. */
150 /* Did not find it. */
154 ep
= *hp
= &ht
->entry_tab
[ht
->first_free_entry
++];
157 ep
->node
= (char *) node
;
158 ep
->coll_link
= ep2
; /* ep2 is NULL if not collision. */
163 /* Insert path NODE, copied from inode number INO and device number DEV,
164 into the hash structure in the global variable `htab', if an entry with
165 the same inode and device was not found already.
166 Return NULL if inserted, otherwise non-NULL. */
169 hash_insert (ino_t ino
, dev_t dev
, const char *node
)
171 struct htab
*htab_r
= htab
;
173 if (htab_r
->first_free_entry
>= htab_r
->entry_tab_size
)
178 unsigned entry_tab_size
;
180 /* Increase the number of hash entries, and re-hash the data.
181 The method of shrinking and increasing is made to compactify
182 the heap. If twice as much data would be allocated
183 straightforwardly, we would never re-use a byte of memory. */
185 /* Let htab shrink. Keep only the header, not the pointer vector. */
187 htab_r
= (struct htab
*)
188 xrealloc ((char *) htab_r
, sizeof (struct htab
));
190 modulus
= 2 * htab_r
->modulus
;
191 entry_tab_size
= 2 * htab_r
->entry_tab_size
;
193 /* Increase the number of possible entries. */
195 htab_r
->entry_tab
= (struct entry
*)
196 xrealloc ((char *) htab_r
->entry_tab
,
197 sizeof (struct entry
) * entry_tab_size
);
199 /* Increase the size of htab again. */
201 htab_r
= (struct htab
*)
202 xrealloc ((char *) htab_r
,
203 sizeof (struct htab
) + sizeof (struct entry
*) * modulus
);
205 htab_r
->modulus
= modulus
;
206 htab_r
->entry_tab_size
= entry_tab_size
;
209 i
= htab_r
->first_free_entry
;
211 /* Make the increased hash table empty. The entries are still
212 available in htab->entry_tab. */
216 /* Go through the entries and install them in the pointer vector
217 htab->hash. The items are actually inserted in htab->entry_tab at
218 the position where they already are. The htab->coll_link need
219 however be updated. Could be made a little more efficient. */
221 for (ep
= htab_r
->entry_tab
; i
> 0; i
--)
223 hash_insert2 (htab_r
, ep
->ino
, ep
->dev
, ep
->node
);
228 return hash_insert2 (htab_r
, ino
, dev
, node
);