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