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.
26 extern int make_backups;
27 extern int log_format_has_i;
28 extern char *basis_dir[];
29 extern struct file_list *the_file_list;
31 #ifdef SUPPORT_HARD_LINKS
33 #define SKIPPED_LINK (-1)
34 #define FINISHED_LINK (-2)
36 #define FPTR(i) (the_file_list->files[i])
37 #define LINKED(p1,p2) (FPTR(p1)->F_DEV == FPTR(p2)->F_DEV \
38 && FPTR(p1)->F_INODE == FPTR(p2)->F_INODE)
40 static int hlink_compare(int *int1, int *int2)
42 struct file_struct *f1 = FPTR(*int1);
43 struct file_struct *f2 = FPTR(*int2);
45 if (f1->F_DEV != f2->F_DEV)
46 return (int) (f1->F_DEV > f2->F_DEV ? 1 : -1);
48 if (f1->F_INODE != f2->F_INODE)
49 return (int) (f1->F_INODE > f2->F_INODE ? 1 : -1);
51 return f_name_cmp(f1, f2);
54 static int *hlink_list;
55 static int hlink_count;
57 /* Analyze the data in the hlink_list[], remove items that aren't multiply
58 * linked, and replace the dev+inode data with the hlindex+next linked list. */
59 static void link_idev_data(void)
61 int cur, from, to, start;
63 alloc_pool_t hlink_pool;
64 alloc_pool_t idev_pool = the_file_list->hlink_pool;
66 hlink_pool = pool_create(128 * 1024, sizeof (struct hlink),
67 out_of_memory, POOL_INTERN);
69 for (from = to = 0; from < hlink_count; from++) {
72 cur = hlink_list[from];
73 if (from == hlink_count-1
74 || !LINKED(cur, hlink_list[from+1]))
76 pool_free(idev_pool, 0, FPTR(cur)->link_u.idev);
77 FPTR(cur)->link_u.links = pool_talloc(hlink_pool,
78 struct hlink, 1, "hlink_list");
80 FPTR(cur)->F_HLINDEX = to;
81 FPTR(cur)->F_NEXT = hlink_list[++from];
83 pool_free(idev_pool, 0, FPTR(cur)->link_u.idev);
85 int head = hlink_list[start];
86 FPTR(cur)->link_u.links = pool_talloc(hlink_pool,
87 struct hlink, 1, "hlink_list");
89 FPTR(head)->flags |= FLAG_HLINK_TOL;
90 FPTR(cur)->F_HLINDEX = to;
91 FPTR(cur)->F_NEXT = head;
92 FPTR(cur)->flags |= FLAG_HLINK_EOL;
93 hlink_list[to++] = head;
95 FPTR(cur)->link_u.links = NULL;
101 pool_destroy(hlink_pool);
105 hlink_list = realloc_array(hlink_list, int, hlink_count);
107 out_of_memory("init_hard_links");
109 the_file_list->hlink_pool = hlink_pool;
110 pool_destroy(idev_pool);
114 void init_hard_links(void)
116 #ifdef SUPPORT_HARD_LINKS
122 if (!(hlink_list = new_array(int, the_file_list->count)))
123 out_of_memory("init_hard_links");
126 for (i = 0; i < the_file_list->count; i++) {
127 if (FPTR(i)->link_u.idev)
128 hlink_list[hlink_count++] = i;
131 qsort(hlink_list, hlink_count,
132 sizeof hlink_list[0], (int (*)()) hlink_compare);
142 #ifdef SUPPORT_HARD_LINKS
143 static int maybe_hard_link(struct file_struct *file, int ndx,
144 char *fname, int statret, STRUCT_STAT *st,
145 char *toname, STRUCT_STAT *to_st,
146 int itemizing, enum logcode code)
149 if (st->st_dev == to_st->st_dev
150 && st->st_ino == to_st->st_ino) {
152 itemize(file, ndx, statret, st,
153 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
159 if (!make_backup(fname))
161 } else if (robust_unlink(fname)) {
162 rsyserr(FERROR, errno, "unlink %s failed",
167 return hard_link_one(file, ndx, fname, statret, st, toname,
172 int hard_link_check(struct file_struct *file, int ndx, char *fname,
173 int statret, STRUCT_STAT *st, int itemizing,
174 enum logcode code, int skip)
176 #ifdef SUPPORT_HARD_LINKS
178 if (skip && !(file->flags & FLAG_HLINK_EOL))
179 head = hlink_list[file->F_HLINDEX] = file->F_NEXT;
181 head = hlink_list[file->F_HLINDEX];
183 struct file_struct *head_file = FPTR(head);
184 if (!log_format_has_i && verbose > 1) {
185 rprintf(FINFO, "\"%s\" is a hard link\n",
188 if (head_file->F_HLINDEX == FINISHED_LINK) {
189 STRUCT_STAT st2, st3;
190 char *toname = f_name(head_file, NULL);
191 if (link_stat(toname, &st2, 0) < 0) {
192 rsyserr(FERROR, errno, "stat %s failed",
196 if (statret < 0 && basis_dir[0] != NULL) {
197 char cmpbuf[MAXPATHLEN];
200 pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
201 if (link_stat(cmpbuf, &st3, 0) < 0)
204 if (st2.st_dev != st3.st_dev
205 || st2.st_ino != st3.st_ino)
209 if (verbose < 2 || !log_format_has_i)
210 itemizing = code = 0;
213 if (!unchanged_file(cmpbuf, file, &st3))
217 if (unchanged_attrs(file, &st3))
219 } while (basis_dir[++j] != NULL);
221 maybe_hard_link(file, ndx, fname, statret, st,
222 toname, &st2, itemizing, code);
223 file->F_HLINDEX = FINISHED_LINK;
225 file->F_HLINDEX = SKIPPED_LINK;
232 #ifdef SUPPORT_HARD_LINKS
233 int hard_link_one(struct file_struct *file, int ndx, char *fname,
234 int statret, STRUCT_STAT *st, char *toname, int terse,
235 int itemizing, enum logcode code)
237 if (do_link(toname, fname)) {
244 rsyserr(code, errno, "link %s => %s failed",
245 full_fname(fname), toname);
250 itemize(file, ndx, statret, st,
251 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0,
252 terse ? "" : toname);
254 if (code && verbose && !terse)
255 rprintf(code, "%s => %s\n", fname, toname);
261 void hard_link_cluster(struct file_struct *file, int master, int itemizing,
264 #ifdef SUPPORT_HARD_LINKS
265 char hlink1[MAXPATHLEN];
267 STRUCT_STAT st1, st2;
268 int statret, ndx = master;
270 file->F_HLINDEX = FINISHED_LINK;
271 if (link_stat(f_name(file, hlink1), &st1, 0) < 0)
273 if (!(file->flags & FLAG_HLINK_TOL)) {
274 while (!(file->flags & FLAG_HLINK_EOL)) {
282 if (file->F_HLINDEX != SKIPPED_LINK)
284 hlink2 = f_name(file, NULL);
285 statret = link_stat(hlink2, &st2, 0);
286 maybe_hard_link(file, ndx, hlink2, statret, &st2,
287 hlink1, &st1, itemizing, code);
288 file->F_HLINDEX = FINISHED_LINK;
289 } while (!(file->flags & FLAG_HLINK_EOL));