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