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