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