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 char *basis_dir[];
34 extern struct file_list *the_file_list;
36 #ifdef SUPPORT_HARD_LINKS
38 #define SKIPPED_LINK (-1)
39 #define FINISHED_LINK (-2)
41 #define FPTR(i) (the_file_list->files[i])
42 #define LINKED(i1,i2) ((i1)->dev == (i2)->dev && (i1)->ino == (i2)->ino)
44 static int hlink_compare(int *int1, int *int2)
46 struct file_struct *f1 = FPTR(*int1);
47 struct file_struct *f2 = FPTR(*int2);
48 struct idev *i1 = F_IDEV(f1);
49 struct idev *i2 = F_IDEV(f2);
51 if (i1->dev != i2->dev)
52 return i1->dev > i2->dev ? 1 : -1;
54 if (i1->ino != i2->ino)
55 return i1->ino > i2->ino ? 1 : -1;
57 return f_name_cmp(f1, f2);
60 static int32 *hlink_list;
61 static int32 hlink_count;
63 /* Analyze the data in the hlink_list[], remove items that aren't multiply
64 * linked, and replace the dev+inode data with the hlindex+next linked list. */
65 static void link_idev_data(void)
67 int32 from, to, start;
68 struct file_struct *file, *file_next;
69 struct idev *idev, *idev_next;
72 alloc_pool_t hlink_pool;
73 alloc_pool_t idev_pool = the_file_list->hlink_pool;
75 hlink_pool = pool_create(128 * 1024, sizeof (struct hlist), out_of_memory, POOL_INTERN);
77 for (from = to = 0; from < hlink_count; from++) {
79 for (file = FPTR(hlink_list[from]), idev = F_IDEV(file);
81 file = file_next, idev = idev_next)
83 file_next = FPTR(hlink_list[from+1]);
84 idev_next = F_IDEV(file_next);
85 if (!LINKED(idev, idev_next))
87 pool_free(idev_pool, 0, idev);
88 hl = pool_talloc(hlink_pool, struct hlist, 1,
91 hl->next = hlink_list[++from];
95 pool_free(idev_pool, 0, idev);
97 int head = hlink_list[start];
98 hl = pool_talloc(hlink_pool, struct hlist, 1,
100 FPTR(head)->flags |= FLAG_HLINK_FIRST;
104 hlink_list[to++] = head;
105 file->flags |= FLAG_HLINK_LAST;
108 file->flags &= ~FLAG_HLINKED;
114 pool_destroy(hlink_pool);
118 hlink_list = realloc_array(hlink_list, int32, hlink_count);
120 out_of_memory("init_hard_links");
122 the_file_list->hlink_pool = hlink_pool;
123 pool_destroy(idev_pool);
127 void init_hard_links(void)
129 #ifdef SUPPORT_HARD_LINKS
135 if (!(hlink_list = new_array(int32, the_file_list->count)))
136 out_of_memory("init_hard_links");
139 for (i = 0; i < the_file_list->count; i++) {
140 if (F_IS_HLINKED(FPTR(i)))
141 hlink_list[hlink_count++] = i;
144 qsort(hlink_list, hlink_count,
145 sizeof hlink_list[0], (int (*)()) hlink_compare);
155 #ifdef SUPPORT_HARD_LINKS
156 static int maybe_hard_link(struct file_struct *file, int ndx,
157 char *fname, int statret, STRUCT_STAT *st,
158 char *toname, STRUCT_STAT *to_st,
159 int itemizing, enum logcode code)
162 if (st->st_dev == to_st->st_dev
163 && st->st_ino == to_st->st_ino) {
165 itemize(file, ndx, statret, st,
166 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
172 if (!make_backup(fname))
174 } else if (robust_unlink(fname)) {
175 rsyserr(FERROR, errno, "unlink %s failed",
180 return hard_link_one(file, ndx, fname, statret, st, toname,
185 int hard_link_check(struct file_struct *file, int ndx, char *fname,
186 int statret, STRUCT_STAT *st, int itemizing,
187 enum logcode code, int skip)
189 #ifdef SUPPORT_HARD_LINKS
191 struct hlist *hl = F_HLIST(file);
193 if (skip && !(file->flags & FLAG_HLINK_LAST))
194 head = hlink_list[hl->hlindex] = hl->next;
196 head = hlink_list[hl->hlindex];
198 struct file_struct *head_file = FPTR(head);
199 struct hlist *hf_hl = F_HLIST(head_file);
200 if (!stdout_format_has_i && verbose > 1) {
201 rprintf(FINFO, "\"%s\" is a hard link\n",
204 if (hf_hl->hlindex == FINISHED_LINK) {
205 STRUCT_STAT st2, st3;
206 char toname[MAXPATHLEN];
207 int ldu = hf_hl->dest_used;
209 pathjoin(toname, MAXPATHLEN, basis_dir[ldu-1],
210 f_name(head_file, NULL));
212 f_name(head_file, toname);
213 if (link_stat(toname, &st2, 0) < 0) {
214 rsyserr(FERROR, errno, "stat %s failed",
218 if (statret < 0 && basis_dir[0] != NULL) {
219 char cmpbuf[MAXPATHLEN];
222 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
223 if (link_stat(cmpbuf, &st3, 0) < 0)
226 if (st2.st_dev != st3.st_dev
227 || st2.st_ino != st3.st_ino)
231 if (verbose < 2 || !stdout_format_has_i) {
237 if (!unchanged_file(cmpbuf, file, &st3))
241 if (unchanged_attrs(file, &st3))
243 } while (basis_dir[++j] != NULL);
245 maybe_hard_link(file, ndx, fname, statret, st,
246 toname, &st2, itemizing, code);
247 if (remove_source_files == 1 && do_xfers)
248 send_msg_int(MSG_SUCCESS, ndx);
249 hl->hlindex = FINISHED_LINK;
251 hl->hlindex = SKIPPED_LINK;
258 #ifdef SUPPORT_HARD_LINKS
259 int hard_link_one(struct file_struct *file, int ndx, char *fname,
260 int statret, STRUCT_STAT *st, char *toname, int terse,
261 int itemizing, enum logcode code)
263 if (do_link(toname, fname)) {
270 rsyserr(code, errno, "link %s => %s failed",
271 full_fname(fname), toname);
276 itemize(file, ndx, statret, st,
277 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0,
278 terse ? "" : toname);
280 if (code != FNONE && verbose && !terse)
281 rprintf(code, "%s => %s\n", fname, toname);
287 void hard_link_cluster(struct file_struct *file, int master, int itemizing,
288 enum logcode code, int dest_used)
290 #ifdef SUPPORT_HARD_LINKS
291 char hlink1[MAXPATHLEN];
293 STRUCT_STAT st1, st2;
294 int statret, ndx = master;
295 struct hlist *hl = F_HLIST(file);
297 hl->hlindex = FINISHED_LINK;
299 hl->dest_used = dest_used + 1;
300 if (link_stat(f_name(file, hlink1), &st1, 0) < 0)
302 if (!(file->flags & FLAG_HLINK_FIRST)) {
303 while (!(file->flags & FLAG_HLINK_LAST)) {
313 if (hl->hlindex != SKIPPED_LINK)
315 hlink2 = f_name(file, NULL);
316 statret = link_stat(hlink2, &st2, 0);
317 maybe_hard_link(file, ndx, hlink2, statret, &st2,
318 hlink1, &st1, itemizing, code);
319 if (remove_source_files == 1 && do_xfers)
320 send_msg_int(MSG_SUCCESS, ndx);
321 hl->hlindex = FINISHED_LINK;
322 } while (!(file->flags & FLAG_HLINK_LAST));