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