Tweaked some whitespace to match the latest version from autoconf.
[rsync/rsync.git] / hlink.c
CommitLineData
6aae748e 1/*
dc5ddbcc
AT
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
6e69cff1 4 Copyright (C) 2002 by Martin Pool <mbp@samba.org>
6aae748e 5
dc5ddbcc
AT
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
6aae748e 10
dc5ddbcc
AT
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
6aae748e 15
dc5ddbcc
AT
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19*/
20
21#include "rsync.h"
22
9a52223b 23extern int verbose;
541b23d1 24extern int link_dest;
417c99f6 25extern int make_backups;
a45f581b 26extern int log_format_has_i;
541b23d1 27extern char *basis_dir[];
9f2e3c3f 28extern struct file_list *the_file_list;
dc5ddbcc 29
4f5b0756 30#ifdef SUPPORT_HARD_LINKS
9f2e3c3f 31
3cd5301f
WD
32#define SKIPPED_LINK (-1)
33#define FINISHED_LINK (-2)
34
9f2e3c3f
WD
35#define FPTR(i) (the_file_list->files[i])
36#define LINKED(p1,p2) (FPTR(p1)->F_DEV == FPTR(p2)->F_DEV \
37 && FPTR(p1)->F_INODE == FPTR(p2)->F_INODE)
38
39static int hlink_compare(int *int1, int *int2)
dc5ddbcc 40{
9f2e3c3f
WD
41 struct file_struct *f1 = FPTR(*int1);
42 struct file_struct *f2 = FPTR(*int2);
11dc2740 43
92cc9dd7
WD
44 if (f1->F_DEV != f2->F_DEV)
45 return (int) (f1->F_DEV > f2->F_DEV ? 1 : -1);
dc5ddbcc 46
92cc9dd7
WD
47 if (f1->F_INODE != f2->F_INODE)
48 return (int) (f1->F_INODE > f2->F_INODE ? 1 : -1);
dc5ddbcc 49
122d1771 50 return f_name_cmp(f1, f2);
11dc2740 51}
dc5ddbcc 52
9f2e3c3f 53static int *hlink_list;
417c99f6 54static int hlink_count;
1d5cda22 55
1d5cda22 56/* Analyze the data in the hlink_list[], remove items that aren't multiply
90481755 57 * linked, and replace the dev+inode data with the hlindex+next linked list. */
9f2e3c3f 58static void link_idev_data(void)
1d5cda22 59{
97e786c3 60 int cur, from, to, start;
1d5cda22 61
9935066b 62 alloc_pool_t hlink_pool;
9f2e3c3f 63 alloc_pool_t idev_pool = the_file_list->hlink_pool;
9935066b
S
64
65 hlink_pool = pool_create(128 * 1024, sizeof (struct hlink),
66 out_of_memory, POOL_INTERN);
67
1d5cda22
WD
68 for (from = to = 0; from < hlink_count; from++) {
69 start = from;
97e786c3
WD
70 while (1) {
71 cur = hlink_list[from];
72 if (from == hlink_count-1
73 || !LINKED(cur, hlink_list[from+1]))
74 break;
75 pool_free(idev_pool, 0, FPTR(cur)->link_u.idev);
76 FPTR(cur)->link_u.links = pool_talloc(hlink_pool,
9935066b
S
77 struct hlink, 1, "hlink_list");
78
97e786c3
WD
79 FPTR(cur)->F_HLINDEX = to;
80 FPTR(cur)->F_NEXT = hlink_list[++from];
1d5cda22 81 }
97e786c3 82 pool_free(idev_pool, 0, FPTR(cur)->link_u.idev);
1d5cda22 83 if (from > start) {
97e786c3
WD
84 int head = hlink_list[start];
85 FPTR(cur)->link_u.links = pool_talloc(hlink_pool,
9935066b
S
86 struct hlink, 1, "hlink_list");
87
9f2e3c3f 88 FPTR(head)->flags |= FLAG_HLINK_TOL;
97e786c3
WD
89 FPTR(cur)->F_HLINDEX = to;
90 FPTR(cur)->F_NEXT = head;
91 FPTR(cur)->flags |= FLAG_HLINK_EOL;
1d5cda22 92 hlink_list[to++] = head;
97e786c3
WD
93 } else
94 FPTR(cur)->link_u.links = NULL;
1d5cda22
WD
95 }
96
97 if (!to) {
98 free(hlink_list);
99 hlink_list = NULL;
9935066b
S
100 pool_destroy(hlink_pool);
101 hlink_pool = NULL;
1d5cda22
WD
102 } else {
103 hlink_count = to;
9f2e3c3f
WD
104 hlink_list = realloc_array(hlink_list, int, hlink_count);
105 if (!hlink_list)
1d5cda22
WD
106 out_of_memory("init_hard_links");
107 }
9f2e3c3f 108 the_file_list->hlink_pool = hlink_pool;
9935066b 109 pool_destroy(idev_pool);
1d5cda22 110}
dc5ddbcc
AT
111#endif
112
9f2e3c3f 113void init_hard_links(void)
dc5ddbcc 114{
4f5b0756 115#ifdef SUPPORT_HARD_LINKS
dd04a034 116 int i;
11dc2740 117
6e69cff1
MP
118 if (hlink_list)
119 free(hlink_list);
120
9f2e3c3f 121 if (!(hlink_list = new_array(int, the_file_list->count)))
dd04a034 122 out_of_memory("init_hard_links");
6aae748e 123
11dc2740 124 hlink_count = 0;
9f2e3c3f
WD
125 for (i = 0; i < the_file_list->count; i++) {
126 if (FPTR(i)->link_u.idev)
127 hlink_list[hlink_count++] = i;
11dc2740 128 }
dc5ddbcc 129
11dc2740 130 qsort(hlink_list, hlink_count,
0d6e308b 131 sizeof hlink_list[0], (int (*)()) hlink_compare);
dc5ddbcc 132
6aae748e
WD
133 if (!hlink_count) {
134 free(hlink_list);
135 hlink_list = NULL;
1d5cda22 136 } else
9f2e3c3f 137 link_idev_data();
dc5ddbcc 138#endif
dc5ddbcc
AT
139}
140
3cd5301f
WD
141#ifdef SUPPORT_HARD_LINKS
142static int maybe_hard_link(struct file_struct *file, int ndx,
143 char *fname, int statret, STRUCT_STAT *st,
144 char *toname, STRUCT_STAT *to_st,
145 int itemizing, enum logcode code)
146{
147 if (statret == 0) {
148 if (st->st_dev == to_st->st_dev
149 && st->st_ino == to_st->st_ino) {
150 if (itemizing) {
151 itemize(file, ndx, statret, st,
152 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
153 0, "");
154 }
155 return 0;
156 }
157 if (make_backups) {
158 if (!make_backup(fname))
159 return -1;
160 } else if (robust_unlink(fname)) {
161 rsyserr(FERROR, errno, "unlink %s failed",
162 full_fname(fname));
163 return -1;
164 }
165 }
166 return hard_link_one(file, ndx, fname, statret, st, toname,
167 0, itemizing, code);
168}
169#endif
170
171int hard_link_check(struct file_struct *file, int ndx, char *fname,
172 int statret, STRUCT_STAT *st, int itemizing,
173 enum logcode code, int skip)
d38fc305 174{
4f5b0756 175#ifdef SUPPORT_HARD_LINKS
3cd5301f 176 int head;
d38fc305 177 if (skip && !(file->flags & FLAG_HLINK_EOL))
3cd5301f
WD
178 head = hlink_list[file->F_HLINDEX] = file->F_NEXT;
179 else
180 head = hlink_list[file->F_HLINDEX];
181 if (ndx != head) {
182 struct file_struct *head_file = FPTR(head);
6cbde57d
WD
183 if (!log_format_has_i && verbose > 1) {
184 rprintf(FINFO, "\"%s\" is a hard link\n",
185 f_name(file, NULL));
186 }
3cd5301f 187 if (head_file->F_HLINDEX == FINISHED_LINK) {
541b23d1 188 STRUCT_STAT st2, st3;
6cbde57d 189 char *toname = f_name(head_file, NULL);
3cd5301f
WD
190 if (link_stat(toname, &st2, 0) < 0) {
191 rsyserr(FERROR, errno, "stat %s failed",
192 full_fname(toname));
193 return -1;
194 }
541b23d1
WD
195 if (statret < 0 && basis_dir[0] != NULL) {
196 char cmpbuf[MAXPATHLEN];
197 int j = 0;
198 do {
199 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
200 if (link_stat(cmpbuf, &st3, 0) < 0)
201 continue;
202 if (link_dest) {
203 if (st2.st_dev != st3.st_dev
204 || st2.st_ino != st3.st_ino)
205 continue;
206 statret = 1;
207 st = &st3;
208 if (verbose < 2 || !log_format_has_i)
209 itemizing = code = 0;
210 break;
211 }
212 if (!unchanged_file(cmpbuf, file, &st3))
213 continue;
214 statret = 1;
215 st = &st3;
216 if (unchanged_attrs(file, &st3))
217 break;
218 } while (basis_dir[++j] != NULL);
219 }
3cd5301f
WD
220 maybe_hard_link(file, ndx, fname, statret, st,
221 toname, &st2, itemizing, code);
222 file->F_HLINDEX = FINISHED_LINK;
223 } else
224 file->F_HLINDEX = SKIPPED_LINK;
d38fc305
WD
225 return 1;
226 }
bb91a624 227#endif
d38fc305
WD
228 return 0;
229}
230
4f5b0756 231#ifdef SUPPORT_HARD_LINKS
9f2e3c3f
WD
232int hard_link_one(struct file_struct *file, int ndx, char *fname,
233 int statret, STRUCT_STAT *st, char *toname, int terse,
234 int itemizing, enum logcode code)
a16bbc39 235{
9f2e3c3f 236 if (do_link(toname, fname)) {
3cd5301f
WD
237 if (terse) {
238 if (!verbose)
239 return -1;
240 code = FINFO;
241 } else
242 code = FERROR;
243 rsyserr(code, errno, "link %s => %s failed",
45c49b52 244 full_fname(fname), toname);
9f2e3c3f
WD
245 return -1;
246 }
247
248 if (itemizing) {
b7d4d28b
WD
249 itemize(file, ndx, statret, st,
250 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0,
9f2e3c3f
WD
251 terse ? "" : toname);
252 }
45c49b52
WD
253 if (code && verbose && !terse)
254 rprintf(code, "%s => %s\n", fname, toname);
9f2e3c3f 255 return 0;
a16bbc39
AT
256}
257#endif
258
fae5bb31 259
9f2e3c3f
WD
260void hard_link_cluster(struct file_struct *file, int master, int itemizing,
261 enum logcode code)
dc5ddbcc 262{
4f5b0756 263#ifdef SUPPORT_HARD_LINKS
3fef5364
WD
264 char hlink1[MAXPATHLEN];
265 char *hlink2;
1d5cda22 266 STRUCT_STAT st1, st2;
9f2e3c3f 267 int statret, ndx = master;
a16bbc39 268
3cd5301f 269 file->F_HLINDEX = FINISHED_LINK;
6cbde57d 270 if (link_stat(f_name(file, hlink1), &st1, 0) < 0)
6e69cff1 271 return;
9f2e3c3f
WD
272 if (!(file->flags & FLAG_HLINK_TOL)) {
273 while (!(file->flags & FLAG_HLINK_EOL)) {
274 ndx = file->F_NEXT;
275 file = FPTR(ndx);
276 }
277 }
278 do {
279 ndx = file->F_NEXT;
280 file = FPTR(ndx);
3cd5301f 281 if (file->F_HLINDEX != SKIPPED_LINK)
1d5cda22 282 continue;
6cbde57d 283 hlink2 = f_name(file, NULL);
3cd5301f
WD
284 statret = link_stat(hlink2, &st2, 0);
285 maybe_hard_link(file, ndx, hlink2, statret, &st2,
286 hlink1, &st1, itemizing, code);
287 file->F_HLINDEX = FINISHED_LINK;
9f2e3c3f 288 } while (!(file->flags & FLAG_HLINK_EOL));
dc5ddbcc
AT
289#endif
290}