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