Die if we overflowed the args[] array when building up the remote
[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
26 #if SUPPORT_HARD_LINKS
27 static int hlink_compare(struct file_struct **file1, struct file_struct **file2)
28 {
29         struct file_struct *f1 = *file1;
30         struct file_struct *f2 = *file2;
31
32         if (f1->F_DEV != f2->F_DEV)
33                 return (int) (f1->F_DEV > f2->F_DEV ? 1 : -1);
34
35         if (f1->F_INODE != f2->F_INODE)
36                 return (int) (f1->F_INODE > f2->F_INODE ? 1 : -1);
37
38         return file_compare(file1, file2);
39 }
40
41 struct file_struct **hlink_list;
42 int hlink_count;
43
44 #define LINKED(p1,p2) ((p1)->F_DEV == (p2)->F_DEV \
45                     && (p1)->F_INODE == (p2)->F_INODE)
46
47 /* Analyze the data in the hlink_list[], remove items that aren't multiply
48  * linked, and replace the dev+inode data with the hlindex+next linked list. */
49 static void link_idev_data(void)
50 {
51         struct file_struct *head;
52         int from, to, start;
53
54         for (from = to = 0; from < hlink_count; from++) {
55                 start = from;
56                 head = hlink_list[start];
57                 while (from < hlink_count-1
58                     && LINKED(hlink_list[from], hlink_list[from+1])) {
59                         hlink_list[from]->F_HLINDEX = to;
60                         hlink_list[from]->F_NEXT = hlink_list[from+1];
61                         from++;
62                 }
63                 if (from > start) {
64                         hlink_list[from]->F_HLINDEX = to;
65                         hlink_list[from]->F_NEXT = head;
66                         hlink_list[from]->flags |= FLAG_HLINK_EOL;
67                         hlink_list[to++] = head;
68                 } else {
69                         head->link_u.idev = NULL;
70                 }
71         }
72
73         if (!to) {
74                 free(hlink_list);
75                 hlink_list = NULL;
76         } else {
77                 hlink_count = to;
78                 if (!(hlink_list = realloc_array(hlink_list,
79                     struct file_struct *, hlink_count)))
80                         out_of_memory("init_hard_links");
81         }
82 }
83 #endif
84
85 void init_hard_links(struct file_list *flist)
86 {
87 #if SUPPORT_HARD_LINKS
88         int i;
89
90         if (flist->count < 2)
91                 return;
92
93         if (hlink_list)
94                 free(hlink_list);
95
96         if (!(hlink_list = new_array(struct file_struct *, flist->count)))
97                 out_of_memory("init_hard_links");
98
99         hlink_count = 0;
100         for (i = 0; i < flist->count; i++) {
101                 if (flist->files[i]->link_u.idev)
102                         hlink_list[hlink_count++] = flist->files[i];
103         }
104
105         qsort(hlink_list, hlink_count,
106             sizeof hlink_list[0], (int (*)()) hlink_compare);
107
108         if (!hlink_count) {
109                 free(hlink_list);
110                 hlink_list = NULL;
111         } else
112                 link_idev_data();
113 #endif
114 }
115
116 int hard_link_check(struct file_struct *file, int skip)
117 {
118         if (!file->link_u.links)
119                 return 0;
120         if (skip && !(file->flags & FLAG_HLINK_EOL))
121                 hlink_list[file->F_HLINDEX] = file->F_NEXT;
122         if (hlink_list[file->F_HLINDEX] != file) {
123                 if (verbose > 1) {
124                         rprintf(FINFO, "\"%s\" is a hard link\n",
125                             f_name(file));
126                 }
127                 return 1;
128         }
129         return 0;
130 }
131
132 #if SUPPORT_HARD_LINKS
133 static void hard_link_one(char *hlink1, char *hlink2)
134 {
135         if (do_link(hlink1, hlink2)) {
136                 if (verbose) {
137                         rprintf(FINFO, "link %s => %s failed: %s\n",
138                             hlink2, hlink1, strerror(errno));
139                 }
140         }
141         else if (verbose)
142                 rprintf(FINFO, "%s => %s\n", hlink2, hlink1);
143 }
144 #endif
145
146
147
148 /**
149  * Create any hard links in the global hlink_list.  They were put
150  * there by running init_hard_links on the filelist.
151  **/
152 void do_hard_links(void)
153 {
154 #if SUPPORT_HARD_LINKS
155         struct file_struct *file, *first;
156         char hlink1[MAXPATHLEN];
157         char *hlink2;
158         STRUCT_STAT st1, st2;
159         int i;
160
161         if (!hlink_list)
162                 return;
163
164         for (i = 0; i < hlink_count; i++) {
165                 first = file = hlink_list[i];
166                 if (link_stat(f_name_to(first, hlink1), &st1) != 0)
167                         continue;
168                 while ((file = file->F_NEXT) != first) {
169                         hlink2 = f_name(file);
170                         if (link_stat(hlink2, &st2) == 0) {
171                                 if (st2.st_dev == st1.st_dev
172                                     && st2.st_ino == st1.st_ino)
173                                         continue;
174                                 if (robust_unlink(hlink2)) {
175                                         if (verbose > 0) {
176                                                 rprintf(FINFO,
177                                                     "unlink %s failed: %s\n",
178                                                     full_fname(hlink2), 
179                                                     strerror(errno));
180                                         }
181                                         continue;
182                                 }
183                         }
184                         hard_link_one(hlink1, hlink2);
185                 }
186         }
187 #endif
188 }