For protocol 30, the sender uses the new idev_node() hashfile
[rsync/rsync.git] / hlink.c
... / ...
CommitLineData
1/*
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 *
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.
22 */
23
24#include "rsync.h"
25
26extern int verbose;
27extern int dry_run;
28extern int do_xfers;
29extern int link_dest;
30extern int make_backups;
31extern int remove_source_files;
32extern int stdout_format_has_i;
33extern int maybe_ATTRS_REPORT;
34extern char *basis_dir[];
35extern struct file_list *the_file_list;
36
37#ifdef SUPPORT_HARD_LINKS
38
39alloc_pool_t hlink_pool;
40
41#define FPTR(i) (the_file_list->files[i])
42#define LINKED(i1,i2) ((i1)->dev == (i2)->dev && (i1)->ino == (i2)->ino)
43
44void 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
51static int hlink_compare(int *int1, int *int2)
52{
53 struct file_struct *f1 = FPTR(*int1);
54 struct file_struct *f2 = FPTR(*int2);
55 struct idev *i1 = F_HL_IDEV(f1);
56 struct idev *i2 = F_HL_IDEV(f2);
57
58 if (i1->dev != i2->dev)
59 return i1->dev > i2->dev ? 1 : -1;
60
61 if (i1->ino != i2->ino)
62 return i1->ino > i2->ino ? 1 : -1;
63
64 return f_name_cmp(f1, f2);
65}
66
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. */
71void match_hard_links(void)
72{
73 int32 from, prev, *ndx_list;
74 struct file_struct *file, *file_next;
75 struct idev *idev, *idev_next;
76 int i, ndx_count = 0;
77
78 if (!(ndx_list = new_array(int32, the_file_list->count)))
79 out_of_memory("match_hard_links");
80
81 for (i = 0; i < the_file_list->count; i++) {
82 if (F_IS_HLINKED(FPTR(i)))
83 ndx_list[ndx_count++] = i;
84 }
85
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++])
98 {
99 file_next = FPTR(ndx_list[from+1]);
100 idev_next = F_HL_IDEV(file_next);
101 if (!LINKED(idev, idev_next))
102 break;
103 pool_free(hlink_pool, 0, idev);
104 if (prev < 0)
105 file->flags |= FLAG_HLINK_FIRST;
106 F_HL_PREV(file) = prev;
107 }
108 pool_free(hlink_pool, 0, idev);
109 if (prev < 0)
110 file->flags &= ~FLAG_HLINKED;
111 else {
112 file->flags |= FLAG_HLINK_LAST;
113 F_HL_PREV(file) = prev;
114 }
115 }
116
117 pool_destroy(hlink_pool);
118 free(ndx_list);
119}
120
121static int maybe_hard_link(struct file_struct *file, int ndx,
122 const char *fname, int statret, STRUCT_STAT *stp,
123 const char *oldname, STRUCT_STAT *old_stp,
124 const char *realname, int itemizing, enum logcode code)
125{
126 if (statret == 0) {
127 if (stp->st_dev == old_stp->st_dev
128 && stp->st_ino == old_stp->st_ino) {
129 if (itemizing) {
130 itemize(file, ndx, statret, stp,
131 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
132 0, "");
133 }
134 if (verbose > 1 && maybe_ATTRS_REPORT)
135 rprintf(FCLIENT, "%s is uptodate\n", fname);
136 file->flags |= FLAG_HLINK_DONE;
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 }
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;
160}
161
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. */
164int hard_link_check(struct file_struct *file, int ndx, const char *fname,
165 int statret, STRUCT_STAT *stp, int itemizing,
166 enum logcode code)
167{
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;
184 }
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;
235 }
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);
243 }
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;
253}
254
255int hard_link_one(struct file_struct *file, const char *fname,
256 const char *oldname, int terse)
257{
258 if (do_link(oldname, fname) < 0) {
259 enum logcode code;
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",
267 full_fname(fname), oldname);
268 return 0;
269 }
270
271 file->flags |= FLAG_HLINK_DONE;
272
273 return 1;
274}
275
276void finish_hard_link(struct file_struct *file, const char *fname,
277 STRUCT_STAT *stp, int itemizing, enum logcode code,
278 int alt_dest)
279{
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;
290 }
291 stp = &st;
292 }
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) {
305 file = FPTR(ndx);
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)
312 continue;
313 if (remove_source_files == 1 && do_xfers)
314 send_msg_int(MSG_SUCCESS, ndx);
315 }
316}
317#endif