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 | |
8e41b68e WD |
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. | |
0f78b815 WD |
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 | * | |
e7c67065 | 19 | * You should have received a copy of the GNU General Public License along |
4fd842f9 | 20 | * with this program; if not, visit the http://fsf.org website. |
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; |
332cf6df WD |
37 | #ifdef ICONV_OPTION |
38 | extern int ic_ndx; | |
39 | #endif | |
dc5ddbcc | 40 | |
4f5b0756 | 41 | #ifdef SUPPORT_HARD_LINKS |
9f2e3c3f | 42 | |
c905bf37 WD |
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. */ | |
47 | ||
62606570 | 48 | struct hashtable *dev_tbl; |
9f2e3c3f | 49 | |
aadc84d3 WD |
50 | void init_hard_links(void) |
51 | { | |
62606570 | 52 | dev_tbl = hashtable_create(16, SIZEOF_INT64 == 8); |
c905bf37 WD |
53 | } |
54 | ||
62606570 | 55 | struct ht_int64_node *idev_find(int64 dev, int64 ino) |
c905bf37 | 56 | { |
62606570 WD |
57 | static struct ht_int64_node *dev_node = NULL; |
58 | struct hashtable *tbl; | |
c905bf37 WD |
59 | |
60 | if (!dev_node || dev_node->key != dev) { | |
61 | /* We keep a separate hash table of inodes for every device. */ | |
62606570 | 62 | dev_node = hashtable_find(dev_tbl, dev, 1); |
c905bf37 | 63 | if (!(tbl = dev_node->data)) |
62606570 | 64 | tbl = dev_node->data = hashtable_create(512, SIZEOF_INT64 == 8); |
c905bf37 WD |
65 | } else |
66 | tbl = dev_node->data; | |
67 | ||
62606570 | 68 | return hashtable_find(tbl, ino, 1); |
c905bf37 WD |
69 | } |
70 | ||
71 | void idev_destroy(void) | |
72 | { | |
73 | int i; | |
74 | ||
75 | for (i = 0; i < dev_tbl->size; i++) { | |
62606570 WD |
76 | struct ht_int32_node *node = HT_NODE(dev_tbl, dev_tbl->nodes, i); |
77 | if (node->data) | |
78 | hashtable_destroy(node->data); | |
c905bf37 WD |
79 | } |
80 | ||
62606570 | 81 | hashtable_destroy(dev_tbl); |
c905bf37 WD |
82 | } |
83 | ||
c905bf37 | 84 | static int hlink_compare_gnum(int *int1, int *int2) |
1d5cda22 | 85 | { |
332cf6df WD |
86 | struct file_struct *f1 = cur_flist->sorted[*int1]; |
87 | struct file_struct *f2 = cur_flist->sorted[*int2]; | |
719985cb WD |
88 | int32 gnum1 = F_HL_GNUM(f1); |
89 | int32 gnum2 = F_HL_GNUM(f2); | |
1d5cda22 | 90 | |
c905bf37 WD |
91 | if (gnum1 != gnum2) |
92 | return gnum1 > gnum2 ? 1 : -1; | |
9935066b | 93 | |
c905bf37 WD |
94 | return *int1 > *int2 ? 1 : -1; |
95 | } | |
9935066b | 96 | |
c905bf37 WD |
97 | static void match_gnums(int32 *ndx_list, int ndx_count) |
98 | { | |
99 | int32 from, prev; | |
100 | struct file_struct *file, *file_next; | |
719985cb | 101 | int32 gnum, gnum_next; |
c905bf37 WD |
102 | |
103 | qsort(ndx_list, ndx_count, sizeof ndx_list[0], | |
104 | (int (*)()) hlink_compare_gnum); | |
105 | ||
106 | for (from = 0; from < ndx_count; from++) { | |
332cf6df | 107 | for (file = cur_flist->sorted[ndx_list[from]], gnum = F_HL_GNUM(file), prev = -1; |
c905bf37 | 108 | from < ndx_count-1; |
3d0a159d | 109 | file = file_next, gnum = gnum_next, from++) /*SHARED ITERATOR*/ |
c905bf37 | 110 | { |
332cf6df | 111 | file_next = cur_flist->sorted[ndx_list[from+1]]; |
c905bf37 WD |
112 | gnum_next = F_HL_GNUM(file_next); |
113 | if (gnum != gnum_next) | |
114 | break; | |
115 | if (prev < 0) | |
116 | file->flags |= FLAG_HLINK_FIRST; | |
117 | F_HL_PREV(file) = prev; | |
332cf6df WD |
118 | /* The linked list must use raw ndx values. */ |
119 | #ifdef ICONV_OPTION | |
120 | if (ic_ndx) | |
121 | prev = F_NDX(file); | |
122 | else | |
123 | #endif | |
124 | prev = ndx_list[from]; | |
c905bf37 WD |
125 | } |
126 | if (prev < 0) | |
127 | file->flags &= ~FLAG_HLINKED; | |
128 | else { | |
129 | file->flags |= FLAG_HLINK_LAST; | |
130 | F_HL_PREV(file) = prev; | |
131 | } | |
132 | } | |
133 | } | |
134 | ||
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) | |
140 | { | |
141 | int i, ndx_count = 0; | |
142 | int32 *ndx_list; | |
143 | ||
9decb4d2 | 144 | if (!(ndx_list = new_array(int32, cur_flist->used))) |
c905bf37 WD |
145 | out_of_memory("match_hard_links"); |
146 | ||
9decb4d2 | 147 | for (i = 0; i < cur_flist->used; i++) { |
332cf6df | 148 | if (F_IS_HLINKED(cur_flist->sorted[i])) |
c905bf37 WD |
149 | ndx_list[ndx_count++] = i; |
150 | } | |
1d5cda22 | 151 | |
007996b4 WD |
152 | if (ndx_count) |
153 | match_gnums(ndx_list, ndx_count); | |
154 | ||
aadc84d3 | 155 | free(ndx_list); |
007996b4 WD |
156 | if (protocol_version < 30) |
157 | idev_destroy(); | |
dc5ddbcc AT |
158 | } |
159 | ||
3cd5301f | 160 | static int maybe_hard_link(struct file_struct *file, int ndx, |
1c3344a1 | 161 | const char *fname, int statret, statx *sxp, |
aadc84d3 WD |
162 | const char *oldname, STRUCT_STAT *old_stp, |
163 | const char *realname, int itemizing, enum logcode code) | |
3cd5301f WD |
164 | { |
165 | if (statret == 0) { | |
1c3344a1 WD |
166 | if (sxp->st.st_dev == old_stp->st_dev |
167 | && sxp->st.st_ino == old_stp->st_ino) { | |
3cd5301f | 168 | if (itemizing) { |
1c3344a1 | 169 | itemize(fname, file, ndx, statret, sxp, |
3cd5301f WD |
170 | ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, |
171 | 0, ""); | |
172 | } | |
aadc84d3 WD |
173 | if (verbose > 1 && maybe_ATTRS_REPORT) |
174 | rprintf(FCLIENT, "%s is uptodate\n", fname); | |
175 | file->flags |= FLAG_HLINK_DONE; | |
3cd5301f WD |
176 | return 0; |
177 | } | |
3ac830b9 | 178 | if (make_backups > 0) { |
3cd5301f WD |
179 | if (!make_backup(fname)) |
180 | return -1; | |
181 | } else if (robust_unlink(fname)) { | |
182 | rsyserr(FERROR, errno, "unlink %s failed", | |
183 | full_fname(fname)); | |
184 | return -1; | |
185 | } | |
186 | } | |
aadc84d3 WD |
187 | |
188 | if (hard_link_one(file, fname, oldname, 0)) { | |
189 | if (itemizing) { | |
1c3344a1 | 190 | itemize(fname, file, ndx, statret, sxp, |
aadc84d3 WD |
191 | ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0, |
192 | realname); | |
193 | } | |
194 | if (code != FNONE && verbose) | |
195 | rprintf(code, "%s => %s\n", fname, realname); | |
196 | return 0; | |
197 | } | |
198 | return -1; | |
3cd5301f | 199 | } |
3cd5301f | 200 | |
aadc84d3 WD |
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. */ | |
a2ebbffc | 203 | int hard_link_check(struct file_struct *file, int ndx, const char *fname, |
1c3344a1 | 204 | int statret, statx *sxp, int itemizing, |
aadc84d3 | 205 | enum logcode code) |
d38fc305 | 206 | { |
aadc84d3 WD |
207 | STRUCT_STAT prev_st; |
208 | char prev_name[MAXPATHLEN], altbuf[MAXPATHLEN], *realname; | |
209 | int alt_dest, prev_ndx = F_HL_PREV(file); | |
332cf6df | 210 | struct file_struct *prev_file = cur_flist->files[prev_ndx]; |
aadc84d3 WD |
211 | |
212 | /* Is the previous link is not complete yet? */ | |
213 | if (!(prev_file->flags & FLAG_HLINK_DONE)) { | |
214 | /* Is the previous link being transferred? */ | |
3ac830b9 | 215 | if (prev_file->flags & FLAG_FILE_SENT) { |
aadc84d3 WD |
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; | |
3ac830b9 | 221 | file->flags |= FLAG_FILE_SENT; |
aadc84d3 | 222 | return 1; |
6cbde57d | 223 | } |
aadc84d3 WD |
224 | return 0; |
225 | } | |
226 | ||
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); | |
332cf6df | 231 | prev_file = cur_flist->files[prev_ndx]; |
aadc84d3 WD |
232 | /* Update our previous pointer to point to the first. */ |
233 | F_HL_PREV(file) = prev_ndx; | |
234 | } | |
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); | |
240 | realname = altbuf; | |
241 | } else { | |
242 | f_name(prev_file, prev_name); | |
243 | realname = prev_name; | |
244 | } | |
245 | ||
246 | if (link_stat(prev_name, &prev_st, 0) < 0) { | |
247 | rsyserr(FERROR, errno, "stat %s failed", | |
248 | full_fname(prev_name)); | |
249 | return -1; | |
250 | } | |
251 | ||
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]; | |
1c3344a1 | 255 | statx alt_sx; |
aadc84d3 | 256 | int j = 0; |
1c3344a1 WD |
257 | #ifdef SUPPORT_ACLS |
258 | alt_sx.acc_acl = alt_sx.def_acl = NULL; | |
259 | #endif | |
aadc84d3 WD |
260 | do { |
261 | pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname); | |
1c3344a1 | 262 | if (link_stat(cmpbuf, &alt_sx.st, 0) < 0) |
aadc84d3 WD |
263 | continue; |
264 | if (link_dest) { | |
1c3344a1 WD |
265 | if (prev_st.st_dev != alt_sx.st.st_dev |
266 | || prev_st.st_ino != alt_sx.st.st_ino) | |
aadc84d3 WD |
267 | continue; |
268 | statret = 1; | |
aadc84d3 WD |
269 | if (verbose < 2 || !stdout_format_has_i) { |
270 | itemizing = 0; | |
271 | code = FNONE; | |
272 | if (verbose > 1 && maybe_ATTRS_REPORT) | |
273 | rprintf(FCLIENT, "%s is uptodate\n", fname); | |
274 | } | |
275 | break; | |
541b23d1 | 276 | } |
1c3344a1 | 277 | if (!unchanged_file(cmpbuf, file, &alt_sx.st)) |
aadc84d3 WD |
278 | continue; |
279 | statret = 1; | |
1c3344a1 | 280 | if (unchanged_attrs(cmpbuf, file, &alt_sx)) |
aadc84d3 WD |
281 | break; |
282 | } while (basis_dir[++j] != NULL); | |
1c3344a1 WD |
283 | if (statret == 1) { |
284 | sxp->st = alt_sx.st; | |
285 | #ifdef SUPPORT_ACLS | |
286 | if (preserve_acls && !S_ISLNK(file->mode)) { | |
287 | if (!ACL_READY(*sxp)) | |
288 | get_acl(cmpbuf, sxp); | |
289 | else { | |
290 | sxp->acc_acl = alt_sx.acc_acl; | |
291 | sxp->def_acl = alt_sx.def_acl; | |
292 | } | |
293 | } | |
294 | #endif | |
295 | } | |
296 | #ifdef SUPPORT_ACLS | |
297 | else if (preserve_acls) | |
298 | free_acl(&alt_sx); | |
299 | #endif | |
d38fc305 | 300 | } |
aadc84d3 | 301 | |
1c3344a1 | 302 | if (maybe_hard_link(file, ndx, fname, statret, sxp, prev_name, &prev_st, |
aadc84d3 WD |
303 | realname, itemizing, code) < 0) |
304 | return -1; | |
305 | ||
306 | if (remove_source_files == 1 && do_xfers) | |
307 | send_msg_int(MSG_SUCCESS, ndx); | |
308 | ||
309 | return 1; | |
d38fc305 WD |
310 | } |
311 | ||
aadc84d3 WD |
312 | int hard_link_one(struct file_struct *file, const char *fname, |
313 | const char *oldname, int terse) | |
a16bbc39 | 314 | { |
aadc84d3 WD |
315 | if (do_link(oldname, fname) < 0) { |
316 | enum logcode code; | |
3cd5301f WD |
317 | if (terse) { |
318 | if (!verbose) | |
319 | return -1; | |
320 | code = FINFO; | |
321 | } else | |
322 | code = FERROR; | |
323 | rsyserr(code, errno, "link %s => %s failed", | |
aadc84d3 WD |
324 | full_fname(fname), oldname); |
325 | return 0; | |
9f2e3c3f WD |
326 | } |
327 | ||
aadc84d3 WD |
328 | file->flags |= FLAG_HLINK_DONE; |
329 | ||
330 | return 1; | |
a16bbc39 | 331 | } |
fae5bb31 | 332 | |
aadc84d3 WD |
333 | void finish_hard_link(struct file_struct *file, const char *fname, |
334 | STRUCT_STAT *stp, int itemizing, enum logcode code, | |
335 | int alt_dest) | |
dc5ddbcc | 336 | { |
1c3344a1 WD |
337 | statx prev_sx; |
338 | STRUCT_STAT st; | |
aadc84d3 WD |
339 | char alt_name[MAXPATHLEN], *prev_name; |
340 | const char *our_name; | |
341 | int prev_statret, ndx, prev_ndx = F_HL_PREV(file); | |
342 | ||
343 | if (stp == NULL && prev_ndx >= 0) { | |
344 | if (link_stat(fname, &st, 0) < 0) { | |
345 | rsyserr(FERROR, errno, "stat %s failed", | |
346 | full_fname(fname)); | |
347 | return; | |
9f2e3c3f | 348 | } |
aadc84d3 | 349 | stp = &st; |
9f2e3c3f | 350 | } |
aadc84d3 WD |
351 | |
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], | |
357 | f_name(file, NULL)); | |
358 | our_name = alt_name; | |
359 | } else | |
360 | our_name = fname; | |
361 | ||
1c3344a1 WD |
362 | #ifdef SUPPORT_ACLS |
363 | prev_sx.acc_acl = prev_sx.def_acl = NULL; | |
364 | #endif | |
365 | ||
aadc84d3 | 366 | while ((ndx = prev_ndx) >= 0) { |
1c3344a1 | 367 | int val; |
332cf6df | 368 | file = cur_flist->files[ndx]; |
aadc84d3 WD |
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); | |
1c3344a1 WD |
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); | |
375 | #ifdef SUPPORT_ACLS | |
376 | if (preserve_acls) | |
377 | free_acl(&prev_sx); | |
378 | #endif | |
379 | if (val < 0) | |
1d5cda22 | 380 | continue; |
663b2857 WD |
381 | if (remove_source_files == 1 && do_xfers) |
382 | send_msg_int(MSG_SUCCESS, ndx); | |
aadc84d3 | 383 | } |
dc5ddbcc | 384 | } |
a2ebbffc | 385 | #endif |