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 #if SUPPORT_HARD_LINKS
27 static int hlink_compare(struct file_struct *f1, struct file_struct *f2)
29 if (!S_ISREG(f1->mode) && !S_ISREG(f2->mode))
31 if (!S_ISREG(f1->mode))
33 if (!S_ISREG(f2->mode))
36 if (f1->dev != f2->dev)
37 return (int) (f1->dev > f2->dev ? 1 : -1);
39 if (f1->inode != f2->inode)
40 return (int) (f1->inode > f2->inode ? 1 : -1);
42 return file_compare(&f1, &f2);
46 static struct file_struct *hlink_list;
47 static int hlink_count;
50 void init_hard_links(struct file_list *flist)
52 #if SUPPORT_HARD_LINKS
60 if (!(hlink_list = new_array(struct file_struct, flist->count)))
61 out_of_memory("init_hard_links");
63 for (i = 0; i < flist->count; i++)
64 memcpy(&hlink_list[i], flist->files[i],
65 sizeof(hlink_list[0]));
67 qsort(hlink_list, flist->count,
68 sizeof(hlink_list[0]), (int (*)()) hlink_compare);
70 hlink_count = flist->count;
74 /* check if a file should be skipped because it is the same as an
76 int check_hard_link(struct file_struct *file)
78 #if SUPPORT_HARD_LINKS
79 int low = 0, high = hlink_count - 1;
82 if (!hlink_list || !S_ISREG(file->mode))
86 int mid = (low + high) / 2;
87 ret = hlink_compare(&hlink_list[mid], file);
98 /* XXX: To me this looks kind of dodgy -- why do we use [low]
99 * here and [low-1] below? -- mbp */
100 if (hlink_compare(&hlink_list[low], file) != 0)
104 S_ISREG(hlink_list[low - 1].mode) &&
105 file->dev == hlink_list[low - 1].dev &&
106 file->inode == hlink_list[low - 1].inode) {
108 rprintf(FINFO, "check_hard_link: \"%s\" is a hard link to file %d, \"%s\"\n",
109 f_name(file), low-1, f_name(&hlink_list[low-1]));
119 #if SUPPORT_HARD_LINKS
120 static void hard_link_one(int i)
122 STRUCT_STAT st1, st2;
124 if (link_stat(f_name(&hlink_list[i - 1]), &st1) != 0)
127 if (link_stat(f_name(&hlink_list[i]), &st2) != 0) {
129 (f_name(&hlink_list[i - 1]),
130 f_name(&hlink_list[i])) != 0) {
132 rprintf(FINFO, "link %s => %s : %s\n",
133 f_name(&hlink_list[i]),
134 f_name(&hlink_list[i - 1]),
139 if (st2.st_dev == st1.st_dev && st2.st_ino == st1.st_ino)
142 if (robust_unlink(f_name(&hlink_list[i])) != 0 ||
143 do_link(f_name(&hlink_list[i - 1]),
144 f_name(&hlink_list[i])) != 0) {
146 rprintf(FINFO, "link %s => %s : %s\n",
147 f_name(&hlink_list[i]),
148 f_name(&hlink_list[i - 1]),
154 rprintf(FINFO, "%s => %s\n",
155 f_name(&hlink_list[i]),
156 f_name(&hlink_list[i - 1]));
163 * Create any hard links in the global hlink_list. They were put
164 * there by running init_hard_links on the filelist.
166 void do_hard_links(void)
168 #if SUPPORT_HARD_LINKS
174 for (i = 1; i < hlink_count; i++) {
175 if (S_ISREG(hlink_list[i].mode) &&
176 S_ISREG(hlink_list[i - 1].mode) &&
177 hlink_list[i].basename && hlink_list[i - 1].basename &&
178 hlink_list[i].dev == hlink_list[i - 1].dev &&
179 hlink_list[i].inode == hlink_list[i - 1].inode) {