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 struct file_list *the_file_list;
29 #ifdef SUPPORT_HARD_LINKS
31 #define SKIPPED_LINK (-1)
32 #define FINISHED_LINK (-2)
34 #define FPTR(i) (the_file_list->files[i])
35 #define LINKED(p1,p2) (FPTR(p1)->F_DEV == FPTR(p2)->F_DEV \
36 && FPTR(p1)->F_INODE == FPTR(p2)->F_INODE)
38 static int hlink_compare(int *int1, int *int2)
40 struct file_struct *f1 = FPTR(*int1);
41 struct file_struct *f2 = FPTR(*int2);
43 if (f1->F_DEV != f2->F_DEV)
44 return (int) (f1->F_DEV > f2->F_DEV ? 1 : -1);
46 if (f1->F_INODE != f2->F_INODE)
47 return (int) (f1->F_INODE > f2->F_INODE ? 1 : -1);
49 return f_name_cmp(f1, f2);
52 static int *hlink_list;
53 static int hlink_count;
55 /* Analyze the data in the hlink_list[], remove items that aren't multiply
56 * linked, and replace the dev+inode data with the hlindex+next linked list. */
57 static void link_idev_data(void)
59 int cur, from, to, start;
61 alloc_pool_t hlink_pool;
62 alloc_pool_t idev_pool = the_file_list->hlink_pool;
64 hlink_pool = pool_create(128 * 1024, sizeof (struct hlink),
65 out_of_memory, POOL_INTERN);
67 for (from = to = 0; from < hlink_count; from++) {
70 cur = hlink_list[from];
71 if (from == hlink_count-1
72 || !LINKED(cur, hlink_list[from+1]))
74 pool_free(idev_pool, 0, FPTR(cur)->link_u.idev);
75 FPTR(cur)->link_u.links = pool_talloc(hlink_pool,
76 struct hlink, 1, "hlink_list");
78 FPTR(cur)->F_HLINDEX = to;
79 FPTR(cur)->F_NEXT = hlink_list[++from];
81 pool_free(idev_pool, 0, FPTR(cur)->link_u.idev);
83 int head = hlink_list[start];
84 FPTR(cur)->link_u.links = pool_talloc(hlink_pool,
85 struct hlink, 1, "hlink_list");
87 FPTR(head)->flags |= FLAG_HLINK_TOL;
88 FPTR(cur)->F_HLINDEX = to;
89 FPTR(cur)->F_NEXT = head;
90 FPTR(cur)->flags |= FLAG_HLINK_EOL;
91 hlink_list[to++] = head;
93 FPTR(cur)->link_u.links = NULL;
99 pool_destroy(hlink_pool);
103 hlink_list = realloc_array(hlink_list, int, hlink_count);
105 out_of_memory("init_hard_links");
107 the_file_list->hlink_pool = hlink_pool;
108 pool_destroy(idev_pool);
112 void init_hard_links(void)
114 #ifdef SUPPORT_HARD_LINKS
120 if (!(hlink_list = new_array(int, the_file_list->count)))
121 out_of_memory("init_hard_links");
124 for (i = 0; i < the_file_list->count; i++) {
125 if (FPTR(i)->link_u.idev)
126 hlink_list[hlink_count++] = i;
129 qsort(hlink_list, hlink_count,
130 sizeof hlink_list[0], (int (*)()) hlink_compare);
140 #ifdef SUPPORT_HARD_LINKS
141 static int maybe_hard_link(struct file_struct *file, int ndx,
142 char *fname, int statret, STRUCT_STAT *st,
143 char *toname, STRUCT_STAT *to_st,
144 int itemizing, enum logcode code)
147 if (st->st_dev == to_st->st_dev
148 && st->st_ino == to_st->st_ino) {
150 itemize(file, ndx, statret, st,
151 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
157 if (!make_backup(fname))
159 } else if (robust_unlink(fname)) {
160 rsyserr(FERROR, errno, "unlink %s failed",
165 return hard_link_one(file, ndx, fname, statret, st, toname,
170 int hard_link_check(struct file_struct *file, int ndx, char *fname,
171 int statret, STRUCT_STAT *st, int itemizing,
172 enum logcode code, int skip)
174 #ifdef SUPPORT_HARD_LINKS
176 if (skip && !(file->flags & FLAG_HLINK_EOL))
177 head = hlink_list[file->F_HLINDEX] = file->F_NEXT;
179 head = hlink_list[file->F_HLINDEX];
181 struct file_struct *head_file = FPTR(head);
182 if (!log_format_has_i && verbose > 1) {
183 rprintf(FINFO, "\"%s\" is a hard link\n",
184 safe_fname(f_name(file)));
186 if (head_file->F_HLINDEX == FINISHED_LINK) {
188 char *toname = f_name(head_file);
189 if (link_stat(toname, &st2, 0) < 0) {
190 rsyserr(FERROR, errno, "stat %s failed",
194 maybe_hard_link(file, ndx, fname, statret, st,
195 toname, &st2, itemizing, code);
196 file->F_HLINDEX = FINISHED_LINK;
198 file->F_HLINDEX = SKIPPED_LINK;
205 #ifdef SUPPORT_HARD_LINKS
206 int hard_link_one(struct file_struct *file, int ndx, char *fname,
207 int statret, STRUCT_STAT *st, char *toname, int terse,
208 int itemizing, enum logcode code)
210 if (do_link(toname, fname)) {
217 rsyserr(code, errno, "link %s => %s failed",
218 full_fname(fname), safe_fname(toname));
223 itemize(file, ndx, statret, st,
224 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0,
225 terse ? "" : toname);
227 if (code && verbose && !terse) {
228 rprintf(code, "%s => %s\n",
229 safe_fname(fname), safe_fname(toname));
236 void hard_link_cluster(struct file_struct *file, int master, int itemizing,
239 #ifdef SUPPORT_HARD_LINKS
240 char hlink1[MAXPATHLEN];
242 STRUCT_STAT st1, st2;
243 int statret, ndx = master;
245 file->F_HLINDEX = FINISHED_LINK;
246 if (link_stat(f_name_to(file, hlink1), &st1, 0) < 0)
248 if (!(file->flags & FLAG_HLINK_TOL)) {
249 while (!(file->flags & FLAG_HLINK_EOL)) {
257 if (file->F_HLINDEX != SKIPPED_LINK)
259 hlink2 = f_name(file);
260 statret = link_stat(hlink2, &st2, 0);
261 maybe_hard_link(file, ndx, hlink2, statret, &st2,
262 hlink1, &st1, itemizing, code);
263 file->F_HLINDEX = FINISHED_LINK;
264 } while (!(file->flags & FLAG_HLINK_EOL));