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;
27 #if SUPPORT_HARD_LINKS
28 static int hlink_compare(struct file_struct **file1, struct file_struct **file2)
30 struct file_struct *f1 = *file1;
31 struct file_struct *f2 = *file2;
33 if (f1->F_DEV != f2->F_DEV)
34 return (int) (f1->F_DEV > f2->F_DEV ? 1 : -1);
36 if (f1->F_INODE != f2->F_INODE)
37 return (int) (f1->F_INODE > f2->F_INODE ? 1 : -1);
39 return file_compare(file1, file2);
42 static struct file_struct **hlink_list;
43 static int hlink_count;
45 #define LINKED(p1,p2) ((p1)->F_DEV == (p2)->F_DEV \
46 && (p1)->F_INODE == (p2)->F_INODE)
48 /* Analyze the data in the hlink_list[], remove items that aren't multiply
49 * linked, and replace the dev+inode data with the hlindex+next linked list. */
50 static void link_idev_data(struct file_list *flist)
52 struct file_struct *head;
55 alloc_pool_t hlink_pool;
56 alloc_pool_t idev_pool = flist->hlink_pool;
58 hlink_pool = pool_create(128 * 1024, sizeof (struct hlink),
59 out_of_memory, POOL_INTERN);
61 for (from = to = 0; from < hlink_count; from++) {
63 head = hlink_list[start];
64 while (from < hlink_count-1
65 && LINKED(hlink_list[from], hlink_list[from+1])) {
66 pool_free(idev_pool, 0, hlink_list[from]->link_u.idev);
67 hlink_list[from]->link_u.links = pool_talloc(hlink_pool,
68 struct hlink, 1, "hlink_list");
70 hlink_list[from]->F_HLINDEX = to;
71 hlink_list[from]->F_NEXT = hlink_list[from+1];
75 pool_free(idev_pool, 0, hlink_list[from]->link_u.idev);
76 hlink_list[from]->link_u.links = pool_talloc(hlink_pool,
77 struct hlink, 1, "hlink_list");
79 hlink_list[from]->F_HLINDEX = to;
80 hlink_list[from]->F_NEXT = head;
81 hlink_list[from]->flags |= FLAG_HLINK_EOL;
82 hlink_list[to++] = head;
84 pool_free(idev_pool, 0, head->link_u.idev);
85 head->link_u.idev = NULL;
92 pool_destroy(hlink_pool);
96 if (!(hlink_list = realloc_array(hlink_list,
97 struct file_struct *, hlink_count)))
98 out_of_memory("init_hard_links");
100 flist->hlink_pool = hlink_pool;
101 pool_destroy(idev_pool);
105 void init_hard_links(struct file_list *flist)
107 #if SUPPORT_HARD_LINKS
110 if (flist->count < 2)
116 if (!(hlink_list = new_array(struct file_struct *, flist->count)))
117 out_of_memory("init_hard_links");
120 for (i = 0; i < flist->count; i++) {
121 if (flist->files[i]->link_u.idev)
122 hlink_list[hlink_count++] = flist->files[i];
125 qsort(hlink_list, hlink_count,
126 sizeof hlink_list[0], (int (*)()) hlink_compare);
132 link_idev_data(flist);
136 int hard_link_check(struct file_struct *file, int skip)
138 if (!hlink_list || !file->link_u.links)
140 if (skip && !(file->flags & FLAG_HLINK_EOL))
141 hlink_list[file->F_HLINDEX] = file->F_NEXT;
142 if (hlink_list[file->F_HLINDEX] != file) {
144 rprintf(FINFO, "\"%s\" is a hard link\n",
152 #if SUPPORT_HARD_LINKS
153 static void hard_link_one(char *hlink1, char *hlink2)
155 if (do_link(hlink1, hlink2)) {
157 rsyserr(FINFO, errno, "link %s => %s failed",
162 rprintf(FINFO, "%s => %s\n", hlink2, hlink1);
169 * Create any hard links in the global hlink_list. They were put
170 * there by running init_hard_links on the filelist.
172 void do_hard_links(void)
174 #if SUPPORT_HARD_LINKS
175 struct file_struct *file, *first;
176 char hlink1[MAXPATHLEN];
178 STRUCT_STAT st1, st2;
184 for (i = 0; i < hlink_count; i++) {
185 first = file = hlink_list[i];
186 if (link_stat(f_name_to(first, hlink1), &st1) != 0)
188 while ((file = file->F_NEXT) != first) {
189 hlink2 = f_name(file);
190 if (link_stat(hlink2, &st2) == 0) {
191 if (st2.st_dev == st1.st_dev
192 && st2.st_ino == st1.st_ino)
195 if (!make_backup(hlink2))
197 } else if (robust_unlink(hlink2)) {
199 rsyserr(FINFO, errno,
206 hard_link_one(hlink1, hlink2);