- Typical tranfers now save 12-20 bytes per file because several vars
[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 flist_extra_ndx;
32extern int remove_source_files;
33extern int stdout_format_has_i;
34extern char *basis_dir[];
35extern struct file_list *the_file_list;
36
37#ifdef SUPPORT_HARD_LINKS
38
39#define SKIPPED_LINK (-1)
40#define FINISHED_LINK (-2)
41
42#define FPTR(i) (the_file_list->files[i])
43#define LINKED(i1,i2) ((i1)->dev == (i2)->dev && (i1)->ino == (i2)->ino)
44
45static int hlink_compare(int *int1, int *int2)
46{
47 struct file_struct *f1 = FPTR(*int1);
48 struct file_struct *f2 = FPTR(*int2);
49 struct idev *i1 = F_IDEV(f1);
50 struct idev *i2 = F_IDEV(f2);
51
52 if (i1->dev != i2->dev)
53 return (int)(i1->dev > i2->dev ? 1 : -1);
54
55 if (i1->ino != i2->ino)
56 return (int)(i1->ino > i2->ino ? 1 : -1);
57
58 return f_name_cmp(f1, f2);
59}
60
61static int32 *hlink_list;
62static int32 hlink_count;
63
64/* Analyze the data in the hlink_list[], remove items that aren't multiply
65 * linked, and replace the dev+inode data with the hlindex+next linked list. */
66static void link_idev_data(void)
67{
68 int32 from, to, start;
69 struct file_struct *file, *file_next;
70 struct idev *idev, *idev_next;
71 struct hlist *hl;
72
73 alloc_pool_t hlink_pool;
74 alloc_pool_t idev_pool = the_file_list->hlink_pool;
75
76 hlink_pool = pool_create(128 * 1024, sizeof (struct hlist), out_of_memory, POOL_INTERN);
77
78 for (from = to = 0; from < hlink_count; from++) {
79 start = from;
80 for (file = FPTR(hlink_list[from]), idev = F_IDEV(file);
81 from < hlink_count-1;
82 file = file_next, idev = idev_next)
83 {
84 file_next = FPTR(hlink_list[from+1]);
85 idev_next = F_IDEV(file_next);
86 if (!LINKED(idev, idev_next))
87 break;
88 pool_free(idev_pool, 0, idev);
89 hl = pool_talloc(hlink_pool, struct hlist, 1,
90 "hlink_list");
91 hl->hlindex = to;
92 hl->next = hlink_list[++from];
93 hl->dest_used = 0;
94 F_HLIST(file) = hl;
95 }
96 pool_free(idev_pool, 0, idev);
97 if (from > start) {
98 int head = hlink_list[start];
99 hl = pool_talloc(hlink_pool, struct hlist, 1,
100 "hlink_list");
101 FPTR(head)->flags |= FLAG_HLINK_FIRST;
102 hl->hlindex = to;
103 hl->next = head;
104 hl->dest_used = 0;
105 hlink_list[to++] = head;
106 file->flags |= FLAG_HLINK_LAST;
107 F_HLIST(file) = hl;
108 } else
109 file->flags &= ~FLAG_HLINK_INFO;
110 }
111
112 if (!to) {
113 free(hlink_list);
114 hlink_list = NULL;
115 pool_destroy(hlink_pool);
116 hlink_pool = NULL;
117 } else {
118 hlink_count = to;
119 hlink_list = realloc_array(hlink_list, int32, hlink_count);
120 if (!hlink_list)
121 out_of_memory("init_hard_links");
122 }
123 the_file_list->hlink_pool = hlink_pool;
124 pool_destroy(idev_pool);
125}
126#endif
127
128void init_hard_links(void)
129{
130#ifdef SUPPORT_HARD_LINKS
131 int i;
132
133 if (hlink_list)
134 free(hlink_list);
135
136 if (!(hlink_list = new_array(int32, the_file_list->count)))
137 out_of_memory("init_hard_links");
138
139 hlink_count = 0;
140 for (i = 0; i < the_file_list->count; i++) {
141 if (IS_HLINKED(FPTR(i)))
142 hlink_list[hlink_count++] = i;
143 }
144
145 qsort(hlink_list, hlink_count,
146 sizeof hlink_list[0], (int (*)()) hlink_compare);
147
148 if (!hlink_count) {
149 free(hlink_list);
150 hlink_list = NULL;
151 } else
152 link_idev_data();
153#endif
154}
155
156#ifdef SUPPORT_HARD_LINKS
157static int maybe_hard_link(struct file_struct *file, int ndx,
158 char *fname, int statret, STRUCT_STAT *st,
159 char *toname, STRUCT_STAT *to_st,
160 int itemizing, enum logcode code)
161{
162 if (statret == 0) {
163 if (st->st_dev == to_st->st_dev
164 && st->st_ino == to_st->st_ino) {
165 if (itemizing) {
166 itemize(file, ndx, statret, st,
167 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
168 0, "");
169 }
170 return 0;
171 }
172 if (make_backups) {
173 if (!make_backup(fname))
174 return -1;
175 } else if (robust_unlink(fname)) {
176 rsyserr(FERROR, errno, "unlink %s failed",
177 full_fname(fname));
178 return -1;
179 }
180 }
181 return hard_link_one(file, ndx, fname, statret, st, toname,
182 0, itemizing, code);
183}
184#endif
185
186int hard_link_check(struct file_struct *file, int ndx, char *fname,
187 int statret, STRUCT_STAT *st, int itemizing,
188 enum logcode code, int skip)
189{
190#ifdef SUPPORT_HARD_LINKS
191 int head;
192 struct hlist *hl = F_HLIST(file);
193
194 if (skip && !(file->flags & FLAG_HLINK_LAST))
195 head = hlink_list[hl->hlindex] = hl->next;
196 else
197 head = hlink_list[hl->hlindex];
198 if (ndx != head) {
199 struct file_struct *head_file = FPTR(head);
200 struct hlist *hf_hl = F_HLIST(head_file);
201 if (!stdout_format_has_i && verbose > 1) {
202 rprintf(FINFO, "\"%s\" is a hard link\n",
203 f_name(file, NULL));
204 }
205 if (hf_hl->hlindex == FINISHED_LINK) {
206 STRUCT_STAT st2, st3;
207 char toname[MAXPATHLEN];
208 int ldu = hf_hl->dest_used;
209 if (ldu) {
210 pathjoin(toname, MAXPATHLEN, basis_dir[ldu-1],
211 f_name(head_file, NULL));
212 } else
213 f_name(head_file, toname);
214 if (link_stat(toname, &st2, 0) < 0) {
215 rsyserr(FERROR, errno, "stat %s failed",
216 full_fname(toname));
217 return -1;
218 }
219 if (statret < 0 && basis_dir[0] != NULL) {
220 char cmpbuf[MAXPATHLEN];
221 int j = 0;
222 do {
223 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
224 if (link_stat(cmpbuf, &st3, 0) < 0)
225 continue;
226 if (link_dest) {
227 if (st2.st_dev != st3.st_dev
228 || st2.st_ino != st3.st_ino)
229 continue;
230 statret = 1;
231 st = &st3;
232 if (verbose < 2 || !stdout_format_has_i) {
233 itemizing = 0;
234 code = FNONE;
235 }
236 break;
237 }
238 if (!unchanged_file(cmpbuf, file, &st3))
239 continue;
240 statret = 1;
241 st = &st3;
242 if (unchanged_attrs(file, &st3))
243 break;
244 } while (basis_dir[++j] != NULL);
245 }
246 maybe_hard_link(file, ndx, fname, statret, st,
247 toname, &st2, itemizing, code);
248 if (remove_source_files == 1 && do_xfers) {
249 char numbuf[4];
250 SIVAL(numbuf, 0, ndx);
251 send_msg(MSG_SUCCESS, numbuf, 4);
252 }
253 hl->hlindex = FINISHED_LINK;
254 } else
255 hl->hlindex = SKIPPED_LINK;
256 return 1;
257 }
258#endif
259 return 0;
260}
261
262#ifdef SUPPORT_HARD_LINKS
263int hard_link_one(struct file_struct *file, int ndx, char *fname,
264 int statret, STRUCT_STAT *st, char *toname, int terse,
265 int itemizing, enum logcode code)
266{
267 if (do_link(toname, fname)) {
268 if (terse) {
269 if (!verbose)
270 return -1;
271 code = FINFO;
272 } else
273 code = FERROR;
274 rsyserr(code, errno, "link %s => %s failed",
275 full_fname(fname), toname);
276 return -1;
277 }
278
279 if (itemizing) {
280 itemize(file, ndx, statret, st,
281 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0,
282 terse ? "" : toname);
283 }
284 if (code != FNONE && verbose && !terse)
285 rprintf(code, "%s => %s\n", fname, toname);
286 return 0;
287}
288#endif
289
290
291void hard_link_cluster(struct file_struct *file, int master, int itemizing,
292 enum logcode code, int dest_used)
293{
294#ifdef SUPPORT_HARD_LINKS
295 char hlink1[MAXPATHLEN];
296 char *hlink2;
297 STRUCT_STAT st1, st2;
298 int statret, ndx = master;
299 struct hlist *hl = F_HLIST(file);
300
301 hl->hlindex = FINISHED_LINK;
302 if (dry_run)
303 hl->dest_used = dest_used + 1;
304 if (link_stat(f_name(file, hlink1), &st1, 0) < 0)
305 return;
306 if (!(file->flags & FLAG_HLINK_FIRST)) {
307 while (!(file->flags & FLAG_HLINK_LAST)) {
308 ndx = hl->next;
309 file = FPTR(ndx);
310 hl = F_HLIST(file);
311 }
312 }
313 do {
314 ndx = hl->next;
315 file = FPTR(ndx);
316 hl = F_HLIST(file);
317 if (hl->hlindex != SKIPPED_LINK)
318 continue;
319 hlink2 = f_name(file, NULL);
320 statret = link_stat(hlink2, &st2, 0);
321 maybe_hard_link(file, ndx, hlink2, statret, &st2,
322 hlink1, &st1, itemizing, code);
323 if (remove_source_files == 1 && do_xfers) {
324 char numbuf[4];
325 SIVAL(numbuf, 0, ndx);
326 send_msg(MSG_SUCCESS, numbuf, 4);
327 }
328 hl->hlindex = FINISHED_LINK;
329 } while (!(file->flags & FLAG_HLINK_LAST));
330#endif
331}