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