Use the new dev+inode union in the flist_struct.
[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 static struct file_struct **hlink_list;
42 static int hlink_count;
43 #endif
44
45 void init_hard_links(struct file_list *flist)
46 {
47 #if SUPPORT_HARD_LINKS
48         int i;
49
50         if (flist->count < 2)
51                 return;
52
53         if (hlink_list)
54                 free(hlink_list);
55
56         if (!(hlink_list = new_array(struct file_struct *, flist->count)))
57                 out_of_memory("init_hard_links");
58
59 /*      we'll want to restore the memcpy when we purge the
60  *      hlink list after the sort.
61  *      memcpy(hlink_list, flist->files, sizeof(hlink_list[0]) * flist->count); 
62  */
63         hlink_count = 0;
64         for (i = 0; i < flist->count; i++) {
65                 if (flist->files[i]->link_u.idev)
66                         hlink_list[hlink_count++] = flist->files[i];
67         }
68
69         qsort(hlink_list, hlink_count,
70               sizeof(hlink_list[0]), (int (*)()) hlink_compare);
71
72         if (!hlink_count) {
73                 free(hlink_list);
74                 hlink_list = NULL;
75         } else if (!(hlink_list = realloc_array(hlink_list,
76                                         struct file_struct *, hlink_count)))
77                 out_of_memory("init_hard_links");
78 #endif
79 }
80
81 /* check if a file should be skipped because it is the same as an
82    earlier hard link */
83 int check_hard_link(struct file_struct *file)
84 {
85 #if SUPPORT_HARD_LINKS
86         int low = 0, high = hlink_count - 1;
87         int ret = 0;
88
89         if (!hlink_list || !file->link_u.idev)
90                 return 0;
91
92         while (low != high) {
93                 int mid = (low + high) / 2;
94                 ret = hlink_compare(&hlink_list[mid], &file);
95                 if (ret == 0) {
96                         low = mid;
97                         break;
98                 }
99                 if (ret > 0)
100                         high = mid;
101                 else
102                         low = mid + 1;
103         }
104
105         /* XXX: To me this looks kind of dodgy -- why do we use [low]
106          * here and [low-1] below? -- mbp */
107         if (hlink_compare(&hlink_list[low], &file) != 0)
108                 return 0;
109
110         if (low > 0 &&
111             file->F_DEV == hlink_list[low - 1]->F_DEV &&
112             file->F_INODE == hlink_list[low - 1]->F_INODE) {
113                 if (verbose >= 2) {
114                         rprintf(FINFO, "check_hard_link: \"%s\" is a hard link to file %d, \"%s\"\n",
115                                 f_name(file), low-1, f_name(hlink_list[low-1]));
116                 }
117                 return 1;
118         }
119 #endif
120
121         return 0;
122 }
123
124
125 #if SUPPORT_HARD_LINKS
126 static void hard_link_one(int i)
127 {
128         STRUCT_STAT st1, st2;
129         char *hlink2, *hlink1 = f_name(hlink_list[i - 1]);
130
131         if (link_stat(hlink1, &st1) != 0)
132                 return;
133
134         hlink2 = f_name(hlink_list[i]);
135         if (link_stat(hlink2, &st2) != 0) {
136                 if (do_link(hlink1, hlink2)) {
137                         if (verbose > 0) {
138                                 rprintf(FINFO, "link %s => %s : %s\n",
139                                         hlink2, hlink1, strerror(errno));
140                         }
141                         return;
142                 }
143         } else {
144                 if (st2.st_dev == st1.st_dev && st2.st_ino == st1.st_ino)
145                         return;
146
147                 if (robust_unlink(hlink2) || do_link(hlink1, hlink2)) {
148                         if (verbose > 0) {
149                                 rprintf(FINFO, "link %s => %s : %s\n",
150                                         hlink2, hlink1, strerror(errno));
151                         }
152                         return;
153                 }
154         }
155         if (verbose > 0)
156                 rprintf(FINFO, "%s => %s\n", hlink2, hlink1);
157 }
158 #endif
159
160
161
162 /**
163  * Create any hard links in the global hlink_list.  They were put
164  * there by running init_hard_links on the filelist.
165  **/
166 void do_hard_links(void)
167 {
168 #if SUPPORT_HARD_LINKS
169         int i;
170
171         if (!hlink_list)
172                 return;
173
174         for (i = 1; i < hlink_count; i++) {
175                 if (hlink_list[i]->basename && hlink_list[i - 1]->basename &&
176                     hlink_list[i]->F_DEV == hlink_list[i - 1]->F_DEV &&
177                     hlink_list[i]->F_INODE == hlink_list[i - 1]->F_INODE) {
178                         hard_link_one(i);
179                 }
180         }
181 #endif
182 }