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