- Updated itemize() to handle sending of hard-link-name info. Made
[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;
dc5ddbcc 26
4f5b0756 27#ifdef SUPPORT_HARD_LINKS
11dc2740 28static int hlink_compare(struct file_struct **file1, struct file_struct **file2)
dc5ddbcc 29{
11dc2740
S
30 struct file_struct *f1 = *file1;
31 struct file_struct *f2 = *file2;
32
92cc9dd7
WD
33 if (f1->F_DEV != f2->F_DEV)
34 return (int) (f1->F_DEV > f2->F_DEV ? 1 : -1);
dc5ddbcc 35
92cc9dd7
WD
36 if (f1->F_INODE != f2->F_INODE)
37 return (int) (f1->F_INODE > f2->F_INODE ? 1 : -1);
dc5ddbcc 38
122d1771 39 return f_name_cmp(f1, f2);
11dc2740 40}
dc5ddbcc 41
417c99f6
WD
42static struct file_struct **hlink_list;
43static int hlink_count;
1d5cda22
WD
44
45#define LINKED(p1,p2) ((p1)->F_DEV == (p2)->F_DEV \
46 && (p1)->F_INODE == (p2)->F_INODE)
47
48/* Analyze the data in the hlink_list[], remove items that aren't multiply
90481755 49 * linked, and replace the dev+inode data with the hlindex+next linked list. */
9935066b 50static void link_idev_data(struct file_list *flist)
1d5cda22
WD
51{
52 struct file_struct *head;
53 int from, to, start;
54
9935066b
S
55 alloc_pool_t hlink_pool;
56 alloc_pool_t idev_pool = flist->hlink_pool;
57
58 hlink_pool = pool_create(128 * 1024, sizeof (struct hlink),
59 out_of_memory, POOL_INTERN);
60
1d5cda22
WD
61 for (from = to = 0; from < hlink_count; from++) {
62 start = from;
63 head = hlink_list[start];
64 while (from < hlink_count-1
65 && LINKED(hlink_list[from], hlink_list[from+1])) {
9935066b
S
66 pool_free(idev_pool, 0, hlink_list[from]->link_u.idev);
67 hlink_list[from]->link_u.links = pool_talloc(hlink_pool,
68 struct hlink, 1, "hlink_list");
69
d38fc305 70 hlink_list[from]->F_HLINDEX = to;
1d5cda22
WD
71 hlink_list[from]->F_NEXT = hlink_list[from+1];
72 from++;
73 }
74 if (from > start) {
9935066b
S
75 pool_free(idev_pool, 0, hlink_list[from]->link_u.idev);
76 hlink_list[from]->link_u.links = pool_talloc(hlink_pool,
77 struct hlink, 1, "hlink_list");
78
d38fc305
WD
79 hlink_list[from]->F_HLINDEX = to;
80 hlink_list[from]->F_NEXT = head;
81 hlink_list[from]->flags |= FLAG_HLINK_EOL;
1d5cda22
WD
82 hlink_list[to++] = head;
83 } else {
9935066b 84 pool_free(idev_pool, 0, head->link_u.idev);
1d5cda22
WD
85 head->link_u.idev = NULL;
86 }
87 }
88
89 if (!to) {
90 free(hlink_list);
91 hlink_list = NULL;
9935066b
S
92 pool_destroy(hlink_pool);
93 hlink_pool = NULL;
1d5cda22
WD
94 } else {
95 hlink_count = to;
96 if (!(hlink_list = realloc_array(hlink_list,
0d6e308b 97 struct file_struct *, hlink_count)))
1d5cda22
WD
98 out_of_memory("init_hard_links");
99 }
9935066b
S
100 flist->hlink_pool = hlink_pool;
101 pool_destroy(idev_pool);
1d5cda22 102}
dc5ddbcc
AT
103#endif
104
105void init_hard_links(struct file_list *flist)
106{
4f5b0756 107#ifdef SUPPORT_HARD_LINKS
dd04a034 108 int i;
11dc2740 109
6e69cff1
MP
110 if (flist->count < 2)
111 return;
dc5ddbcc 112
6e69cff1
MP
113 if (hlink_list)
114 free(hlink_list);
115
11dc2740 116 if (!(hlink_list = new_array(struct file_struct *, flist->count)))
dd04a034 117 out_of_memory("init_hard_links");
6aae748e 118
11dc2740
S
119 hlink_count = 0;
120 for (i = 0; i < flist->count; i++) {
92cc9dd7 121 if (flist->files[i]->link_u.idev)
11dc2740
S
122 hlink_list[hlink_count++] = flist->files[i];
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
9935066b 132 link_idev_data(flist);
dc5ddbcc 133#endif
dc5ddbcc
AT
134}
135
d38fc305
WD
136int hard_link_check(struct file_struct *file, int skip)
137{
4f5b0756 138#ifdef SUPPORT_HARD_LINKS
028fdddb 139 if (!hlink_list || !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;
143 if (hlink_list[file->F_HLINDEX] != file) {
144 if (verbose > 1) {
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
1d5cda22 155static void hard_link_one(char *hlink1, char *hlink2)
a16bbc39 156{
1d5cda22 157 if (do_link(hlink1, hlink2)) {
0d6e308b 158 if (verbose) {
d62bcc17 159 rsyserr(FINFO, errno, "link %s => %s failed",
1dca857b 160 full_fname(hlink2), safe_fname(hlink1));
a16bbc39
AT
161 }
162 }
0d6e308b 163 else if (verbose)
71903f60 164 rprintf(FINFO, "%s => %s\n", safe_fname(hlink2), safe_fname(hlink1));
a16bbc39
AT
165}
166#endif
167
fae5bb31
MP
168
169
170/**
171 * Create any hard links in the global hlink_list. They were put
172 * there by running init_hard_links on the filelist.
173 **/
0f80c3e8 174void do_hard_links(int allowed_lull, int flist_count)
dc5ddbcc 175{
4f5b0756 176#ifdef SUPPORT_HARD_LINKS
d38fc305 177 struct file_struct *file, *first;
3fef5364
WD
178 char hlink1[MAXPATHLEN];
179 char *hlink2;
1d5cda22 180 STRUCT_STAT st1, st2;
a16bbc39 181 int i;
a16bbc39 182
6e69cff1
MP
183 if (!hlink_list)
184 return;
185
1d5cda22 186 for (i = 0; i < hlink_count; i++) {
d38fc305 187 first = file = hlink_list[i];
373ef160 188 if (link_stat(f_name_to(first, hlink1), &st1, 0) < 0)
1d5cda22 189 continue;
d38fc305 190 while ((file = file->F_NEXT) != first) {
1d5cda22 191 hlink2 = f_name(file);
373ef160 192 if (link_stat(hlink2, &st2, 0) == 0) {
1d5cda22
WD
193 if (st2.st_dev == st1.st_dev
194 && st2.st_ino == st1.st_ino)
195 continue;
417c99f6
WD
196 if (make_backups) {
197 if (!make_backup(hlink2))
198 continue;
199 } else if (robust_unlink(hlink2)) {
1d5cda22 200 if (verbose > 0) {
d62bcc17
WD
201 rsyserr(FINFO, errno,
202 "unlink %s failed",
203 full_fname(hlink2));
1d5cda22
WD
204 }
205 continue;
206 }
207 }
208 hard_link_one(hlink1, hlink2);
6e69cff1 209 }
0f80c3e8
WD
210 if (allowed_lull)
211 maybe_send_keepalive(allowed_lull, flist_count);
dc5ddbcc 212 }
dc5ddbcc
AT
213#endif
214}