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