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