- If a finished link came from a link-dest dir and --dry-run is in
[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),
71             out_of_memory, POOL_INTERN);
72
73         for (from = to = 0; from < hlink_count; from++) {
74                 start = from;
75                 while (1) {
76                         cur = hlink_list[from];
77                         if (from == hlink_count-1
78                             || !LINKED(cur, hlink_list[from+1]))
79                                 break;
80                         pool_free(idev_pool, 0, FPTR(cur)->link_u.idev);
81                         FPTR(cur)->link_u.links = pool_talloc(hlink_pool,
82                             struct hlink, 1, "hlink_list");
83
84                         FPTR(cur)->F_HLINDEX = to;
85                         FPTR(cur)->F_NEXT = hlink_list[++from];
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                         hlink_list[to++] = head;
98                 } else
99                         FPTR(cur)->link_u.links = NULL;
100         }
101
102         if (!to) {
103                 free(hlink_list);
104                 hlink_list = NULL;
105                 pool_destroy(hlink_pool);
106                 hlink_pool = NULL;
107         } else {
108                 hlink_count = to;
109                 hlink_list = realloc_array(hlink_list, int32, hlink_count);
110                 if (!hlink_list)
111                         out_of_memory("init_hard_links");
112         }
113         the_file_list->hlink_pool = hlink_pool;
114         pool_destroy(idev_pool);
115 }
116 #endif
117
118 void init_hard_links(void)
119 {
120 #ifdef SUPPORT_HARD_LINKS
121         int i;
122
123         if (hlink_list)
124                 free(hlink_list);
125
126         if (!(hlink_list = new_array(int32, the_file_list->count)))
127                 out_of_memory("init_hard_links");
128
129         hlink_count = 0;
130         for (i = 0; i < the_file_list->count; i++) {
131                 if (FPTR(i)->link_u.idev)
132                         hlink_list[hlink_count++] = i;
133         }
134
135         qsort(hlink_list, hlink_count,
136             sizeof hlink_list[0], (int (*)()) hlink_compare);
137
138         if (!hlink_count) {
139                 free(hlink_list);
140                 hlink_list = NULL;
141         } else
142                 link_idev_data();
143 #endif
144 }
145
146 #ifdef SUPPORT_HARD_LINKS
147 static int maybe_hard_link(struct file_struct *file, int ndx,
148                            char *fname, int statret, STRUCT_STAT *st,
149                            char *toname, STRUCT_STAT *to_st,
150                            int itemizing, enum logcode code)
151 {
152         if (statret == 0) {
153                 if (st->st_dev == to_st->st_dev
154                  && st->st_ino == to_st->st_ino) {
155                         if (itemizing) {
156                                 itemize(file, ndx, statret, st,
157                                         ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS,
158                                         0, "");
159                         }
160                         return 0;
161                 }
162                 if (make_backups) {
163                         if (!make_backup(fname))
164                                 return -1;
165                 } else if (robust_unlink(fname)) {
166                         rsyserr(FERROR, errno, "unlink %s failed",
167                                 full_fname(fname));
168                         return -1;
169                 }
170         }
171         return hard_link_one(file, ndx, fname, statret, st, toname,
172                              0, itemizing, code);
173 }
174 #endif
175
176 int hard_link_check(struct file_struct *file, int ndx, char *fname,
177                     int statret, STRUCT_STAT *st, int itemizing,
178                     enum logcode code, int skip)
179 {
180 #ifdef SUPPORT_HARD_LINKS
181         int head;
182         if (skip && !(file->flags & FLAG_HLINK_EOL))
183                 head = hlink_list[file->F_HLINDEX] = file->F_NEXT;
184         else
185                 head = hlink_list[file->F_HLINDEX];
186         if (ndx != head) {
187                 struct file_struct *head_file = FPTR(head);
188                 if (!stdout_format_has_i && verbose > 1) {
189                         rprintf(FINFO, "\"%s\" is a hard link\n",
190                                 f_name(file, NULL));
191                 }
192                 if (head_file->F_HLINDEX == FINISHED_LINK) {
193                         STRUCT_STAT st2, st3;
194                         char toname[MAXPATHLEN];
195                         int ldu = head_file->link_u.links->link_dest_used;
196                         if (ldu) {
197                                 pathjoin(toname, MAXPATHLEN, basis_dir[ldu-1],
198                                          f_name(head_file, NULL));
199                         } else
200                                 f_name(head_file, toname);
201                         if (link_stat(toname, &st2, 0) < 0) {
202                                 rsyserr(FERROR, errno, "stat %s failed",
203                                         full_fname(toname));
204                                 return -1;
205                         }
206                         if (statret < 0 && basis_dir[0] != NULL) {
207                                 char cmpbuf[MAXPATHLEN];
208                                 int j = 0;
209                                 do {
210                                         pathjoin(cmpbuf, MAXPATHLEN, basis_dir[j], fname);
211                                         if (link_stat(cmpbuf, &st3, 0) < 0)
212                                                 continue;
213                                         if (link_dest) {
214                                                 if (st2.st_dev != st3.st_dev
215                                                  || st2.st_ino != st3.st_ino)
216                                                         continue;
217                                                 statret = 1;
218                                                 st = &st3;
219                                                 if (verbose < 2 || !stdout_format_has_i)
220                                                         itemizing = code = 0;
221                                                 break;
222                                         }
223                                         if (!unchanged_file(cmpbuf, file, &st3))
224                                                 continue;
225                                         statret = 1;
226                                         st = &st3;
227                                         if (unchanged_attrs(file, &st3))
228                                                 break;
229                                 } while (basis_dir[++j] != NULL);
230                         }
231                         maybe_hard_link(file, ndx, fname, statret, st,
232                                         toname, &st2, itemizing, code);
233                         if (remove_source_files == 1 && do_xfers) {
234                                 char numbuf[4];
235                                 SIVAL(numbuf, 0, ndx);
236                                 send_msg(MSG_SUCCESS, numbuf, 4);
237                         }
238                         file->F_HLINDEX = FINISHED_LINK;
239                 } else
240                         file->F_HLINDEX = SKIPPED_LINK;
241                 return 1;
242         }
243 #endif
244         return 0;
245 }
246
247 #ifdef SUPPORT_HARD_LINKS
248 int hard_link_one(struct file_struct *file, int ndx, char *fname,
249                   int statret, STRUCT_STAT *st, char *toname, int terse,
250                   int itemizing, enum logcode code)
251 {
252         if (do_link(toname, fname)) {
253                 if (terse) {
254                         if (!verbose)
255                                 return -1;
256                         code = FINFO;
257                 } else
258                         code = FERROR;
259                 rsyserr(code, errno, "link %s => %s failed",
260                         full_fname(fname), toname);
261                 return -1;
262         }
263
264         if (itemizing) {
265                 itemize(file, ndx, statret, st,
266                         ITEM_LOCAL_CHANGE | ITEM_XNAME_FOLLOWS, 0,
267                         terse ? "" : toname);
268         }
269         if (code && verbose && !terse)
270                 rprintf(code, "%s => %s\n", fname, toname);
271         return 0;
272 }
273 #endif
274
275
276 void hard_link_cluster(struct file_struct *file, int master, int itemizing,
277                        enum logcode code)
278 {
279 #ifdef SUPPORT_HARD_LINKS
280         char hlink1[MAXPATHLEN];
281         char *hlink2;
282         STRUCT_STAT st1, st2;
283         int statret, ndx = master;
284
285         file->F_HLINDEX = FINISHED_LINK;
286         if (link_stat(f_name(file, hlink1), &st1, 0) < 0)
287                 return;
288         if (!(file->flags & FLAG_HLINK_TOL)) {
289                 while (!(file->flags & FLAG_HLINK_EOL)) {
290                         ndx = file->F_NEXT;
291                         file = FPTR(ndx);
292                 }
293         }
294         do {
295                 ndx = file->F_NEXT;
296                 file = FPTR(ndx);
297                 if (file->F_HLINDEX != SKIPPED_LINK)
298                         continue;
299                 hlink2 = f_name(file, NULL);
300                 statret = link_stat(hlink2, &st2, 0);
301                 maybe_hard_link(file, ndx, hlink2, statret, &st2,
302                                 hlink1, &st1, itemizing, code);
303                 if (remove_source_files == 1 && do_xfers) {
304                         char numbuf[4];
305                         SIVAL(numbuf, 0, ndx);
306                         send_msg(MSG_SUCCESS, numbuf, 4);
307                 }
308                 file->F_HLINDEX = FINISHED_LINK;
309         } while (!(file->flags & FLAG_HLINK_EOL));
310 #endif
311 }