2 * Routines to support hard-linking.
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
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.
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.
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.
29 extern int preserve_acls;
30 extern int make_backups;
31 extern int protocol_version;
32 extern int remove_source_files;
33 extern int stdout_format_has_i;
34 extern int maybe_ATTRS_REPORT;
35 extern char *basis_dir[];
36 extern struct file_list *cur_flist;
41 #ifdef SUPPORT_HARD_LINKS
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. */
48 struct hashtable *dev_tbl;
50 void init_hard_links(void)
52 dev_tbl = hashtable_create(16, SIZEOF_INT64 == 8);
55 struct ht_int64_node *idev_find(int64 dev, int64 ino)
57 static struct ht_int64_node *dev_node = NULL;
58 struct hashtable *tbl;
60 if (!dev_node || dev_node->key != dev) {
61 /* We keep a separate hash table of inodes for every device. */
62 dev_node = hashtable_find(dev_tbl, dev, 1);
63 if (!(tbl = dev_node->data))
64 tbl = dev_node->data = hashtable_create(512, SIZEOF_INT64 == 8);
68 return hashtable_find(tbl, ino, 1);
71 void idev_destroy(void)
75 for (i = 0; i < dev_tbl->size; i++) {
76 struct ht_int32_node *node = HT_NODE(dev_tbl, dev_tbl->nodes, i);
78 hashtable_destroy(node->data);
81 hashtable_destroy(dev_tbl);
84 static int hlink_compare_gnum(int *int1, int *int2)
86 struct file_struct *f1 = cur_flist->sorted[*int1];
87 struct file_struct *f2 = cur_flist->sorted[*int2];
88 int32 gnum1 = F_HL_GNUM(f1);
89 int32 gnum2 = F_HL_GNUM(f2);
92 return gnum1 > gnum2 ? 1 : -1;
94 return *int1 > *int2 ? 1 : -1;
97 static void match_gnums(int32 *ndx_list, int ndx_count)
100 struct file_struct *file, *file_next;
101 int32 gnum, gnum_next;
103 qsort(ndx_list, ndx_count, sizeof ndx_list[0],
104 (int (*)()) hlink_compare_gnum);
106 for (from = 0; from < ndx_count; from++) {
107 for (file = cur_flist->sorted[ndx_list[from]], gnum = F_HL_GNUM(file), prev = -1;
109 file = file_next, gnum = gnum_next, from++) /*SHARED ITERATOR*/
111 file_next = cur_flist->sorted[ndx_list[from+1]];
112 gnum_next = F_HL_GNUM(file_next);
113 if (gnum != gnum_next)
116 file->flags |= FLAG_HLINK_FIRST;
117 F_HL_PREV(file) = prev;
118 /* The linked list must use raw ndx values. */
124 prev = ndx_list[from];
127 file->flags &= ~FLAG_HLINKED;
129 file->flags |= FLAG_HLINK_LAST;
130 F_HL_PREV(file) = prev;
135 /* Analyze the hard-links in the file-list by creating a list of all the
136 * items that have hlink data, sorting them, and matching up identical
137 * values into clusters. These will be a single linked list from last
138 * to first when we're done. */
139 void match_hard_links(void)
141 int i, ndx_count = 0;
144 if (!(ndx_list = new_array(int32, cur_flist->used)))
145 out_of_memory("match_hard_links");
147 for (i = 0; i < cur_flist->used; i++) {
148 if (F_IS_HLINKED(cur_flist->sorted[i]))
149 ndx_list[ndx_count++] = i;
153 match_gnums(ndx_list, ndx_count);
156 if (protocol_version < 30)
160 static int maybe_hard_link(struct file_struct *file, int ndx,
161 const char *fname, int statret, statx *sxp,
162 const char *oldname, STRUCT_STAT *old_stp,
163 const char *realname, int itemizing, enum logcode code)
166 if (sxp->st.st_dev == old_stp->st_dev
167 && sxp->st.st_ino == old_stp->st_ino) {
169 itemize(fname, file, ndx, statret, sxp,
170 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
173 if (verbose > 1 && maybe_ATTRS_REPORT)
174 rprintf(FCLIENT, "%s is uptodate\n", fname);
175 file->flags |= FLAG_HLINK_DONE;
178 if (make_backups > 0) {
179 if (!make_backup(fname))
181 } else if (robust_unlink(fname)) {
182 rsyserr(FERROR, errno, "unlink %s failed",
188 if (hard_link_one(file, fname, oldname, 0)) {
190 itemize(fname, file, ndx, statret, sxp,
191 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0,
194 if (code != FNONE && verbose)
195 rprintf(code, "%s => %s\n", fname, realname);
201 /* Only called if FLAG_HLINKED is set and FLAG_HLINK_FIRST is not. Returns:
202 * 0 = process the file, 1 = skip the file, -1 = error occurred. */
203 int hard_link_check(struct file_struct *file, int ndx, const char *fname,
204 int statret, statx *sxp, int itemizing,
208 char prev_name[MAXPATHLEN], altbuf[MAXPATHLEN], *realname;
209 int alt_dest, prev_ndx = F_HL_PREV(file);
210 struct file_struct *prev_file = cur_flist->files[prev_ndx];
212 /* Is the previous link is not complete yet? */
213 if (!(prev_file->flags & FLAG_HLINK_DONE)) {
214 /* Is the previous link being transferred? */
215 if (prev_file->flags & FLAG_FILE_SENT) {
216 /* Add ourselves to the list of files that will be
217 * updated when the transfer completes, and mark
218 * ourself as waiting for the transfer. */
219 F_HL_PREV(file) = F_HL_PREV(prev_file);
220 F_HL_PREV(prev_file) = ndx;
221 file->flags |= FLAG_FILE_SENT;
227 /* There is a finished file to link with! */
228 if (!(prev_file->flags & FLAG_HLINK_FIRST)) {
229 /* The previous previous will be marked with FIRST. */
230 prev_ndx = F_HL_PREV(prev_file);
231 prev_file = cur_flist->files[prev_ndx];
232 /* Update our previous pointer to point to the first. */
233 F_HL_PREV(file) = prev_ndx;
235 alt_dest = F_HL_PREV(prev_file); /* alternate value when DONE && FIRST */
236 if (alt_dest >= 0 && dry_run) {
237 pathjoin(prev_name, MAXPATHLEN, basis_dir[alt_dest],
238 f_name(prev_file, NULL));
239 f_name(prev_file, altbuf);
242 f_name(prev_file, prev_name);
243 realname = prev_name;
246 if (link_stat(prev_name, &prev_st, 0) < 0) {
247 rsyserr(FERROR, errno, "stat %s failed",
248 full_fname(prev_name));
252 if (statret < 0 && basis_dir[0] != NULL) {
253 /* If we match an alt-dest item, we don't output this as a change. */
254 char cmpbuf[MAXPATHLEN];
258 alt_sx.acc_acl = alt_sx.def_acl = NULL;
261 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
262 if (link_stat(cmpbuf, &alt_sx.st, 0) < 0)
265 if (prev_st.st_dev != alt_sx.st.st_dev
266 || prev_st.st_ino != alt_sx.st.st_ino)
269 if (verbose < 2 || !stdout_format_has_i) {
272 if (verbose > 1 && maybe_ATTRS_REPORT)
273 rprintf(FCLIENT, "%s is uptodate\n", fname);
277 if (!unchanged_file(cmpbuf, file, &alt_sx.st))
280 if (unchanged_attrs(cmpbuf, file, &alt_sx))
282 } while (basis_dir[++j] != NULL);
286 if (preserve_acls && !S_ISLNK(file->mode)) {
287 if (!ACL_READY(*sxp))
288 get_acl(cmpbuf, sxp);
290 sxp->acc_acl = alt_sx.acc_acl;
291 sxp->def_acl = alt_sx.def_acl;
297 else if (preserve_acls)
302 if (maybe_hard_link(file, ndx, fname, statret, sxp, prev_name, &prev_st,
303 realname, itemizing, code) < 0)
306 if (remove_source_files == 1 && do_xfers)
307 send_msg_int(MSG_SUCCESS, ndx);
312 int hard_link_one(struct file_struct *file, const char *fname,
313 const char *oldname, int terse)
315 if (do_link(oldname, fname) < 0) {
323 rsyserr(code, errno, "link %s => %s failed",
324 full_fname(fname), oldname);
328 file->flags |= FLAG_HLINK_DONE;
333 void finish_hard_link(struct file_struct *file, const char *fname,
334 STRUCT_STAT *stp, int itemizing, enum logcode code,
339 char alt_name[MAXPATHLEN], *prev_name;
340 const char *our_name;
341 int prev_statret, ndx, prev_ndx = F_HL_PREV(file);
343 if (stp == NULL && prev_ndx >= 0) {
344 if (link_stat(fname, &st, 0) < 0) {
345 rsyserr(FERROR, errno, "stat %s failed",
352 /* FIRST combined with DONE means we were the first to get done. */
353 file->flags |= FLAG_HLINK_FIRST | FLAG_HLINK_DONE;
354 F_HL_PREV(file) = alt_dest;
355 if (alt_dest >= 0 && dry_run) {
356 pathjoin(alt_name, MAXPATHLEN, basis_dir[alt_dest],
363 prev_sx.acc_acl = prev_sx.def_acl = NULL;
366 while ((ndx = prev_ndx) >= 0) {
368 file = cur_flist->files[ndx];
369 file->flags = (file->flags & ~FLAG_HLINK_FIRST) | FLAG_HLINK_DONE;
370 prev_ndx = F_HL_PREV(file);
371 prev_name = f_name(file, NULL);
372 prev_statret = link_stat(prev_name, &prev_sx.st, 0);
373 val = maybe_hard_link(file, ndx, prev_name, prev_statret, &prev_sx,
374 our_name, stp, fname, itemizing, code);
381 if (remove_source_files == 1 && do_xfers)
382 send_msg_int(MSG_SUCCESS, ndx);