2 * Routines to support hard-linking.
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
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.
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.
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.
30 extern int make_backups;
31 extern int remove_source_files;
32 extern int stdout_format_has_i;
33 extern int maybe_ATTRS_REPORT;
34 extern char *basis_dir[];
35 extern struct file_list *the_file_list;
37 #ifdef SUPPORT_HARD_LINKS
39 alloc_pool_t hlink_pool;
41 #define FPTR(i) (the_file_list->files[i])
42 #define LINKED(i1,i2) ((i1)->dev == (i2)->dev && (i1)->ino == (i2)->ino)
44 void init_hard_links(void)
46 if (!(hlink_pool = pool_create(HLINK_EXTENT, sizeof (struct idev),
47 out_of_memory, POOL_INTERN)))
48 out_of_memory("init_hard_links");
51 static int hlink_compare(int *int1, int *int2)
53 struct file_struct *f1 = FPTR(*int1);
54 struct file_struct *f2 = FPTR(*int2);
55 struct idev *i1 = F_HL_IDEV(f1);
56 struct idev *i2 = F_HL_IDEV(f2);
58 if (i1->dev != i2->dev)
59 return i1->dev > i2->dev ? 1 : -1;
61 if (i1->ino != i2->ino)
62 return i1->ino > i2->ino ? 1 : -1;
64 return f_name_cmp(f1, f2);
67 /* Analyze the dev+inode data in the file-list by creating a list of all
68 * the items that have hlink data, sorting them, and matching up identical
69 * values into clusters. These will be a single linked list from last to
70 * first when we're done. */
71 void match_hard_links(void)
73 int32 from, prev, *ndx_list;
74 struct file_struct *file, *file_next;
75 struct idev *idev, *idev_next;
78 if (!(ndx_list = new_array(int32, the_file_list->count)))
79 out_of_memory("match_hard_links");
81 for (i = 0; i < the_file_list->count; i++) {
82 if (F_IS_HLINKED(FPTR(i)))
83 ndx_list[ndx_count++] = i;
91 qsort(ndx_list, ndx_count, sizeof ndx_list[0],
92 (int (*)()) hlink_compare);
94 for (from = 0; from < ndx_count; from++) {
95 for (file = FPTR(ndx_list[from]), idev = F_HL_IDEV(file), prev = -1;
97 file = file_next, idev = idev_next, prev = ndx_list[from++])
99 file_next = FPTR(ndx_list[from+1]);
100 idev_next = F_HL_IDEV(file_next);
101 if (!LINKED(idev, idev_next))
103 pool_free(hlink_pool, 0, idev);
105 file->flags |= FLAG_HLINK_FIRST;
106 F_HL_PREV(file) = prev;
108 pool_free(hlink_pool, 0, idev);
110 file->flags &= ~FLAG_HLINKED;
112 file->flags |= FLAG_HLINK_LAST;
113 F_HL_PREV(file) = prev;
117 pool_destroy(hlink_pool);
121 static int maybe_hard_link(struct file_struct *file, int ndx,
122 const char *fname, int statret, STRUCT_STAT *stp,
123 const char *oldname, STRUCT_STAT *old_stp,
124 const char *realname, int itemizing, enum logcode code)
127 if (stp->st_dev == old_stp->st_dev
128 && stp->st_ino == old_stp->st_ino) {
130 itemize(file, ndx, statret, stp,
131 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
134 if (verbose > 1 && maybe_ATTRS_REPORT)
135 rprintf(FCLIENT, "%s is uptodate\n", fname);
136 file->flags |= FLAG_HLINK_DONE;
140 if (!make_backup(fname))
142 } else if (robust_unlink(fname)) {
143 rsyserr(FERROR, errno, "unlink %s failed",
149 if (hard_link_one(file, fname, oldname, 0)) {
151 itemize(file, ndx, statret, stp,
152 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0,
155 if (code != FNONE && verbose)
156 rprintf(code, "%s => %s\n", fname, realname);
162 /* Only called if FLAG_HLINKED is set and FLAG_HLINK_FIRST is not. Returns:
163 * 0 = process the file, 1 = skip the file, -1 = error occurred. */
164 int hard_link_check(struct file_struct *file, int ndx, const char *fname,
165 int statret, STRUCT_STAT *stp, int itemizing,
169 char prev_name[MAXPATHLEN], altbuf[MAXPATHLEN], *realname;
170 int alt_dest, prev_ndx = F_HL_PREV(file);
171 struct file_struct *prev_file = FPTR(prev_ndx);
173 /* Is the previous link is not complete yet? */
174 if (!(prev_file->flags & FLAG_HLINK_DONE)) {
175 /* Is the previous link being transferred? */
176 if (prev_file->flags & FLAG_SENT) {
177 /* Add ourselves to the list of files that will be
178 * updated when the transfer completes, and mark
179 * ourself as waiting for the transfer. */
180 F_HL_PREV(file) = F_HL_PREV(prev_file);
181 F_HL_PREV(prev_file) = ndx;
182 file->flags |= FLAG_SENT;
188 /* There is a finished file to link with! */
189 if (!(prev_file->flags & FLAG_HLINK_FIRST)) {
190 /* The previous previous will be marked with FIRST. */
191 prev_ndx = F_HL_PREV(prev_file);
192 prev_file = FPTR(prev_ndx);
193 /* Update our previous pointer to point to the first. */
194 F_HL_PREV(file) = prev_ndx;
196 alt_dest = F_HL_PREV(prev_file); /* alternate value when DONE && FIRST */
197 if (alt_dest >= 0 && dry_run) {
198 pathjoin(prev_name, MAXPATHLEN, basis_dir[alt_dest],
199 f_name(prev_file, NULL));
200 f_name(prev_file, altbuf);
203 f_name(prev_file, prev_name);
204 realname = prev_name;
207 if (link_stat(prev_name, &prev_st, 0) < 0) {
208 rsyserr(FERROR, errno, "stat %s failed",
209 full_fname(prev_name));
213 if (statret < 0 && basis_dir[0] != NULL) {
214 /* If we match an alt-dest item, we don't output this as a change. */
215 char cmpbuf[MAXPATHLEN];
219 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
220 if (link_stat(cmpbuf, &alt_st, 0) < 0)
223 if (prev_st.st_dev != alt_st.st_dev
224 || prev_st.st_ino != alt_st.st_ino)
228 if (verbose < 2 || !stdout_format_has_i) {
231 if (verbose > 1 && maybe_ATTRS_REPORT)
232 rprintf(FCLIENT, "%s is uptodate\n", fname);
236 if (!unchanged_file(cmpbuf, file, &alt_st))
240 if (unchanged_attrs(file, &alt_st))
242 } while (basis_dir[++j] != NULL);
245 if (maybe_hard_link(file, ndx, fname, statret, stp, prev_name, &prev_st,
246 realname, itemizing, code) < 0)
249 if (remove_source_files == 1 && do_xfers)
250 send_msg_int(MSG_SUCCESS, ndx);
255 int hard_link_one(struct file_struct *file, const char *fname,
256 const char *oldname, int terse)
258 if (do_link(oldname, fname) < 0) {
266 rsyserr(code, errno, "link %s => %s failed",
267 full_fname(fname), oldname);
271 file->flags |= FLAG_HLINK_DONE;
276 void finish_hard_link(struct file_struct *file, const char *fname,
277 STRUCT_STAT *stp, int itemizing, enum logcode code,
280 STRUCT_STAT st, prev_st;
281 char alt_name[MAXPATHLEN], *prev_name;
282 const char *our_name;
283 int prev_statret, ndx, prev_ndx = F_HL_PREV(file);
285 if (stp == NULL && prev_ndx >= 0) {
286 if (link_stat(fname, &st, 0) < 0) {
287 rsyserr(FERROR, errno, "stat %s failed",
294 /* FIRST combined with DONE means we were the first to get done. */
295 file->flags |= FLAG_HLINK_FIRST | FLAG_HLINK_DONE;
296 F_HL_PREV(file) = alt_dest;
297 if (alt_dest >= 0 && dry_run) {
298 pathjoin(alt_name, MAXPATHLEN, basis_dir[alt_dest],
304 while ((ndx = prev_ndx) >= 0) {
306 file->flags = (file->flags & ~FLAG_HLINK_FIRST) | FLAG_HLINK_DONE;
307 prev_ndx = F_HL_PREV(file);
308 prev_name = f_name(file, NULL);
309 prev_statret = link_stat(prev_name, &prev_st, 0);
310 if (maybe_hard_link(file, ndx, prev_name, prev_statret, &prev_st,
311 our_name, stp, fname, itemizing, code) < 0)
313 if (remove_source_files == 1 && do_xfers)
314 send_msg_int(MSG_SUCCESS, ndx);