Fix copyright.
[rsync/rsync.git] / hlink.c
CommitLineData
dc5ddbcc
AT
1/*
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
6e69cff1 4 Copyright (C) 2002 by Martin Pool <mbp@samba.org>
dc5ddbcc
AT
5
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.
10
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.
15
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.
19*/
20
21#include "rsync.h"
22
dc5ddbcc 23extern int dry_run;
9a52223b 24extern int verbose;
dc5ddbcc
AT
25
26#if SUPPORT_HARD_LINKS
6e69cff1 27static int hlink_compare(struct file_struct *f1, struct file_struct *f2)
dc5ddbcc 28{
6e69cff1
MP
29 if (!S_ISREG(f1->mode) && !S_ISREG(f2->mode))
30 return 0;
31 if (!S_ISREG(f1->mode))
32 return -1;
33 if (!S_ISREG(f2->mode))
34 return 1;
dc5ddbcc 35
6e69cff1
MP
36 if (f1->dev != f2->dev)
37 return (int) (f1->dev > f2->dev ? 1 : -1);
dc5ddbcc 38
6e69cff1
MP
39 if (f1->inode != f2->inode)
40 return (int) (f1->inode > f2->inode ? 1 : -1);
dc5ddbcc 41
6e69cff1 42 return file_compare(&f1, &f2);
dc5ddbcc
AT
43}
44
45
3a6a366f
AT
46static struct file_struct *hlink_list;
47static int hlink_count;
dc5ddbcc
AT
48#endif
49
50void init_hard_links(struct file_list *flist)
51{
52#if SUPPORT_HARD_LINKS
dd04a034 53 int i;
6e69cff1
MP
54 if (flist->count < 2)
55 return;
dc5ddbcc 56
6e69cff1
MP
57 if (hlink_list)
58 free(hlink_list);
59
60 if (!(hlink_list =
61 (struct file_struct *) malloc(sizeof(hlink_list[0]) *
62 flist->count)))
dd04a034 63 out_of_memory("init_hard_links");
dc5ddbcc 64
dd04a034 65 for (i = 0; i < flist->count; i++)
6e69cff1
MP
66 memcpy(&hlink_list[i], flist->files[i],
67 sizeof(hlink_list[0]));
dc5ddbcc 68
6e69cff1
MP
69 qsort(hlink_list, flist->count,
70 sizeof(hlink_list[0]), (int (*)()) hlink_compare);
dc5ddbcc 71
6e69cff1 72 hlink_count = flist->count;
dc5ddbcc
AT
73#endif
74}
75
76/* check if a file should be skipped because it is the same as an
77 earlier hard link */
78int check_hard_link(struct file_struct *file)
79{
80#if SUPPORT_HARD_LINKS
6e69cff1
MP
81 int low = 0, high = hlink_count - 1;
82 int ret = 0;
83
84 if (!hlink_list || !S_ISREG(file->mode))
85 return 0;
86
87 while (low != high) {
88 int mid = (low + high) / 2;
89 ret = hlink_compare(&hlink_list[mid], file);
90 if (ret == 0) {
91 low = mid;
92 break;
93 }
94 if (ret > 0)
95 high = mid;
96 else
97 low = mid + 1;
98 }
99
527a51ce
MP
100 /* XXX: To me this looks kind of dodgy -- why do we use [low]
101 * here and [low-1] below? -- mbp */
6e69cff1
MP
102 if (hlink_compare(&hlink_list[low], file) != 0)
103 return 0;
104
105 if (low > 0 &&
106 S_ISREG(hlink_list[low - 1].mode) &&
107 file->dev == hlink_list[low - 1].dev &&
4f2dcb17
MP
108 file->inode == hlink_list[low - 1].inode) {
109 if (verbose >= 2) {
110 rprintf(FINFO, "check_hard_link: \"%s\" is a hard link to file %d, \"%s\"\n",
20c15aea 111 f_name(file), low-1, f_name(&hlink_list[low-1]));
4f2dcb17 112 }
6e69cff1 113 return 1;
4f2dcb17 114 }
dc5ddbcc
AT
115#endif
116
6e69cff1 117 return 0;
dc5ddbcc
AT
118}
119
120
a16bbc39
AT
121#if SUPPORT_HARD_LINKS
122static void hard_link_one(int i)
123{
6e69cff1 124 STRUCT_STAT st1, st2;
a16bbc39 125
6e69cff1
MP
126 if (link_stat(f_name(&hlink_list[i - 1]), &st1) != 0)
127 return;
a16bbc39 128
6e69cff1
MP
129 if (link_stat(f_name(&hlink_list[i]), &st2) != 0) {
130 if (do_link
131 (f_name(&hlink_list[i - 1]),
132 f_name(&hlink_list[i])) != 0) {
a16bbc39 133 if (verbose > 0)
6e69cff1 134 rprintf(FINFO, "link %s => %s : %s\n",
a16bbc39 135 f_name(&hlink_list[i]),
6e69cff1
MP
136 f_name(&hlink_list[i - 1]),
137 strerror(errno));
a16bbc39
AT
138 return;
139 }
140 } else {
6e69cff1
MP
141 if (st2.st_dev == st1.st_dev && st2.st_ino == st1.st_ino)
142 return;
143
c7c11a0d 144 if (robust_unlink(f_name(&hlink_list[i])) != 0 ||
6e69cff1
MP
145 do_link(f_name(&hlink_list[i - 1]),
146 f_name(&hlink_list[i])) != 0) {
a16bbc39 147 if (verbose > 0)
6e69cff1 148 rprintf(FINFO, "link %s => %s : %s\n",
a16bbc39 149 f_name(&hlink_list[i]),
6e69cff1
MP
150 f_name(&hlink_list[i - 1]),
151 strerror(errno));
a16bbc39
AT
152 return;
153 }
154 }
155 if (verbose > 0)
6e69cff1
MP
156 rprintf(FINFO, "%s => %s\n",
157 f_name(&hlink_list[i]),
158 f_name(&hlink_list[i - 1]));
a16bbc39
AT
159}
160#endif
161
fae5bb31
MP
162
163
164/**
165 * Create any hard links in the global hlink_list. They were put
166 * there by running init_hard_links on the filelist.
167 **/
168void do_hard_links(void)
dc5ddbcc
AT
169{
170#if SUPPORT_HARD_LINKS
a16bbc39 171 int i;
a16bbc39 172
6e69cff1
MP
173 if (!hlink_list)
174 return;
175
176 for (i = 1; i < hlink_count; i++) {
a16bbc39 177 if (S_ISREG(hlink_list[i].mode) &&
6e69cff1
MP
178 S_ISREG(hlink_list[i - 1].mode) &&
179 hlink_list[i].basename && hlink_list[i - 1].basename &&
180 hlink_list[i].dev == hlink_list[i - 1].dev &&
181 hlink_list[i].inode == hlink_list[i - 1].inode) {
a16bbc39 182 hard_link_one(i);
6e69cff1 183 }
dc5ddbcc 184 }
dc5ddbcc
AT
185#endif
186}