Changed flist_extra_ndx into flist_extra_cnt.
[rsync/rsync.git] / hlink.c
1 /*
2  * Routines to support hard-linking.
3  *
4  * Copyright (C) 1996 Andrew Tridgell
5  * Copyright (C) 1996 Paul Mackerras
6  * Copyright (C) 2002 Martin Pool <mbp@samba.org>
7  * Copyright (C) 2004, 2005, 2006 Wayne Davison
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
22  */
23
24 #include "rsync.h"
25
26 extern int verbose;
27 extern int dry_run;
28 extern int do_xfers;
29 extern int link_dest;
30 extern int make_backups;
31 extern int flist_extra_ndx;
32 extern int remove_source_files;
33 extern int stdout_format_has_i;
34 extern char *basis_dir[];
35 extern struct file_list *the_file_list;
36
37 #ifdef SUPPORT_HARD_LINKS
38
39 #define SKIPPED_LINK (-1)
40 #define FINISHED_LINK (-2)
41
42 #define FPTR(i) (the_file_list->files[i])
43 #define LINKED(i1,i2) ((i1)->dev == (i2)->dev && (i1)->ino == (i2)->ino)
44
45 static int hlink_compare(int *int1, int *int2)
46 {
47         struct file_struct *f1 = FPTR(*int1);
48         struct file_struct *f2 = FPTR(*int2);
49         struct idev *i1 = F_IDEV(f1);
50         struct idev *i2 = F_IDEV(f2);
51
52         if (i1->dev != i2->dev)
53                 return i1->dev > i2->dev ? 1 : -1;
54
55         if (i1->ino != i2->ino)
56                 return i1->ino > i2->ino ? 1 : -1;
57
58         return f_name_cmp(f1, f2);
59 }
60
61 static int32 *hlink_list;
62 static int32 hlink_count;
63
64 /* Analyze the data in the hlink_list[], remove items that aren't multiply
65  * linked, and replace the dev+inode data with the hlindex+next linked list. */
66 static void link_idev_data(void)
67 {
68         int32 from, to, start;
69         struct file_struct *file, *file_next;
70         struct idev *idev, *idev_next;
71         struct hlist *hl;
72
73         alloc_pool_t hlink_pool;
74         alloc_pool_t idev_pool = the_file_list->hlink_pool;
75
76         hlink_pool = pool_create(128 * 1024, sizeof (struct hlist), out_of_memory, POOL_INTERN);
77
78         for (from = to = 0; from < hlink_count; from++) {
79                 start = from;
80                 for (file = FPTR(hlink_list[from]), idev = F_IDEV(file);
81                      from < hlink_count-1;
82                      file = file_next, idev = idev_next)
83                 {
84                         file_next = FPTR(hlink_list[from+1]);
85                         idev_next = F_IDEV(file_next);
86                         if (!LINKED(idev, idev_next))
87                                 break;
88                         pool_free(idev_pool, 0, idev);
89                         hl = pool_talloc(hlink_pool, struct hlist, 1,
90                                          "hlink_list");
91                         hl->hlindex = to;
92                         hl->next = hlink_list[++from];
93                         hl->dest_used = 0;
94                         F_HLIST(file) = hl;
95                 }
96                 pool_free(idev_pool, 0, idev);
97                 if (from > start) {
98                         int head = hlink_list[start];
99                         hl = pool_talloc(hlink_pool, struct hlist, 1,
100                                          "hlink_list");
101                         FPTR(head)->flags |= FLAG_HLINK_FIRST;
102                         hl->hlindex = to;
103                         hl->next = head;
104                         hl->dest_used = 0;
105                         hlink_list[to++] = head;
106                         file->flags |= FLAG_HLINK_LAST;
107                         F_HLIST(file) = hl;
108                 } else
109                         file->flags &= ~FLAG_HLINK_INFO;
110         }
111
112         if (!to) {
113                 free(hlink_list);
114                 hlink_list = NULL;
115                 pool_destroy(hlink_pool);
116                 hlink_pool = NULL;
117         } else {
118                 hlink_count = to;
119                 hlink_list = realloc_array(hlink_list, int32, hlink_count);
120                 if (!hlink_list)
121                         out_of_memory("init_hard_links");
122         }
123         the_file_list->hlink_pool = hlink_pool;
124         pool_destroy(idev_pool);
125 }
126 #endif
127
128 void init_hard_links(void)
129 {
130 #ifdef SUPPORT_HARD_LINKS
131         int i;
132
133         if (hlink_list)
134                 free(hlink_list);
135
136         if (!(hlink_list = new_array(int32, the_file_list->count)))
137                 out_of_memory("init_hard_links");
138
139         hlink_count = 0;
140         for (i = 0; i < the_file_list->count; i++) {
141                 if (IS_HLINKED(FPTR(i)))
142                         hlink_list[hlink_count++] = i;
143         }
144
145         qsort(hlink_list, hlink_count,
146             sizeof hlink_list[0], (int (*)()) hlink_compare);
147
148         if (!hlink_count) {
149                 free(hlink_list);
150                 hlink_list = NULL;
151         } else
152                 link_idev_data();
153 #endif
154 }
155
156 #ifdef SUPPORT_HARD_LINKS
157 static int maybe_hard_link(struct file_struct *file, int ndx,
158                            char *fname, int statret, STRUCT_STAT *st,
159                            char *toname, STRUCT_STAT *to_st,
160                            int itemizing, enum logcode code)
161 {
162         if (statret == 0) {
163                 if (st->st_dev == to_st->st_dev
164                  && st->st_ino == to_st->st_ino) {
165                         if (itemizing) {
166                                 itemize(file, ndx, statret, st,
167                                         ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
168                                         0, "");
169                         }
170                         return 0;
171                 }
172                 if (make_backups) {
173                         if (!make_backup(fname))
174                                 return -1;
175                 } else if (robust_unlink(fname)) {
176                         rsyserr(FERROR, errno, "unlink %s failed",
177                                 full_fname(fname));
178                         return -1;
179                 }
180         }
181         return hard_link_one(file, ndx, fname, statret, st, toname,
182                              0, itemizing, code);
183 }
184 #endif
185
186 int hard_link_check(struct file_struct *file, int ndx, char *fname,
187                     int statret, STRUCT_STAT *st, int itemizing,
188                     enum logcode code, int skip)
189 {
190 #ifdef SUPPORT_HARD_LINKS
191         int head;
192         struct hlist *hl = F_HLIST(file);
193
194         if (skip && !(file->flags & FLAG_HLINK_LAST))
195                 head = hlink_list[hl->hlindex] = hl->next;
196         else
197                 head = hlink_list[hl->hlindex];
198         if (ndx != head) {
199                 struct file_struct *head_file = FPTR(head);
200                 struct hlist *hf_hl = F_HLIST(head_file);
201                 if (!stdout_format_has_i && verbose > 1) {
202                         rprintf(FINFO, "\"%s\" is a hard link\n",
203                                 f_name(file, NULL));
204                 }
205                 if (hf_hl->hlindex == FINISHED_LINK) {
206                         STRUCT_STAT st2, st3;
207                         char toname[MAXPATHLEN];
208                         int ldu = hf_hl->dest_used;
209                         if (ldu) {
210                                 pathjoin(toname, MAXPATHLEN, basis_dir[ldu-1],
211                                          f_name(head_file, NULL));
212                         } else
213                                 f_name(head_file, toname);
214                         if (link_stat(toname, &st2, 0) < 0) {
215                                 rsyserr(FERROR, errno, "stat %s failed",
216                                         full_fname(toname));
217                                 return -1;
218                         }
219                         if (statret < 0 && basis_dir[0] != NULL) {
220                                 char cmpbuf[MAXPATHLEN];
221                                 int j = 0;
222                                 do {
223                                         pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
224                                         if (link_stat(cmpbuf, &st3, 0) < 0)
225                                                 continue;
226                                         if (link_dest) {
227                                                 if (st2.st_dev != st3.st_dev
228                                                  || st2.st_ino != st3.st_ino)
229                                                         continue;
230                                                 statret = 1;
231                                                 st = &st3;
232                                                 if (verbose < 2 || !stdout_format_has_i) {
233                                                         itemizing = 0;
234                                                         code = FNONE;
235                                                 }
236                                                 break;
237                                         }
238                                         if (!unchanged_file(cmpbuf, file, &st3))
239                                                 continue;
240                                         statret = 1;
241                                         st = &st3;
242                                         if (unchanged_attrs(file, &st3))
243                                                 break;
244                                 } while (basis_dir[++j] != NULL);
245                         }
246                         maybe_hard_link(file, ndx, fname, statret, st,
247                                         toname, &st2, itemizing, code);
248                         if (remove_source_files == 1 && do_xfers)
249                                 send_msg_int(MSG_SUCCESS, ndx);
250                         hl->hlindex = FINISHED_LINK;
251                 } else
252                         hl->hlindex = SKIPPED_LINK;
253                 return 1;
254         }
255 #endif
256         return 0;
257 }
258
259 #ifdef SUPPORT_HARD_LINKS
260 int hard_link_one(struct file_struct *file, int ndx, char *fname,
261                   int statret, STRUCT_STAT *st, char *toname, int terse,
262                   int itemizing, enum logcode code)
263 {
264         if (do_link(toname, fname)) {
265                 if (terse) {
266                         if (!verbose)
267                                 return -1;
268                         code = FINFO;
269                 } else
270                         code = FERROR;
271                 rsyserr(code, errno, "link %s => %s failed",
272                         full_fname(fname), toname);
273                 return -1;
274         }
275
276         if (itemizing) {
277                 itemize(file, ndx, statret, st,
278                         ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0,
279                         terse ? "" : toname);
280         }
281         if (code != FNONE && verbose && !terse)
282                 rprintf(code, "%s => %s\n", fname, toname);
283         return 0;
284 }
285 #endif
286
287
288 void hard_link_cluster(struct file_struct *file, int master, int itemizing,
289                        enum logcode code, int dest_used)
290 {
291 #ifdef SUPPORT_HARD_LINKS
292         char hlink1[MAXPATHLEN];
293         char *hlink2;
294         STRUCT_STAT st1, st2;
295         int statret, ndx = master;
296         struct hlist *hl = F_HLIST(file);
297
298         hl->hlindex = FINISHED_LINK;
299         if (dry_run)
300                 hl->dest_used = dest_used + 1;
301         if (link_stat(f_name(file, hlink1), &st1, 0) < 0)
302                 return;
303         if (!(file->flags & FLAG_HLINK_FIRST)) {
304                 while (!(file->flags & FLAG_HLINK_LAST)) {
305                         ndx = hl->next;
306                         file = FPTR(ndx);
307                         hl = F_HLIST(file);
308                 }
309         }
310         do {
311                 ndx = hl->next;
312                 file = FPTR(ndx);
313                 hl = F_HLIST(file);
314                 if (hl->hlindex != SKIPPED_LINK)
315                         continue;
316                 hlink2 = f_name(file, NULL);
317                 statret = link_stat(hlink2, &st2, 0);
318                 maybe_hard_link(file, ndx, hlink2, statret, &st2,
319                                 hlink1, &st1, itemizing, code);
320                 if (remove_source_files == 1 && do_xfers)
321                         send_msg_int(MSG_SUCCESS, ndx);
322                 hl->hlindex = FINISHED_LINK;
323         } while (!(file->flags & FLAG_HLINK_LAST));
324 #endif
325 }