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; |
82ad07c4 | 27 | extern int dry_run; |
841d9436 | 28 | extern int do_xfers; |
541b23d1 | 29 | extern int link_dest; |
417c99f6 | 30 | extern int make_backups; |
47c11975 | 31 | extern int remove_source_files; |
20f90d5e | 32 | extern int stdout_format_has_i; |
aadc84d3 | 33 | extern int maybe_ATTRS_REPORT; |
541b23d1 | 34 | extern char *basis_dir[]; |
9f2e3c3f | 35 | extern struct file_list *the_file_list; |
dc5ddbcc | 36 | |
4f5b0756 | 37 | #ifdef SUPPORT_HARD_LINKS |
9f2e3c3f | 38 | |
aadc84d3 | 39 | alloc_pool_t hlink_pool; |
3cd5301f | 40 | |
9f2e3c3f | 41 | #define FPTR(i) (the_file_list->files[i]) |
82ad07c4 | 42 | #define LINKED(i1,i2) ((i1)->dev == (i2)->dev && (i1)->ino == (i2)->ino) |
9f2e3c3f | 43 | |
aadc84d3 WD |
44 | void init_hard_links(void) |
45 | { | |
46 | if (!(hlink_pool = pool_create(HLINK_EXTENT, sizeof (struct idev), | |
47 | out_of_memory, POOL_INTERN))) | |
48 | out_of_memory("init_hard_links"); | |
49 | } | |
50 | ||
9f2e3c3f | 51 | static int hlink_compare(int *int1, int *int2) |
dc5ddbcc | 52 | { |
9f2e3c3f WD |
53 | struct file_struct *f1 = FPTR(*int1); |
54 | struct file_struct *f2 = FPTR(*int2); | |
c2a608d9 WD |
55 | struct idev *i1 = F_HL_IDEV(f1); |
56 | struct idev *i2 = F_HL_IDEV(f2); | |
11dc2740 | 57 | |
82ad07c4 | 58 | if (i1->dev != i2->dev) |
663b2857 | 59 | return i1->dev > i2->dev ? 1 : -1; |
dc5ddbcc | 60 | |
82ad07c4 | 61 | if (i1->ino != i2->ino) |
663b2857 | 62 | return i1->ino > i2->ino ? 1 : -1; |
dc5ddbcc | 63 | |
122d1771 | 64 | return f_name_cmp(f1, f2); |
11dc2740 | 65 | } |
dc5ddbcc | 66 | |
aadc84d3 WD |
67 | /* Analyze the dev+inode data in the file-list by creating a list of all |
68 | * the items that have hlink data, sorting them, and matching up identical | |
69 | * values into clusters. These will be a single linked list from last to | |
70 | * first when we're done. */ | |
71 | void match_hard_links(void) | |
1d5cda22 | 72 | { |
aadc84d3 | 73 | int32 from, prev, *ndx_list; |
82ad07c4 WD |
74 | struct file_struct *file, *file_next; |
75 | struct idev *idev, *idev_next; | |
aadc84d3 | 76 | int i, ndx_count = 0; |
1d5cda22 | 77 | |
aadc84d3 WD |
78 | if (!(ndx_list = new_array(int32, the_file_list->count))) |
79 | out_of_memory("match_hard_links"); | |
9935066b | 80 | |
aadc84d3 WD |
81 | for (i = 0; i < the_file_list->count; i++) { |
82 | if (F_IS_HLINKED(FPTR(i))) | |
83 | ndx_list[ndx_count++] = i; | |
84 | } | |
9935066b | 85 | |
aadc84d3 WD |
86 | if (!ndx_count) { |
87 | free(ndx_list); | |
88 | return; | |
89 | } | |
90 | ||
91 | qsort(ndx_list, ndx_count, sizeof ndx_list[0], | |
92 | (int (*)()) hlink_compare); | |
93 | ||
94 | for (from = 0; from < ndx_count; from++) { | |
95 | for (file = FPTR(ndx_list[from]), idev = F_HL_IDEV(file), prev = -1; | |
96 | from < ndx_count-1; | |
97 | file = file_next, idev = idev_next, prev = ndx_list[from++]) | |
82ad07c4 | 98 | { |
aadc84d3 | 99 | file_next = FPTR(ndx_list[from+1]); |
c2a608d9 | 100 | idev_next = F_HL_IDEV(file_next); |
82ad07c4 | 101 | if (!LINKED(idev, idev_next)) |
97e786c3 | 102 | break; |
aadc84d3 WD |
103 | pool_free(hlink_pool, 0, idev); |
104 | if (prev < 0) | |
105 | file->flags |= FLAG_HLINK_FIRST; | |
106 | F_HL_PREV(file) = prev; | |
1d5cda22 | 107 | } |
aadc84d3 WD |
108 | pool_free(hlink_pool, 0, idev); |
109 | if (prev < 0) | |
112d728f | 110 | file->flags &= ~FLAG_HLINKED; |
aadc84d3 WD |
111 | else { |
112 | file->flags |= FLAG_HLINK_LAST; | |
113 | F_HL_PREV(file) = prev; | |
114 | } | |
1d5cda22 WD |
115 | } |
116 | ||
aadc84d3 WD |
117 | pool_destroy(hlink_pool); |
118 | free(ndx_list); | |
dc5ddbcc AT |
119 | } |
120 | ||
3cd5301f | 121 | static int maybe_hard_link(struct file_struct *file, int ndx, |
a2ebbffc | 122 | const char *fname, int statret, STRUCT_STAT *stp, |
aadc84d3 WD |
123 | const char *oldname, STRUCT_STAT *old_stp, |
124 | const char *realname, int itemizing, enum logcode code) | |
3cd5301f WD |
125 | { |
126 | if (statret == 0) { | |
aadc84d3 WD |
127 | if (stp->st_dev == old_stp->st_dev |
128 | && stp->st_ino == old_stp->st_ino) { | |
3cd5301f | 129 | if (itemizing) { |
a2ebbffc | 130 | itemize(file, ndx, statret, stp, |
3cd5301f WD |
131 | ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, |
132 | 0, ""); | |
133 | } | |
aadc84d3 WD |
134 | if (verbose > 1 && maybe_ATTRS_REPORT) |
135 | rprintf(FCLIENT, "%s is uptodate\n", fname); | |
136 | file->flags |= FLAG_HLINK_DONE; | |
3cd5301f WD |
137 | return 0; |
138 | } | |
139 | if (make_backups) { | |
140 | if (!make_backup(fname)) | |
141 | return -1; | |
142 | } else if (robust_unlink(fname)) { | |
143 | rsyserr(FERROR, errno, "unlink %s failed", | |
144 | full_fname(fname)); | |
145 | return -1; | |
146 | } | |
147 | } | |
aadc84d3 WD |
148 | |
149 | if (hard_link_one(file, fname, oldname, 0)) { | |
150 | if (itemizing) { | |
151 | itemize(file, ndx, statret, stp, | |
152 | ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0, | |
153 | realname); | |
154 | } | |
155 | if (code != FNONE && verbose) | |
156 | rprintf(code, "%s => %s\n", fname, realname); | |
157 | return 0; | |
158 | } | |
159 | return -1; | |
3cd5301f | 160 | } |
3cd5301f | 161 | |
aadc84d3 WD |
162 | /* Only called if FLAG_HLINKED is set and FLAG_HLINK_FIRST is not. Returns: |
163 | * 0 = process the file, 1 = skip the file, -1 = error occurred. */ | |
a2ebbffc WD |
164 | int hard_link_check(struct file_struct *file, int ndx, const char *fname, |
165 | int statret, STRUCT_STAT *stp, int itemizing, | |
aadc84d3 | 166 | enum logcode code) |
d38fc305 | 167 | { |
aadc84d3 WD |
168 | STRUCT_STAT prev_st; |
169 | char prev_name[MAXPATHLEN], altbuf[MAXPATHLEN], *realname; | |
170 | int alt_dest, prev_ndx = F_HL_PREV(file); | |
171 | struct file_struct *prev_file = FPTR(prev_ndx); | |
172 | ||
173 | /* Is the previous link is not complete yet? */ | |
174 | if (!(prev_file->flags & FLAG_HLINK_DONE)) { | |
175 | /* Is the previous link being transferred? */ | |
176 | if (prev_file->flags & FLAG_SENT) { | |
177 | /* Add ourselves to the list of files that will be | |
178 | * updated when the transfer completes, and mark | |
179 | * ourself as waiting for the transfer. */ | |
180 | F_HL_PREV(file) = F_HL_PREV(prev_file); | |
181 | F_HL_PREV(prev_file) = ndx; | |
182 | file->flags |= FLAG_SENT; | |
183 | return 1; | |
6cbde57d | 184 | } |
aadc84d3 WD |
185 | return 0; |
186 | } | |
187 | ||
188 | /* There is a finished file to link with! */ | |
189 | if (!(prev_file->flags & FLAG_HLINK_FIRST)) { | |
190 | /* The previous previous will be marked with FIRST. */ | |
191 | prev_ndx = F_HL_PREV(prev_file); | |
192 | prev_file = FPTR(prev_ndx); | |
193 | /* Update our previous pointer to point to the first. */ | |
194 | F_HL_PREV(file) = prev_ndx; | |
195 | } | |
196 | alt_dest = F_HL_PREV(prev_file); /* alternate value when DONE && FIRST */ | |
197 | if (alt_dest >= 0 && dry_run) { | |
198 | pathjoin(prev_name, MAXPATHLEN, basis_dir[alt_dest], | |
199 | f_name(prev_file, NULL)); | |
200 | f_name(prev_file, altbuf); | |
201 | realname = altbuf; | |
202 | } else { | |
203 | f_name(prev_file, prev_name); | |
204 | realname = prev_name; | |
205 | } | |
206 | ||
207 | if (link_stat(prev_name, &prev_st, 0) < 0) { | |
208 | rsyserr(FERROR, errno, "stat %s failed", | |
209 | full_fname(prev_name)); | |
210 | return -1; | |
211 | } | |
212 | ||
213 | if (statret < 0 && basis_dir[0] != NULL) { | |
214 | /* If we match an alt-dest item, we don't output this as a change. */ | |
215 | char cmpbuf[MAXPATHLEN]; | |
216 | STRUCT_STAT alt_st; | |
217 | int j = 0; | |
218 | do { | |
219 | pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname); | |
220 | if (link_stat(cmpbuf, &alt_st, 0) < 0) | |
221 | continue; | |
222 | if (link_dest) { | |
223 | if (prev_st.st_dev != alt_st.st_dev | |
224 | || prev_st.st_ino != alt_st.st_ino) | |
225 | continue; | |
226 | statret = 1; | |
227 | *stp = alt_st; | |
228 | if (verbose < 2 || !stdout_format_has_i) { | |
229 | itemizing = 0; | |
230 | code = FNONE; | |
231 | if (verbose > 1 && maybe_ATTRS_REPORT) | |
232 | rprintf(FCLIENT, "%s is uptodate\n", fname); | |
233 | } | |
234 | break; | |
541b23d1 | 235 | } |
aadc84d3 WD |
236 | if (!unchanged_file(cmpbuf, file, &alt_st)) |
237 | continue; | |
238 | statret = 1; | |
239 | *stp = alt_st; | |
240 | if (unchanged_attrs(file, &alt_st)) | |
241 | break; | |
242 | } while (basis_dir[++j] != NULL); | |
d38fc305 | 243 | } |
aadc84d3 WD |
244 | |
245 | if (maybe_hard_link(file, ndx, fname, statret, stp, prev_name, &prev_st, | |
246 | realname, itemizing, code) < 0) | |
247 | return -1; | |
248 | ||
249 | if (remove_source_files == 1 && do_xfers) | |
250 | send_msg_int(MSG_SUCCESS, ndx); | |
251 | ||
252 | return 1; | |
d38fc305 WD |
253 | } |
254 | ||
aadc84d3 WD |
255 | int hard_link_one(struct file_struct *file, const char *fname, |
256 | const char *oldname, int terse) | |
a16bbc39 | 257 | { |
aadc84d3 WD |
258 | if (do_link(oldname, fname) < 0) { |
259 | enum logcode code; | |
3cd5301f WD |
260 | if (terse) { |
261 | if (!verbose) | |
262 | return -1; | |
263 | code = FINFO; | |
264 | } else | |
265 | code = FERROR; | |
266 | rsyserr(code, errno, "link %s => %s failed", | |
aadc84d3 WD |
267 | full_fname(fname), oldname); |
268 | return 0; | |
9f2e3c3f WD |
269 | } |
270 | ||
aadc84d3 WD |
271 | file->flags |= FLAG_HLINK_DONE; |
272 | ||
273 | return 1; | |
a16bbc39 | 274 | } |
fae5bb31 | 275 | |
aadc84d3 WD |
276 | void finish_hard_link(struct file_struct *file, const char *fname, |
277 | STRUCT_STAT *stp, int itemizing, enum logcode code, | |
278 | int alt_dest) | |
dc5ddbcc | 279 | { |
aadc84d3 WD |
280 | STRUCT_STAT st, prev_st; |
281 | char alt_name[MAXPATHLEN], *prev_name; | |
282 | const char *our_name; | |
283 | int prev_statret, ndx, prev_ndx = F_HL_PREV(file); | |
284 | ||
285 | if (stp == NULL && prev_ndx >= 0) { | |
286 | if (link_stat(fname, &st, 0) < 0) { | |
287 | rsyserr(FERROR, errno, "stat %s failed", | |
288 | full_fname(fname)); | |
289 | return; | |
9f2e3c3f | 290 | } |
aadc84d3 | 291 | stp = &st; |
9f2e3c3f | 292 | } |
aadc84d3 WD |
293 | |
294 | /* FIRST combined with DONE means we were the first to get done. */ | |
295 | file->flags |= FLAG_HLINK_FIRST | FLAG_HLINK_DONE; | |
296 | F_HL_PREV(file) = alt_dest; | |
297 | if (alt_dest >= 0 && dry_run) { | |
298 | pathjoin(alt_name, MAXPATHLEN, basis_dir[alt_dest], | |
299 | f_name(file, NULL)); | |
300 | our_name = alt_name; | |
301 | } else | |
302 | our_name = fname; | |
303 | ||
304 | while ((ndx = prev_ndx) >= 0) { | |
9f2e3c3f | 305 | file = FPTR(ndx); |
aadc84d3 WD |
306 | file->flags = (file->flags & ~FLAG_HLINK_FIRST) | FLAG_HLINK_DONE; |
307 | prev_ndx = F_HL_PREV(file); | |
308 | prev_name = f_name(file, NULL); | |
309 | prev_statret = link_stat(prev_name, &prev_st, 0); | |
310 | if (maybe_hard_link(file, ndx, prev_name, prev_statret, &prev_st, | |
311 | our_name, stp, fname, itemizing, code) < 0) | |
1d5cda22 | 312 | continue; |
663b2857 WD |
313 | if (remove_source_files == 1 && do_xfers) |
314 | send_msg_int(MSG_SUCCESS, ndx); | |
aadc84d3 | 315 | } |
dc5ddbcc | 316 | } |
a2ebbffc | 317 | #endif |