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