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