Debian Codegraph @

hlink.c

raw symbols SCIP
1 /*
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 <[email protected]>
7 * Copyright (C) 2004-2022 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 3 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 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, visit the http://fsf.org website.
21 */
22
23 #include "rsync.h"
24 #include "inums.h"
25 #include "ifuncs.h"
26
27 extern int dry_run;
28 extern int list_only;
29 extern int am_sender;
30 extern int inc_recurse;
31 extern int do_xfers;
32 extern int alt_dest_type;
33 extern int preserve_acls;
34 extern int preserve_xattrs;
35 extern int protocol_version;
36 extern int remove_source_files;
37 extern int stdout_format_has_i;
38 extern int maybe_ATTRS_REPORT;
39 extern int unsort_ndx;
40 extern char *basis_dir[MAX_BASIS_DIRS+1];
41 extern struct file_list *cur_flist;
42
43 #ifdef SUPPORT_HARD_LINKS
44
45 /* Starting with protocol 30, we use a simple hashtable on the sending side
46 * for hashing the st_dev and st_ino info. The receiving side gets told
47 * (via flags and a "group index") which items are hard-linked together, so
48 * we can avoid the pool of dev+inode data. For incremental recursion mode,
49 * the receiver will use a ndx hash to remember old pathnames. */
50
51 static void *data_when_new = "";
52
53 static struct hashtable *dev_tbl;
54
55 static struct hashtable *prior_hlinks;
56
57 static struct file_list *hlink_flist;
58
59 void init_hard_links(void)
60 {
61 if (am_sender || protocol_version < 30)
62 dev_tbl = hashtable_create(16, HT_KEY64);
63 else if (inc_recurse)
64 prior_hlinks = hashtable_create(1024, HT_KEY32);
65 }
66
67 struct ht_int64_node *idev_find(int64 dev, int64 ino)
68 {
69 static struct ht_int64_node *dev_node = NULL;
70
71 /* Note that some OSes have a dev == 0, so increment to avoid storing a 0. */
72 if (!dev_node || dev_node->key != dev+1) {
73 /* We keep a separate hash table of inodes for every device. */
74 dev_node = hashtable_find(dev_tbl, dev+1, data_when_new);
75 if (dev_node->data == data_when_new) {
76 dev_node->data = hashtable_create(512, HT_KEY64);
77 if (DEBUG_GTE(HLINK, 3)) {
78 rprintf(FINFO, "[%s] created hashtable for dev %s\n",
79 who_am_i(), big_num(dev));
80 }
81 }
82 }
83
84 return hashtable_find(dev_node->data, ino, (void*)-1L);
85 }
86
87 void idev_destroy(void)
88 {
89 int i;
90
91 for (i = 0; i < dev_tbl->size; i++) {
92 struct ht_int32_node *node = HT_NODE(dev_tbl, dev_tbl->nodes, i);
93 if (node->data)
94 hashtable_destroy(node->data);
95 }
96
97 hashtable_destroy(dev_tbl);
98 }
99
100 static int hlink_compare_gnum(int *int1, int *int2)
101 {
102 struct file_struct *f1 = hlink_flist->sorted[*int1];
103 struct file_struct *f2 = hlink_flist->sorted[*int2];
104 int32 gnum1 = F_HL_GNUM(f1);
105 int32 gnum2 = F_HL_GNUM(f2);
106
107 if (gnum1 != gnum2)
108 return gnum1 > gnum2 ? 1 : -1;
109
110 return *int1 > *int2 ? 1 : -1;
111 }
112
113 static void match_gnums(int32 *ndx_list, int ndx_count)
114 {
115 int32 from, prev;
116 struct file_struct *file, *file_next;
117 struct ht_int32_node *node = NULL;
118 int32 gnum, gnum_next;
119
120 qsort(ndx_list, ndx_count, sizeof ndx_list[0], (int (*)(const void*, const void*))hlink_compare_gnum);
121
122 for (from = 0; from < ndx_count; from++) {
123 file = hlink_flist->sorted[ndx_list[from]];
124 gnum = F_HL_GNUM(file);
125 if (inc_recurse) {
126 node = hashtable_find(prior_hlinks, gnum, data_when_new);
127 if (node->data == data_when_new) {
128 if (gnum < hlink_flist->ndx_start) {
129 /* A non-first hard-link entry whose
130 * gnum points before this flist's
131 * ndx_start should already have been
132 * recorded in prior_hlinks by an
133 * earlier flist. A peer that sends
134 * such a back-reference on the first
135 * flist (or to a gnum that was never
136 * declared XMIT_HLINK_FIRST) is
137 * misbehaving. */
138 rprintf(FERROR,
139 "hard-link gnum %d precedes flist start %d\n",
140 (int)gnum, (int)hlink_flist->ndx_start);
141 exit_cleanup(RERR_PROTOCOL);
142 }
143 node->data = new_array0(char, 5);
144 file->flags |= FLAG_HLINK_FIRST;
145 prev = -1;
146 } else if (CVAL(node->data, 0) == 0) {
147 struct file_list *flist;
148 prev = IVAL(node->data, 1);
149 flist = flist_for_ndx(prev, NULL);
150 if (flist)
151 flist->files[prev - flist->ndx_start]->flags &= ~FLAG_HLINK_LAST;
152 else {
153 /* We skipped all prior files in this
154 * group, so mark this as a "first". */
155 file->flags |= FLAG_HLINK_FIRST;
156 prev = -1;
157 }
158 } else
159 prev = -1;
160 } else {
161 file->flags |= FLAG_HLINK_FIRST;
162 prev = -1;
163 }
164 for ( ; from < ndx_count-1; file = file_next, gnum = gnum_next, from++) { /*SHARED ITERATOR*/
165 file_next = hlink_flist->sorted[ndx_list[from+1]];
166 gnum_next = F_HL_GNUM(file_next);
167 if (gnum != gnum_next)
168 break;
169 F_HL_PREV(file) = prev;
170 /* The linked list uses over-the-wire ndx values. */
171 if (unsort_ndx)
172 prev = F_NDX(file);
173 else
174 prev = ndx_list[from] + hlink_flist->ndx_start;
175 }
176 if (prev < 0 && !inc_recurse) {
177 /* Disable hard-link bit and set DONE so that
178 * HLINK_BUMP()-dependent values are unaffected. */
179 file->flags &= ~(FLAG_HLINKED | FLAG_HLINK_FIRST);
180 file->flags |= FLAG_HLINK_DONE;
181 continue;
182 }
183
184 file->flags |= FLAG_HLINK_LAST;
185 F_HL_PREV(file) = prev;
186 if (inc_recurse && CVAL(node->data, 0) == 0) {
187 if (unsort_ndx)
188 prev = F_NDX(file);
189 else
190 prev = ndx_list[from] + hlink_flist->ndx_start;
191 SIVAL(node->data, 1, prev);
192 }
193 }
194 }
195
196 /* Analyze the hard-links in the file-list by creating a list of all the
197 * items that have hlink data, sorting them, and matching up identical
198 * values into clusters. These will be a single linked list from last
199 * to first when we're done. */
200 void match_hard_links(struct file_list *flist)
201 {
202 if (!list_only && flist->used) {
203 int i, ndx_count = 0;
204 int32 *ndx_list;
205
206 ndx_list = new_array(int32, flist->used);
207
208 for (i = 0; i < flist->used; i++) {
209 if (F_IS_HLINKED(flist->sorted[i]))
210 ndx_list[ndx_count++] = i;
211 }
212
213 hlink_flist = flist;
214
215 if (ndx_count)
216 match_gnums(ndx_list, ndx_count);
217
218 free(ndx_list);
219 }
220 if (protocol_version < 30)
221 idev_destroy();
222 }
223
224 static int maybe_hard_link(struct file_struct *file, int ndx,
225 char *fname, int statret, stat_x *sxp,
226 const char *oldname, STRUCT_STAT *old_stp,
227 const char *realname, int itemizing, enum logcode code)
228 {
229 if (statret == 0) {
230 if (sxp->st.st_dev == old_stp->st_dev
231 && sxp->st.st_ino == old_stp->st_ino) {
232 if (itemizing) {
233 itemize(fname, file, ndx, statret, sxp,
234 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
235 0, "");
236 }
237 if (INFO_GTE(NAME, 2) && maybe_ATTRS_REPORT)
238 rprintf(FCLIENT, "%s is uptodate\n", fname);
239 file->flags |= FLAG_HLINK_DONE;
240 return 0;
241 }
242 }
243
244 if (atomic_create(file, fname, NULL, oldname, MAKEDEV(0, 0), sxp, statret == 0 ? DEL_FOR_FILE : 0)) {
245 if (itemizing) {
246 itemize(fname, file, ndx, statret, sxp,
247 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0,
248 realname);
249 }
250 if (code != FNONE && INFO_GTE(NAME, 1))
251 rprintf(code, "%s => %s\n", fname, realname);
252 return 0;
253 }
254
255 return -1;
256 }
257
258 /* Figure out if a prior entry is still there or if we just have a
259 * cached name for it. */
260 static char *check_prior(struct file_struct *file, int gnum,
261 int *prev_ndx_p, struct file_list **flist_p)
262 {
263 struct file_struct *fp;
264 struct ht_int32_node *node;
265 int prev_ndx = F_HL_PREV(file);
266
267 while (1) {
268 struct file_list *flist;
269 if (prev_ndx < 0
270 || (flist = flist_for_ndx(prev_ndx, NULL)) == NULL)
271 break;
272 fp = flist->files[prev_ndx - flist->ndx_start];
273 if (!(fp->flags & FLAG_SKIP_HLINK)) {
274 *prev_ndx_p = prev_ndx;
275 *flist_p = flist;
276 return NULL;
277 }
278 F_HL_PREV(file) = prev_ndx = F_HL_PREV(fp);
279 }
280
281 if (inc_recurse
282 && (node = hashtable_find(prior_hlinks, gnum, NULL)) != NULL) {
283 assert(node->data != NULL);
284 if (CVAL(node->data, 0) != 0) {
285 *prev_ndx_p = -1;
286 *flist_p = NULL;
287 return node->data;
288 }
289 /* The prior file must have been skipped. */
290 F_HL_PREV(file) = -1;
291 }
292
293 *prev_ndx_p = -1;
294 *flist_p = NULL;
295 return NULL;
296 }
297
298 /* Only called if FLAG_HLINKED is set and FLAG_HLINK_FIRST is not. Returns:
299 * 0 = process the file, 1 = skip the file, -1 = error occurred. */
300 int hard_link_check(struct file_struct *file, int ndx, char *fname,
301 int statret, stat_x *sxp, int itemizing,
302 enum logcode code)
303 {
304 STRUCT_STAT prev_st;
305 char namebuf[MAXPATHLEN], altbuf[MAXPATHLEN];
306 char *realname, *prev_name;
307 struct file_list *flist;
308 int gnum = inc_recurse ? F_HL_GNUM(file) : -1;
309 int prev_ndx;
310
311 prev_name = realname = check_prior(file, gnum, &prev_ndx, &flist);
312
313 if (!prev_name) {
314 struct file_struct *prev_file;
315
316 if (!flist) {
317 /* The previous file was skipped, so this one is
318 * treated as if it were the first in its group. */
319 if (DEBUG_GTE(HLINK, 2)) {
320 rprintf(FINFO, "hlink for %d (%s,%d): virtual first\n",
321 ndx, f_name(file, NULL), gnum);
322 }
323 return 0;
324 }
325
326 prev_file = flist->files[prev_ndx - flist->ndx_start];
327
328 /* Is the previous link not complete yet? */
329 if (!(prev_file->flags & FLAG_HLINK_DONE)) {
330 /* Is the previous link being transferred? */
331 if (prev_file->flags & FLAG_FILE_SENT) {
332 /* Add ourselves to the list of files that will
333 * be updated when the transfer completes, and
334 * mark ourself as waiting for the transfer. */
335 F_HL_PREV(file) = F_HL_PREV(prev_file);
336 F_HL_PREV(prev_file) = ndx;
337 file->flags |= FLAG_FILE_SENT;
338 cur_flist->in_progress++;
339 if (DEBUG_GTE(HLINK, 2)) {
340 rprintf(FINFO, "hlink for %d (%s,%d): waiting for %d\n",
341 ndx, f_name(file, NULL), gnum, F_HL_PREV(file));
342 }
343 return 1;
344 }
345 if (DEBUG_GTE(HLINK, 2)) {
346 rprintf(FINFO, "hlink for %d (%s,%d): looking for a leader\n",
347 ndx, f_name(file, NULL), gnum);
348 }
349 return 0;
350 }
351
352 /* There is a finished file to link with! */
353 if (!(prev_file->flags & FLAG_HLINK_FIRST)) {
354 /* The previous previous is FIRST when prev is not. */
355 prev_name = realname = check_prior(prev_file, gnum, &prev_ndx, &flist);
356 /* Update our previous pointer to point to the FIRST. */
357 F_HL_PREV(file) = prev_ndx;
358 }
359
360 if (!prev_name) {
361 int alt_dest;
362
363 assert(flist != NULL);
364 prev_file = flist->files[prev_ndx - flist->ndx_start];
365 /* F_HL_PREV() is alt_dest value when DONE && FIRST. */
366 alt_dest = F_HL_PREV(prev_file);
367 if (DEBUG_GTE(HLINK, 2)) {
368 rprintf(FINFO, "hlink for %d (%s,%d): found flist match (alt %d)\n",
369 ndx, f_name(file, NULL), gnum, alt_dest);
370 }
371
372 if (alt_dest >= 0 && dry_run) {
373 pathjoin(namebuf, MAXPATHLEN, basis_dir[alt_dest],
374 f_name(prev_file, NULL));
375 prev_name = namebuf;
376 realname = f_name(prev_file, altbuf);
377 } else {
378 prev_name = f_name(prev_file, namebuf);
379 realname = prev_name;
380 }
381 }
382 }
383
384 if (DEBUG_GTE(HLINK, 2)) {
385 rprintf(FINFO, "hlink for %d (%s,%d): leader is %d (%s)\n",
386 ndx, f_name(file, NULL), gnum, prev_ndx, prev_name);
387 }
388
389 if (link_stat(prev_name, &prev_st, 0) < 0) {
390 if (!dry_run || errno != ENOENT) {
391 rsyserr(FERROR_XFER, errno, "stat %s failed", full_fname(prev_name));
392 return -1;
393 }
394 /* A new hard-link will get a new dev & inode, so approximate
395 * those values in dry-run mode by zeroing them. */
396 memset(&prev_st, 0, sizeof prev_st);
397 }
398
399 if (statret < 0 && basis_dir[0] != NULL) {
400 /* If we match an alt-dest item, we don't output this as a change. */
401 char cmpbuf[MAXPATHLEN];
402 stat_x alt_sx;
403 int j = 0;
404 init_stat_x(&alt_sx);
405 do {
406 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
407 if (link_stat(cmpbuf, &alt_sx.st, 0) < 0)
408 continue;
409 if (alt_dest_type == LINK_DEST) {
410 if (prev_st.st_dev != alt_sx.st.st_dev
411 || prev_st.st_ino != alt_sx.st.st_ino)
412 continue;
413 statret = 1;
414 if (stdout_format_has_i == 0
415 || (!INFO_GTE(NAME, 2) && stdout_format_has_i < 2)) {
416 itemizing = 0;
417 code = FNONE;
418 if (INFO_GTE(NAME, 2) && maybe_ATTRS_REPORT)
419 rprintf(FCLIENT, "%s is uptodate\n", fname);
420 }
421 break;
422 }
423 /* Content-based basis match only applies to regular
424 * files: for a hard-linked symlink/device/special the
425 * exact-inode check above is the only meaningful test,
426 * and quick_check_ok(FT_REG, ...) would read F_SUM()
427 * on a file_struct that has no SUM_EXTRA_CNT space
428 * (recv_file_entry only allocates it for S_ISREG). */
429 if (!S_ISREG(file->mode)
430 || !quick_check_ok(FT_REG, cmpbuf, file, &alt_sx.st))
431 continue;
432 statret = 1;
433 if (unchanged_attrs(cmpbuf, file, &alt_sx))
434 break;
435 } while (basis_dir[++j] != NULL);
436 if (statret == 1) {
437 sxp->st = alt_sx.st;
438 #ifdef SUPPORT_ACLS
439 if (preserve_acls && !S_ISLNK(file->mode)) {
440 free_acl(sxp);
441 if (!ACL_READY(alt_sx))
442 get_acl(cmpbuf, sxp);
443 else {
444 sxp->acc_acl = alt_sx.acc_acl;
445 sxp->def_acl = alt_sx.def_acl;
446 alt_sx.acc_acl = alt_sx.def_acl = NULL;
447 }
448 }
449 #endif
450 #ifdef SUPPORT_XATTRS
451 if (preserve_xattrs) {
452 free_xattr(sxp);
453 if (!XATTR_READY(alt_sx))
454 get_xattr(cmpbuf, -1, sxp);
455 else {
456 sxp->xattr = alt_sx.xattr;
457 alt_sx.xattr = NULL;
458 }
459 }
460 #endif
461 } else
462 free_stat_x(&alt_sx);
463 }
464
465 if (maybe_hard_link(file, ndx, fname, statret, sxp, prev_name, &prev_st,
466 realname, itemizing, code) < 0)
467 return -1;
468
469 if (remove_source_files == 1 && do_xfers)
470 send_msg_success(fname, ndx);
471
472 return 1;
473 }
474
475 int hard_link_one(struct file_struct *file, const char *fname,
476 const char *oldname, int terse)
477 {
478 if (do_link_at(oldname, fname) < 0) {
479 enum logcode code;
480 if (terse) {
481 if (!INFO_GTE(NAME, 1))
482 return 0;
483 code = FINFO;
484 } else
485 code = FERROR_XFER;
486 rsyserr(code, errno, "link %s => %s failed",
487 full_fname(fname), oldname);
488 return 0;
489 }
490
491 file->flags |= FLAG_HLINK_DONE;
492
493 return 1;
494 }
495
496 void finish_hard_link(struct file_struct *file, const char *fname, int fin_ndx,
497 STRUCT_STAT *stp, int itemizing, enum logcode code,
498 int alt_dest)
499 {
500 stat_x prev_sx;
501 STRUCT_STAT st;
502 char prev_name[MAXPATHLEN], alt_name[MAXPATHLEN];
503 const char *our_name;
504 struct file_list *flist;
505 int prev_statret, ndx, prev_ndx = F_HL_PREV(file);
506
507 if (stp == NULL && prev_ndx >= 0) {
508 if (link_stat(fname, &st, 0) < 0 && !dry_run) {
509 rsyserr(FERROR_XFER, errno, "stat %s failed",
510 full_fname(fname));
511 return;
512 }
513 stp = &st;
514 }
515
516 /* FIRST combined with DONE means we were the first to get done. */
517 file->flags |= FLAG_HLINK_FIRST | FLAG_HLINK_DONE;
518 F_HL_PREV(file) = alt_dest;
519 if (alt_dest >= 0 && dry_run) {
520 pathjoin(alt_name, MAXPATHLEN, basis_dir[alt_dest],
521 f_name(file, NULL));
522 our_name = alt_name;
523 } else
524 our_name = fname;
525
526 init_stat_x(&prev_sx);
527
528 while ((ndx = prev_ndx) >= 0) {
529 int val;
530 flist = flist_for_ndx(ndx, "finish_hard_link");
531 file = flist->files[ndx - flist->ndx_start];
532 file->flags = (file->flags & ~FLAG_HLINK_FIRST) | FLAG_HLINK_DONE;
533 prev_ndx = F_HL_PREV(file);
534 F_HL_PREV(file) = fin_ndx;
535 prev_statret = link_stat(f_name(file, prev_name), &prev_sx.st, 0);
536 val = maybe_hard_link(file, ndx, prev_name, prev_statret, &prev_sx,
537 our_name, stp, fname, itemizing, code);
538 flist->in_progress--;
539 free_stat_x(&prev_sx);
540 if (val < 0)
541 continue;
542 if (remove_source_files == 1 && do_xfers)
543 send_msg_success(fname, ndx);
544 }
545
546 if (inc_recurse) {
547 int gnum = F_HL_GNUM(file);
548 struct ht_int32_node *node = hashtable_find(prior_hlinks, gnum, NULL);
549 if (node == NULL) {
550 rprintf(FERROR, "Unable to find a hlink node for %d (%s)\n", gnum, f_name(file, prev_name));
551 exit_cleanup(RERR_MESSAGEIO);
552 }
553 if (node->data == NULL) {
554 rprintf(FERROR, "Hlink node data for %d is NULL (%s)\n", gnum, f_name(file, prev_name));
555 exit_cleanup(RERR_MESSAGEIO);
556 }
557 if (CVAL(node->data, 0) != 0) {
558 rprintf(FERROR, "Hlink node data for %d already has path=%s (%s)\n",
559 gnum, (char*)node->data, f_name(file, prev_name));
560 exit_cleanup(RERR_MESSAGEIO);
561 }
562 free(node->data);
563 node->data = strdup(our_name);
564 }
565 }
566
567 int skip_hard_link(struct file_struct *file, struct file_list **flist_p)
568 {
569 struct file_list *flist;
570 int prev_ndx;
571
572 file->flags |= FLAG_SKIP_HLINK;
573 if (!(file->flags & FLAG_HLINK_LAST))
574 return -1;
575
576 check_prior(file, F_HL_GNUM(file), &prev_ndx, &flist);
577 if (prev_ndx >= 0) {
578 file = flist->files[prev_ndx - flist->ndx_start];
579 if (file->flags & (FLAG_HLINK_DONE|FLAG_FILE_SENT))
580 return -1;
581 file->flags |= FLAG_HLINK_LAST;
582 *flist_p = flist;
583 }
584
585 return prev_ndx;
586 }
587 #endif