2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
4 Copyright (C) 2002 by Martin Pool <mbp@samba.org>
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.
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.
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.
25 extern int make_backups;
26 extern struct file_list *the_file_list;
28 #ifdef SUPPORT_HARD_LINKS
30 #define SKIPPED_LINK (-1)
31 #define FINISHED_LINK (-2)
33 #define FPTR(i) (the_file_list->files[i])
34 #define LINKED(p1,p2) (FPTR(p1)->F_DEV == FPTR(p2)->F_DEV \
35 && FPTR(p1)->F_INODE == FPTR(p2)->F_INODE)
37 static int hlink_compare(int *int1, int *int2)
39 struct file_struct *f1 = FPTR(*int1);
40 struct file_struct *f2 = FPTR(*int2);
42 if (f1->F_DEV != f2->F_DEV)
43 return (int) (f1->F_DEV > f2->F_DEV ? 1 : -1);
45 if (f1->F_INODE != f2->F_INODE)
46 return (int) (f1->F_INODE > f2->F_INODE ? 1 : -1);
48 return f_name_cmp(f1, f2);
51 static int *hlink_list;
52 static int hlink_count;
54 /* Analyze the data in the hlink_list[], remove items that aren't multiply
55 * linked, and replace the dev+inode data with the hlindex+next linked list. */
56 static void link_idev_data(void)
58 int cur, from, to, start;
60 alloc_pool_t hlink_pool;
61 alloc_pool_t idev_pool = the_file_list->hlink_pool;
63 hlink_pool = pool_create(128 * 1024, sizeof (struct hlink),
64 out_of_memory, POOL_INTERN);
66 for (from = to = 0; from < hlink_count; from++) {
69 cur = hlink_list[from];
70 if (from == hlink_count-1
71 || !LINKED(cur, hlink_list[from+1]))
73 pool_free(idev_pool, 0, FPTR(cur)->link_u.idev);
74 FPTR(cur)->link_u.links = pool_talloc(hlink_pool,
75 struct hlink, 1, "hlink_list");
77 FPTR(cur)->F_HLINDEX = to;
78 FPTR(cur)->F_NEXT = hlink_list[++from];
80 pool_free(idev_pool, 0, FPTR(cur)->link_u.idev);
82 int head = hlink_list[start];
83 FPTR(cur)->link_u.links = pool_talloc(hlink_pool,
84 struct hlink, 1, "hlink_list");
86 FPTR(head)->flags |= FLAG_HLINK_TOL;
87 FPTR(cur)->F_HLINDEX = to;
88 FPTR(cur)->F_NEXT = head;
89 FPTR(cur)->flags |= FLAG_HLINK_EOL;
90 hlink_list[to++] = head;
92 FPTR(cur)->link_u.links = NULL;
98 pool_destroy(hlink_pool);
102 hlink_list = realloc_array(hlink_list, int, hlink_count);
104 out_of_memory("init_hard_links");
106 the_file_list->hlink_pool = hlink_pool;
107 pool_destroy(idev_pool);
111 void init_hard_links(void)
113 #ifdef SUPPORT_HARD_LINKS
119 if (!(hlink_list = new_array(int, the_file_list->count)))
120 out_of_memory("init_hard_links");
123 for (i = 0; i < the_file_list->count; i++) {
124 if (FPTR(i)->link_u.idev)
125 hlink_list[hlink_count++] = i;
128 qsort(hlink_list, hlink_count,
129 sizeof hlink_list[0], (int (*)()) hlink_compare);
139 #ifdef SUPPORT_HARD_LINKS
140 static int maybe_hard_link(struct file_struct *file, int ndx,
141 char *fname, int statret, STRUCT_STAT *st,
142 char *toname, STRUCT_STAT *to_st,
143 int itemizing, enum logcode code)
146 if (st->st_dev == to_st->st_dev
147 && st->st_ino == to_st->st_ino) {
149 itemize(file, ndx, statret, st,
150 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
156 if (!make_backup(fname))
158 } else if (robust_unlink(fname)) {
159 rsyserr(FERROR, errno, "unlink %s failed",
164 return hard_link_one(file, ndx, fname, statret, st, toname,
169 int hard_link_check(struct file_struct *file, int ndx, char *fname,
170 int statret, STRUCT_STAT *st, int itemizing,
171 enum logcode code, int skip)
173 #ifdef SUPPORT_HARD_LINKS
175 if (!file->link_u.links)
177 if (skip && !(file->flags & FLAG_HLINK_EOL))
178 head = hlink_list[file->F_HLINDEX] = file->F_NEXT;
180 head = hlink_list[file->F_HLINDEX];
182 struct file_struct *head_file = FPTR(head);
184 rprintf(FINFO, "\"%s\" is a hard link\n",
185 safe_fname(f_name(file)));
187 if (head_file->F_HLINDEX == FINISHED_LINK) {
189 char *toname = f_name(head_file);
190 if (link_stat(toname, &st2, 0) < 0) {
191 rsyserr(FERROR, errno, "stat %s failed",
195 maybe_hard_link(file, ndx, fname, statret, st,
196 toname, &st2, itemizing, code);
197 file->F_HLINDEX = FINISHED_LINK;
199 file->F_HLINDEX = SKIPPED_LINK;
206 #ifdef SUPPORT_HARD_LINKS
207 int hard_link_one(struct file_struct *file, int ndx, char *fname,
208 int statret, STRUCT_STAT *st, char *toname, int terse,
209 int itemizing, enum logcode code)
211 if (do_link(toname, fname)) {
218 rsyserr(code, errno, "link %s => %s failed",
219 full_fname(fname), safe_fname(toname));
224 itemize(file, ndx, statret, st,
225 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0,
226 terse ? "" : toname);
228 if (code && verbose && !terse) {
229 rprintf(code, "%s => %s\n",
230 safe_fname(fname), safe_fname(toname));
237 void hard_link_cluster(struct file_struct *file, int master, int itemizing,
240 #ifdef SUPPORT_HARD_LINKS
241 char hlink1[MAXPATHLEN];
243 STRUCT_STAT st1, st2;
244 int statret, ndx = master;
246 file->F_HLINDEX = FINISHED_LINK;
247 if (link_stat(f_name_to(file, hlink1), &st1, 0) < 0)
249 if (!(file->flags & FLAG_HLINK_TOL)) {
250 while (!(file->flags & FLAG_HLINK_EOL)) {
258 if (file->F_HLINDEX != SKIPPED_LINK)
260 hlink2 = f_name(file);
261 statret = link_stat(hlink2, &st2, 0);
262 maybe_hard_link(file, ndx, hlink2, statret, &st2,
263 hlink1, &st1, itemizing, code);
264 file->F_HLINDEX = FINISHED_LINK;
265 } while (!(file->flags & FLAG_HLINK_EOL));