Got rid of calls to (the soon to vanish) safe_fname() function.
[rsync/rsync.git] / hlink.c
... / ...
CommitLineData
1/*
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
4 Copyright (C) 2002 by Martin Pool <mbp@samba.org>
5
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.
10
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.
15
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
23extern int dry_run;
24extern int verbose;
25extern int link_dest;
26extern int make_backups;
27extern int log_format_has_i;
28extern char *basis_dir[];
29extern struct file_list *the_file_list;
30
31#ifdef SUPPORT_HARD_LINKS
32
33#define SKIPPED_LINK (-1)
34#define FINISHED_LINK (-2)
35
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)
41{
42 struct file_struct *f1 = FPTR(*int1);
43 struct file_struct *f2 = FPTR(*int2);
44
45 if (f1->F_DEV != f2->F_DEV)
46 return (int) (f1->F_DEV > f2->F_DEV ? 1 : -1);
47
48 if (f1->F_INODE != f2->F_INODE)
49 return (int) (f1->F_INODE > f2->F_INODE ? 1 : -1);
50
51 return f_name_cmp(f1, f2);
52}
53
54static int *hlink_list;
55static int hlink_count;
56
57/* Analyze the data in the hlink_list[], remove items that aren't multiply
58 * linked, and replace the dev+inode data with the hlindex+next linked list. */
59static void link_idev_data(void)
60{
61 int cur, from, to, start;
62
63 alloc_pool_t hlink_pool;
64 alloc_pool_t idev_pool = the_file_list->hlink_pool;
65
66 hlink_pool = pool_create(128 * 1024, sizeof (struct hlink),
67 out_of_memory, POOL_INTERN);
68
69 for (from = to = 0; from < hlink_count; from++) {
70 start = from;
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,
78 struct hlink, 1, "hlink_list");
79
80 FPTR(cur)->F_HLINDEX = to;
81 FPTR(cur)->F_NEXT = hlink_list[++from];
82 }
83 pool_free(idev_pool, 0, FPTR(cur)->link_u.idev);
84 if (from > start) {
85 int head = hlink_list[start];
86 FPTR(cur)->link_u.links = pool_talloc(hlink_pool,
87 struct hlink, 1, "hlink_list");
88
89 FPTR(head)->flags |= FLAG_HLINK_TOL;
90 FPTR(cur)->F_HLINDEX = to;
91 FPTR(cur)->F_NEXT = head;
92 FPTR(cur)->flags |= FLAG_HLINK_EOL;
93 hlink_list[to++] = head;
94 } else
95 FPTR(cur)->link_u.links = NULL;
96 }
97
98 if (!to) {
99 free(hlink_list);
100 hlink_list = NULL;
101 pool_destroy(hlink_pool);
102 hlink_pool = NULL;
103 } else {
104 hlink_count = to;
105 hlink_list = realloc_array(hlink_list, int, hlink_count);
106 if (!hlink_list)
107 out_of_memory("init_hard_links");
108 }
109 the_file_list->hlink_pool = hlink_pool;
110 pool_destroy(idev_pool);
111}
112#endif
113
114void init_hard_links(void)
115{
116#ifdef SUPPORT_HARD_LINKS
117 int i;
118
119 if (hlink_list)
120 free(hlink_list);
121
122 if (!(hlink_list = new_array(int, the_file_list->count)))
123 out_of_memory("init_hard_links");
124
125 hlink_count = 0;
126 for (i = 0; i < the_file_list->count; i++) {
127 if (FPTR(i)->link_u.idev)
128 hlink_list[hlink_count++] = i;
129 }
130
131 qsort(hlink_list, hlink_count,
132 sizeof hlink_list[0], (int (*)()) hlink_compare);
133
134 if (!hlink_count) {
135 free(hlink_list);
136 hlink_list = NULL;
137 } else
138 link_idev_data();
139#endif
140}
141
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)
175{
176#ifdef SUPPORT_HARD_LINKS
177 int head;
178 if (skip && !(file->flags & FLAG_HLINK_EOL))
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);
184 if (!log_format_has_i && verbose > 1)
185 rprintf(FINFO, "\"%s\" is a hard link\n", f_name(file));
186 if (head_file->F_HLINDEX == FINISHED_LINK) {
187 STRUCT_STAT st2, st3;
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 }
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 }
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;
224 return 1;
225 }
226#endif
227 return 0;
228}
229
230#ifdef SUPPORT_HARD_LINKS
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)
234{
235 if (do_link(toname, fname)) {
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",
243 full_fname(fname), toname);
244 return -1;
245 }
246
247 if (itemizing) {
248 itemize(file, ndx, statret, st,
249 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0,
250 terse ? "" : toname);
251 }
252 if (code && verbose && !terse)
253 rprintf(code, "%s => %s\n", fname, toname);
254 return 0;
255}
256#endif
257
258
259void hard_link_cluster(struct file_struct *file, int master, int itemizing,
260 enum logcode code)
261{
262#ifdef SUPPORT_HARD_LINKS
263 char hlink1[MAXPATHLEN];
264 char *hlink2;
265 STRUCT_STAT st1, st2;
266 int statret, ndx = master;
267
268 file->F_HLINDEX = FINISHED_LINK;
269 if (link_stat(f_name_to(file, hlink1), &st1, 0) < 0)
270 return;
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);
280 if (file->F_HLINDEX != SKIPPED_LINK)
281 continue;
282 hlink2 = f_name(file);
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;
287 } while (!(file->flags & FLAG_HLINK_EOL));
288#endif
289}