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