added support for 64 bit file offsets under Solaris 2.6. Not tested
[rsync/rsync.git] / hlink.c
CommitLineData
dc5ddbcc
AT
1/*
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
4
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.
9
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.
14
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.
18*/
19
20#include "rsync.h"
21
22extern int am_server;
23extern int dry_run;
9a52223b 24extern int verbose;
dc5ddbcc
AT
25
26#if SUPPORT_HARD_LINKS
27static int hlink_compare(struct file_struct *f1,struct file_struct *f2)
28{
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;
32
33 if (f1->dev != f2->dev)
a16bbc39 34 return (int)(f1->dev>f2->dev?1:-1);
dc5ddbcc
AT
35
36 if (f1->inode != f2->inode)
a16bbc39 37 return (int)(f1->inode>f2->inode?1:-1);
dc5ddbcc 38
3ec4dd97 39 return file_compare(&f1,&f2);
dc5ddbcc
AT
40}
41
42
3a6a366f
AT
43static struct file_struct *hlink_list;
44static int hlink_count;
dc5ddbcc
AT
45#endif
46
47void init_hard_links(struct file_list *flist)
48{
49#if SUPPORT_HARD_LINKS
dd04a034
AT
50 int i;
51 if (flist->count < 2) return;
dc5ddbcc 52
dd04a034 53 if (hlink_list) free(hlink_list);
dc5ddbcc 54
dd04a034
AT
55 if (!(hlink_list =
56 (struct file_struct *)malloc(sizeof(hlink_list[0])*flist->count)))
57 out_of_memory("init_hard_links");
dc5ddbcc 58
dd04a034
AT
59 for (i = 0; i < flist->count; i++)
60 bcopy(flist->files[i], &hlink_list[i], sizeof(hlink_list[0]));
dc5ddbcc 61
dd04a034
AT
62 qsort(hlink_list,flist->count,
63 sizeof(hlink_list[0]),
64 (int (*)())hlink_compare);
dc5ddbcc 65
dd04a034 66 hlink_count=flist->count;
dc5ddbcc
AT
67#endif
68}
69
70/* check if a file should be skipped because it is the same as an
71 earlier hard link */
72int check_hard_link(struct file_struct *file)
73{
74#if SUPPORT_HARD_LINKS
527cea66 75 int low=0,high=hlink_count-1;
e3cd198f 76 int ret=0;
dc5ddbcc
AT
77
78 if (!hlink_list || !S_ISREG(file->mode)) return 0;
79
80 while (low != high) {
e3cd198f 81 int mid = (low+high)/2;
dc5ddbcc 82 ret = hlink_compare(&hlink_list[mid],file);
c778aaa0
AT
83 if (ret == 0) {
84 low = mid;
85 break;
86 }
dc5ddbcc
AT
87 if (ret > 0)
88 high=mid;
89 else
90 low=mid+1;
91 }
92
e3cd198f 93 if (hlink_compare(&hlink_list[low],file) != 0) return 0;
dc5ddbcc 94
e3cd198f
AT
95 if (low > 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)
dc5ddbcc
AT
99 return 1;
100#endif
101
102 return 0;
103}
104
105
a16bbc39
AT
106#if SUPPORT_HARD_LINKS
107static void hard_link_one(int i)
108{
bcacc18b 109 STRUCT_STAT st1,st2;
a16bbc39
AT
110
111 if (link_stat(f_name(&hlink_list[i-1]),&st1) != 0) return;
112
113 if (link_stat(f_name(&hlink_list[i]),&st2) != 0) {
114 if (do_link(f_name(&hlink_list[i-1]),f_name(&hlink_list[i])) != 0) {
115 if (verbose > 0)
116 fprintf(FINFO,"link %s => %s : %s\n",
117 f_name(&hlink_list[i]),
118 f_name(&hlink_list[i-1]),strerror(errno));
119 return;
120 }
121 } else {
122 if (st2.st_dev == st1.st_dev && st2.st_ino == st1.st_ino) return;
123
124 if (do_unlink(f_name(&hlink_list[i])) != 0 ||
125 do_link(f_name(&hlink_list[i-1]),f_name(&hlink_list[i])) != 0) {
126 if (verbose > 0)
127 fprintf(FINFO,"link %s => %s : %s\n",
128 f_name(&hlink_list[i]),
129 f_name(&hlink_list[i-1]),strerror(errno));
130 return;
131 }
132 }
133 if (verbose > 0)
134 fprintf(FINFO,"%s => %s\n",
135 f_name(&hlink_list[i]),f_name(&hlink_list[i-1]));
136}
137#endif
138
dc5ddbcc
AT
139/* create any hard links in the flist */
140void do_hard_links(struct file_list *flist)
141{
142#if SUPPORT_HARD_LINKS
a16bbc39 143 int i;
dc5ddbcc 144
a16bbc39
AT
145 if (!hlink_list) return;
146
147 for (i=1;i<hlink_count;i++) {
148 if (S_ISREG(hlink_list[i].mode) &&
149 S_ISREG(hlink_list[i-1].mode) &&
150 hlink_list[i].basename && hlink_list[i-1].basename &&
151 hlink_list[i].dev == hlink_list[i-1].dev &&
152 hlink_list[i].inode == hlink_list[i-1].inode) {
153 hard_link_one(i);
154 }
dc5ddbcc 155 }
dc5ddbcc
AT
156#endif
157}