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; |
417c99f6 | 25 | extern int make_backups; |
9f2e3c3f | 26 | extern struct file_list *the_file_list; |
dc5ddbcc | 27 | |
4f5b0756 | 28 | #ifdef SUPPORT_HARD_LINKS |
9f2e3c3f | 29 | |
3cd5301f WD |
30 | #define SKIPPED_LINK (-1) |
31 | #define FINISHED_LINK (-2) | |
32 | ||
9f2e3c3f WD |
33 | #define FPTR(i) (the_file_list->files[i]) |
34 | #define LINKED(p1,p2) (FPTR(p1)->F_DEV == FPTR(p2)->F_DEV \ | |
35 | && FPTR(p1)->F_INODE == FPTR(p2)->F_INODE) | |
36 | ||
37 | static int hlink_compare(int *int1, int *int2) | |
dc5ddbcc | 38 | { |
9f2e3c3f WD |
39 | struct file_struct *f1 = FPTR(*int1); |
40 | struct file_struct *f2 = FPTR(*int2); | |
11dc2740 | 41 | |
92cc9dd7 WD |
42 | if (f1->F_DEV != f2->F_DEV) |
43 | return (int) (f1->F_DEV > f2->F_DEV ? 1 : -1); | |
dc5ddbcc | 44 | |
92cc9dd7 WD |
45 | if (f1->F_INODE != f2->F_INODE) |
46 | return (int) (f1->F_INODE > f2->F_INODE ? 1 : -1); | |
dc5ddbcc | 47 | |
122d1771 | 48 | return f_name_cmp(f1, f2); |
11dc2740 | 49 | } |
dc5ddbcc | 50 | |
9f2e3c3f | 51 | static int *hlink_list; |
417c99f6 | 52 | static int hlink_count; |
1d5cda22 | 53 | |
1d5cda22 | 54 | /* Analyze the data in the hlink_list[], remove items that aren't multiply |
90481755 | 55 | * linked, and replace the dev+inode data with the hlindex+next linked list. */ |
9f2e3c3f | 56 | static void link_idev_data(void) |
1d5cda22 | 57 | { |
97e786c3 | 58 | int cur, from, to, start; |
1d5cda22 | 59 | |
9935066b | 60 | alloc_pool_t hlink_pool; |
9f2e3c3f | 61 | alloc_pool_t idev_pool = the_file_list->hlink_pool; |
9935066b S |
62 | |
63 | hlink_pool = pool_create(128 * 1024, sizeof (struct hlink), | |
64 | out_of_memory, POOL_INTERN); | |
65 | ||
1d5cda22 WD |
66 | for (from = to = 0; from < hlink_count; from++) { |
67 | start = from; | |
97e786c3 WD |
68 | while (1) { |
69 | cur = hlink_list[from]; | |
70 | if (from == hlink_count-1 | |
71 | || !LINKED(cur, hlink_list[from+1])) | |
72 | break; | |
73 | pool_free(idev_pool, 0, FPTR(cur)->link_u.idev); | |
74 | FPTR(cur)->link_u.links = pool_talloc(hlink_pool, | |
9935066b S |
75 | struct hlink, 1, "hlink_list"); |
76 | ||
97e786c3 WD |
77 | FPTR(cur)->F_HLINDEX = to; |
78 | FPTR(cur)->F_NEXT = hlink_list[++from]; | |
1d5cda22 | 79 | } |
97e786c3 | 80 | pool_free(idev_pool, 0, FPTR(cur)->link_u.idev); |
1d5cda22 | 81 | if (from > start) { |
97e786c3 WD |
82 | int head = hlink_list[start]; |
83 | FPTR(cur)->link_u.links = pool_talloc(hlink_pool, | |
9935066b S |
84 | struct hlink, 1, "hlink_list"); |
85 | ||
9f2e3c3f | 86 | FPTR(head)->flags |= FLAG_HLINK_TOL; |
97e786c3 WD |
87 | FPTR(cur)->F_HLINDEX = to; |
88 | FPTR(cur)->F_NEXT = head; | |
89 | FPTR(cur)->flags |= FLAG_HLINK_EOL; | |
1d5cda22 | 90 | hlink_list[to++] = head; |
97e786c3 WD |
91 | } else |
92 | FPTR(cur)->link_u.links = NULL; | |
1d5cda22 WD |
93 | } |
94 | ||
95 | if (!to) { | |
96 | free(hlink_list); | |
97 | hlink_list = NULL; | |
9935066b S |
98 | pool_destroy(hlink_pool); |
99 | hlink_pool = NULL; | |
1d5cda22 WD |
100 | } else { |
101 | hlink_count = to; | |
9f2e3c3f WD |
102 | hlink_list = realloc_array(hlink_list, int, hlink_count); |
103 | if (!hlink_list) | |
1d5cda22 WD |
104 | out_of_memory("init_hard_links"); |
105 | } | |
9f2e3c3f | 106 | the_file_list->hlink_pool = hlink_pool; |
9935066b | 107 | pool_destroy(idev_pool); |
1d5cda22 | 108 | } |
dc5ddbcc AT |
109 | #endif |
110 | ||
9f2e3c3f | 111 | void init_hard_links(void) |
dc5ddbcc | 112 | { |
4f5b0756 | 113 | #ifdef SUPPORT_HARD_LINKS |
dd04a034 | 114 | int i; |
11dc2740 | 115 | |
6e69cff1 MP |
116 | if (hlink_list) |
117 | free(hlink_list); | |
118 | ||
9f2e3c3f | 119 | if (!(hlink_list = new_array(int, the_file_list->count))) |
dd04a034 | 120 | out_of_memory("init_hard_links"); |
6aae748e | 121 | |
11dc2740 | 122 | hlink_count = 0; |
9f2e3c3f WD |
123 | for (i = 0; i < the_file_list->count; i++) { |
124 | if (FPTR(i)->link_u.idev) | |
125 | hlink_list[hlink_count++] = i; | |
11dc2740 | 126 | } |
dc5ddbcc | 127 | |
11dc2740 | 128 | qsort(hlink_list, hlink_count, |
0d6e308b | 129 | sizeof hlink_list[0], (int (*)()) hlink_compare); |
dc5ddbcc | 130 | |
6aae748e WD |
131 | if (!hlink_count) { |
132 | free(hlink_list); | |
133 | hlink_list = NULL; | |
1d5cda22 | 134 | } else |
9f2e3c3f | 135 | link_idev_data(); |
dc5ddbcc | 136 | #endif |
dc5ddbcc AT |
137 | } |
138 | ||
3cd5301f WD |
139 | #ifdef SUPPORT_HARD_LINKS |
140 | static int maybe_hard_link(struct file_struct *file, int ndx, | |
141 | char *fname, int statret, STRUCT_STAT *st, | |
142 | char *toname, STRUCT_STAT *to_st, | |
143 | int itemizing, enum logcode code) | |
144 | { | |
145 | if (statret == 0) { | |
146 | if (st->st_dev == to_st->st_dev | |
147 | && st->st_ino == to_st->st_ino) { | |
148 | if (itemizing) { | |
149 | itemize(file, ndx, statret, st, | |
150 | ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, | |
151 | 0, ""); | |
152 | } | |
153 | return 0; | |
154 | } | |
155 | if (make_backups) { | |
156 | if (!make_backup(fname)) | |
157 | return -1; | |
158 | } else if (robust_unlink(fname)) { | |
159 | rsyserr(FERROR, errno, "unlink %s failed", | |
160 | full_fname(fname)); | |
161 | return -1; | |
162 | } | |
163 | } | |
164 | return hard_link_one(file, ndx, fname, statret, st, toname, | |
165 | 0, itemizing, code); | |
166 | } | |
167 | #endif | |
168 | ||
169 | int hard_link_check(struct file_struct *file, int ndx, char *fname, | |
170 | int statret, STRUCT_STAT *st, int itemizing, | |
171 | enum logcode code, int skip) | |
d38fc305 | 172 | { |
4f5b0756 | 173 | #ifdef SUPPORT_HARD_LINKS |
3cd5301f | 174 | int head; |
97e786c3 | 175 | if (!file->link_u.links) |
d38fc305 WD |
176 | return 0; |
177 | if (skip && !(file->flags & FLAG_HLINK_EOL)) | |
3cd5301f WD |
178 | head = hlink_list[file->F_HLINDEX] = file->F_NEXT; |
179 | else | |
180 | head = hlink_list[file->F_HLINDEX]; | |
181 | if (ndx != head) { | |
182 | struct file_struct *head_file = FPTR(head); | |
9f2e3c3f | 183 | if (verbose > 2) { |
d38fc305 | 184 | rprintf(FINFO, "\"%s\" is a hard link\n", |
71903f60 | 185 | safe_fname(f_name(file))); |
d38fc305 | 186 | } |
3cd5301f WD |
187 | if (head_file->F_HLINDEX == FINISHED_LINK) { |
188 | STRUCT_STAT st2; | |
189 | char *toname = f_name(head_file); | |
190 | if (link_stat(toname, &st2, 0) < 0) { | |
191 | rsyserr(FERROR, errno, "stat %s failed", | |
192 | full_fname(toname)); | |
193 | return -1; | |
194 | } | |
195 | maybe_hard_link(file, ndx, fname, statret, st, | |
196 | toname, &st2, itemizing, code); | |
197 | file->F_HLINDEX = FINISHED_LINK; | |
198 | } else | |
199 | file->F_HLINDEX = SKIPPED_LINK; | |
d38fc305 WD |
200 | return 1; |
201 | } | |
bb91a624 | 202 | #endif |
d38fc305 WD |
203 | return 0; |
204 | } | |
205 | ||
4f5b0756 | 206 | #ifdef SUPPORT_HARD_LINKS |
9f2e3c3f WD |
207 | int hard_link_one(struct file_struct *file, int ndx, char *fname, |
208 | int statret, STRUCT_STAT *st, char *toname, int terse, | |
209 | int itemizing, enum logcode code) | |
a16bbc39 | 210 | { |
9f2e3c3f | 211 | if (do_link(toname, fname)) { |
3cd5301f WD |
212 | if (terse) { |
213 | if (!verbose) | |
214 | return -1; | |
215 | code = FINFO; | |
216 | } else | |
217 | code = FERROR; | |
218 | rsyserr(code, errno, "link %s => %s failed", | |
219 | full_fname(fname), safe_fname(toname)); | |
9f2e3c3f WD |
220 | return -1; |
221 | } | |
222 | ||
223 | if (itemizing) { | |
b7d4d28b WD |
224 | itemize(file, ndx, statret, st, |
225 | ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0, | |
9f2e3c3f WD |
226 | terse ? "" : toname); |
227 | } | |
228 | if (code && verbose && !terse) { | |
229 | rprintf(code, "%s => %s\n", | |
230 | safe_fname(fname), safe_fname(toname)); | |
a16bbc39 | 231 | } |
9f2e3c3f | 232 | return 0; |
a16bbc39 AT |
233 | } |
234 | #endif | |
235 | ||
fae5bb31 | 236 | |
9f2e3c3f WD |
237 | void hard_link_cluster(struct file_struct *file, int master, int itemizing, |
238 | enum logcode code) | |
dc5ddbcc | 239 | { |
4f5b0756 | 240 | #ifdef SUPPORT_HARD_LINKS |
3fef5364 WD |
241 | char hlink1[MAXPATHLEN]; |
242 | char *hlink2; | |
1d5cda22 | 243 | STRUCT_STAT st1, st2; |
9f2e3c3f | 244 | int statret, ndx = master; |
a16bbc39 | 245 | |
3cd5301f | 246 | file->F_HLINDEX = FINISHED_LINK; |
9f2e3c3f | 247 | if (link_stat(f_name_to(file, hlink1), &st1, 0) < 0) |
6e69cff1 | 248 | return; |
9f2e3c3f WD |
249 | if (!(file->flags & FLAG_HLINK_TOL)) { |
250 | while (!(file->flags & FLAG_HLINK_EOL)) { | |
251 | ndx = file->F_NEXT; | |
252 | file = FPTR(ndx); | |
253 | } | |
254 | } | |
255 | do { | |
256 | ndx = file->F_NEXT; | |
257 | file = FPTR(ndx); | |
3cd5301f | 258 | if (file->F_HLINDEX != SKIPPED_LINK) |
1d5cda22 | 259 | continue; |
9f2e3c3f | 260 | hlink2 = f_name(file); |
3cd5301f WD |
261 | statret = link_stat(hlink2, &st2, 0); |
262 | maybe_hard_link(file, ndx, hlink2, statret, &st2, | |
263 | hlink1, &st1, itemizing, code); | |
264 | file->F_HLINDEX = FINISHED_LINK; | |
9f2e3c3f | 265 | } while (!(file->flags & FLAG_HLINK_EOL)); |
dc5ddbcc AT |
266 | #endif |
267 | } |