Moved the inode & dev items out of the flist_struct. Based on a
[rsync/rsync.git] / hlink.c
CommitLineData
6aae748e 1/*
dc5ddbcc
AT
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
6e69cff1 4 Copyright (C) 2002 by Martin Pool <mbp@samba.org>
6aae748e 5
dc5ddbcc
AT
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.
6aae748e 10
dc5ddbcc
AT
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.
6aae748e 15
dc5ddbcc
AT
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
dc5ddbcc 23extern int dry_run;
9a52223b 24extern int verbose;
dc5ddbcc
AT
25
26#if SUPPORT_HARD_LINKS
11dc2740 27static int hlink_compare(struct file_struct **file1, struct file_struct **file2)
dc5ddbcc 28{
11dc2740
S
29 struct file_struct *f1 = *file1;
30 struct file_struct *f2 = *file2;
31
6e69cff1
MP
32 if (f1->dev != f2->dev)
33 return (int) (f1->dev > f2->dev ? 1 : -1);
dc5ddbcc 34
6e69cff1
MP
35 if (f1->inode != f2->inode)
36 return (int) (f1->inode > f2->inode ? 1 : -1);
dc5ddbcc 37
11dc2740
S
38 return file_compare(file1, file2);
39}
dc5ddbcc 40
11dc2740 41static struct file_struct **hlink_list;
3a6a366f 42static int hlink_count;
dc5ddbcc
AT
43#endif
44
45void init_hard_links(struct file_list *flist)
46{
47#if SUPPORT_HARD_LINKS
dd04a034 48 int i;
11dc2740 49
6e69cff1
MP
50 if (flist->count < 2)
51 return;
dc5ddbcc 52
6e69cff1
MP
53 if (hlink_list)
54 free(hlink_list);
55
11dc2740 56 if (!(hlink_list = new_array(struct file_struct *, flist->count)))
dd04a034 57 out_of_memory("init_hard_links");
6aae748e 58
11dc2740
S
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++) {
c4b4df4f 65 if (flist->files[i]->flags & HAS_INODE_DATA)
11dc2740
S
66 hlink_list[hlink_count++] = flist->files[i];
67 }
dc5ddbcc 68
11dc2740 69 qsort(hlink_list, hlink_count,
6e69cff1 70 sizeof(hlink_list[0]), (int (*)()) hlink_compare);
dc5ddbcc 71
6aae748e
WD
72 if (!hlink_count) {
73 free(hlink_list);
74 hlink_list = NULL;
75 } else if (!(hlink_list = realloc_array(hlink_list,
11dc2740
S
76 struct file_struct *, hlink_count)))
77 out_of_memory("init_hard_links");
dc5ddbcc
AT
78#endif
79}
80
81/* check if a file should be skipped because it is the same as an
82 earlier hard link */
83int check_hard_link(struct file_struct *file)
84{
85#if SUPPORT_HARD_LINKS
6e69cff1
MP
86 int low = 0, high = hlink_count - 1;
87 int ret = 0;
88
c4b4df4f 89 if (!hlink_list || !(file->flags & HAS_INODE_DATA))
6e69cff1
MP
90 return 0;
91
92 while (low != high) {
93 int mid = (low + high) / 2;
11dc2740 94 ret = hlink_compare(&hlink_list[mid], &file);
6e69cff1
MP
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
527a51ce
MP
105 /* XXX: To me this looks kind of dodgy -- why do we use [low]
106 * here and [low-1] below? -- mbp */
11dc2740 107 if (hlink_compare(&hlink_list[low], &file) != 0)
6e69cff1
MP
108 return 0;
109
110 if (low > 0 &&
11dc2740
S
111 file->dev == hlink_list[low - 1]->dev &&
112 file->inode == hlink_list[low - 1]->inode) {
4f2dcb17
MP
113 if (verbose >= 2) {
114 rprintf(FINFO, "check_hard_link: \"%s\" is a hard link to file %d, \"%s\"\n",
11dc2740 115 f_name(file), low-1, f_name(hlink_list[low-1]));
4f2dcb17 116 }
6e69cff1 117 return 1;
4f2dcb17 118 }
dc5ddbcc
AT
119#endif
120
6e69cff1 121 return 0;
dc5ddbcc
AT
122}
123
124
a16bbc39
AT
125#if SUPPORT_HARD_LINKS
126static void hard_link_one(int i)
127{
6e69cff1 128 STRUCT_STAT st1, st2;
11dc2740 129 char *hlink2, *hlink1 = f_name(hlink_list[i - 1]);
a16bbc39 130
310c9f30 131 if (link_stat(hlink1, &st1) != 0)
6e69cff1 132 return;
a16bbc39 133
11dc2740 134 hlink2 = f_name(hlink_list[i]);
310c9f30
WD
135 if (link_stat(hlink2, &st2) != 0) {
136 if (do_link(hlink1, hlink2)) {
137 if (verbose > 0) {
6e69cff1 138 rprintf(FINFO, "link %s => %s : %s\n",
310c9f30
WD
139 hlink2, hlink1, strerror(errno));
140 }
a16bbc39
AT
141 return;
142 }
143 } else {
6e69cff1
MP
144 if (st2.st_dev == st1.st_dev && st2.st_ino == st1.st_ino)
145 return;
146
310c9f30
WD
147 if (robust_unlink(hlink2) || do_link(hlink1, hlink2)) {
148 if (verbose > 0) {
6e69cff1 149 rprintf(FINFO, "link %s => %s : %s\n",
310c9f30
WD
150 hlink2, hlink1, strerror(errno));
151 }
a16bbc39
AT
152 return;
153 }
154 }
155 if (verbose > 0)
310c9f30 156 rprintf(FINFO, "%s => %s\n", hlink2, hlink1);
a16bbc39
AT
157}
158#endif
159
fae5bb31
MP
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 **/
166void do_hard_links(void)
dc5ddbcc
AT
167{
168#if SUPPORT_HARD_LINKS
a16bbc39 169 int i;
a16bbc39 170
6e69cff1
MP
171 if (!hlink_list)
172 return;
173
174 for (i = 1; i < hlink_count; i++) {
6aae748e 175 if (hlink_list[i]->basename && hlink_list[i - 1]->basename &&
11dc2740
S
176 hlink_list[i]->dev == hlink_list[i - 1]->dev &&
177 hlink_list[i]->inode == hlink_list[i - 1]->inode) {
a16bbc39 178 hard_link_one(i);
6e69cff1 179 }
dc5ddbcc 180 }
dc5ddbcc
AT
181#endif
182}