Saved 9 more bytes per file in a typical transfer by making the length
[rsync/rsync.git] / hlink.c
CommitLineData
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 26extern int verbose;
82ad07c4 27extern int dry_run;
841d9436 28extern int do_xfers;
541b23d1 29extern int link_dest;
417c99f6 30extern int make_backups;
82ad07c4 31extern int flist_extra_ndx;
47c11975 32extern int remove_source_files;
20f90d5e 33extern int stdout_format_has_i;
541b23d1 34extern char *basis_dir[];
9f2e3c3f 35extern struct file_list *the_file_list;
dc5ddbcc 36
4f5b0756 37#ifdef SUPPORT_HARD_LINKS
9f2e3c3f 38
3cd5301f
WD
39#define SKIPPED_LINK (-1)
40#define FINISHED_LINK (-2)
41
9f2e3c3f 42#define FPTR(i) (the_file_list->files[i])
82ad07c4 43#define LINKED(i1,i2) ((i1)->dev == (i2)->dev && (i1)->ino == (i2)->ino)
9f2e3c3f
WD
44
45static int hlink_compare(int *int1, int *int2)
dc5ddbcc 46{
9f2e3c3f
WD
47 struct file_struct *f1 = FPTR(*int1);
48 struct file_struct *f2 = FPTR(*int2);
82ad07c4
WD
49 struct idev *i1 = F_IDEV(f1);
50 struct idev *i2 = F_IDEV(f2);
11dc2740 51
82ad07c4 52 if (i1->dev != i2->dev)
663b2857 53 return i1->dev > i2->dev ? 1 : -1;
dc5ddbcc 54
82ad07c4 55 if (i1->ino != i2->ino)
663b2857 56 return i1->ino > i2->ino ? 1 : -1;
dc5ddbcc 57
122d1771 58 return f_name_cmp(f1, f2);
11dc2740 59}
dc5ddbcc 60
1a05de2b
WD
61static int32 *hlink_list;
62static int32 hlink_count;
1d5cda22 63
1d5cda22 64/* Analyze the data in the hlink_list[], remove items that aren't multiply
90481755 65 * linked, and replace the dev+inode data with the hlindex+next linked list. */
9f2e3c3f 66static void link_idev_data(void)
1d5cda22 67{
82ad07c4
WD
68 int32 from, to, start;
69 struct file_struct *file, *file_next;
70 struct idev *idev, *idev_next;
71 struct hlist *hl;
1d5cda22 72
9935066b 73 alloc_pool_t hlink_pool;
9f2e3c3f 74 alloc_pool_t idev_pool = the_file_list->hlink_pool;
9935066b 75
82ad07c4 76 hlink_pool = pool_create(128 * 1024, sizeof (struct hlist), out_of_memory, POOL_INTERN);
9935066b 77
1d5cda22
WD
78 for (from = to = 0; from < hlink_count; from++) {
79 start = from;
82ad07c4
WD
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))
97e786c3 87 break;
82ad07c4
WD
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;
1d5cda22 95 }
82ad07c4 96 pool_free(idev_pool, 0, idev);
1d5cda22 97 if (from > start) {
97e786c3 98 int head = hlink_list[start];
82ad07c4
WD
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;
1d5cda22 105 hlink_list[to++] = head;
82ad07c4
WD
106 file->flags |= FLAG_HLINK_LAST;
107 F_HLIST(file) = hl;
97e786c3 108 } else
82ad07c4 109 file->flags &= ~FLAG_HLINK_INFO;
1d5cda22
WD
110 }
111
112 if (!to) {
113 free(hlink_list);
114 hlink_list = NULL;
9935066b
S
115 pool_destroy(hlink_pool);
116 hlink_pool = NULL;
1d5cda22
WD
117 } else {
118 hlink_count = to;
1a05de2b 119 hlink_list = realloc_array(hlink_list, int32, hlink_count);
9f2e3c3f 120 if (!hlink_list)
1d5cda22
WD
121 out_of_memory("init_hard_links");
122 }
9f2e3c3f 123 the_file_list->hlink_pool = hlink_pool;
9935066b 124 pool_destroy(idev_pool);
1d5cda22 125}
dc5ddbcc
AT
126#endif
127
9f2e3c3f 128void init_hard_links(void)
dc5ddbcc 129{
4f5b0756 130#ifdef SUPPORT_HARD_LINKS
dd04a034 131 int i;
11dc2740 132
6e69cff1
MP
133 if (hlink_list)
134 free(hlink_list);
135
1a05de2b 136 if (!(hlink_list = new_array(int32, the_file_list->count)))
dd04a034 137 out_of_memory("init_hard_links");
6aae748e 138
11dc2740 139 hlink_count = 0;
9f2e3c3f 140 for (i = 0; i < the_file_list->count; i++) {
82ad07c4 141 if (IS_HLINKED(FPTR(i)))
9f2e3c3f 142 hlink_list[hlink_count++] = i;
11dc2740 143 }
dc5ddbcc 144
11dc2740 145 qsort(hlink_list, hlink_count,
0d6e308b 146 sizeof hlink_list[0], (int (*)()) hlink_compare);
dc5ddbcc 147
6aae748e
WD
148 if (!hlink_count) {
149 free(hlink_list);
150 hlink_list = NULL;
1d5cda22 151 } else
9f2e3c3f 152 link_idev_data();
dc5ddbcc 153#endif
dc5ddbcc
AT
154}
155
3cd5301f
WD
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)
d38fc305 189{
4f5b0756 190#ifdef SUPPORT_HARD_LINKS
3cd5301f 191 int head;
82ad07c4
WD
192 struct hlist *hl = F_HLIST(file);
193
194 if (skip && !(file->flags & FLAG_HLINK_LAST))
195 head = hlink_list[hl->hlindex] = hl->next;
3cd5301f 196 else
82ad07c4 197 head = hlink_list[hl->hlindex];
3cd5301f
WD
198 if (ndx != head) {
199 struct file_struct *head_file = FPTR(head);
82ad07c4 200 struct hlist *hf_hl = F_HLIST(head_file);
20f90d5e 201 if (!stdout_format_has_i && verbose > 1) {
6cbde57d
WD
202 rprintf(FINFO, "\"%s\" is a hard link\n",
203 f_name(file, NULL));
204 }
82ad07c4 205 if (hf_hl->hlindex == FINISHED_LINK) {
541b23d1 206 STRUCT_STAT st2, st3;
1a05de2b 207 char toname[MAXPATHLEN];
82ad07c4 208 int ldu = hf_hl->dest_used;
1a05de2b
WD
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);
3cd5301f
WD
214 if (link_stat(toname, &st2, 0) < 0) {
215 rsyserr(FERROR, errno, "stat %s failed",
216 full_fname(toname));
217 return -1;
218 }
541b23d1
WD
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;
36e2ea60
WD
232 if (verbose < 2 || !stdout_format_has_i) {
233 itemizing = 0;
234 code = FNONE;
235 }
541b23d1
WD
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 }
3cd5301f
WD
246 maybe_hard_link(file, ndx, fname, statret, st,
247 toname, &st2, itemizing, code);
663b2857
WD
248 if (remove_source_files == 1 && do_xfers)
249 send_msg_int(MSG_SUCCESS, ndx);
82ad07c4 250 hl->hlindex = FINISHED_LINK;
3cd5301f 251 } else
82ad07c4 252 hl->hlindex = SKIPPED_LINK;
d38fc305
WD
253 return 1;
254 }
bb91a624 255#endif
d38fc305
WD
256 return 0;
257}
258
4f5b0756 259#ifdef SUPPORT_HARD_LINKS
9f2e3c3f
WD
260int hard_link_one(struct file_struct *file, int ndx, char *fname,
261 int statret, STRUCT_STAT *st, char *toname, int terse,
262 int itemizing, enum logcode code)
a16bbc39 263{
9f2e3c3f 264 if (do_link(toname, fname)) {
3cd5301f
WD
265 if (terse) {
266 if (!verbose)
267 return -1;
268 code = FINFO;
269 } else
270 code = FERROR;
271 rsyserr(code, errno, "link %s => %s failed",
45c49b52 272 full_fname(fname), toname);
9f2e3c3f
WD
273 return -1;
274 }
275
276 if (itemizing) {
b7d4d28b
WD
277 itemize(file, ndx, statret, st,
278 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0,
9f2e3c3f
WD
279 terse ? "" : toname);
280 }
36e2ea60 281 if (code != FNONE && verbose && !terse)
45c49b52 282 rprintf(code, "%s => %s\n", fname, toname);
9f2e3c3f 283 return 0;
a16bbc39
AT
284}
285#endif
286
fae5bb31 287
9f2e3c3f 288void hard_link_cluster(struct file_struct *file, int master, int itemizing,
82ad07c4 289 enum logcode code, int dest_used)
dc5ddbcc 290{
4f5b0756 291#ifdef SUPPORT_HARD_LINKS
3fef5364
WD
292 char hlink1[MAXPATHLEN];
293 char *hlink2;
1d5cda22 294 STRUCT_STAT st1, st2;
9f2e3c3f 295 int statret, ndx = master;
82ad07c4 296 struct hlist *hl = F_HLIST(file);
a16bbc39 297
82ad07c4
WD
298 hl->hlindex = FINISHED_LINK;
299 if (dry_run)
300 hl->dest_used = dest_used + 1;
6cbde57d 301 if (link_stat(f_name(file, hlink1), &st1, 0) < 0)
6e69cff1 302 return;
82ad07c4
WD
303 if (!(file->flags & FLAG_HLINK_FIRST)) {
304 while (!(file->flags & FLAG_HLINK_LAST)) {
305 ndx = hl->next;
9f2e3c3f 306 file = FPTR(ndx);
82ad07c4 307 hl = F_HLIST(file);
9f2e3c3f
WD
308 }
309 }
310 do {
82ad07c4 311 ndx = hl->next;
9f2e3c3f 312 file = FPTR(ndx);
82ad07c4
WD
313 hl = F_HLIST(file);
314 if (hl->hlindex != SKIPPED_LINK)
1d5cda22 315 continue;
6cbde57d 316 hlink2 = f_name(file, NULL);
3cd5301f
WD
317 statret = link_stat(hlink2, &st2, 0);
318 maybe_hard_link(file, ndx, hlink2, statret, &st2,
319 hlink1, &st1, itemizing, code);
663b2857
WD
320 if (remove_source_files == 1 && do_xfers)
321 send_msg_int(MSG_SUCCESS, ndx);
82ad07c4
WD
322 hl->hlindex = FINISHED_LINK;
323 } while (!(file->flags & FLAG_HLINK_LAST));
dc5ddbcc
AT
324#endif
325}