Batch-mode fix.
[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
58cadc86 60 if (!(hlink_list = new_array(struct file_struct, flist->count)))
dd04a034 61 out_of_memory("init_hard_links");
dc5ddbcc 62
dd04a034 63 for (i = 0; i < flist->count; i++)
6e69cff1
MP
64 memcpy(&hlink_list[i], flist->files[i],
65 sizeof(hlink_list[0]));
dc5ddbcc 66
6e69cff1
MP
67 qsort(hlink_list, flist->count,
68 sizeof(hlink_list[0]), (int (*)()) hlink_compare);
dc5ddbcc 69
6e69cff1 70 hlink_count = flist->count;
dc5ddbcc
AT
71#endif
72}
73
74/* check if a file should be skipped because it is the same as an
75 earlier hard link */
76int check_hard_link(struct file_struct *file)
77{
78#if SUPPORT_HARD_LINKS
6e69cff1
MP
79 int low = 0, high = hlink_count - 1;
80 int ret = 0;
81
82 if (!hlink_list || !S_ISREG(file->mode))
83 return 0;
84
85 while (low != high) {
86 int mid = (low + high) / 2;
87 ret = hlink_compare(&hlink_list[mid], file);
88 if (ret == 0) {
89 low = mid;
90 break;
91 }
92 if (ret > 0)
93 high = mid;
94 else
95 low = mid + 1;
96 }
97
527a51ce
MP
98 /* XXX: To me this looks kind of dodgy -- why do we use [low]
99 * here and [low-1] below? -- mbp */
6e69cff1
MP
100 if (hlink_compare(&hlink_list[low], file) != 0)
101 return 0;
102
103 if (low > 0 &&
104 S_ISREG(hlink_list[low - 1].mode) &&
105 file->dev == hlink_list[low - 1].dev &&
4f2dcb17
MP
106 file->inode == hlink_list[low - 1].inode) {
107 if (verbose >= 2) {
108 rprintf(FINFO, "check_hard_link: \"%s\" is a hard link to file %d, \"%s\"\n",
20c15aea 109 f_name(file), low-1, f_name(&hlink_list[low-1]));
4f2dcb17 110 }
6e69cff1 111 return 1;
4f2dcb17 112 }
dc5ddbcc
AT
113#endif
114
6e69cff1 115 return 0;
dc5ddbcc
AT
116}
117
118
a16bbc39
AT
119#if SUPPORT_HARD_LINKS
120static void hard_link_one(int i)
121{
6e69cff1 122 STRUCT_STAT st1, st2;
310c9f30 123 char *hlink2, *hlink1 = f_name(&hlink_list[i - 1]);
a16bbc39 124
310c9f30 125 if (link_stat(hlink1, &st1) != 0)
6e69cff1 126 return;
a16bbc39 127
310c9f30
WD
128 hlink2 = f_name(&hlink_list[i]);
129 if (link_stat(hlink2, &st2) != 0) {
130 if (do_link(hlink1, hlink2)) {
131 if (verbose > 0) {
6e69cff1 132 rprintf(FINFO, "link %s => %s : %s\n",
310c9f30
WD
133 hlink2, hlink1, strerror(errno));
134 }
a16bbc39
AT
135 return;
136 }
137 } else {
6e69cff1
MP
138 if (st2.st_dev == st1.st_dev && st2.st_ino == st1.st_ino)
139 return;
140
310c9f30
WD
141 if (robust_unlink(hlink2) || do_link(hlink1, hlink2)) {
142 if (verbose > 0) {
6e69cff1 143 rprintf(FINFO, "link %s => %s : %s\n",
310c9f30
WD
144 hlink2, hlink1, strerror(errno));
145 }
a16bbc39
AT
146 return;
147 }
148 }
149 if (verbose > 0)
310c9f30 150 rprintf(FINFO, "%s => %s\n", hlink2, hlink1);
a16bbc39
AT
151}
152#endif
153
fae5bb31
MP
154
155
156/**
157 * Create any hard links in the global hlink_list. They were put
158 * there by running init_hard_links on the filelist.
159 **/
160void do_hard_links(void)
dc5ddbcc
AT
161{
162#if SUPPORT_HARD_LINKS
a16bbc39 163 int i;
a16bbc39 164
6e69cff1
MP
165 if (!hlink_list)
166 return;
167
168 for (i = 1; i < hlink_count; i++) {
a16bbc39 169 if (S_ISREG(hlink_list[i].mode) &&
6e69cff1
MP
170 S_ISREG(hlink_list[i - 1].mode) &&
171 hlink_list[i].basename && hlink_list[i - 1].basename &&
172 hlink_list[i].dev == hlink_list[i - 1].dev &&
173 hlink_list[i].inode == hlink_list[i - 1].inode) {
a16bbc39 174 hard_link_one(i);
6e69cff1 175 }
dc5ddbcc 176 }
dc5ddbcc
AT
177#endif
178}