Commit | Line | Data |
---|---|---|
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 | 23 | extern int dry_run; |
9a52223b | 24 | extern int verbose; |
dc5ddbcc AT |
25 | |
26 | #if SUPPORT_HARD_LINKS | |
11dc2740 | 27 | static 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 | ||
92cc9dd7 WD |
32 | if (f1->F_DEV != f2->F_DEV) |
33 | return (int) (f1->F_DEV > f2->F_DEV ? 1 : -1); | |
dc5ddbcc | 34 | |
92cc9dd7 WD |
35 | if (f1->F_INODE != f2->F_INODE) |
36 | return (int) (f1->F_INODE > f2->F_INODE ? 1 : -1); | |
dc5ddbcc | 37 | |
11dc2740 S |
38 | return file_compare(file1, file2); |
39 | } | |
dc5ddbcc | 40 | |
11dc2740 | 41 | static struct file_struct **hlink_list; |
3a6a366f | 42 | static int hlink_count; |
dc5ddbcc AT |
43 | #endif |
44 | ||
45 | void 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++) { | |
92cc9dd7 | 65 | if (flist->files[i]->link_u.idev) |
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 */ | |
83 | int 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 | ||
92cc9dd7 | 89 | if (!hlink_list || !file->link_u.idev) |
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 | ||
cb7fb45e | 105 | /* Check if we ended up finding the file struct or not. */ |
11dc2740 | 106 | if (hlink_compare(&hlink_list[low], &file) != 0) |
6e69cff1 MP |
107 | return 0; |
108 | ||
cb7fb45e WD |
109 | /* Now check if the previous item shares the current one's device |
110 | * and inode. If so, we're not the "master", so return 1. */ | |
6e69cff1 | 111 | if (low > 0 && |
92cc9dd7 WD |
112 | file->F_DEV == hlink_list[low - 1]->F_DEV && |
113 | file->F_INODE == hlink_list[low - 1]->F_INODE) { | |
4f2dcb17 MP |
114 | if (verbose >= 2) { |
115 | rprintf(FINFO, "check_hard_link: \"%s\" is a hard link to file %d, \"%s\"\n", | |
11dc2740 | 116 | f_name(file), low-1, f_name(hlink_list[low-1])); |
4f2dcb17 | 117 | } |
6e69cff1 | 118 | return 1; |
4f2dcb17 | 119 | } |
dc5ddbcc AT |
120 | #endif |
121 | ||
6e69cff1 | 122 | return 0; |
dc5ddbcc AT |
123 | } |
124 | ||
125 | ||
a16bbc39 AT |
126 | #if SUPPORT_HARD_LINKS |
127 | static void hard_link_one(int i) | |
128 | { | |
6e69cff1 | 129 | STRUCT_STAT st1, st2; |
11dc2740 | 130 | char *hlink2, *hlink1 = f_name(hlink_list[i - 1]); |
a16bbc39 | 131 | |
310c9f30 | 132 | if (link_stat(hlink1, &st1) != 0) |
6e69cff1 | 133 | return; |
a16bbc39 | 134 | |
11dc2740 | 135 | hlink2 = f_name(hlink_list[i]); |
310c9f30 WD |
136 | if (link_stat(hlink2, &st2) != 0) { |
137 | if (do_link(hlink1, hlink2)) { | |
138 | if (verbose > 0) { | |
6e69cff1 | 139 | rprintf(FINFO, "link %s => %s : %s\n", |
310c9f30 WD |
140 | hlink2, hlink1, strerror(errno)); |
141 | } | |
a16bbc39 AT |
142 | return; |
143 | } | |
144 | } else { | |
6e69cff1 MP |
145 | if (st2.st_dev == st1.st_dev && st2.st_ino == st1.st_ino) |
146 | return; | |
147 | ||
310c9f30 WD |
148 | if (robust_unlink(hlink2) || do_link(hlink1, hlink2)) { |
149 | if (verbose > 0) { | |
6e69cff1 | 150 | rprintf(FINFO, "link %s => %s : %s\n", |
310c9f30 WD |
151 | hlink2, hlink1, strerror(errno)); |
152 | } | |
a16bbc39 AT |
153 | return; |
154 | } | |
155 | } | |
156 | if (verbose > 0) | |
310c9f30 | 157 | rprintf(FINFO, "%s => %s\n", hlink2, hlink1); |
a16bbc39 AT |
158 | } |
159 | #endif | |
160 | ||
fae5bb31 MP |
161 | |
162 | ||
163 | /** | |
164 | * Create any hard links in the global hlink_list. They were put | |
165 | * there by running init_hard_links on the filelist. | |
166 | **/ | |
167 | void do_hard_links(void) | |
dc5ddbcc AT |
168 | { |
169 | #if SUPPORT_HARD_LINKS | |
a16bbc39 | 170 | int i; |
a16bbc39 | 171 | |
6e69cff1 MP |
172 | if (!hlink_list) |
173 | return; | |
174 | ||
175 | for (i = 1; i < hlink_count; i++) { | |
6aae748e | 176 | if (hlink_list[i]->basename && hlink_list[i - 1]->basename && |
92cc9dd7 WD |
177 | hlink_list[i]->F_DEV == hlink_list[i - 1]->F_DEV && |
178 | hlink_list[i]->F_INODE == hlink_list[i - 1]->F_INODE) { | |
a16bbc39 | 179 | hard_link_one(i); |
6e69cff1 | 180 | } |
dc5ddbcc | 181 | } |
dc5ddbcc AT |
182 | #endif |
183 | } |