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 | |
9f2e3c3f | 58 | static int *hlink_list; |
417c99f6 | 59 | static int 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 | { |
97e786c3 | 65 | int 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]; | |
1d5cda22 | 86 | } |
97e786c3 | 87 | pool_free(idev_pool, 0, FPTR(cur)->link_u.idev); |
1d5cda22 | 88 | if (from > start) { |
97e786c3 WD |
89 | int head = hlink_list[start]; |
90 | FPTR(cur)->link_u.links = pool_talloc(hlink_pool, | |
9935066b S |
91 | struct hlink, 1, "hlink_list"); |
92 | ||
9f2e3c3f | 93 | FPTR(head)->flags |= FLAG_HLINK_TOL; |
97e786c3 WD |
94 | FPTR(cur)->F_HLINDEX = to; |
95 | FPTR(cur)->F_NEXT = head; | |
96 | FPTR(cur)->flags |= FLAG_HLINK_EOL; | |
1d5cda22 | 97 | hlink_list[to++] = head; |
97e786c3 WD |
98 | } else |
99 | FPTR(cur)->link_u.links = NULL; | |
1d5cda22 WD |
100 | } |
101 | ||
102 | if (!to) { | |
103 | free(hlink_list); | |
104 | hlink_list = NULL; | |
9935066b S |
105 | pool_destroy(hlink_pool); |
106 | hlink_pool = NULL; | |
1d5cda22 WD |
107 | } else { |
108 | hlink_count = to; | |
9f2e3c3f WD |
109 | hlink_list = realloc_array(hlink_list, int, hlink_count); |
110 | if (!hlink_list) | |
1d5cda22 WD |
111 | out_of_memory("init_hard_links"); |
112 | } | |
9f2e3c3f | 113 | the_file_list->hlink_pool = hlink_pool; |
9935066b | 114 | pool_destroy(idev_pool); |
1d5cda22 | 115 | } |
dc5ddbcc AT |
116 | #endif |
117 | ||
9f2e3c3f | 118 | void init_hard_links(void) |
dc5ddbcc | 119 | { |
4f5b0756 | 120 | #ifdef SUPPORT_HARD_LINKS |
dd04a034 | 121 | int i; |
11dc2740 | 122 | |
6e69cff1 MP |
123 | if (hlink_list) |
124 | free(hlink_list); | |
125 | ||
9f2e3c3f | 126 | if (!(hlink_list = new_array(int, the_file_list->count))) |
dd04a034 | 127 | out_of_memory("init_hard_links"); |
6aae748e | 128 | |
11dc2740 | 129 | hlink_count = 0; |
9f2e3c3f WD |
130 | for (i = 0; i < the_file_list->count; i++) { |
131 | if (FPTR(i)->link_u.idev) | |
132 | hlink_list[hlink_count++] = i; | |
11dc2740 | 133 | } |
dc5ddbcc | 134 | |
11dc2740 | 135 | qsort(hlink_list, hlink_count, |
0d6e308b | 136 | sizeof hlink_list[0], (int (*)()) hlink_compare); |
dc5ddbcc | 137 | |
6aae748e WD |
138 | if (!hlink_count) { |
139 | free(hlink_list); | |
140 | hlink_list = NULL; | |
1d5cda22 | 141 | } else |
9f2e3c3f | 142 | link_idev_data(); |
dc5ddbcc | 143 | #endif |
dc5ddbcc AT |
144 | } |
145 | ||
3cd5301f WD |
146 | #ifdef SUPPORT_HARD_LINKS |
147 | static int maybe_hard_link(struct file_struct *file, int ndx, | |
148 | char *fname, int statret, STRUCT_STAT *st, | |
149 | char *toname, STRUCT_STAT *to_st, | |
150 | int itemizing, enum logcode code) | |
151 | { | |
152 | if (statret == 0) { | |
153 | if (st->st_dev == to_st->st_dev | |
154 | && st->st_ino == to_st->st_ino) { | |
155 | if (itemizing) { | |
156 | itemize(file, ndx, statret, st, | |
157 | ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, | |
158 | 0, ""); | |
159 | } | |
160 | return 0; | |
161 | } | |
162 | if (make_backups) { | |
163 | if (!make_backup(fname)) | |
164 | return -1; | |
165 | } else if (robust_unlink(fname)) { | |
166 | rsyserr(FERROR, errno, "unlink %s failed", | |
167 | full_fname(fname)); | |
168 | return -1; | |
169 | } | |
170 | } | |
171 | return hard_link_one(file, ndx, fname, statret, st, toname, | |
172 | 0, itemizing, code); | |
173 | } | |
174 | #endif | |
175 | ||
176 | int hard_link_check(struct file_struct *file, int ndx, char *fname, | |
177 | int statret, STRUCT_STAT *st, int itemizing, | |
178 | enum logcode code, int skip) | |
d38fc305 | 179 | { |
4f5b0756 | 180 | #ifdef SUPPORT_HARD_LINKS |
3cd5301f | 181 | int head; |
d38fc305 | 182 | if (skip && !(file->flags & FLAG_HLINK_EOL)) |
3cd5301f WD |
183 | head = hlink_list[file->F_HLINDEX] = file->F_NEXT; |
184 | else | |
185 | head = hlink_list[file->F_HLINDEX]; | |
186 | if (ndx != head) { | |
187 | struct file_struct *head_file = FPTR(head); | |
20f90d5e | 188 | if (!stdout_format_has_i && verbose > 1) { |
6cbde57d WD |
189 | rprintf(FINFO, "\"%s\" is a hard link\n", |
190 | f_name(file, NULL)); | |
191 | } | |
3cd5301f | 192 | if (head_file->F_HLINDEX == FINISHED_LINK) { |
541b23d1 | 193 | STRUCT_STAT st2, st3; |
6cbde57d | 194 | char *toname = f_name(head_file, NULL); |
3cd5301f WD |
195 | if (link_stat(toname, &st2, 0) < 0) { |
196 | rsyserr(FERROR, errno, "stat %s failed", | |
197 | full_fname(toname)); | |
198 | return -1; | |
199 | } | |
541b23d1 WD |
200 | if (statret < 0 && basis_dir[0] != NULL) { |
201 | char cmpbuf[MAXPATHLEN]; | |
202 | int j = 0; | |
203 | do { | |
204 | pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname); | |
205 | if (link_stat(cmpbuf, &st3, 0) < 0) | |
206 | continue; | |
207 | if (link_dest) { | |
208 | if (st2.st_dev != st3.st_dev | |
209 | || st2.st_ino != st3.st_ino) | |
210 | continue; | |
211 | statret = 1; | |
212 | st = &st3; | |
20f90d5e | 213 | if (verbose < 2 || !stdout_format_has_i) |
541b23d1 WD |
214 | itemizing = code = 0; |
215 | break; | |
216 | } | |
217 | if (!unchanged_file(cmpbuf, file, &st3)) | |
218 | continue; | |
219 | statret = 1; | |
220 | st = &st3; | |
221 | if (unchanged_attrs(file, &st3)) | |
222 | break; | |
223 | } while (basis_dir[++j] != NULL); | |
224 | } | |
3cd5301f WD |
225 | maybe_hard_link(file, ndx, fname, statret, st, |
226 | toname, &st2, itemizing, code); | |
47c11975 | 227 | if (remove_source_files == 1 && do_xfers) { |
841d9436 WD |
228 | char numbuf[4]; |
229 | SIVAL(numbuf, 0, ndx); | |
230 | send_msg(MSG_SUCCESS, numbuf, 4); | |
231 | } | |
3cd5301f WD |
232 | file->F_HLINDEX = FINISHED_LINK; |
233 | } else | |
234 | file->F_HLINDEX = SKIPPED_LINK; | |
d38fc305 WD |
235 | return 1; |
236 | } | |
bb91a624 | 237 | #endif |
d38fc305 WD |
238 | return 0; |
239 | } | |
240 | ||
4f5b0756 | 241 | #ifdef SUPPORT_HARD_LINKS |
9f2e3c3f WD |
242 | int hard_link_one(struct file_struct *file, int ndx, char *fname, |
243 | int statret, STRUCT_STAT *st, char *toname, int terse, | |
244 | int itemizing, enum logcode code) | |
a16bbc39 | 245 | { |
9f2e3c3f | 246 | if (do_link(toname, fname)) { |
3cd5301f WD |
247 | if (terse) { |
248 | if (!verbose) | |
249 | return -1; | |
250 | code = FINFO; | |
251 | } else | |
252 | code = FERROR; | |
253 | rsyserr(code, errno, "link %s => %s failed", | |
45c49b52 | 254 | full_fname(fname), toname); |
9f2e3c3f WD |
255 | return -1; |
256 | } | |
257 | ||
258 | if (itemizing) { | |
b7d4d28b WD |
259 | itemize(file, ndx, statret, st, |
260 | ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0, | |
9f2e3c3f WD |
261 | terse ? "" : toname); |
262 | } | |
45c49b52 WD |
263 | if (code && verbose && !terse) |
264 | rprintf(code, "%s => %s\n", fname, toname); | |
9f2e3c3f | 265 | return 0; |
a16bbc39 AT |
266 | } |
267 | #endif | |
268 | ||
fae5bb31 | 269 | |
9f2e3c3f WD |
270 | void hard_link_cluster(struct file_struct *file, int master, int itemizing, |
271 | enum logcode code) | |
dc5ddbcc | 272 | { |
4f5b0756 | 273 | #ifdef SUPPORT_HARD_LINKS |
3fef5364 WD |
274 | char hlink1[MAXPATHLEN]; |
275 | char *hlink2; | |
1d5cda22 | 276 | STRUCT_STAT st1, st2; |
9f2e3c3f | 277 | int statret, ndx = master; |
a16bbc39 | 278 | |
3cd5301f | 279 | file->F_HLINDEX = FINISHED_LINK; |
6cbde57d | 280 | if (link_stat(f_name(file, hlink1), &st1, 0) < 0) |
6e69cff1 | 281 | return; |
9f2e3c3f WD |
282 | if (!(file->flags & FLAG_HLINK_TOL)) { |
283 | while (!(file->flags & FLAG_HLINK_EOL)) { | |
284 | ndx = file->F_NEXT; | |
285 | file = FPTR(ndx); | |
286 | } | |
287 | } | |
288 | do { | |
289 | ndx = file->F_NEXT; | |
290 | file = FPTR(ndx); | |
3cd5301f | 291 | if (file->F_HLINDEX != SKIPPED_LINK) |
1d5cda22 | 292 | continue; |
6cbde57d | 293 | hlink2 = f_name(file, NULL); |
3cd5301f WD |
294 | statret = link_stat(hlink2, &st2, 0); |
295 | maybe_hard_link(file, ndx, hlink2, statret, &st2, | |
296 | hlink1, &st1, itemizing, code); | |
47c11975 | 297 | if (remove_source_files == 1 && do_xfers) { |
841d9436 WD |
298 | char numbuf[4]; |
299 | SIVAL(numbuf, 0, ndx); | |
300 | send_msg(MSG_SUCCESS, numbuf, 4); | |
301 | } | |
3cd5301f | 302 | file->F_HLINDEX = FINISHED_LINK; |
9f2e3c3f | 303 | } while (!(file->flags & FLAG_HLINK_EOL)); |
dc5ddbcc AT |
304 | #endif |
305 | } |