f82d3b90d5327562d3fe1a3180f3dbc722109bbc
[rsync/rsync.git] / hlink.c
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-2007 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
25 extern int verbose;
26 extern int dry_run;
27 extern int am_sender;
28 extern int inc_recurse;
29 extern int do_xfers;
30 extern int link_dest;
31 extern int preserve_acls;
32 extern int make_backups;
33 extern int protocol_version;
34 extern int remove_source_files;
35 extern int stdout_format_has_i;
36 extern int maybe_ATTRS_REPORT;
37 extern char *basis_dir[];
38 extern struct file_list *cur_flist;
39 #ifdef ICONV_OPTION
40 extern int ic_ndx;
41 #endif
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 struct hashtable *dev_tbl;
52
53 static struct hashtable *prior_hlinks;
54
55 static struct file_list *hlink_flist;
56
57 void init_hard_links(void)
58 {
59         if (am_sender || protocol_version < 30)
60                 dev_tbl = hashtable_create(16, SIZEOF_INT64 == 8);
61         else if (inc_recurse)
62                 prior_hlinks = hashtable_create(1024, 0);
63 }
64
65 struct ht_int64_node *idev_find(int64 dev, int64 ino)
66 {
67         static struct ht_int64_node *dev_node = NULL;
68         struct hashtable *tbl;
69
70         if (!dev_node || dev_node->key != dev) {
71                 /* We keep a separate hash table of inodes for every device. */
72                 dev_node = hashtable_find(dev_tbl, dev, 1);
73                 if (!(tbl = dev_node->data))
74                         tbl = dev_node->data = hashtable_create(512, SIZEOF_INT64 == 8);
75         } else
76                 tbl = dev_node->data;
77
78         return hashtable_find(tbl, ino, 1);
79 }
80
81 void idev_destroy(void)
82 {
83         int i;
84
85         for (i = 0; i < dev_tbl->size; i++) {
86                 struct ht_int32_node *node = HT_NODE(dev_tbl, dev_tbl->nodes, i);
87                 if (node->data)
88                         hashtable_destroy(node->data);
89         }
90
91         hashtable_destroy(dev_tbl);
92 }
93
94 static int hlink_compare_gnum(int *int1, int *int2)
95 {
96         struct file_struct *f1 = hlink_flist->sorted[*int1];
97         struct file_struct *f2 = hlink_flist->sorted[*int2];
98         int32 gnum1 = F_HL_GNUM(f1);
99         int32 gnum2 = F_HL_GNUM(f2);
100
101         if (gnum1 != gnum2)
102                 return gnum1 > gnum2 ? 1 : -1;
103
104         return *int1 > *int2 ? 1 : -1;
105 }
106
107 static void match_gnums(int32 *ndx_list, int ndx_count)
108 {
109         int32 from, prev;
110         struct file_struct *file, *file_next;
111         struct ht_int32_node *node = NULL;
112         int32 gnum, gnum_next;
113
114         qsort(ndx_list, ndx_count, sizeof ndx_list[0],
115              (int (*)()) hlink_compare_gnum);
116
117         for (from = 0; from < ndx_count; from++) {
118                 file = hlink_flist->sorted[ndx_list[from]];
119                 gnum = F_HL_GNUM(file);
120                 if (inc_recurse) {
121                         node = hashtable_find(prior_hlinks, gnum, 1);
122                         if (!node->data) {
123                                 node->data = new_array0(char, 5);
124                                 assert(gnum >= hlink_flist->ndx_start);
125                         }
126                 }
127                 if (gnum >= hlink_flist->ndx_start) {
128                         file->flags |= FLAG_HLINK_FIRST;
129                         prev = -1;
130                 } else if (CVAL(node->data, 0) == 0) {
131                         struct file_list *flist;
132                         prev = IVAL(node->data, 1);
133                         flist = flist_for_ndx(prev);
134                         assert(flist != NULL);
135                         file_next = flist->files[prev - flist->ndx_start];
136                         file_next->flags &= ~FLAG_HLINK_LAST;
137                 } else
138                         prev = -1;
139                 for ( ; from < ndx_count-1; file = file_next, gnum = gnum_next, from++) { /*SHARED ITERATOR*/
140                         file_next = hlink_flist->sorted[ndx_list[from+1]];
141                         gnum_next = F_HL_GNUM(file_next);
142                         if (gnum != gnum_next)
143                                 break;
144                         F_HL_PREV(file) = prev;
145                         /* The linked list must use raw ndx values. */
146 #ifdef ICONV_OPTION
147                         if (ic_ndx)
148                                 prev = F_NDX(file);
149                         else
150 #endif
151                                 prev = ndx_list[from] + hlink_flist->ndx_start;
152                 }
153                 if (prev < 0 && !inc_recurse) {
154                         file->flags &= ~(FLAG_HLINKED | FLAG_HLINK_FIRST);
155                         continue;
156                 }
157
158                 file->flags |= FLAG_HLINK_LAST;
159                 F_HL_PREV(file) = prev;
160                 if (inc_recurse && CVAL(node->data, 0) == 0) {
161 #ifdef ICONV_OPTION
162                         if (ic_ndx)
163                                 prev = F_NDX(file);
164                         else
165 #endif
166                                 prev = ndx_list[from] + hlink_flist->ndx_start;
167                         SIVAL(node->data, 1, prev);
168                 }
169         }
170 }
171
172 /* Analyze the hard-links in the file-list by creating a list of all the
173  * items that have hlink data, sorting them, and matching up identical
174  * values into clusters.  These will be a single linked list from last
175  * to first when we're done. */
176 void match_hard_links(struct file_list *flist)
177 {
178         int i, ndx_count = 0;
179         int32 *ndx_list;
180
181         if (!(ndx_list = new_array(int32, flist->used)))
182                 out_of_memory("match_hard_links");
183
184         for (i = 0; i < flist->used; i++) {
185                 if (F_IS_HLINKED(flist->sorted[i]))
186                         ndx_list[ndx_count++] = i;
187         }
188
189         hlink_flist = flist;
190
191         if (ndx_count)
192                 match_gnums(ndx_list, ndx_count);
193
194         free(ndx_list);
195         if (protocol_version < 30)
196                 idev_destroy();
197 }
198
199 static int maybe_hard_link(struct file_struct *file, int ndx,
200                            const char *fname, int statret, statx *sxp,
201                            const char *oldname, STRUCT_STAT *old_stp,
202                            const char *realname, int itemizing, enum logcode code)
203 {
204         if (statret == 0) {
205                 if (sxp->st.st_dev == old_stp->st_dev
206                  && sxp->st.st_ino == old_stp->st_ino) {
207                         if (itemizing) {
208                                 itemize(fname, file, ndx, statret, sxp,
209                                         ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
210                                         0, "");
211                         }
212                         if (verbose > 1 && maybe_ATTRS_REPORT)
213                                 rprintf(FCLIENT, "%s is uptodate\n", fname);
214                         file->flags |= FLAG_HLINK_DONE;
215                         return 0;
216                 }
217                 if (make_backups > 0) {
218                         if (!make_backup(fname))
219                                 return -1;
220                 } else if (robust_unlink(fname)) {
221                         rsyserr(FERROR, errno, "unlink %s failed",
222                                 full_fname(fname));
223                         return -1;
224                 }
225         }
226
227         if (hard_link_one(file, fname, oldname, 0)) {
228                 if (itemizing) {
229                         itemize(fname, file, ndx, statret, sxp,
230                                 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0,
231                                 realname);
232                 }
233                 if (code != FNONE && verbose)
234                         rprintf(code, "%s => %s\n", fname, realname);
235                 return 0;
236         }
237         return -1;
238 }
239
240 /* Figure out if a prior entry is still there or if we just have a
241  * cached name for it.  Never called with a FLAG_HLINK_FIRST entry. */
242 static char *check_prior(int prev_ndx, int gnum)
243 {
244         struct file_list *flist = flist_for_ndx(prev_ndx);
245         struct ht_int32_node *node;
246
247         if (flist) {
248                 hlink_flist = flist;
249                 return NULL;
250         }
251
252         node = hashtable_find(prior_hlinks, gnum, 0);
253         assert(node != NULL && node->data);
254         assert(CVAL(node->data, 0) != 0);
255         return node->data;
256 }
257
258 /* Only called if FLAG_HLINKED is set and FLAG_HLINK_FIRST is not.  Returns:
259  * 0 = process the file, 1 = skip the file, -1 = error occurred. */
260 int hard_link_check(struct file_struct *file, int ndx, const char *fname,
261                     int statret, statx *sxp, int itemizing,
262                     enum logcode code)
263 {
264         STRUCT_STAT prev_st;
265         char namebuf[MAXPATHLEN], altbuf[MAXPATHLEN];
266         char *realname, *prev_name;
267         int gnum = F_HL_GNUM(file);
268         int prev_ndx = F_HL_PREV(file);
269
270         prev_name = realname = check_prior(prev_ndx, gnum);
271
272         if (!prev_name) {
273                 struct file_struct *prev_file = hlink_flist->files[prev_ndx - hlink_flist->ndx_start];
274
275                 /* Is the previous link is not complete yet? */
276                 if (!(prev_file->flags & FLAG_HLINK_DONE)) {
277                         /* Is the previous link being transferred? */
278                         if (prev_file->flags & FLAG_FILE_SENT) {
279                                 /* Add ourselves to the list of files that will be
280                                  * updated when the transfer completes, and mark
281                                  * ourself as waiting for the transfer. */
282                                 F_HL_PREV(file) = F_HL_PREV(prev_file);
283                                 F_HL_PREV(prev_file) = ndx;
284                                 file->flags |= FLAG_FILE_SENT;
285                                 cur_flist->in_progress++;
286                                 return 1;
287                         }
288                         return 0;
289                 }
290
291                 /* There is a finished file to link with! */
292                 if (!(prev_file->flags & FLAG_HLINK_FIRST)) {
293                         /* The previous previous will be marked with FIRST. */
294                         prev_ndx = F_HL_PREV(prev_file);
295                         prev_name = realname = check_prior(prev_ndx, gnum);
296                         /* Update our previous pointer to point to the first. */
297                         F_HL_PREV(file) = prev_ndx;
298                 }
299         }
300         if (!prev_name) {
301                 struct file_struct *prev_file = hlink_flist->files[prev_ndx - hlink_flist->ndx_start];
302                 int alt_dest = F_HL_PREV(prev_file); /* alternate value when DONE && FIRST */
303
304                 if (alt_dest >= 0 && dry_run) {
305                         pathjoin(namebuf, MAXPATHLEN, basis_dir[alt_dest],
306                                  f_name(prev_file, NULL));
307                         prev_name = namebuf;
308                         realname = f_name(prev_file, altbuf);
309                 } else {
310                         prev_name = f_name(prev_file, namebuf);
311                         realname = prev_name;
312                 }
313         }
314
315         if (link_stat(prev_name, &prev_st, 0) < 0) {
316                 rsyserr(FERROR, errno, "stat %s failed",
317                         full_fname(prev_name));
318                 return -1;
319         }
320
321         if (statret < 0 && basis_dir[0] != NULL) {
322                 /* If we match an alt-dest item, we don't output this as a change. */
323                 char cmpbuf[MAXPATHLEN];
324                 statx alt_sx;
325                 int j = 0;
326 #ifdef SUPPORT_ACLS
327                 alt_sx.acc_acl = alt_sx.def_acl = NULL;
328 #endif
329                 do {
330                         pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
331                         if (link_stat(cmpbuf, &alt_sx.st, 0) < 0)
332                                 continue;
333                         if (link_dest) {
334                                 if (prev_st.st_dev != alt_sx.st.st_dev
335                                  || prev_st.st_ino != alt_sx.st.st_ino)
336                                         continue;
337                                 statret = 1;
338                                 if (verbose < 2 || !stdout_format_has_i) {
339                                         itemizing = 0;
340                                         code = FNONE;
341                                         if (verbose > 1 && maybe_ATTRS_REPORT)
342                                                 rprintf(FCLIENT, "%s is uptodate\n", fname);
343                                 }
344                                 break;
345                         }
346                         if (!unchanged_file(cmpbuf, file, &alt_sx.st))
347                                 continue;
348                         statret = 1;
349                         if (unchanged_attrs(cmpbuf, file, &alt_sx))
350                                 break;
351                 } while (basis_dir[++j] != NULL);
352                 if (statret == 1) {
353                         sxp->st = alt_sx.st;
354 #ifdef SUPPORT_ACLS
355                         if (preserve_acls && !S_ISLNK(file->mode)) {
356                                 if (!ACL_READY(*sxp))
357                                         get_acl(cmpbuf, sxp);
358                                 else {
359                                         sxp->acc_acl = alt_sx.acc_acl;
360                                         sxp->def_acl = alt_sx.def_acl;
361                                 }
362                         }
363 #endif
364                 }
365 #ifdef SUPPORT_ACLS
366                 else if (preserve_acls)
367                         free_acl(&alt_sx);
368 #endif
369         }
370
371         if (maybe_hard_link(file, ndx, fname, statret, sxp, prev_name, &prev_st,
372                             realname, itemizing, code) < 0)
373                 return -1;
374
375         if (remove_source_files == 1 && do_xfers)
376                 send_msg_int(MSG_SUCCESS, ndx);
377
378         return 1;
379 }
380
381 int hard_link_one(struct file_struct *file, const char *fname,
382                   const char *oldname, int terse)
383 {
384         if (do_link(oldname, fname) < 0) {
385                 enum logcode code;
386                 if (terse) {
387                         if (!verbose)
388                                 return -1;
389                         code = FINFO;
390                 } else
391                         code = FERROR;
392                 rsyserr(code, errno, "link %s => %s failed",
393                         full_fname(fname), oldname);
394                 return 0;
395         }
396
397         file->flags |= FLAG_HLINK_DONE;
398
399         return 1;
400 }
401
402 void finish_hard_link(struct file_struct *file, const char *fname,
403                       STRUCT_STAT *stp, int itemizing, enum logcode code,
404                       int alt_dest)
405 {
406         statx prev_sx;
407         STRUCT_STAT st;
408         char alt_name[MAXPATHLEN], *prev_name;
409         const char *our_name;
410         struct file_list *flist;
411         int prev_statret, ndx, prev_ndx = F_HL_PREV(file);
412
413         if (stp == NULL && prev_ndx >= 0) {
414                 if (link_stat(fname, &st, 0) < 0) {
415                         rsyserr(FERROR, errno, "stat %s failed",
416                                 full_fname(fname));
417                         return;
418                 }
419                 stp = &st;
420         }
421
422         /* FIRST combined with DONE means we were the first to get done. */
423         file->flags |= FLAG_HLINK_FIRST | FLAG_HLINK_DONE;
424         F_HL_PREV(file) = alt_dest;
425         if (alt_dest >= 0 && dry_run) {
426                 pathjoin(alt_name, MAXPATHLEN, basis_dir[alt_dest],
427                          f_name(file, NULL));
428                 our_name = alt_name;
429         } else
430                 our_name = fname;
431
432 #ifdef SUPPORT_ACLS
433         prev_sx.acc_acl = prev_sx.def_acl = NULL;
434 #endif
435
436         while ((ndx = prev_ndx) >= 0) {
437                 int val;
438                 flist = flist_for_ndx(ndx);
439                 assert(flist != NULL);
440                 file = flist->files[ndx - flist->ndx_start];
441                 file->flags = (file->flags & ~FLAG_HLINK_FIRST) | FLAG_HLINK_DONE;
442                 prev_ndx = F_HL_PREV(file);
443                 prev_name = f_name(file, NULL);
444                 prev_statret = link_stat(prev_name, &prev_sx.st, 0);
445                 val = maybe_hard_link(file, ndx, prev_name, prev_statret, &prev_sx,
446                                       our_name, stp, fname, itemizing, code);
447                 flist->in_progress--;
448 #ifdef SUPPORT_ACLS
449                 if (preserve_acls)
450                         free_acl(&prev_sx);
451 #endif
452                 if (val < 0)
453                         continue;
454                 if (remove_source_files == 1 && do_xfers)
455                         send_msg_int(MSG_SUCCESS, ndx);
456         }
457
458         if (inc_recurse) {
459                 int gnum = F_HL_GNUM(file);
460                 struct ht_int32_node *node = hashtable_find(prior_hlinks, gnum, 0);
461                 assert(node != NULL && node->data != NULL);
462                 assert(CVAL(node->data, 0) == 0);
463                 free(node->data);
464                 if (!(node->data = strdup(our_name)))
465                         out_of_memory("finish_hard_link");
466         }
467 }
468 #endif