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