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 **file1, struct file_struct **file2)
29 struct file_struct *f1 = *file1;
30 struct file_struct *f2 = *file2;
32 if (f1->dev != f2->dev)
33 return (int) (f1->dev > f2->dev ? 1 : -1);
35 if (f1->inode != f2->inode)
36 return (int) (f1->inode > f2->inode ? 1 : -1);
38 return file_compare(file1, file2);
41 static struct file_struct **hlink_list;
42 static int hlink_count;
45 void init_hard_links(struct file_list *flist)
47 #if SUPPORT_HARD_LINKS
56 if (!(hlink_list = new_array(struct file_struct *, flist->count)))
57 out_of_memory("init_hard_links");
59 /* we'll want to restore the memcpy when we purge the
60 * hlink list after the sort.
61 * memcpy(hlink_list, flist->files, sizeof(hlink_list[0]) * flist->count);
64 for (i = 0; i < flist->count; i++) {
65 if (flist->files[i]->flags & HAS_INODE_DATA)
66 hlink_list[hlink_count++] = flist->files[i];
69 qsort(hlink_list, hlink_count,
70 sizeof(hlink_list[0]), (int (*)()) hlink_compare);
75 } else if (!(hlink_list = realloc_array(hlink_list,
76 struct file_struct *, hlink_count)))
77 out_of_memory("init_hard_links");
81 /* check if a file should be skipped because it is the same as an
83 int check_hard_link(struct file_struct *file)
85 #if SUPPORT_HARD_LINKS
86 int low = 0, high = hlink_count - 1;
89 if (!hlink_list || !(file->flags & HAS_INODE_DATA))
93 int mid = (low + high) / 2;
94 ret = hlink_compare(&hlink_list[mid], &file);
105 /* XXX: To me this looks kind of dodgy -- why do we use [low]
106 * here and [low-1] below? -- mbp */
107 if (hlink_compare(&hlink_list[low], &file) != 0)
111 file->dev == hlink_list[low - 1]->dev &&
112 file->inode == hlink_list[low - 1]->inode) {
114 rprintf(FINFO, "check_hard_link: \"%s\" is a hard link to file %d, \"%s\"\n",
115 f_name(file), low-1, f_name(hlink_list[low-1]));
125 #if SUPPORT_HARD_LINKS
126 static void hard_link_one(int i)
128 STRUCT_STAT st1, st2;
129 char *hlink2, *hlink1 = f_name(hlink_list[i - 1]);
131 if (link_stat(hlink1, &st1) != 0)
134 hlink2 = f_name(hlink_list[i]);
135 if (link_stat(hlink2, &st2) != 0) {
136 if (do_link(hlink1, hlink2)) {
138 rprintf(FINFO, "link %s => %s : %s\n",
139 hlink2, hlink1, strerror(errno));
144 if (st2.st_dev == st1.st_dev && st2.st_ino == st1.st_ino)
147 if (robust_unlink(hlink2) || do_link(hlink1, hlink2)) {
149 rprintf(FINFO, "link %s => %s : %s\n",
150 hlink2, hlink1, strerror(errno));
156 rprintf(FINFO, "%s => %s\n", hlink2, hlink1);
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 (hlink_list[i]->basename && hlink_list[i - 1]->basename &&
176 hlink_list[i]->dev == hlink_list[i - 1]->dev &&
177 hlink_list[i]->inode == hlink_list[i - 1]->inode) {