Martin gave his approval to use GPLv3 with this code.
[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>
ba2133d6 7 * Copyright (C) 2004-2007 Wayne Davison
0f78b815
WD
8 *
9 * This program is free software; you can redistribute it and/or modify
4fd842f9 10 * it under the terms of the GNU General Public License version 3 as
ba2133d6 11 * published by the Free Software Foundation.
0f78b815
WD
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
e7c67065 18 * You should have received a copy of the GNU General Public License along
4fd842f9 19 * with this program; if not, visit the http://fsf.org website.
0f78b815 20 */
dc5ddbcc
AT
21
22#include "rsync.h"
23
9a52223b 24extern int verbose;
82ad07c4 25extern int dry_run;
841d9436 26extern int do_xfers;
541b23d1 27extern int link_dest;
1c3344a1 28extern int preserve_acls;
417c99f6 29extern int make_backups;
c905bf37 30extern int protocol_version;
47c11975 31extern int remove_source_files;
20f90d5e 32extern int stdout_format_has_i;
aadc84d3 33extern int maybe_ATTRS_REPORT;
541b23d1 34extern char *basis_dir[];
3ac830b9 35extern struct file_list *cur_flist;
332cf6df
WD
36#ifdef ICONV_OPTION
37extern int ic_ndx;
38#endif
dc5ddbcc 39
4f5b0756 40#ifdef SUPPORT_HARD_LINKS
9f2e3c3f 41
c905bf37 42#define HASH_LOAD_LIMIT(size) ((size)*3/4)
c905bf37
WD
43
44struct ihash_table {
45 int32 size;
46 int32 entries;
47 struct idev_node *buckets;
48} *dev_tbl;
49
50static struct idev_node *ihash_node(struct ihash_table *tbl, int64 key);
51
52/* Starting with protocol 30, we use a simple hashtable on the sending side
53 * for hashing the st_dev and st_ino info. The receiving side gets told
54 * (via flags and a "group index") which items are hard-linked together, so
55 * we can avoid the pool of dev+inode data. */
56
57static struct ihash_table *ihash_create(int size)
58{
59 struct ihash_table *tbl;
60
61 /* Pick a power of 2 that can hold the requested size. */
719985cb 62 if (size & (size-1) || size < 16) {
c905bf37 63 int req = size;
719985cb 64 size = 16;
c905bf37
WD
65 while (size < req)
66 size *= 2;
67 }
68
69 if (!(tbl = new(struct ihash_table))
70 || !(tbl->buckets = new_array(struct idev_node, size)))
71 out_of_memory("ihash_create");
72 memset(tbl->buckets, 0, size * sizeof tbl->buckets[0]);
73 tbl->size = size;
74 tbl->entries = 0;
75
76 return tbl;
77}
78
79static void ihash_destroy(struct ihash_table *tbl)
80{
81 free(tbl->buckets);
82 free(tbl);
83}
9f2e3c3f 84
aadc84d3
WD
85void init_hard_links(void)
86{
007996b4 87 dev_tbl = ihash_create(16);
aadc84d3
WD
88}
89
c905bf37
WD
90static void expand_ihash(struct ihash_table *tbl)
91{
719985cb 92 struct idev_node *old_buckets = tbl->buckets;
c905bf37
WD
93 int size = tbl->size * 2;
94 int i;
95
c905bf37
WD
96 if (!(tbl->buckets = new_array(struct idev_node, size)))
97 out_of_memory("ihash_create");
719985cb 98 memset(tbl->buckets, 0, size * sizeof (struct idev_node));
c905bf37
WD
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{
332cf6df
WD
204 struct file_struct *f1 = cur_flist->sorted[*int1];
205 struct file_struct *f2 = cur_flist->sorted[*int2];
719985cb
WD
206 int32 gnum1 = F_HL_GNUM(f1);
207 int32 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;
719985cb 219 int32 gnum, gnum_next;
c905bf37
WD
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++) {
332cf6df 225 for (file = cur_flist->sorted[ndx_list[from]], gnum = F_HL_GNUM(file), prev = -1;
c905bf37 226 from < ndx_count-1;
3d0a159d 227 file = file_next, gnum = gnum_next, from++) /*SHARED ITERATOR*/
c905bf37 228 {
332cf6df 229 file_next = cur_flist->sorted[ndx_list[from+1]];
c905bf37
WD
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;
332cf6df
WD
236 /* The linked list must use raw ndx values. */
237#ifdef ICONV_OPTION
238 if (ic_ndx)
239 prev = F_NDX(file);
240 else
241#endif
242 prev = ndx_list[from];
c905bf37
WD
243 }
244 if (prev < 0)
245 file->flags &= ~FLAG_HLINKED;
246 else {
247 file->flags |= FLAG_HLINK_LAST;
248 F_HL_PREV(file) = prev;
249 }
250 }
251}
252
253/* Analyze the hard-links in the file-list by creating a list of all the
254 * items that have hlink data, sorting them, and matching up identical
255 * values into clusters. These will be a single linked list from last
256 * to first when we're done. */
257void match_hard_links(void)
258{
259 int i, ndx_count = 0;
260 int32 *ndx_list;
261
9decb4d2 262 if (!(ndx_list = new_array(int32, cur_flist->used)))
c905bf37
WD
263 out_of_memory("match_hard_links");
264
9decb4d2 265 for (i = 0; i < cur_flist->used; i++) {
332cf6df 266 if (F_IS_HLINKED(cur_flist->sorted[i]))
c905bf37
WD
267 ndx_list[ndx_count++] = i;
268 }
1d5cda22 269
007996b4
WD
270 if (ndx_count)
271 match_gnums(ndx_list, ndx_count);
272
aadc84d3 273 free(ndx_list);
007996b4
WD
274 if (protocol_version < 30)
275 idev_destroy();
dc5ddbcc
AT
276}
277
3cd5301f 278static int maybe_hard_link(struct file_struct *file, int ndx,
1c3344a1 279 const char *fname, int statret, statx *sxp,
aadc84d3
WD
280 const char *oldname, STRUCT_STAT *old_stp,
281 const char *realname, int itemizing, enum logcode code)
3cd5301f
WD
282{
283 if (statret == 0) {
1c3344a1
WD
284 if (sxp->st.st_dev == old_stp->st_dev
285 && sxp->st.st_ino == old_stp->st_ino) {
3cd5301f 286 if (itemizing) {
1c3344a1 287 itemize(fname, file, ndx, statret, sxp,
3cd5301f
WD
288 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
289 0, "");
290 }
aadc84d3
WD
291 if (verbose > 1 && maybe_ATTRS_REPORT)
292 rprintf(FCLIENT, "%s is uptodate\n", fname);
293 file->flags |= FLAG_HLINK_DONE;
3cd5301f
WD
294 return 0;
295 }
3ac830b9 296 if (make_backups > 0) {
3cd5301f
WD
297 if (!make_backup(fname))
298 return -1;
299 } else if (robust_unlink(fname)) {
300 rsyserr(FERROR, errno, "unlink %s failed",
301 full_fname(fname));
302 return -1;
303 }
304 }
aadc84d3
WD
305
306 if (hard_link_one(file, fname, oldname, 0)) {
307 if (itemizing) {
1c3344a1 308 itemize(fname, file, ndx, statret, sxp,
aadc84d3
WD
309 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0,
310 realname);
311 }
312 if (code != FNONE && verbose)
313 rprintf(code, "%s => %s\n", fname, realname);
314 return 0;
315 }
316 return -1;
3cd5301f 317}
3cd5301f 318
aadc84d3
WD
319/* Only called if FLAG_HLINKED is set and FLAG_HLINK_FIRST is not. Returns:
320 * 0 = process the file, 1 = skip the file, -1 = error occurred. */
a2ebbffc 321int hard_link_check(struct file_struct *file, int ndx, const char *fname,
1c3344a1 322 int statret, statx *sxp, int itemizing,
aadc84d3 323 enum logcode code)
d38fc305 324{
aadc84d3
WD
325 STRUCT_STAT prev_st;
326 char prev_name[MAXPATHLEN], altbuf[MAXPATHLEN], *realname;
327 int alt_dest, prev_ndx = F_HL_PREV(file);
332cf6df 328 struct file_struct *prev_file = cur_flist->files[prev_ndx];
aadc84d3
WD
329
330 /* Is the previous link is not complete yet? */
331 if (!(prev_file->flags & FLAG_HLINK_DONE)) {
332 /* Is the previous link being transferred? */
3ac830b9 333 if (prev_file->flags & FLAG_FILE_SENT) {
aadc84d3
WD
334 /* Add ourselves to the list of files that will be
335 * updated when the transfer completes, and mark
336 * ourself as waiting for the transfer. */
337 F_HL_PREV(file) = F_HL_PREV(prev_file);
338 F_HL_PREV(prev_file) = ndx;
3ac830b9 339 file->flags |= FLAG_FILE_SENT;
aadc84d3 340 return 1;
6cbde57d 341 }
aadc84d3
WD
342 return 0;
343 }
344
345 /* There is a finished file to link with! */
346 if (!(prev_file->flags & FLAG_HLINK_FIRST)) {
347 /* The previous previous will be marked with FIRST. */
348 prev_ndx = F_HL_PREV(prev_file);
332cf6df 349 prev_file = cur_flist->files[prev_ndx];
aadc84d3
WD
350 /* Update our previous pointer to point to the first. */
351 F_HL_PREV(file) = prev_ndx;
352 }
353 alt_dest = F_HL_PREV(prev_file); /* alternate value when DONE && FIRST */
354 if (alt_dest >= 0 && dry_run) {
355 pathjoin(prev_name, MAXPATHLEN, basis_dir[alt_dest],
356 f_name(prev_file, NULL));
357 f_name(prev_file, altbuf);
358 realname = altbuf;
359 } else {
360 f_name(prev_file, prev_name);
361 realname = prev_name;
362 }
363
364 if (link_stat(prev_name, &prev_st, 0) < 0) {
365 rsyserr(FERROR, errno, "stat %s failed",
366 full_fname(prev_name));
367 return -1;
368 }
369
370 if (statret < 0 && basis_dir[0] != NULL) {
371 /* If we match an alt-dest item, we don't output this as a change. */
372 char cmpbuf[MAXPATHLEN];
1c3344a1 373 statx alt_sx;
aadc84d3 374 int j = 0;
1c3344a1
WD
375#ifdef SUPPORT_ACLS
376 alt_sx.acc_acl = alt_sx.def_acl = NULL;
377#endif
aadc84d3
WD
378 do {
379 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
1c3344a1 380 if (link_stat(cmpbuf, &alt_sx.st, 0) < 0)
aadc84d3
WD
381 continue;
382 if (link_dest) {
1c3344a1
WD
383 if (prev_st.st_dev != alt_sx.st.st_dev
384 || prev_st.st_ino != alt_sx.st.st_ino)
aadc84d3
WD
385 continue;
386 statret = 1;
aadc84d3
WD
387 if (verbose < 2 || !stdout_format_has_i) {
388 itemizing = 0;
389 code = FNONE;
390 if (verbose > 1 && maybe_ATTRS_REPORT)
391 rprintf(FCLIENT, "%s is uptodate\n", fname);
392 }
393 break;
541b23d1 394 }
1c3344a1 395 if (!unchanged_file(cmpbuf, file, &alt_sx.st))
aadc84d3
WD
396 continue;
397 statret = 1;
1c3344a1 398 if (unchanged_attrs(cmpbuf, file, &alt_sx))
aadc84d3
WD
399 break;
400 } while (basis_dir[++j] != NULL);
1c3344a1
WD
401 if (statret == 1) {
402 sxp->st = alt_sx.st;
403#ifdef SUPPORT_ACLS
404 if (preserve_acls && !S_ISLNK(file->mode)) {
405 if (!ACL_READY(*sxp))
406 get_acl(cmpbuf, sxp);
407 else {
408 sxp->acc_acl = alt_sx.acc_acl;
409 sxp->def_acl = alt_sx.def_acl;
410 }
411 }
412#endif
413 }
414#ifdef SUPPORT_ACLS
415 else if (preserve_acls)
416 free_acl(&alt_sx);
417#endif
d38fc305 418 }
aadc84d3 419
1c3344a1 420 if (maybe_hard_link(file, ndx, fname, statret, sxp, prev_name, &prev_st,
aadc84d3
WD
421 realname, itemizing, code) < 0)
422 return -1;
423
424 if (remove_source_files == 1 && do_xfers)
425 send_msg_int(MSG_SUCCESS, ndx);
426
427 return 1;
d38fc305
WD
428}
429
aadc84d3
WD
430int hard_link_one(struct file_struct *file, const char *fname,
431 const char *oldname, int terse)
a16bbc39 432{
aadc84d3
WD
433 if (do_link(oldname, fname) < 0) {
434 enum logcode code;
3cd5301f
WD
435 if (terse) {
436 if (!verbose)
437 return -1;
438 code = FINFO;
439 } else
440 code = FERROR;
441 rsyserr(code, errno, "link %s => %s failed",
aadc84d3
WD
442 full_fname(fname), oldname);
443 return 0;
9f2e3c3f
WD
444 }
445
aadc84d3
WD
446 file->flags |= FLAG_HLINK_DONE;
447
448 return 1;
a16bbc39 449}
fae5bb31 450
aadc84d3
WD
451void finish_hard_link(struct file_struct *file, const char *fname,
452 STRUCT_STAT *stp, int itemizing, enum logcode code,
453 int alt_dest)
dc5ddbcc 454{
1c3344a1
WD
455 statx prev_sx;
456 STRUCT_STAT st;
aadc84d3
WD
457 char alt_name[MAXPATHLEN], *prev_name;
458 const char *our_name;
459 int prev_statret, ndx, prev_ndx = F_HL_PREV(file);
460
461 if (stp == NULL && prev_ndx >= 0) {
462 if (link_stat(fname, &st, 0) < 0) {
463 rsyserr(FERROR, errno, "stat %s failed",
464 full_fname(fname));
465 return;
9f2e3c3f 466 }
aadc84d3 467 stp = &st;
9f2e3c3f 468 }
aadc84d3
WD
469
470 /* FIRST combined with DONE means we were the first to get done. */
471 file->flags |= FLAG_HLINK_FIRST | FLAG_HLINK_DONE;
472 F_HL_PREV(file) = alt_dest;
473 if (alt_dest >= 0 && dry_run) {
474 pathjoin(alt_name, MAXPATHLEN, basis_dir[alt_dest],
475 f_name(file, NULL));
476 our_name = alt_name;
477 } else
478 our_name = fname;
479
1c3344a1
WD
480#ifdef SUPPORT_ACLS
481 prev_sx.acc_acl = prev_sx.def_acl = NULL;
482#endif
483
aadc84d3 484 while ((ndx = prev_ndx) >= 0) {
1c3344a1 485 int val;
332cf6df 486 file = cur_flist->files[ndx];
aadc84d3
WD
487 file->flags = (file->flags & ~FLAG_HLINK_FIRST) | FLAG_HLINK_DONE;
488 prev_ndx = F_HL_PREV(file);
489 prev_name = f_name(file, NULL);
1c3344a1
WD
490 prev_statret = link_stat(prev_name, &prev_sx.st, 0);
491 val = maybe_hard_link(file, ndx, prev_name, prev_statret, &prev_sx,
492 our_name, stp, fname, itemizing, code);
493#ifdef SUPPORT_ACLS
494 if (preserve_acls)
495 free_acl(&prev_sx);
496#endif
497 if (val < 0)
1d5cda22 498 continue;
663b2857
WD
499 if (remove_source_files == 1 && do_xfers)
500 send_msg_int(MSG_SUCCESS, ndx);
aadc84d3 501 }
dc5ddbcc 502}
a2ebbffc 503#endif