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> | |
7 | * Copyright (C) 2004, 2005, 2006 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 2 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 | * | |
e7c67065 WD |
19 | * You should have received a copy of the GNU General Public License along |
20 | * with this program; if not, write to the Free Software Foundation, Inc., | |
21 | * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. | |
0f78b815 | 22 | */ |
dc5ddbcc AT |
23 | |
24 | #include "rsync.h" | |
25 | ||
9a52223b | 26 | extern int verbose; |
841d9436 | 27 | extern int do_xfers; |
541b23d1 | 28 | extern int link_dest; |
417c99f6 | 29 | extern int make_backups; |
47c11975 | 30 | extern int remove_source_files; |
20f90d5e | 31 | extern int stdout_format_has_i; |
541b23d1 | 32 | extern char *basis_dir[]; |
9f2e3c3f | 33 | extern struct file_list *the_file_list; |
dc5ddbcc | 34 | |
4f5b0756 | 35 | #ifdef SUPPORT_HARD_LINKS |
9f2e3c3f | 36 | |
3cd5301f WD |
37 | #define SKIPPED_LINK (-1) |
38 | #define FINISHED_LINK (-2) | |
39 | ||
9f2e3c3f WD |
40 | #define FPTR(i) (the_file_list->files[i]) |
41 | #define LINKED(p1,p2) (FPTR(p1)->F_DEV == FPTR(p2)->F_DEV \ | |
42 | && FPTR(p1)->F_INODE == FPTR(p2)->F_INODE) | |
43 | ||
44 | static int hlink_compare(int *int1, int *int2) | |
dc5ddbcc | 45 | { |
9f2e3c3f WD |
46 | struct file_struct *f1 = FPTR(*int1); |
47 | struct file_struct *f2 = FPTR(*int2); | |
11dc2740 | 48 | |
92cc9dd7 WD |
49 | if (f1->F_DEV != f2->F_DEV) |
50 | return (int) (f1->F_DEV > f2->F_DEV ? 1 : -1); | |
dc5ddbcc | 51 | |
92cc9dd7 WD |
52 | if (f1->F_INODE != f2->F_INODE) |
53 | return (int) (f1->F_INODE > f2->F_INODE ? 1 : -1); | |
dc5ddbcc | 54 | |
122d1771 | 55 | return f_name_cmp(f1, f2); |
11dc2740 | 56 | } |
dc5ddbcc | 57 | |
1a05de2b WD |
58 | static int32 *hlink_list; |
59 | static int32 hlink_count; | |
1d5cda22 | 60 | |
1d5cda22 | 61 | /* Analyze the data in the hlink_list[], remove items that aren't multiply |
90481755 | 62 | * linked, and replace the dev+inode data with the hlindex+next linked list. */ |
9f2e3c3f | 63 | static void link_idev_data(void) |
1d5cda22 | 64 | { |
1a05de2b | 65 | int32 cur, from, to, start; |
1d5cda22 | 66 | |
9935066b | 67 | alloc_pool_t hlink_pool; |
9f2e3c3f | 68 | alloc_pool_t idev_pool = the_file_list->hlink_pool; |
9935066b S |
69 | |
70 | hlink_pool = pool_create(128 * 1024, sizeof (struct hlink), | |
71 | out_of_memory, POOL_INTERN); | |
72 | ||
1d5cda22 WD |
73 | for (from = to = 0; from < hlink_count; from++) { |
74 | start = from; | |
97e786c3 WD |
75 | while (1) { |
76 | cur = hlink_list[from]; | |
77 | if (from == hlink_count-1 | |
78 | || !LINKED(cur, hlink_list[from+1])) | |
79 | break; | |
80 | pool_free(idev_pool, 0, FPTR(cur)->link_u.idev); | |
81 | FPTR(cur)->link_u.links = pool_talloc(hlink_pool, | |
9935066b S |
82 | struct hlink, 1, "hlink_list"); |
83 | ||
97e786c3 WD |
84 | FPTR(cur)->F_HLINDEX = to; |
85 | FPTR(cur)->F_NEXT = hlink_list[++from]; | |
7b4a40ed | 86 | FPTR(cur)->link_u.links->link_dest_used = 0; |
1d5cda22 | 87 | } |
97e786c3 | 88 | pool_free(idev_pool, 0, FPTR(cur)->link_u.idev); |
1d5cda22 | 89 | if (from > start) { |
97e786c3 WD |
90 | int head = hlink_list[start]; |
91 | FPTR(cur)->link_u.links = pool_talloc(hlink_pool, | |
9935066b S |
92 | struct hlink, 1, "hlink_list"); |
93 | ||
9f2e3c3f | 94 | FPTR(head)->flags |= FLAG_HLINK_TOL; |
97e786c3 WD |
95 | FPTR(cur)->F_HLINDEX = to; |
96 | FPTR(cur)->F_NEXT = head; | |
97 | FPTR(cur)->flags |= FLAG_HLINK_EOL; | |
7b4a40ed | 98 | FPTR(cur)->link_u.links->link_dest_used = 0; |
1d5cda22 | 99 | hlink_list[to++] = head; |
97e786c3 WD |
100 | } else |
101 | FPTR(cur)->link_u.links = NULL; | |
1d5cda22 WD |
102 | } |
103 | ||
104 | if (!to) { | |
105 | free(hlink_list); | |
106 | hlink_list = NULL; | |
9935066b S |
107 | pool_destroy(hlink_pool); |
108 | hlink_pool = NULL; | |
1d5cda22 WD |
109 | } else { |
110 | hlink_count = to; | |
1a05de2b | 111 | hlink_list = realloc_array(hlink_list, int32, hlink_count); |
9f2e3c3f | 112 | if (!hlink_list) |
1d5cda22 WD |
113 | out_of_memory("init_hard_links"); |
114 | } | |
9f2e3c3f | 115 | the_file_list->hlink_pool = hlink_pool; |
9935066b | 116 | pool_destroy(idev_pool); |
1d5cda22 | 117 | } |
dc5ddbcc AT |
118 | #endif |
119 | ||
9f2e3c3f | 120 | void init_hard_links(void) |
dc5ddbcc | 121 | { |
4f5b0756 | 122 | #ifdef SUPPORT_HARD_LINKS |
dd04a034 | 123 | int i; |
11dc2740 | 124 | |
6e69cff1 MP |
125 | if (hlink_list) |
126 | free(hlink_list); | |
127 | ||
1a05de2b | 128 | if (!(hlink_list = new_array(int32, the_file_list->count))) |
dd04a034 | 129 | out_of_memory("init_hard_links"); |
6aae748e | 130 | |
11dc2740 | 131 | hlink_count = 0; |
9f2e3c3f WD |
132 | for (i = 0; i < the_file_list->count; i++) { |
133 | if (FPTR(i)->link_u.idev) | |
134 | hlink_list[hlink_count++] = i; | |
11dc2740 | 135 | } |
dc5ddbcc | 136 | |
11dc2740 | 137 | qsort(hlink_list, hlink_count, |
0d6e308b | 138 | sizeof hlink_list[0], (int (*)()) hlink_compare); |
dc5ddbcc | 139 | |
6aae748e WD |
140 | if (!hlink_count) { |
141 | free(hlink_list); | |
142 | hlink_list = NULL; | |
1d5cda22 | 143 | } else |
9f2e3c3f | 144 | link_idev_data(); |
dc5ddbcc | 145 | #endif |
dc5ddbcc AT |
146 | } |
147 | ||
3cd5301f WD |
148 | #ifdef SUPPORT_HARD_LINKS |
149 | static int maybe_hard_link(struct file_struct *file, int ndx, | |
150 | char *fname, int statret, STRUCT_STAT *st, | |
151 | char *toname, STRUCT_STAT *to_st, | |
152 | int itemizing, enum logcode code) | |
153 | { | |
154 | if (statret == 0) { | |
155 | if (st->st_dev == to_st->st_dev | |
156 | && st->st_ino == to_st->st_ino) { | |
157 | if (itemizing) { | |
158 | itemize(file, ndx, statret, st, | |
159 | ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, | |
160 | 0, ""); | |
161 | } | |
162 | return 0; | |
163 | } | |
164 | if (make_backups) { | |
165 | if (!make_backup(fname)) | |
166 | return -1; | |
167 | } else if (robust_unlink(fname)) { | |
168 | rsyserr(FERROR, errno, "unlink %s failed", | |
169 | full_fname(fname)); | |
170 | return -1; | |
171 | } | |
172 | } | |
173 | return hard_link_one(file, ndx, fname, statret, st, toname, | |
174 | 0, itemizing, code); | |
175 | } | |
176 | #endif | |
177 | ||
178 | int hard_link_check(struct file_struct *file, int ndx, char *fname, | |
179 | int statret, STRUCT_STAT *st, int itemizing, | |
180 | enum logcode code, int skip) | |
d38fc305 | 181 | { |
4f5b0756 | 182 | #ifdef SUPPORT_HARD_LINKS |
3cd5301f | 183 | int head; |
d38fc305 | 184 | if (skip && !(file->flags & FLAG_HLINK_EOL)) |
3cd5301f WD |
185 | head = hlink_list[file->F_HLINDEX] = file->F_NEXT; |
186 | else | |
187 | head = hlink_list[file->F_HLINDEX]; | |
188 | if (ndx != head) { | |
189 | struct file_struct *head_file = FPTR(head); | |
20f90d5e | 190 | if (!stdout_format_has_i && verbose > 1) { |
6cbde57d WD |
191 | rprintf(FINFO, "\"%s\" is a hard link\n", |
192 | f_name(file, NULL)); | |
193 | } | |
3cd5301f | 194 | if (head_file->F_HLINDEX == FINISHED_LINK) { |
541b23d1 | 195 | STRUCT_STAT st2, st3; |
1a05de2b WD |
196 | char toname[MAXPATHLEN]; |
197 | int ldu = head_file->link_u.links->link_dest_used; | |
198 | if (ldu) { | |
199 | pathjoin(toname, MAXPATHLEN, basis_dir[ldu-1], | |
200 | f_name(head_file, NULL)); | |
201 | } else | |
202 | f_name(head_file, toname); | |
3cd5301f WD |
203 | if (link_stat(toname, &st2, 0) < 0) { |
204 | rsyserr(FERROR, errno, "stat %s failed", | |
205 | full_fname(toname)); | |
206 | return -1; | |
207 | } | |
541b23d1 WD |
208 | if (statret < 0 && basis_dir[0] != NULL) { |
209 | char cmpbuf[MAXPATHLEN]; | |
210 | int j = 0; | |
211 | do { | |
212 | pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname); | |
213 | if (link_stat(cmpbuf, &st3, 0) < 0) | |
214 | continue; | |
215 | if (link_dest) { | |
216 | if (st2.st_dev != st3.st_dev | |
217 | || st2.st_ino != st3.st_ino) | |
218 | continue; | |
219 | statret = 1; | |
220 | st = &st3; | |
36e2ea60 WD |
221 | if (verbose < 2 || !stdout_format_has_i) { |
222 | itemizing = 0; | |
223 | code = FNONE; | |
224 | } | |
541b23d1 WD |
225 | break; |
226 | } | |
227 | if (!unchanged_file(cmpbuf, file, &st3)) | |
228 | continue; | |
229 | statret = 1; | |
230 | st = &st3; | |
231 | if (unchanged_attrs(file, &st3)) | |
232 | break; | |
233 | } while (basis_dir[++j] != NULL); | |
234 | } | |
3cd5301f WD |
235 | maybe_hard_link(file, ndx, fname, statret, st, |
236 | toname, &st2, itemizing, code); | |
47c11975 | 237 | if (remove_source_files == 1 && do_xfers) { |
841d9436 WD |
238 | char numbuf[4]; |
239 | SIVAL(numbuf, 0, ndx); | |
240 | send_msg(MSG_SUCCESS, numbuf, 4); | |
241 | } | |
3cd5301f WD |
242 | file->F_HLINDEX = FINISHED_LINK; |
243 | } else | |
244 | file->F_HLINDEX = SKIPPED_LINK; | |
d38fc305 WD |
245 | return 1; |
246 | } | |
bb91a624 | 247 | #endif |
d38fc305 WD |
248 | return 0; |
249 | } | |
250 | ||
4f5b0756 | 251 | #ifdef SUPPORT_HARD_LINKS |
9f2e3c3f WD |
252 | int hard_link_one(struct file_struct *file, int ndx, char *fname, |
253 | int statret, STRUCT_STAT *st, char *toname, int terse, | |
254 | int itemizing, enum logcode code) | |
a16bbc39 | 255 | { |
9f2e3c3f | 256 | if (do_link(toname, fname)) { |
3cd5301f WD |
257 | if (terse) { |
258 | if (!verbose) | |
259 | return -1; | |
260 | code = FINFO; | |
261 | } else | |
262 | code = FERROR; | |
263 | rsyserr(code, errno, "link %s => %s failed", | |
45c49b52 | 264 | full_fname(fname), toname); |
9f2e3c3f WD |
265 | return -1; |
266 | } | |
267 | ||
268 | if (itemizing) { | |
b7d4d28b WD |
269 | itemize(file, ndx, statret, st, |
270 | ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0, | |
9f2e3c3f WD |
271 | terse ? "" : toname); |
272 | } | |
36e2ea60 | 273 | if (code != FNONE && verbose && !terse) |
45c49b52 | 274 | rprintf(code, "%s => %s\n", fname, toname); |
9f2e3c3f | 275 | return 0; |
a16bbc39 AT |
276 | } |
277 | #endif | |
278 | ||
fae5bb31 | 279 | |
9f2e3c3f WD |
280 | void hard_link_cluster(struct file_struct *file, int master, int itemizing, |
281 | enum logcode code) | |
dc5ddbcc | 282 | { |
4f5b0756 | 283 | #ifdef SUPPORT_HARD_LINKS |
3fef5364 WD |
284 | char hlink1[MAXPATHLEN]; |
285 | char *hlink2; | |
1d5cda22 | 286 | STRUCT_STAT st1, st2; |
9f2e3c3f | 287 | int statret, ndx = master; |
a16bbc39 | 288 | |
3cd5301f | 289 | file->F_HLINDEX = FINISHED_LINK; |
6cbde57d | 290 | if (link_stat(f_name(file, hlink1), &st1, 0) < 0) |
6e69cff1 | 291 | return; |
9f2e3c3f WD |
292 | if (!(file->flags & FLAG_HLINK_TOL)) { |
293 | while (!(file->flags & FLAG_HLINK_EOL)) { | |
294 | ndx = file->F_NEXT; | |
295 | file = FPTR(ndx); | |
296 | } | |
297 | } | |
298 | do { | |
299 | ndx = file->F_NEXT; | |
300 | file = FPTR(ndx); | |
3cd5301f | 301 | if (file->F_HLINDEX != SKIPPED_LINK) |
1d5cda22 | 302 | continue; |
6cbde57d | 303 | hlink2 = f_name(file, NULL); |
3cd5301f WD |
304 | statret = link_stat(hlink2, &st2, 0); |
305 | maybe_hard_link(file, ndx, hlink2, statret, &st2, | |
306 | hlink1, &st1, itemizing, code); | |
47c11975 | 307 | if (remove_source_files == 1 && do_xfers) { |
841d9436 WD |
308 | char numbuf[4]; |
309 | SIVAL(numbuf, 0, ndx); | |
310 | send_msg(MSG_SUCCESS, numbuf, 4); | |
311 | } | |
3cd5301f | 312 | file->F_HLINDEX = FINISHED_LINK; |
9f2e3c3f | 313 | } while (!(file->flags & FLAG_HLINK_EOL)); |
dc5ddbcc AT |
314 | #endif |
315 | } |