Commit | Line | Data |
---|---|---|
6aae748e | 1 | /* |
dc5ddbcc AT |
2 | Copyright (C) Andrew Tridgell 1996 |
3 | Copyright (C) Paul Mackerras 1996 | |
6e69cff1 | 4 | Copyright (C) 2002 by Martin Pool <mbp@samba.org> |
6aae748e | 5 | |
dc5ddbcc AT |
6 | This program is free software; you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2 of the License, or | |
9 | (at your option) any later version. | |
6aae748e | 10 | |
dc5ddbcc AT |
11 | This program is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
6aae748e | 15 | |
dc5ddbcc AT |
16 | You should have received a copy of the GNU General Public License |
17 | along with this program; if not, write to the Free Software | |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
19 | */ | |
20 | ||
21 | #include "rsync.h" | |
22 | ||
dc5ddbcc | 23 | extern int dry_run; |
9a52223b | 24 | extern int verbose; |
541b23d1 | 25 | extern int link_dest; |
417c99f6 | 26 | extern int make_backups; |
a45f581b | 27 | extern int log_format_has_i; |
541b23d1 | 28 | extern char *basis_dir[]; |
9f2e3c3f | 29 | extern struct file_list *the_file_list; |
dc5ddbcc | 30 | |
4f5b0756 | 31 | #ifdef SUPPORT_HARD_LINKS |
9f2e3c3f | 32 | |
3cd5301f WD |
33 | #define SKIPPED_LINK (-1) |
34 | #define FINISHED_LINK (-2) | |
35 | ||
9f2e3c3f WD |
36 | #define FPTR(i) (the_file_list->files[i]) |
37 | #define LINKED(p1,p2) (FPTR(p1)->F_DEV == FPTR(p2)->F_DEV \ | |
38 | && FPTR(p1)->F_INODE == FPTR(p2)->F_INODE) | |
39 | ||
40 | static int hlink_compare(int *int1, int *int2) | |
dc5ddbcc | 41 | { |
9f2e3c3f WD |
42 | struct file_struct *f1 = FPTR(*int1); |
43 | struct file_struct *f2 = FPTR(*int2); | |
11dc2740 | 44 | |
92cc9dd7 WD |
45 | if (f1->F_DEV != f2->F_DEV) |
46 | return (int) (f1->F_DEV > f2->F_DEV ? 1 : -1); | |
dc5ddbcc | 47 | |
92cc9dd7 WD |
48 | if (f1->F_INODE != f2->F_INODE) |
49 | return (int) (f1->F_INODE > f2->F_INODE ? 1 : -1); | |
dc5ddbcc | 50 | |
122d1771 | 51 | return f_name_cmp(f1, f2); |
11dc2740 | 52 | } |
dc5ddbcc | 53 | |
9f2e3c3f | 54 | static int *hlink_list; |
417c99f6 | 55 | static int hlink_count; |
1d5cda22 | 56 | |
1d5cda22 | 57 | /* Analyze the data in the hlink_list[], remove items that aren't multiply |
90481755 | 58 | * linked, and replace the dev+inode data with the hlindex+next linked list. */ |
9f2e3c3f | 59 | static void link_idev_data(void) |
1d5cda22 | 60 | { |
97e786c3 | 61 | int cur, from, to, start; |
1d5cda22 | 62 | |
9935066b | 63 | alloc_pool_t hlink_pool; |
9f2e3c3f | 64 | alloc_pool_t idev_pool = the_file_list->hlink_pool; |
9935066b S |
65 | |
66 | hlink_pool = pool_create(128 * 1024, sizeof (struct hlink), | |
67 | out_of_memory, POOL_INTERN); | |
68 | ||
1d5cda22 WD |
69 | for (from = to = 0; from < hlink_count; from++) { |
70 | start = from; | |
97e786c3 WD |
71 | while (1) { |
72 | cur = hlink_list[from]; | |
73 | if (from == hlink_count-1 | |
74 | || !LINKED(cur, hlink_list[from+1])) | |
75 | break; | |
76 | pool_free(idev_pool, 0, FPTR(cur)->link_u.idev); | |
77 | FPTR(cur)->link_u.links = pool_talloc(hlink_pool, | |
9935066b S |
78 | struct hlink, 1, "hlink_list"); |
79 | ||
97e786c3 WD |
80 | FPTR(cur)->F_HLINDEX = to; |
81 | FPTR(cur)->F_NEXT = hlink_list[++from]; | |
1d5cda22 | 82 | } |
97e786c3 | 83 | pool_free(idev_pool, 0, FPTR(cur)->link_u.idev); |
1d5cda22 | 84 | if (from > start) { |
97e786c3 WD |
85 | int head = hlink_list[start]; |
86 | FPTR(cur)->link_u.links = pool_talloc(hlink_pool, | |
9935066b S |
87 | struct hlink, 1, "hlink_list"); |
88 | ||
9f2e3c3f | 89 | FPTR(head)->flags |= FLAG_HLINK_TOL; |
97e786c3 WD |
90 | FPTR(cur)->F_HLINDEX = to; |
91 | FPTR(cur)->F_NEXT = head; | |
92 | FPTR(cur)->flags |= FLAG_HLINK_EOL; | |
1d5cda22 | 93 | hlink_list[to++] = head; |
97e786c3 WD |
94 | } else |
95 | FPTR(cur)->link_u.links = NULL; | |
1d5cda22 WD |
96 | } |
97 | ||
98 | if (!to) { | |
99 | free(hlink_list); | |
100 | hlink_list = NULL; | |
9935066b S |
101 | pool_destroy(hlink_pool); |
102 | hlink_pool = NULL; | |
1d5cda22 WD |
103 | } else { |
104 | hlink_count = to; | |
9f2e3c3f WD |
105 | hlink_list = realloc_array(hlink_list, int, hlink_count); |
106 | if (!hlink_list) | |
1d5cda22 WD |
107 | out_of_memory("init_hard_links"); |
108 | } | |
9f2e3c3f | 109 | the_file_list->hlink_pool = hlink_pool; |
9935066b | 110 | pool_destroy(idev_pool); |
1d5cda22 | 111 | } |
dc5ddbcc AT |
112 | #endif |
113 | ||
9f2e3c3f | 114 | void init_hard_links(void) |
dc5ddbcc | 115 | { |
4f5b0756 | 116 | #ifdef SUPPORT_HARD_LINKS |
dd04a034 | 117 | int i; |
11dc2740 | 118 | |
6e69cff1 MP |
119 | if (hlink_list) |
120 | free(hlink_list); | |
121 | ||
9f2e3c3f | 122 | if (!(hlink_list = new_array(int, the_file_list->count))) |
dd04a034 | 123 | out_of_memory("init_hard_links"); |
6aae748e | 124 | |
11dc2740 | 125 | hlink_count = 0; |
9f2e3c3f WD |
126 | for (i = 0; i < the_file_list->count; i++) { |
127 | if (FPTR(i)->link_u.idev) | |
128 | hlink_list[hlink_count++] = i; | |
11dc2740 | 129 | } |
dc5ddbcc | 130 | |
11dc2740 | 131 | qsort(hlink_list, hlink_count, |
0d6e308b | 132 | sizeof hlink_list[0], (int (*)()) hlink_compare); |
dc5ddbcc | 133 | |
6aae748e WD |
134 | if (!hlink_count) { |
135 | free(hlink_list); | |
136 | hlink_list = NULL; | |
1d5cda22 | 137 | } else |
9f2e3c3f | 138 | link_idev_data(); |
dc5ddbcc | 139 | #endif |
dc5ddbcc AT |
140 | } |
141 | ||
3cd5301f WD |
142 | #ifdef SUPPORT_HARD_LINKS |
143 | static int maybe_hard_link(struct file_struct *file, int ndx, | |
144 | char *fname, int statret, STRUCT_STAT *st, | |
145 | char *toname, STRUCT_STAT *to_st, | |
146 | int itemizing, enum logcode code) | |
147 | { | |
148 | if (statret == 0) { | |
149 | if (st->st_dev == to_st->st_dev | |
150 | && st->st_ino == to_st->st_ino) { | |
151 | if (itemizing) { | |
152 | itemize(file, ndx, statret, st, | |
153 | ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, | |
154 | 0, ""); | |
155 | } | |
156 | return 0; | |
157 | } | |
158 | if (make_backups) { | |
159 | if (!make_backup(fname)) | |
160 | return -1; | |
161 | } else if (robust_unlink(fname)) { | |
162 | rsyserr(FERROR, errno, "unlink %s failed", | |
163 | full_fname(fname)); | |
164 | return -1; | |
165 | } | |
166 | } | |
167 | return hard_link_one(file, ndx, fname, statret, st, toname, | |
168 | 0, itemizing, code); | |
169 | } | |
170 | #endif | |
171 | ||
172 | int hard_link_check(struct file_struct *file, int ndx, char *fname, | |
173 | int statret, STRUCT_STAT *st, int itemizing, | |
174 | enum logcode code, int skip) | |
d38fc305 | 175 | { |
4f5b0756 | 176 | #ifdef SUPPORT_HARD_LINKS |
3cd5301f | 177 | int head; |
d38fc305 | 178 | if (skip && !(file->flags & FLAG_HLINK_EOL)) |
3cd5301f WD |
179 | head = hlink_list[file->F_HLINDEX] = file->F_NEXT; |
180 | else | |
181 | head = hlink_list[file->F_HLINDEX]; | |
182 | if (ndx != head) { | |
183 | struct file_struct *head_file = FPTR(head); | |
a45f581b | 184 | if (!log_format_has_i && verbose > 1) { |
d38fc305 | 185 | rprintf(FINFO, "\"%s\" is a hard link\n", |
71903f60 | 186 | safe_fname(f_name(file))); |
d38fc305 | 187 | } |
3cd5301f | 188 | if (head_file->F_HLINDEX == FINISHED_LINK) { |
541b23d1 | 189 | STRUCT_STAT st2, st3; |
3cd5301f WD |
190 | char *toname = f_name(head_file); |
191 | if (link_stat(toname, &st2, 0) < 0) { | |
192 | rsyserr(FERROR, errno, "stat %s failed", | |
193 | full_fname(toname)); | |
194 | return -1; | |
195 | } | |
541b23d1 WD |
196 | if (statret < 0 && basis_dir[0] != NULL) { |
197 | char cmpbuf[MAXPATHLEN]; | |
198 | int j = 0; | |
199 | do { | |
200 | pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname); | |
201 | if (link_stat(cmpbuf, &st3, 0) < 0) | |
202 | continue; | |
203 | if (link_dest) { | |
204 | if (st2.st_dev != st3.st_dev | |
205 | || st2.st_ino != st3.st_ino) | |
206 | continue; | |
207 | statret = 1; | |
208 | st = &st3; | |
209 | if (verbose < 2 || !log_format_has_i) | |
210 | itemizing = code = 0; | |
211 | break; | |
212 | } | |
213 | if (!unchanged_file(cmpbuf, file, &st3)) | |
214 | continue; | |
215 | statret = 1; | |
216 | st = &st3; | |
217 | if (unchanged_attrs(file, &st3)) | |
218 | break; | |
219 | } while (basis_dir[++j] != NULL); | |
220 | } | |
3cd5301f WD |
221 | maybe_hard_link(file, ndx, fname, statret, st, |
222 | toname, &st2, itemizing, code); | |
223 | file->F_HLINDEX = FINISHED_LINK; | |
224 | } else | |
225 | file->F_HLINDEX = SKIPPED_LINK; | |
d38fc305 WD |
226 | return 1; |
227 | } | |
bb91a624 | 228 | #endif |
d38fc305 WD |
229 | return 0; |
230 | } | |
231 | ||
4f5b0756 | 232 | #ifdef SUPPORT_HARD_LINKS |
9f2e3c3f WD |
233 | int hard_link_one(struct file_struct *file, int ndx, char *fname, |
234 | int statret, STRUCT_STAT *st, char *toname, int terse, | |
235 | int itemizing, enum logcode code) | |
a16bbc39 | 236 | { |
9f2e3c3f | 237 | if (do_link(toname, fname)) { |
3cd5301f WD |
238 | if (terse) { |
239 | if (!verbose) | |
240 | return -1; | |
241 | code = FINFO; | |
242 | } else | |
243 | code = FERROR; | |
244 | rsyserr(code, errno, "link %s => %s failed", | |
245 | full_fname(fname), safe_fname(toname)); | |
9f2e3c3f WD |
246 | return -1; |
247 | } | |
248 | ||
249 | if (itemizing) { | |
b7d4d28b WD |
250 | itemize(file, ndx, statret, st, |
251 | ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0, | |
9f2e3c3f WD |
252 | terse ? "" : toname); |
253 | } | |
254 | if (code && verbose && !terse) { | |
255 | rprintf(code, "%s => %s\n", | |
256 | safe_fname(fname), safe_fname(toname)); | |
a16bbc39 | 257 | } |
9f2e3c3f | 258 | return 0; |
a16bbc39 AT |
259 | } |
260 | #endif | |
261 | ||
fae5bb31 | 262 | |
9f2e3c3f WD |
263 | void hard_link_cluster(struct file_struct *file, int master, int itemizing, |
264 | enum logcode code) | |
dc5ddbcc | 265 | { |
4f5b0756 | 266 | #ifdef SUPPORT_HARD_LINKS |
3fef5364 WD |
267 | char hlink1[MAXPATHLEN]; |
268 | char *hlink2; | |
1d5cda22 | 269 | STRUCT_STAT st1, st2; |
9f2e3c3f | 270 | int statret, ndx = master; |
a16bbc39 | 271 | |
3cd5301f | 272 | file->F_HLINDEX = FINISHED_LINK; |
9f2e3c3f | 273 | if (link_stat(f_name_to(file, hlink1), &st1, 0) < 0) |
6e69cff1 | 274 | return; |
9f2e3c3f WD |
275 | if (!(file->flags & FLAG_HLINK_TOL)) { |
276 | while (!(file->flags & FLAG_HLINK_EOL)) { | |
277 | ndx = file->F_NEXT; | |
278 | file = FPTR(ndx); | |
279 | } | |
280 | } | |
281 | do { | |
282 | ndx = file->F_NEXT; | |
283 | file = FPTR(ndx); | |
3cd5301f | 284 | if (file->F_HLINDEX != SKIPPED_LINK) |
1d5cda22 | 285 | continue; |
9f2e3c3f | 286 | hlink2 = f_name(file); |
3cd5301f WD |
287 | statret = link_stat(hlink2, &st2, 0); |
288 | maybe_hard_link(file, ndx, hlink2, statret, &st2, | |
289 | hlink1, &st1, itemizing, code); | |
290 | file->F_HLINDEX = FINISHED_LINK; | |
9f2e3c3f | 291 | } while (!(file->flags & FLAG_HLINK_EOL)); |
dc5ddbcc AT |
292 | #endif |
293 | } |