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