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