Got rid of the code that sorted based on struct idev objects since
[rsync/rsync.git] / hlink.c
CommitLineData
6aae748e 1/*
0f78b815
WD
2 * Routines to support hard-linking.
3 *
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2002 Martin Pool <mbp@samba.org>
7 * Copyright (C) 2004, 2005, 2006 Wayne Davison
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
e7c67065
WD
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
0f78b815 22 */
dc5ddbcc
AT
23
24#include "rsync.h"
25
9a52223b 26extern int verbose;
82ad07c4 27extern int dry_run;
841d9436 28extern int do_xfers;
541b23d1 29extern int link_dest;
417c99f6 30extern int make_backups;
c905bf37 31extern int protocol_version;
47c11975 32extern int remove_source_files;
20f90d5e 33extern int stdout_format_has_i;
aadc84d3 34extern int maybe_ATTRS_REPORT;
541b23d1 35extern char *basis_dir[];
9f2e3c3f 36extern struct file_list *the_file_list;
dc5ddbcc 37
4f5b0756 38#ifdef SUPPORT_HARD_LINKS
9f2e3c3f 39
c905bf37 40#define HASH_LOAD_LIMIT(size) ((size)*3/4)
9f2e3c3f 41#define FPTR(i) (the_file_list->files[i])
c905bf37
WD
42
43struct ihash_table {
44 int32 size;
45 int32 entries;
46 struct idev_node *buckets;
47} *dev_tbl;
48
49static struct idev_node *ihash_node(struct ihash_table *tbl, int64 key);
50
51/* Starting with protocol 30, we use a simple hashtable on the sending side
52 * for hashing the st_dev and st_ino info. The receiving side gets told
53 * (via flags and a "group index") which items are hard-linked together, so
54 * we can avoid the pool of dev+inode data. */
55
56static struct ihash_table *ihash_create(int size)
57{
58 struct ihash_table *tbl;
59
60 /* Pick a power of 2 that can hold the requested size. */
61 if (size & (size-1)) {
62 int req = size;
63 size = 32;
64 while (size < req)
65 size *= 2;
66 }
67
68 if (!(tbl = new(struct ihash_table))
69 || !(tbl->buckets = new_array(struct idev_node, size)))
70 out_of_memory("ihash_create");
71 memset(tbl->buckets, 0, size * sizeof tbl->buckets[0]);
72 tbl->size = size;
73 tbl->entries = 0;
74
75 return tbl;
76}
77
78static void ihash_destroy(struct ihash_table *tbl)
79{
80 free(tbl->buckets);
81 free(tbl);
82}
9f2e3c3f 83
aadc84d3
WD
84void init_hard_links(void)
85{
007996b4 86 dev_tbl = ihash_create(16);
aadc84d3
WD
87}
88
c905bf37
WD
89static void expand_ihash(struct ihash_table *tbl)
90{
91 struct idev_node *old_buckets;
92 int size = tbl->size * 2;
93 int i;
94
95 old_buckets = tbl->buckets;
96 if (!(tbl->buckets = new_array(struct idev_node, size)))
97 out_of_memory("ihash_create");
98 memset(tbl->buckets, 0, size * sizeof tbl->buckets[0]);
99 tbl->size = size;
100 tbl->entries = 0;
101
102 for (i = size / 2; i-- > 0; ) {
103 int64 key = old_buckets[i].key;
104 if (key == 0)
105 continue;
106 ihash_node(tbl, key)->data = old_buckets[i].data;
107 }
108
109 free(old_buckets);
110}
111
112/* This returns the node for the indicated key, either newly created,
113 * or already existing. */
114static struct idev_node *ihash_node(struct ihash_table *tbl, int64 key)
115{
116 uint32 bkt;
117
118 if (tbl->entries > HASH_LOAD_LIMIT(tbl->size))
119 expand_ihash(tbl);
120
121#if SIZEOF_INT64 < 8
122 /* Based on Jenkins One-at-a-time hash. */
123 {
124 uchar *keyp = (uchar*)&key;
125 int i;
126
127 for (bkt = 0, i = 0; i < SIZEOF_INT64; i++) {
128 bkt += keyp[i];
129 bkt += (bkt << 10);
130 bkt ^= (bkt >> 6);
131 }
132 bkt += (bkt << 3);
133 bkt ^= (bkt >> 11);
134 bkt += (bkt << 15);
135 }
136#else
137#define rot(x,k) (((x)<<(k)) ^ ((x)>>(32-(k))))
138 /* Based on Jenkins hashword() from lookup3.c. */
139 {
140 uint32 a, b, c;
141
142 /* Set up the internal state */
143 a = b = c = 0xdeadbeef + (8 << 2);
144
145 b += (uint32)(key >> 32);
146 a += (uint32)key;
147 c ^= b; c -= rot(b, 14);
148 a ^= c; a -= rot(c, 11);
149 b ^= a; b -= rot(a, 25);
150 c ^= b; c -= rot(b, 16);
151 a ^= c; a -= rot(c, 4);
152 b ^= a; b -= rot(a, 14);
153 c ^= b; c -= rot(b, 24);
154 bkt = c;
155 }
156#endif
157
158 /* If it already exists, return it. */
159 while (1) {
160 bkt &= tbl->size - 1;
161 if (tbl->buckets[bkt].key == key)
162 return &tbl->buckets[bkt];
163 if (tbl->buckets[bkt].key == 0)
164 break;
165 bkt++;
166 }
167
168 /* Otherwise, take over this empty spot and then return it. */
169 tbl->buckets[bkt].key = key;
170 tbl->entries++;
171 return &tbl->buckets[bkt];
172}
173
174struct idev_node *idev_node(int64 dev, int64 ino)
175{
176 static struct idev_node *dev_node = NULL;
177 struct ihash_table *tbl;
178
179 if (!dev_node || dev_node->key != dev) {
180 /* We keep a separate hash table of inodes for every device. */
181 dev_node = ihash_node(dev_tbl, dev);
182 if (!(tbl = dev_node->data))
183 tbl = dev_node->data = ihash_create(512);
184 } else
185 tbl = dev_node->data;
186
187 return ihash_node(tbl, ino);
188}
189
190void idev_destroy(void)
191{
192 int i;
193
194 for (i = 0; i < dev_tbl->size; i++) {
195 if (dev_tbl->buckets[i].data)
196 ihash_destroy(dev_tbl->buckets[i].data);
197 }
198
199 ihash_destroy(dev_tbl);
200}
201
c905bf37 202static int hlink_compare_gnum(int *int1, int *int2)
1d5cda22 203{
c905bf37
WD
204 struct file_struct *f1 = FPTR(*int1);
205 struct file_struct *f2 = FPTR(*int2);
206 int gnum1 = F_HL_GNUM(f1);
207 int gnum2 = F_HL_GNUM(f2);
1d5cda22 208
c905bf37
WD
209 if (gnum1 != gnum2)
210 return gnum1 > gnum2 ? 1 : -1;
9935066b 211
c905bf37
WD
212 return *int1 > *int2 ? 1 : -1;
213}
9935066b 214
c905bf37
WD
215static void match_gnums(int32 *ndx_list, int ndx_count)
216{
217 int32 from, prev;
218 struct file_struct *file, *file_next;
219 int gnum, gnum_next;
220
221 qsort(ndx_list, ndx_count, sizeof ndx_list[0],
222 (int (*)()) hlink_compare_gnum);
223
224 for (from = 0; from < ndx_count; from++) {
225 for (file = FPTR(ndx_list[from]), gnum = F_HL_GNUM(file), prev = -1;
226 from < ndx_count-1;
227 file = file_next, gnum = gnum_next, prev = ndx_list[from++])
228 {
229 file_next = FPTR(ndx_list[from+1]);
230 gnum_next = F_HL_GNUM(file_next);
231 if (gnum != gnum_next)
232 break;
233 if (prev < 0)
234 file->flags |= FLAG_HLINK_FIRST;
235 F_HL_PREV(file) = prev;
236 }
237 if (prev < 0)
238 file->flags &= ~FLAG_HLINKED;
239 else {
240 file->flags |= FLAG_HLINK_LAST;
241 F_HL_PREV(file) = prev;
242 }
243 }
244}
245
246/* Analyze the hard-links in the file-list by creating a list of all the
247 * items that have hlink data, sorting them, and matching up identical
248 * values into clusters. These will be a single linked list from last
249 * to first when we're done. */
250void match_hard_links(void)
251{
252 int i, ndx_count = 0;
253 int32 *ndx_list;
254
255 if (!(ndx_list = new_array(int32, the_file_list->count)))
256 out_of_memory("match_hard_links");
257
258 for (i = 0; i < the_file_list->count; i++) {
259 if (F_IS_HLINKED(FPTR(i)))
260 ndx_list[ndx_count++] = i;
261 }
1d5cda22 262
007996b4
WD
263 if (ndx_count)
264 match_gnums(ndx_list, ndx_count);
265
aadc84d3 266 free(ndx_list);
007996b4
WD
267 if (protocol_version < 30)
268 idev_destroy();
dc5ddbcc
AT
269}
270
3cd5301f 271static int maybe_hard_link(struct file_struct *file, int ndx,
a2ebbffc 272 const char *fname, int statret, STRUCT_STAT *stp,
aadc84d3
WD
273 const char *oldname, STRUCT_STAT *old_stp,
274 const char *realname, int itemizing, enum logcode code)
3cd5301f
WD
275{
276 if (statret == 0) {
aadc84d3
WD
277 if (stp->st_dev == old_stp->st_dev
278 && stp->st_ino == old_stp->st_ino) {
3cd5301f 279 if (itemizing) {
a2ebbffc 280 itemize(file, ndx, statret, stp,
3cd5301f
WD
281 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
282 0, "");
283 }
aadc84d3
WD
284 if (verbose > 1 && maybe_ATTRS_REPORT)
285 rprintf(FCLIENT, "%s is uptodate\n", fname);
286 file->flags |= FLAG_HLINK_DONE;
3cd5301f
WD
287 return 0;
288 }
289 if (make_backups) {
290 if (!make_backup(fname))
291 return -1;
292 } else if (robust_unlink(fname)) {
293 rsyserr(FERROR, errno, "unlink %s failed",
294 full_fname(fname));
295 return -1;
296 }
297 }
aadc84d3
WD
298
299 if (hard_link_one(file, fname, oldname, 0)) {
300 if (itemizing) {
301 itemize(file, ndx, statret, stp,
302 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0,
303 realname);
304 }
305 if (code != FNONE && verbose)
306 rprintf(code, "%s => %s\n", fname, realname);
307 return 0;
308 }
309 return -1;
3cd5301f 310}
3cd5301f 311
aadc84d3
WD
312/* Only called if FLAG_HLINKED is set and FLAG_HLINK_FIRST is not. Returns:
313 * 0 = process the file, 1 = skip the file, -1 = error occurred. */
a2ebbffc
WD
314int hard_link_check(struct file_struct *file, int ndx, const char *fname,
315 int statret, STRUCT_STAT *stp, int itemizing,
aadc84d3 316 enum logcode code)
d38fc305 317{
aadc84d3
WD
318 STRUCT_STAT prev_st;
319 char prev_name[MAXPATHLEN], altbuf[MAXPATHLEN], *realname;
320 int alt_dest, prev_ndx = F_HL_PREV(file);
321 struct file_struct *prev_file = FPTR(prev_ndx);
322
323 /* Is the previous link is not complete yet? */
324 if (!(prev_file->flags & FLAG_HLINK_DONE)) {
325 /* Is the previous link being transferred? */
326 if (prev_file->flags & FLAG_SENT) {
327 /* Add ourselves to the list of files that will be
328 * updated when the transfer completes, and mark
329 * ourself as waiting for the transfer. */
330 F_HL_PREV(file) = F_HL_PREV(prev_file);
331 F_HL_PREV(prev_file) = ndx;
332 file->flags |= FLAG_SENT;
333 return 1;
6cbde57d 334 }
aadc84d3
WD
335 return 0;
336 }
337
338 /* There is a finished file to link with! */
339 if (!(prev_file->flags & FLAG_HLINK_FIRST)) {
340 /* The previous previous will be marked with FIRST. */
341 prev_ndx = F_HL_PREV(prev_file);
342 prev_file = FPTR(prev_ndx);
343 /* Update our previous pointer to point to the first. */
344 F_HL_PREV(file) = prev_ndx;
345 }
346 alt_dest = F_HL_PREV(prev_file); /* alternate value when DONE && FIRST */
347 if (alt_dest >= 0 && dry_run) {
348 pathjoin(prev_name, MAXPATHLEN, basis_dir[alt_dest],
349 f_name(prev_file, NULL));
350 f_name(prev_file, altbuf);
351 realname = altbuf;
352 } else {
353 f_name(prev_file, prev_name);
354 realname = prev_name;
355 }
356
357 if (link_stat(prev_name, &prev_st, 0) < 0) {
358 rsyserr(FERROR, errno, "stat %s failed",
359 full_fname(prev_name));
360 return -1;
361 }
362
363 if (statret < 0 && basis_dir[0] != NULL) {
364 /* If we match an alt-dest item, we don't output this as a change. */
365 char cmpbuf[MAXPATHLEN];
366 STRUCT_STAT alt_st;
367 int j = 0;
368 do {
369 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
370 if (link_stat(cmpbuf, &alt_st, 0) < 0)
371 continue;
372 if (link_dest) {
373 if (prev_st.st_dev != alt_st.st_dev
374 || prev_st.st_ino != alt_st.st_ino)
375 continue;
376 statret = 1;
377 *stp = alt_st;
378 if (verbose < 2 || !stdout_format_has_i) {
379 itemizing = 0;
380 code = FNONE;
381 if (verbose > 1 && maybe_ATTRS_REPORT)
382 rprintf(FCLIENT, "%s is uptodate\n", fname);
383 }
384 break;
541b23d1 385 }
aadc84d3
WD
386 if (!unchanged_file(cmpbuf, file, &alt_st))
387 continue;
388 statret = 1;
389 *stp = alt_st;
390 if (unchanged_attrs(file, &alt_st))
391 break;
392 } while (basis_dir[++j] != NULL);
d38fc305 393 }
aadc84d3
WD
394
395 if (maybe_hard_link(file, ndx, fname, statret, stp, prev_name, &prev_st,
396 realname, itemizing, code) < 0)
397 return -1;
398
399 if (remove_source_files == 1 && do_xfers)
400 send_msg_int(MSG_SUCCESS, ndx);
401
402 return 1;
d38fc305
WD
403}
404
aadc84d3
WD
405int hard_link_one(struct file_struct *file, const char *fname,
406 const char *oldname, int terse)
a16bbc39 407{
aadc84d3
WD
408 if (do_link(oldname, fname) < 0) {
409 enum logcode code;
3cd5301f
WD
410 if (terse) {
411 if (!verbose)
412 return -1;
413 code = FINFO;
414 } else
415 code = FERROR;
416 rsyserr(code, errno, "link %s => %s failed",
aadc84d3
WD
417 full_fname(fname), oldname);
418 return 0;
9f2e3c3f
WD
419 }
420
aadc84d3
WD
421 file->flags |= FLAG_HLINK_DONE;
422
423 return 1;
a16bbc39 424}
fae5bb31 425
aadc84d3
WD
426void finish_hard_link(struct file_struct *file, const char *fname,
427 STRUCT_STAT *stp, int itemizing, enum logcode code,
428 int alt_dest)
dc5ddbcc 429{
aadc84d3
WD
430 STRUCT_STAT st, prev_st;
431 char alt_name[MAXPATHLEN], *prev_name;
432 const char *our_name;
433 int prev_statret, ndx, prev_ndx = F_HL_PREV(file);
434
435 if (stp == NULL && prev_ndx >= 0) {
436 if (link_stat(fname, &st, 0) < 0) {
437 rsyserr(FERROR, errno, "stat %s failed",
438 full_fname(fname));
439 return;
9f2e3c3f 440 }
aadc84d3 441 stp = &st;
9f2e3c3f 442 }
aadc84d3
WD
443
444 /* FIRST combined with DONE means we were the first to get done. */
445 file->flags |= FLAG_HLINK_FIRST | FLAG_HLINK_DONE;
446 F_HL_PREV(file) = alt_dest;
447 if (alt_dest >= 0 && dry_run) {
448 pathjoin(alt_name, MAXPATHLEN, basis_dir[alt_dest],
449 f_name(file, NULL));
450 our_name = alt_name;
451 } else
452 our_name = fname;
453
454 while ((ndx = prev_ndx) >= 0) {
9f2e3c3f 455 file = FPTR(ndx);
aadc84d3
WD
456 file->flags = (file->flags & ~FLAG_HLINK_FIRST) | FLAG_HLINK_DONE;
457 prev_ndx = F_HL_PREV(file);
458 prev_name = f_name(file, NULL);
459 prev_statret = link_stat(prev_name, &prev_st, 0);
460 if (maybe_hard_link(file, ndx, prev_name, prev_statret, &prev_st,
461 our_name, stp, fname, itemizing, code) < 0)
1d5cda22 462 continue;
663b2857
WD
463 if (remove_source_files == 1 && do_xfers)
464 send_msg_int(MSG_SUCCESS, ndx);
aadc84d3 465 }
dc5ddbcc 466}
a2ebbffc 467#endif