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 flist_extra_ndx;
32 extern int remove_source_files;
33 extern int stdout_format_has_i;
34 extern char *basis_dir[];
35 extern struct file_list *the_file_list;
37 #ifdef SUPPORT_HARD_LINKS
39 #define SKIPPED_LINK (-1)
40 #define FINISHED_LINK (-2)
42 #define FPTR(i) (the_file_list->files[i])
43 #define LINKED(i1,i2) ((i1)->dev == (i2)->dev && (i1)->ino == (i2)->ino)
45 static int hlink_compare(int *int1, int *int2)
47 struct file_struct *f1 = FPTR(*int1);
48 struct file_struct *f2 = FPTR(*int2);
49 struct idev *i1 = F_IDEV(f1);
50 struct idev *i2 = F_IDEV(f2);
52 if (i1->dev != i2->dev)
53 return i1->dev > i2->dev ? 1 : -1;
55 if (i1->ino != i2->ino)
56 return i1->ino > i2->ino ? 1 : -1;
58 return f_name_cmp(f1, f2);
61 static int32 *hlink_list;
62 static int32 hlink_count;
64 /* Analyze the data in the hlink_list[], remove items that aren't multiply
65 * linked, and replace the dev+inode data with the hlindex+next linked list. */
66 static void link_idev_data(void)
68 int32 from, to, start;
69 struct file_struct *file, *file_next;
70 struct idev *idev, *idev_next;
73 alloc_pool_t hlink_pool;
74 alloc_pool_t idev_pool = the_file_list->hlink_pool;
76 hlink_pool = pool_create(128 * 1024, sizeof (struct hlist), out_of_memory, POOL_INTERN);
78 for (from = to = 0; from < hlink_count; from++) {
80 for (file = FPTR(hlink_list[from]), idev = F_IDEV(file);
82 file = file_next, idev = idev_next)
84 file_next = FPTR(hlink_list[from+1]);
85 idev_next = F_IDEV(file_next);
86 if (!LINKED(idev, idev_next))
88 pool_free(idev_pool, 0, idev);
89 hl = pool_talloc(hlink_pool, struct hlist, 1,
92 hl->next = hlink_list[++from];
96 pool_free(idev_pool, 0, idev);
98 int head = hlink_list[start];
99 hl = pool_talloc(hlink_pool, struct hlist, 1,
101 FPTR(head)->flags |= FLAG_HLINK_FIRST;
105 hlink_list[to++] = head;
106 file->flags |= FLAG_HLINK_LAST;
109 file->flags &= ~FLAG_HLINK_INFO;
115 pool_destroy(hlink_pool);
119 hlink_list = realloc_array(hlink_list, int32, hlink_count);
121 out_of_memory("init_hard_links");
123 the_file_list->hlink_pool = hlink_pool;
124 pool_destroy(idev_pool);
128 void init_hard_links(void)
130 #ifdef SUPPORT_HARD_LINKS
136 if (!(hlink_list = new_array(int32, the_file_list->count)))
137 out_of_memory("init_hard_links");
140 for (i = 0; i < the_file_list->count; i++) {
141 if (IS_HLINKED(FPTR(i)))
142 hlink_list[hlink_count++] = i;
145 qsort(hlink_list, hlink_count,
146 sizeof hlink_list[0], (int (*)()) hlink_compare);
156 #ifdef SUPPORT_HARD_LINKS
157 static int maybe_hard_link(struct file_struct *file, int ndx,
158 char *fname, int statret, STRUCT_STAT *st,
159 char *toname, STRUCT_STAT *to_st,
160 int itemizing, enum logcode code)
163 if (st->st_dev == to_st->st_dev
164 && st->st_ino == to_st->st_ino) {
166 itemize(file, ndx, statret, st,
167 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
173 if (!make_backup(fname))
175 } else if (robust_unlink(fname)) {
176 rsyserr(FERROR, errno, "unlink %s failed",
181 return hard_link_one(file, ndx, fname, statret, st, toname,
186 int hard_link_check(struct file_struct *file, int ndx, char *fname,
187 int statret, STRUCT_STAT *st, int itemizing,
188 enum logcode code, int skip)
190 #ifdef SUPPORT_HARD_LINKS
192 struct hlist *hl = F_HLIST(file);
194 if (skip && !(file->flags & FLAG_HLINK_LAST))
195 head = hlink_list[hl->hlindex] = hl->next;
197 head = hlink_list[hl->hlindex];
199 struct file_struct *head_file = FPTR(head);
200 struct hlist *hf_hl = F_HLIST(head_file);
201 if (!stdout_format_has_i && verbose > 1) {
202 rprintf(FINFO, "\"%s\" is a hard link\n",
205 if (hf_hl->hlindex == FINISHED_LINK) {
206 STRUCT_STAT st2, st3;
207 char toname[MAXPATHLEN];
208 int ldu = hf_hl->dest_used;
210 pathjoin(toname, MAXPATHLEN, basis_dir[ldu-1],
211 f_name(head_file, NULL));
213 f_name(head_file, toname);
214 if (link_stat(toname, &st2, 0) < 0) {
215 rsyserr(FERROR, errno, "stat %s failed",
219 if (statret < 0 && basis_dir[0] != NULL) {
220 char cmpbuf[MAXPATHLEN];
223 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
224 if (link_stat(cmpbuf, &st3, 0) < 0)
227 if (st2.st_dev != st3.st_dev
228 || st2.st_ino != st3.st_ino)
232 if (verbose < 2 || !stdout_format_has_i) {
238 if (!unchanged_file(cmpbuf, file, &st3))
242 if (unchanged_attrs(file, &st3))
244 } while (basis_dir[++j] != NULL);
246 maybe_hard_link(file, ndx, fname, statret, st,
247 toname, &st2, itemizing, code);
248 if (remove_source_files == 1 && do_xfers)
249 send_msg_int(MSG_SUCCESS, ndx);
250 hl->hlindex = FINISHED_LINK;
252 hl->hlindex = SKIPPED_LINK;
259 #ifdef SUPPORT_HARD_LINKS
260 int hard_link_one(struct file_struct *file, int ndx, char *fname,
261 int statret, STRUCT_STAT *st, char *toname, int terse,
262 int itemizing, enum logcode code)
264 if (do_link(toname, fname)) {
271 rsyserr(code, errno, "link %s => %s failed",
272 full_fname(fname), toname);
277 itemize(file, ndx, statret, st,
278 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0,
279 terse ? "" : toname);
281 if (code != FNONE && verbose && !terse)
282 rprintf(code, "%s => %s\n", fname, toname);
288 void hard_link_cluster(struct file_struct *file, int master, int itemizing,
289 enum logcode code, int dest_used)
291 #ifdef SUPPORT_HARD_LINKS
292 char hlink1[MAXPATHLEN];
294 STRUCT_STAT st1, st2;
295 int statret, ndx = master;
296 struct hlist *hl = F_HLIST(file);
298 hl->hlindex = FINISHED_LINK;
300 hl->dest_used = dest_used + 1;
301 if (link_stat(f_name(file, hlink1), &st1, 0) < 0)
303 if (!(file->flags & FLAG_HLINK_FIRST)) {
304 while (!(file->flags & FLAG_HLINK_LAST)) {
314 if (hl->hlindex != SKIPPED_LINK)
316 hlink2 = f_name(file, NULL);
317 statret = link_stat(hlink2, &st2, 0);
318 maybe_hard_link(file, ndx, hlink2, statret, &st2,
319 hlink1, &st1, itemizing, code);
320 if (remove_source_files == 1 && do_xfers)
321 send_msg_int(MSG_SUCCESS, ndx);
322 hl->hlindex = FINISHED_LINK;
323 } while (!(file->flags & FLAG_HLINK_LAST));