- Call hard_link_check() with its new args.
[rsync/rsync.git] / hlink.c
CommitLineData
6aae748e 1/*
dc5ddbcc
AT
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
6e69cff1 4 Copyright (C) 2002 by Martin Pool <mbp@samba.org>
6aae748e 5
dc5ddbcc
AT
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.
6aae748e 10
dc5ddbcc
AT
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.
6aae748e 15
dc5ddbcc
AT
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;
417c99f6 25extern int make_backups;
9f2e3c3f 26extern struct file_list *the_file_list;
dc5ddbcc 27
4f5b0756 28#ifdef SUPPORT_HARD_LINKS
9f2e3c3f
WD
29
30#define FPTR(i) (the_file_list->files[i])
31#define LINKED(p1,p2) (FPTR(p1)->F_DEV == FPTR(p2)->F_DEV \
32 && FPTR(p1)->F_INODE == FPTR(p2)->F_INODE)
33
34static int hlink_compare(int *int1, int *int2)
dc5ddbcc 35{
9f2e3c3f
WD
36 struct file_struct *f1 = FPTR(*int1);
37 struct file_struct *f2 = FPTR(*int2);
11dc2740 38
92cc9dd7
WD
39 if (f1->F_DEV != f2->F_DEV)
40 return (int) (f1->F_DEV > f2->F_DEV ? 1 : -1);
dc5ddbcc 41
92cc9dd7
WD
42 if (f1->F_INODE != f2->F_INODE)
43 return (int) (f1->F_INODE > f2->F_INODE ? 1 : -1);
dc5ddbcc 44
122d1771 45 return f_name_cmp(f1, f2);
11dc2740 46}
dc5ddbcc 47
9f2e3c3f 48static int *hlink_list;
417c99f6 49static int hlink_count;
1d5cda22 50
1d5cda22 51/* Analyze the data in the hlink_list[], remove items that aren't multiply
90481755 52 * linked, and replace the dev+inode data with the hlindex+next linked list. */
9f2e3c3f 53static void link_idev_data(void)
1d5cda22 54{
97e786c3 55 int cur, from, to, start;
1d5cda22 56
9935066b 57 alloc_pool_t hlink_pool;
9f2e3c3f 58 alloc_pool_t idev_pool = the_file_list->hlink_pool;
9935066b
S
59
60 hlink_pool = pool_create(128 * 1024, sizeof (struct hlink),
61 out_of_memory, POOL_INTERN);
62
1d5cda22
WD
63 for (from = to = 0; from < hlink_count; from++) {
64 start = from;
97e786c3
WD
65 while (1) {
66 cur = hlink_list[from];
67 if (from == hlink_count-1
68 || !LINKED(cur, hlink_list[from+1]))
69 break;
70 pool_free(idev_pool, 0, FPTR(cur)->link_u.idev);
71 FPTR(cur)->link_u.links = pool_talloc(hlink_pool,
9935066b
S
72 struct hlink, 1, "hlink_list");
73
97e786c3
WD
74 FPTR(cur)->F_HLINDEX = to;
75 FPTR(cur)->F_NEXT = hlink_list[++from];
1d5cda22 76 }
97e786c3 77 pool_free(idev_pool, 0, FPTR(cur)->link_u.idev);
1d5cda22 78 if (from > start) {
97e786c3
WD
79 int head = hlink_list[start];
80 FPTR(cur)->link_u.links = pool_talloc(hlink_pool,
9935066b
S
81 struct hlink, 1, "hlink_list");
82
9f2e3c3f 83 FPTR(head)->flags |= FLAG_HLINK_TOL;
97e786c3
WD
84 FPTR(cur)->F_HLINDEX = to;
85 FPTR(cur)->F_NEXT = head;
86 FPTR(cur)->flags |= FLAG_HLINK_EOL;
1d5cda22 87 hlink_list[to++] = head;
97e786c3
WD
88 } else
89 FPTR(cur)->link_u.links = NULL;
1d5cda22
WD
90 }
91
92 if (!to) {
93 free(hlink_list);
94 hlink_list = NULL;
9935066b
S
95 pool_destroy(hlink_pool);
96 hlink_pool = NULL;
1d5cda22
WD
97 } else {
98 hlink_count = to;
9f2e3c3f
WD
99 hlink_list = realloc_array(hlink_list, int, hlink_count);
100 if (!hlink_list)
1d5cda22
WD
101 out_of_memory("init_hard_links");
102 }
9f2e3c3f 103 the_file_list->hlink_pool = hlink_pool;
9935066b 104 pool_destroy(idev_pool);
1d5cda22 105}
dc5ddbcc
AT
106#endif
107
9f2e3c3f 108void init_hard_links(void)
dc5ddbcc 109{
4f5b0756 110#ifdef SUPPORT_HARD_LINKS
dd04a034 111 int i;
11dc2740 112
6e69cff1
MP
113 if (hlink_list)
114 free(hlink_list);
115
9f2e3c3f 116 if (!(hlink_list = new_array(int, the_file_list->count)))
dd04a034 117 out_of_memory("init_hard_links");
6aae748e 118
11dc2740 119 hlink_count = 0;
9f2e3c3f
WD
120 for (i = 0; i < the_file_list->count; i++) {
121 if (FPTR(i)->link_u.idev)
122 hlink_list[hlink_count++] = i;
11dc2740 123 }
dc5ddbcc 124
11dc2740 125 qsort(hlink_list, hlink_count,
0d6e308b 126 sizeof hlink_list[0], (int (*)()) hlink_compare);
dc5ddbcc 127
6aae748e
WD
128 if (!hlink_count) {
129 free(hlink_list);
130 hlink_list = NULL;
1d5cda22 131 } else
9f2e3c3f 132 link_idev_data();
dc5ddbcc 133#endif
dc5ddbcc
AT
134}
135
9f2e3c3f 136int hard_link_check(struct file_struct *file, int ndx, int skip)
d38fc305 137{
4f5b0756 138#ifdef SUPPORT_HARD_LINKS
97e786c3 139 if (!file->link_u.links)
d38fc305
WD
140 return 0;
141 if (skip && !(file->flags & FLAG_HLINK_EOL))
142 hlink_list[file->F_HLINDEX] = file->F_NEXT;
9f2e3c3f
WD
143 if (hlink_list[file->F_HLINDEX] != ndx) {
144 if (verbose > 2) {
d38fc305 145 rprintf(FINFO, "\"%s\" is a hard link\n",
71903f60 146 safe_fname(f_name(file)));
d38fc305
WD
147 }
148 return 1;
149 }
bb91a624 150#endif
d38fc305
WD
151 return 0;
152}
153
4f5b0756 154#ifdef SUPPORT_HARD_LINKS
9f2e3c3f
WD
155int hard_link_one(struct file_struct *file, int ndx, char *fname,
156 int statret, STRUCT_STAT *st, char *toname, int terse,
157 int itemizing, enum logcode code)
a16bbc39 158{
9f2e3c3f 159 if (do_link(toname, fname)) {
0d6e308b 160 if (verbose) {
9f2e3c3f
WD
161 rsyserr(FERROR, errno, "link %s => %s failed",
162 full_fname(fname), safe_fname(toname));
a16bbc39 163 }
9f2e3c3f
WD
164 return -1;
165 }
166
167 if (itemizing) {
b7d4d28b
WD
168 itemize(file, ndx, statret, st,
169 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0,
9f2e3c3f
WD
170 terse ? "" : toname);
171 }
172 if (code && verbose && !terse) {
173 rprintf(code, "%s => %s\n",
174 safe_fname(fname), safe_fname(toname));
a16bbc39 175 }
9f2e3c3f 176 return 0;
a16bbc39
AT
177}
178#endif
179
fae5bb31 180
9f2e3c3f
WD
181void hard_link_cluster(struct file_struct *file, int master, int itemizing,
182 enum logcode code)
dc5ddbcc 183{
4f5b0756 184#ifdef SUPPORT_HARD_LINKS
3fef5364
WD
185 char hlink1[MAXPATHLEN];
186 char *hlink2;
1d5cda22 187 STRUCT_STAT st1, st2;
9f2e3c3f 188 int statret, ndx = master;
a16bbc39 189
9f2e3c3f 190 if (link_stat(f_name_to(file, hlink1), &st1, 0) < 0)
6e69cff1 191 return;
9f2e3c3f
WD
192 if (!(file->flags & FLAG_HLINK_TOL)) {
193 while (!(file->flags & FLAG_HLINK_EOL)) {
194 ndx = file->F_NEXT;
195 file = FPTR(ndx);
196 }
197 }
198 do {
199 ndx = file->F_NEXT;
200 file = FPTR(ndx);
201 if (ndx == master)
1d5cda22 202 continue;
9f2e3c3f
WD
203 hlink2 = f_name(file);
204 if ((statret = link_stat(hlink2, &st2, 0)) == 0) {
205 if (st2.st_dev == st1.st_dev
206 && st2.st_ino == st1.st_ino) {
207 if (itemizing) {
b7d4d28b
WD
208 itemize(file, ndx, statret, &st2,
209 ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
210 0, "");
9f2e3c3f
WD
211 }
212 continue;
213 }
214 if (make_backups) {
215 if (!make_backup(hlink2))
1d5cda22 216 continue;
9f2e3c3f
WD
217 } else if (robust_unlink(hlink2)) {
218 if (verbose > 0) {
219 rsyserr(FINFO, errno,
220 "unlink %s failed",
221 full_fname(hlink2));
1d5cda22 222 }
9f2e3c3f 223 continue;
1d5cda22 224 }
6e69cff1 225 }
9f2e3c3f
WD
226 hard_link_one(file, ndx, hlink2, statret,
227 &st2, hlink1, 0, itemizing, code);
228 } while (!(file->flags & FLAG_HLINK_EOL));
dc5ddbcc
AT
229#endif
230}