2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 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)) return 0;
30 if (!S_ISREG(f1->mode)) return -1;
31 if (!S_ISREG(f2->mode)) return 1;
33 if (f1->dev != f2->dev)
34 return (f1->dev - f2->dev);
36 if (f1->inode != f2->inode)
37 return (f1->inode - f2->inode);
39 return file_compare(&f1,&f2);
43 static struct file_struct *hlink_list = NULL;
44 static int hlink_count=0;
47 void init_hard_links(struct file_list *flist)
49 #if SUPPORT_HARD_LINKS
50 if (flist->count < 2) return;
52 if (hlink_list) free(hlink_list);
55 (struct file_struct *)malloc(sizeof(hlink_list[0])*flist->count)))
56 out_of_memory("init_hard_links");
58 bcopy((char *)flist->files,
60 sizeof(hlink_list[0])*flist->count);
62 qsort(hlink_list,flist->count,
63 sizeof(hlink_list[0]),
64 (int (*)())hlink_compare);
66 hlink_count=flist->count;
70 /* check if a file should be skipped because it is the same as an
72 int check_hard_link(struct file_struct *file)
74 #if SUPPORT_HARD_LINKS
75 int low=0,high=hlink_count-1;
78 if (!hlink_list || !S_ISREG(file->mode)) return 0;
81 int mid = (low+high)/2;
82 ret = hlink_compare(&hlink_list[mid],file);
93 if (hlink_compare(&hlink_list[low],file) != 0) return 0;
96 S_ISREG(hlink_list[low-1].mode) &&
97 file->dev == hlink_list[low-1].dev &&
98 file->inode == hlink_list[low-1].inode)
106 /* create any hard links in the flist */
107 void do_hard_links(struct file_list *flist)
109 #if SUPPORT_HARD_LINKS
112 if (!hlink_list) return;
114 for (i=1;i<hlink_count;i++) {
115 if (S_ISREG(hlink_list[i].mode) &&
116 S_ISREG(hlink_list[i-1].mode) &&
117 hlink_list[i].basename && hlink_list[i-1].basename &&
118 hlink_list[i].dev == hlink_list[i-1].dev &&
119 hlink_list[i].inode == hlink_list[i-1].inode) {
122 if (link_stat(f_name(&hlink_list[i-1]),&st1) != 0) continue;
123 if (link_stat(f_name(&hlink_list[i]),&st2) != 0) {
124 if (!dry_run && link(f_name(&hlink_list[i-1]),f_name(&hlink_list[i])) != 0) {
126 fprintf(FINFO,"link %s => %s : %s\n",
127 f_name(&hlink_list[i]),
128 f_name(&hlink_list[i-1]),strerror(errno));
132 if (st2.st_dev == st1.st_dev && st2.st_ino == st1.st_ino) continue;
134 if (!dry_run && (unlink(f_name(&hlink_list[i])) != 0 ||
135 link(f_name(&hlink_list[i-1]),f_name(&hlink_list[i])) != 0)) {
137 fprintf(FINFO,"link %s => %s : %s\n",
138 f_name(&hlink_list[i]),
139 f_name(&hlink_list[i-1]),strerror(errno));
144 fprintf(FINFO,"%s => %s\n",
145 f_name(&hlink_list[i]),f_name(&hlink_list[i-1]));