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 int log_format_has_i;
27 extern char *basis_dir[];
28 extern struct file_list *the_file_list;
30 #ifdef SUPPORT_HARD_LINKS
32 #define SKIPPED_LINK (-1)
33 #define FINISHED_LINK (-2)
35 #define FPTR(i) (the_file_list->files[i])
36 #define LINKED(p1,p2) (FPTR(p1)->F_DEV == FPTR(p2)->F_DEV \
37 && FPTR(p1)->F_INODE == FPTR(p2)->F_INODE)
39 static int hlink_compare(int *int1, int *int2)
41 struct file_struct *f1 = FPTR(*int1);
42 struct file_struct *f2 = FPTR(*int2);
44 if (f1->F_DEV != f2->F_DEV)
45 return (int) (f1->F_DEV > f2->F_DEV ? 1 : -1);
47 if (f1->F_INODE != f2->F_INODE)
48 return (int) (f1->F_INODE > f2->F_INODE ? 1 : -1);
50 return f_name_cmp(f1, f2);
53 static int *hlink_list;
54 static int hlink_count;
56 /* Analyze the data in the hlink_list[], remove items that aren't multiply
57 * linked, and replace the dev+inode data with the hlindex+next linked list. */
58 static void link_idev_data(void)
60 int cur, from, to, start;
62 alloc_pool_t hlink_pool;
63 alloc_pool_t idev_pool = the_file_list->hlink_pool;
65 hlink_pool = pool_create(128 * 1024, sizeof (struct hlink),
66 out_of_memory, POOL_INTERN);
68 for (from = to = 0; from < hlink_count; from++) {
71 cur = hlink_list[from];
72 if (from == hlink_count-1
73 || !LINKED(cur, hlink_list[from+1]))
75 pool_free(idev_pool, 0, FPTR(cur)->link_u.idev);
76 FPTR(cur)->link_u.links = pool_talloc(hlink_pool,
77 struct hlink, 1, "hlink_list");
79 FPTR(cur)->F_HLINDEX = to;
80 FPTR(cur)->F_NEXT = hlink_list[++from];
82 pool_free(idev_pool, 0, FPTR(cur)->link_u.idev);
84 int head = hlink_list[start];
85 FPTR(cur)->link_u.links = pool_talloc(hlink_pool,
86 struct hlink, 1, "hlink_list");
88 FPTR(head)->flags |= FLAG_HLINK_TOL;
89 FPTR(cur)->F_HLINDEX = to;
90 FPTR(cur)->F_NEXT = head;
91 FPTR(cur)->flags |= FLAG_HLINK_EOL;
92 hlink_list[to++] = head;
94 FPTR(cur)->link_u.links = NULL;
100 pool_destroy(hlink_pool);
104 hlink_list = realloc_array(hlink_list, int, hlink_count);
106 out_of_memory("init_hard_links");
108 the_file_list->hlink_pool = hlink_pool;
109 pool_destroy(idev_pool);
113 void init_hard_links(void)
115 #ifdef SUPPORT_HARD_LINKS
121 if (!(hlink_list = new_array(int, the_file_list->count)))
122 out_of_memory("init_hard_links");
125 for (i = 0; i < the_file_list->count; i++) {
126 if (FPTR(i)->link_u.idev)
127 hlink_list[hlink_count++] = i;
130 qsort(hlink_list, hlink_count,
131 sizeof hlink_list[0], (int (*)()) hlink_compare);
141 #ifdef SUPPORT_HARD_LINKS
142 static int maybe_hard_link(struct file_struct *file, int ndx,
143 char *fname, int statret, STRUCT_STAT *st,
144 char *toname, STRUCT_STAT *to_st,
145 int itemizing, enum logcode code)
148 if (st->st_dev == to_st->st_dev
149 && st->st_ino == to_st->st_ino) {
151 itemize(file, ndx, statret, st,
152 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
158 if (!make_backup(fname))
160 } else if (robust_unlink(fname)) {
161 rsyserr(FERROR, errno, "unlink %s failed",
166 return hard_link_one(file, ndx, fname, statret, st, toname,
171 int hard_link_check(struct file_struct *file, int ndx, char *fname,
172 int statret, STRUCT_STAT *st, int itemizing,
173 enum logcode code, int skip)
175 #ifdef SUPPORT_HARD_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);
183 if (!log_format_has_i && verbose > 1) {
184 rprintf(FINFO, "\"%s\" is a hard link\n",
187 if (head_file->F_HLINDEX == FINISHED_LINK) {
188 STRUCT_STAT st2, st3;
189 char *toname = f_name(head_file, NULL);
190 if (link_stat(toname, &st2, 0) < 0) {
191 rsyserr(FERROR, errno, "stat %s failed",
195 if (statret < 0 && basis_dir[0] != NULL) {
196 char cmpbuf[MAXPATHLEN];
199 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
200 if (link_stat(cmpbuf, &st3, 0) < 0)
203 if (st2.st_dev != st3.st_dev
204 || st2.st_ino != st3.st_ino)
208 if (verbose < 2 || !log_format_has_i)
209 itemizing = code = 0;
212 if (!unchanged_file(cmpbuf, file, &st3))
216 if (unchanged_attrs(file, &st3))
218 } while (basis_dir[++j] != NULL);
220 maybe_hard_link(file, ndx, fname, statret, st,
221 toname, &st2, itemizing, code);
222 file->F_HLINDEX = FINISHED_LINK;
224 file->F_HLINDEX = SKIPPED_LINK;
231 #ifdef SUPPORT_HARD_LINKS
232 int hard_link_one(struct file_struct *file, int ndx, char *fname,
233 int statret, STRUCT_STAT *st, char *toname, int terse,
234 int itemizing, enum logcode code)
236 if (do_link(toname, fname)) {
243 rsyserr(code, errno, "link %s => %s failed",
244 full_fname(fname), toname);
249 itemize(file, ndx, statret, st,
250 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0,
251 terse ? "" : toname);
253 if (code && verbose && !terse)
254 rprintf(code, "%s => %s\n", fname, toname);
260 void hard_link_cluster(struct file_struct *file, int master, int itemizing,
263 #ifdef SUPPORT_HARD_LINKS
264 char hlink1[MAXPATHLEN];
266 STRUCT_STAT st1, st2;
267 int statret, ndx = master;
269 file->F_HLINDEX = FINISHED_LINK;
270 if (link_stat(f_name(file, hlink1), &st1, 0) < 0)
272 if (!(file->flags & FLAG_HLINK_TOL)) {
273 while (!(file->flags & FLAG_HLINK_EOL)) {
281 if (file->F_HLINDEX != SKIPPED_LINK)
283 hlink2 = f_name(file, NULL);
284 statret = link_stat(hlink2, &st2, 0);
285 maybe_hard_link(file, ndx, hlink2, statret, &st2,
286 hlink1, &st1, itemizing, code);
287 file->F_HLINDEX = FINISHED_LINK;
288 } while (!(file->flags & FLAG_HLINK_EOL));