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