Commit | Line | Data |
---|---|---|
dbda5fbf | 1 | /* |
c627d613 AT |
2 | Copyright (C) Andrew Tridgell 1996 |
3 | Copyright (C) Paul Mackerras 1996 | |
736a6a29 | 4 | Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org> |
dbda5fbf | 5 | |
c627d613 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. | |
dbda5fbf | 10 | |
c627d613 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. | |
dbda5fbf | 15 | |
c627d613 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 | ||
736a6a29 MP |
21 | /** @file flist.c |
22 | * Generate and receive file lists | |
23 | * | |
172875cf MP |
24 | * @sa http://lists.samba.org/pipermail/rsync/2000-June/002351.html |
25 | * | |
736a6a29 | 26 | **/ |
c627d613 AT |
27 | |
28 | #include "rsync.h" | |
29 | ||
a800434a AT |
30 | extern struct stats stats; |
31 | ||
c627d613 | 32 | extern int verbose; |
1bbd10fe | 33 | extern int do_progress; |
f05f993e | 34 | extern int am_root; |
c627d613 | 35 | extern int am_server; |
a8726d2a | 36 | extern int am_daemon; |
56194bcd | 37 | extern int am_sender; |
649f8742 | 38 | extern int delete_during; |
c627d613 | 39 | extern int always_checksum; |
983b1ed3 WD |
40 | extern int module_id; |
41 | extern int ignore_errors; | |
4836c3ee | 42 | extern int numeric_ids; |
c627d613 | 43 | |
a06d19e3 | 44 | extern int recurse; |
7e037c42 | 45 | extern int xfer_dirs; |
808c57c3 | 46 | extern char curr_dir[MAXPATHLEN]; |
c7d970f7 | 47 | extern unsigned int curr_dir_len; |
649f8742 WD |
48 | extern char *backup_dir; |
49 | extern char *backup_suffix; | |
24d0fcde | 50 | extern int filesfrom_fd; |
a06d19e3 | 51 | |
c627d613 | 52 | extern int one_file_system; |
314f4591 | 53 | extern int keep_dirlinks; |
c627d613 | 54 | extern int preserve_links; |
dc5ddbcc | 55 | extern int preserve_hard_links; |
c627d613 AT |
56 | extern int preserve_perms; |
57 | extern int preserve_devices; | |
58 | extern int preserve_uid; | |
59 | extern int preserve_gid; | |
6574b4f7 | 60 | extern int relative_paths; |
24d0fcde | 61 | extern int implied_dirs; |
649f8742 WD |
62 | extern int make_backups; |
63 | extern int backup_suffix_len; | |
82306bf6 | 64 | extern int copy_links; |
b5313607 | 65 | extern int copy_unsafe_links; |
d04e9c51 | 66 | extern int protocol_version; |
cb13abfe | 67 | extern int sanitize_paths; |
314f4591 | 68 | extern int delete_excluded; |
649f8742 | 69 | extern int max_delete; |
314f4591 WD |
70 | extern int orig_umask; |
71 | extern int list_only; | |
c627d613 | 72 | |
7842418b WD |
73 | extern struct filter_list_struct filter_list; |
74 | extern struct filter_list_struct server_filter_list; | |
c627d613 | 75 | |
06c28400 WD |
76 | int io_error; |
77 | ||
fea4db62 | 78 | static char empty_sum[MD4_SUM_LENGTH]; |
9935066b | 79 | static unsigned int file_struct_len; |
23f4587f | 80 | static struct file_list *received_flist; |
3d382777 | 81 | |
827c37f6 | 82 | static void clean_flist(struct file_list *flist, int strip_root, int no_dups); |
48ea74bf | 83 | static void output_flist(struct file_list *flist, const char *whose_list); |
0199b05f | 84 | |
61dec11a WD |
85 | void init_flist(void) |
86 | { | |
1f9ae80a | 87 | struct file_struct f; |
61dec11a | 88 | |
1f9ae80a | 89 | /* Figure out how big the file_struct is without trailing padding */ |
7cf8e8d0 | 90 | file_struct_len = offsetof(struct file_struct, flags) + sizeof f.flags; |
61dec11a WD |
91 | } |
92 | ||
93 | ||
1bbd10fe | 94 | static int show_filelist_p(void) |
58225000 | 95 | { |
7e037c42 | 96 | return verbose && xfer_dirs && !am_server; |
1bbd10fe | 97 | } |
ebed4c3a | 98 | |
1bbd10fe DD |
99 | static void start_filelist_progress(char *kind) |
100 | { | |
101 | rprintf(FINFO, "%s ... ", kind); | |
e5ce3bcf | 102 | if (verbose > 1 || do_progress) |
1bbd10fe DD |
103 | rprintf(FINFO, "\n"); |
104 | rflush(FINFO); | |
58225000 MP |
105 | } |
106 | ||
db719fb0 | 107 | |
d27cbec5 | 108 | static void emit_filelist_progress(const struct file_list *flist) |
db719fb0 | 109 | { |
d27cbec5 | 110 | rprintf(FINFO, " %d files...\r", flist->count); |
db719fb0 MP |
111 | } |
112 | ||
113 | ||
d27cbec5 | 114 | static void maybe_emit_filelist_progress(const struct file_list *flist) |
58225000 | 115 | { |
e5ce3bcf | 116 | if (do_progress && show_filelist_p() && (flist->count % 100) == 0) |
d27cbec5 | 117 | emit_filelist_progress(flist); |
58225000 MP |
118 | } |
119 | ||
120 | ||
1bbd10fe | 121 | static void finish_filelist_progress(const struct file_list *flist) |
58225000 | 122 | { |
1bbd10fe DD |
123 | if (do_progress) { |
124 | /* This overwrites the progress line */ | |
c7b562be MP |
125 | rprintf(FINFO, "%d file%sto consider\n", |
126 | flist->count, flist->count == 1 ? " " : "s "); | |
b7736c79 | 127 | } else |
1bbd10fe | 128 | rprintf(FINFO, "done\n"); |
58225000 MP |
129 | } |
130 | ||
86943126 MP |
131 | void show_flist_stats(void) |
132 | { | |
133 | /* Nothing yet */ | |
134 | } | |
135 | ||
136 | ||
f7632fc6 AT |
137 | static void list_file_entry(struct file_struct *f) |
138 | { | |
740819ef | 139 | char perms[11]; |
f7632fc6 | 140 | |
e5ce3bcf | 141 | if (!f->basename) { |
7212be92 DD |
142 | /* this can happen if duplicate names were removed */ |
143 | return; | |
e5ce3bcf | 144 | } |
7212be92 | 145 | |
740819ef MP |
146 | permstring(perms, f->mode); |
147 | ||
0d162bd1 | 148 | #if SUPPORT_LINKS |
f7632fc6 | 149 | if (preserve_links && S_ISLNK(f->mode)) { |
ebed4c3a MP |
150 | rprintf(FINFO, "%s %11.0f %s %s -> %s\n", |
151 | perms, | |
a4a7e64c | 152 | (double)f->length, timestring(f->modtime), |
728d0922 | 153 | f_name(f), f->u.link); |
0d162bd1 WD |
154 | } else |
155 | #endif | |
e5ce3bcf | 156 | { |
ebed4c3a MP |
157 | rprintf(FINFO, "%s %11.0f %s %s\n", |
158 | perms, | |
a4a7e64c | 159 | (double)f->length, timestring(f->modtime), |
ebed4c3a | 160 | f_name(f)); |
e5ce3bcf | 161 | } |
f7632fc6 AT |
162 | } |
163 | ||
164 | ||
bd9e9ecc MP |
165 | /** |
166 | * Stat either a symlink or its referent, depending on the settings of | |
167 | * copy_links, copy_unsafe_links, etc. | |
168 | * | |
4e5db0ad MP |
169 | * @retval -1 on error |
170 | * | |
171 | * @retval 0 for success | |
172 | * | |
173 | * @post If @p path is a symlink, then @p linkbuf (of size @c | |
bd9e9ecc | 174 | * MAXPATHLEN) contains the symlink target. |
4e5db0ad MP |
175 | * |
176 | * @post @p buffer contains information about the link or the | |
177 | * referrent as appropriate, if they exist. | |
bd9e9ecc | 178 | **/ |
23f4587f | 179 | static int readlink_stat(const char *path, STRUCT_STAT *buffer, char *linkbuf) |
b5313607 DD |
180 | { |
181 | #if SUPPORT_LINKS | |
b7736c79 | 182 | if (copy_links) |
6f2623fd | 183 | return do_stat(path, buffer); |
23f4587f | 184 | if (link_stat(path, buffer, 0) < 0) |
b5313607 | 185 | return -1; |
6f2623fd | 186 | if (S_ISLNK(buffer->st_mode)) { |
a4a7e64c | 187 | int l = readlink((char *)path, linkbuf, MAXPATHLEN - 1); |
dbda5fbf | 188 | if (l == -1) |
b5313607 | 189 | return -1; |
6f2623fd | 190 | linkbuf[l] = 0; |
fc638474 DD |
191 | if (copy_unsafe_links && unsafe_symlink(linkbuf, path)) { |
192 | if (verbose > 1) { | |
dbda5fbf | 193 | rprintf(FINFO,"copying unsafe symlink \"%s\" -> \"%s\"\n", |
fc638474 DD |
194 | path, linkbuf); |
195 | } | |
6f2623fd | 196 | return do_stat(path, buffer); |
b5313607 DD |
197 | } |
198 | } | |
199 | return 0; | |
200 | #else | |
6f2623fd | 201 | return do_stat(path, buffer); |
b5313607 DD |
202 | #endif |
203 | } | |
204 | ||
314f4591 | 205 | int link_stat(const char *path, STRUCT_STAT *buffer, int follow_dirlinks) |
82306bf6 AT |
206 | { |
207 | #if SUPPORT_LINKS | |
b7736c79 | 208 | if (copy_links) |
6f2623fd | 209 | return do_stat(path, buffer); |
314f4591 WD |
210 | if (do_lstat(path, buffer) < 0) |
211 | return -1; | |
212 | if (follow_dirlinks && S_ISLNK(buffer->st_mode)) { | |
213 | STRUCT_STAT st; | |
214 | if (do_stat(path, &st) == 0 && S_ISDIR(st.st_mode)) | |
215 | *buffer = st; | |
216 | } | |
217 | return 0; | |
82306bf6 | 218 | #else |
6f2623fd | 219 | return do_stat(path, buffer); |
82306bf6 AT |
220 | #endif |
221 | } | |
222 | ||
7842418b | 223 | /* This function is used to check if a file should be included/excluded |
429f9828 | 224 | * from the list of files based on its name and type etc. The value of |
7842418b WD |
225 | * filter_level is set to either SERVER_FILTERS or ALL_FILTERS. */ |
226 | static int is_excluded(char *fname, int is_dir, int filter_level) | |
c627d613 | 227 | { |
7d687932 | 228 | #if 0 /* This currently never happens, so avoid a useless compare. */ |
7842418b | 229 | if (filter_level == NO_FILTERS) |
429f9828 WD |
230 | return 0; |
231 | #endif | |
6931c138 | 232 | if (fname) { |
429f9828 | 233 | /* never exclude '.', even if somebody does --exclude '*' */ |
6931c138 WD |
234 | if (fname[0] == '.' && !fname[1]) |
235 | return 0; | |
236 | /* Handle the -R version of the '.' dir. */ | |
237 | if (fname[0] == '/') { | |
238 | int len = strlen(fname); | |
239 | if (fname[len-1] == '.' && fname[len-2] == '/') | |
240 | return 0; | |
241 | } | |
76e26e10 | 242 | } |
7842418b WD |
243 | if (server_filter_list.head |
244 | && check_filter(&server_filter_list, fname, is_dir) < 0) | |
429f9828 | 245 | return 1; |
7842418b | 246 | if (filter_level != ALL_FILTERS) |
429f9828 | 247 | return 0; |
7842418b WD |
248 | if (filter_list.head |
249 | && check_filter(&filter_list, fname, is_dir) < 0) | |
76e26e10 | 250 | return 1; |
76e26e10 | 251 | return 0; |
c627d613 AT |
252 | } |
253 | ||
254 | /* used by the one_file_system code */ | |
255 | static dev_t filesystem_dev; | |
256 | ||
257 | static void set_filesystem(char *fname) | |
258 | { | |
ebed4c3a | 259 | STRUCT_STAT st; |
ea76e761 | 260 | if (do_stat(fname, &st) != 0) |
ebed4c3a MP |
261 | return; |
262 | filesystem_dev = st.st_dev; | |
c627d613 AT |
263 | } |
264 | ||
265 | ||
b280a1f4 AT |
266 | static int to_wire_mode(mode_t mode) |
267 | { | |
0d162bd1 | 268 | #if SUPPORT_LINKS |
b7736c79 | 269 | if (S_ISLNK(mode) && (_S_IFLNK != 0120000)) |
b280a1f4 | 270 | return (mode & ~(_S_IFMT)) | 0120000; |
0d162bd1 | 271 | #endif |
a4a7e64c | 272 | return (int)mode; |
b280a1f4 AT |
273 | } |
274 | ||
275 | static mode_t from_wire_mode(int mode) | |
276 | { | |
b7736c79 | 277 | if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000)) |
efe3037c | 278 | return (mode & ~(_S_IFMT)) | _S_IFLNK; |
a4a7e64c | 279 | return (mode_t)mode; |
b280a1f4 AT |
280 | } |
281 | ||
282 | ||
ebed4c3a | 283 | static void send_directory(int f, struct file_list *flist, char *dir); |
c627d613 | 284 | |
3a6a366f | 285 | static char *flist_dir; |
882e6893 | 286 | static int flist_dir_len; |
c627d613 | 287 | |
3ec4dd97 | 288 | |
d9d6bc52 MP |
289 | /** |
290 | * Make sure @p flist is big enough to hold at least @p flist->count | |
291 | * entries. | |
292 | **/ | |
a85906c7 | 293 | void flist_expand(struct file_list *flist) |
d9d6bc52 | 294 | { |
0501f363 | 295 | struct file_struct **new_ptr; |
2e7d1994 | 296 | |
a85906c7 S |
297 | if (flist->count < flist->malloced) |
298 | return; | |
dbda5fbf | 299 | |
a85906c7 S |
300 | if (flist->malloced < FLIST_START) |
301 | flist->malloced = FLIST_START; | |
302 | else if (flist->malloced >= FLIST_LINEAR) | |
303 | flist->malloced += FLIST_LINEAR; | |
304 | else | |
305 | flist->malloced *= 2; | |
306 | ||
307 | /* | |
308 | * In case count jumped or we are starting the list | |
309 | * with a known size just set it. | |
310 | */ | |
311 | if (flist->malloced < flist->count) | |
312 | flist->malloced = flist->count; | |
313 | ||
0501f363 WD |
314 | new_ptr = realloc_array(flist->files, struct file_struct *, |
315 | flist->malloced); | |
2e7d1994 | 316 | |
8c483820 | 317 | if (verbose >= 2 && flist->malloced != FLIST_START) { |
a85906c7 S |
318 | rprintf(FINFO, "[%s] expand file_list to %.0f bytes, did%s move\n", |
319 | who_am_i(), | |
a4a7e64c | 320 | (double)sizeof flist->files[0] * flist->malloced, |
a85906c7 | 321 | (new_ptr == flist->files) ? " not" : ""); |
d9d6bc52 | 322 | } |
a85906c7 | 323 | |
0501f363 | 324 | flist->files = new_ptr; |
a85906c7 S |
325 | |
326 | if (!flist->files) | |
327 | out_of_memory("flist_expand"); | |
d9d6bc52 MP |
328 | } |
329 | ||
7b1a0c19 | 330 | void send_file_entry(struct file_struct *file, int f, unsigned short base_flags) |
c627d613 | 331 | { |
1ef00d20 | 332 | unsigned short flags; |
5911fee5 WD |
333 | static time_t modtime; |
334 | static mode_t mode; | |
1490812a | 335 | static int64 dev; |
4124540d | 336 | static dev_t rdev; |
9c5e91f8 | 337 | static uint32 rdev_major; |
5911fee5 WD |
338 | static uid_t uid; |
339 | static gid_t gid; | |
340 | static char lastname[MAXPATHLEN]; | |
f376e674 | 341 | char fname[MAXPATHLEN]; |
ebed4c3a | 342 | int l1, l2; |
72914a60 | 343 | |
ebed4c3a MP |
344 | if (f == -1) |
345 | return; | |
72914a60 AT |
346 | |
347 | if (!file) { | |
ebed4c3a | 348 | write_byte(f, 0); |
5911fee5 | 349 | modtime = 0, mode = 0; |
4124540d | 350 | dev = 0, rdev = makedev(0, 0); |
9c5e91f8 | 351 | rdev_major = 0; |
5911fee5 WD |
352 | uid = 0, gid = 0; |
353 | *lastname = '\0'; | |
72914a60 AT |
354 | return; |
355 | } | |
356 | ||
eca2adb4 MP |
357 | io_write_phase = "send_file_entry"; |
358 | ||
f376e674 | 359 | f_name_to(file, fname); |
72914a60 AT |
360 | |
361 | flags = base_flags; | |
362 | ||
30f337c9 | 363 | if (file->mode == mode) |
d01d15e0 | 364 | flags |= XMIT_SAME_MODE; |
1ef00d20 | 365 | else |
30f337c9 | 366 | mode = file->mode; |
75bc8600 WD |
367 | if (preserve_devices) { |
368 | if (protocol_version < 28) { | |
728d0922 | 369 | if (IS_DEVICE(mode)) { |
9c5e91f8 WD |
370 | if (file->u.rdev == rdev) |
371 | flags |= XMIT_SAME_RDEV_pre28; | |
372 | else | |
728d0922 WD |
373 | rdev = file->u.rdev; |
374 | } else | |
9c5e91f8 | 375 | rdev = makedev(0, 0); |
728d0922 | 376 | } else if (IS_DEVICE(mode)) { |
84a3efa0 | 377 | rdev = file->u.rdev; |
9c5e91f8 WD |
378 | if ((uint32)major(rdev) == rdev_major) |
379 | flags |= XMIT_SAME_RDEV_MAJOR; | |
84a3efa0 | 380 | else |
9c5e91f8 WD |
381 | rdev_major = major(rdev); |
382 | if ((uint32)minor(rdev) <= 0xFFu) | |
383 | flags |= XMIT_RDEV_MINOR_IS_SMALL; | |
75bc8600 WD |
384 | } |
385 | } | |
30f337c9 | 386 | if (file->uid == uid) |
d01d15e0 | 387 | flags |= XMIT_SAME_UID; |
1ef00d20 | 388 | else |
30f337c9 WD |
389 | uid = file->uid; |
390 | if (file->gid == gid) | |
d01d15e0 | 391 | flags |= XMIT_SAME_GID; |
1ef00d20 | 392 | else |
30f337c9 WD |
393 | gid = file->gid; |
394 | if (file->modtime == modtime) | |
d01d15e0 | 395 | flags |= XMIT_SAME_TIME; |
1ef00d20 | 396 | else |
30f337c9 | 397 | modtime = file->modtime; |
a289addd | 398 | |
0d162bd1 | 399 | #if SUPPORT_HARD_LINKS |
92cc9dd7 WD |
400 | if (file->link_u.idev) { |
401 | if (file->F_DEV == dev) { | |
c4b4df4f | 402 | if (protocol_version >= 28) |
d01d15e0 | 403 | flags |= XMIT_SAME_DEV; |
728d0922 | 404 | } else |
92cc9dd7 | 405 | dev = file->F_DEV; |
d01d15e0 | 406 | flags |= XMIT_HAS_IDEV_DATA; |
c4b4df4f | 407 | } |
0d162bd1 | 408 | #endif |
ebed4c3a MP |
409 | |
410 | for (l1 = 0; | |
3e491682 S |
411 | lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255); |
412 | l1++) {} | |
eddd5d12 | 413 | l2 = strlen(fname+l1); |
72914a60 | 414 | |
ebed4c3a | 415 | if (l1 > 0) |
d01d15e0 | 416 | flags |= XMIT_SAME_NAME; |
ebed4c3a | 417 | if (l2 > 255) |
d01d15e0 | 418 | flags |= XMIT_LONG_NAME; |
72914a60 | 419 | |
1aa4caf3 WD |
420 | /* We must make sure we don't send a zero flag byte or the |
421 | * other end will terminate the flist transfer. Note that | |
71e27c46 | 422 | * the use of XMIT_DEL_START on a non-dir has no meaning, so |
1aa4caf3 | 423 | * it's harmless way to add a bit to the first flag byte. */ |
75bc8600 | 424 | if (protocol_version >= 28) { |
1aa4caf3 | 425 | if (!flags && !S_ISDIR(mode)) |
71e27c46 | 426 | flags |= XMIT_DEL_START; |
1aa4caf3 | 427 | if ((flags & 0xFF00) || !flags) { |
d01d15e0 | 428 | flags |= XMIT_EXTENDED_FLAGS; |
75bc8600 WD |
429 | write_byte(f, flags); |
430 | write_byte(f, flags >> 8); | |
431 | } else | |
432 | write_byte(f, flags); | |
433 | } else { | |
1aa4caf3 | 434 | if (!(flags & 0xFF) && !S_ISDIR(mode)) |
71e27c46 | 435 | flags |= XMIT_DEL_START; |
0a982011 | 436 | if (!(flags & 0xFF)) |
d01d15e0 | 437 | flags |= XMIT_LONG_NAME; |
75bc8600 WD |
438 | write_byte(f, flags); |
439 | } | |
d01d15e0 | 440 | if (flags & XMIT_SAME_NAME) |
ebed4c3a | 441 | write_byte(f, l1); |
d01d15e0 | 442 | if (flags & XMIT_LONG_NAME) |
ebed4c3a | 443 | write_int(f, l2); |
72914a60 | 444 | else |
ebed4c3a MP |
445 | write_byte(f, l2); |
446 | write_buf(f, fname + l1, l2); | |
72914a60 | 447 | |
ebed4c3a | 448 | write_longint(f, file->length); |
d01d15e0 | 449 | if (!(flags & XMIT_SAME_TIME)) |
30f337c9 | 450 | write_int(f, modtime); |
d01d15e0 | 451 | if (!(flags & XMIT_SAME_MODE)) |
30f337c9 | 452 | write_int(f, to_wire_mode(mode)); |
d01d15e0 | 453 | if (preserve_uid && !(flags & XMIT_SAME_UID)) { |
9f7b8c3b WD |
454 | if (!numeric_ids) |
455 | add_uid(uid); | |
30f337c9 | 456 | write_int(f, uid); |
72914a60 | 457 | } |
d01d15e0 | 458 | if (preserve_gid && !(flags & XMIT_SAME_GID)) { |
9f7b8c3b WD |
459 | if (!numeric_ids) |
460 | add_gid(gid); | |
30f337c9 | 461 | write_int(f, gid); |
72914a60 | 462 | } |
30f337c9 | 463 | if (preserve_devices && IS_DEVICE(mode)) { |
9c5e91f8 WD |
464 | if (protocol_version < 28) { |
465 | if (!(flags & XMIT_SAME_RDEV_pre28)) | |
466 | write_int(f, (int)rdev); | |
467 | } else { | |
468 | if (!(flags & XMIT_SAME_RDEV_MAJOR)) | |
469 | write_int(f, major(rdev)); | |
470 | if (flags & XMIT_RDEV_MINOR_IS_SMALL) | |
471 | write_byte(f, minor(rdev)); | |
472 | else | |
473 | write_int(f, minor(rdev)); | |
474 | } | |
75bc8600 | 475 | } |
c627d613 AT |
476 | |
477 | #if SUPPORT_LINKS | |
30f337c9 | 478 | if (preserve_links && S_ISLNK(mode)) { |
306ffb8c WD |
479 | int len = strlen(file->u.link); |
480 | write_int(f, len); | |
481 | write_buf(f, file->u.link, len); | |
72914a60 | 482 | } |
c627d613 AT |
483 | #endif |
484 | ||
dc5ddbcc | 485 | #if SUPPORT_HARD_LINKS |
d01d15e0 | 486 | if (flags & XMIT_HAS_IDEV_DATA) { |
4124540d | 487 | if (protocol_version < 26) { |
736a6a29 | 488 | /* 32-bit dev_t and ino_t */ |
30f337c9 | 489 | write_int(f, dev); |
92cc9dd7 | 490 | write_int(f, file->F_INODE); |
736a6a29 MP |
491 | } else { |
492 | /* 64-bit dev_t and ino_t */ | |
4124540d WD |
493 | if (!(flags & XMIT_SAME_DEV)) |
494 | write_longint(f, dev); | |
92cc9dd7 | 495 | write_longint(f, file->F_INODE); |
736a6a29 | 496 | } |
72914a60 | 497 | } |
dc5ddbcc AT |
498 | #endif |
499 | ||
728d0922 WD |
500 | if (always_checksum) { |
501 | char *sum; | |
502 | if (S_ISREG(mode)) | |
503 | sum = file->u.sum; | |
504 | else if (protocol_version < 28) { | |
505 | /* Prior to 28, we sent a useless set of nulls. */ | |
506 | sum = empty_sum; | |
507 | } else | |
508 | sum = NULL; | |
509 | if (sum) { | |
3e491682 S |
510 | write_buf(f, sum, |
511 | protocol_version < 21 ? 2 : MD4_SUM_LENGTH); | |
728d0922 | 512 | } |
ebed4c3a | 513 | } |
182dca5c | 514 | |
ebed4c3a | 515 | strlcpy(lastname, fname, MAXPATHLEN); |
eca2adb4 MP |
516 | |
517 | io_write_phase = "unknown"; | |
182dca5c AT |
518 | } |
519 | ||
520 | ||
521 | ||
9935066b | 522 | void receive_file_entry(struct file_struct **fptr, unsigned short flags, |
314f4591 | 523 | struct file_list *flist, int f) |
182dca5c | 524 | { |
5911fee5 WD |
525 | static time_t modtime; |
526 | static mode_t mode; | |
1490812a | 527 | static int64 dev; |
4124540d | 528 | static dev_t rdev; |
9c5e91f8 | 529 | static uint32 rdev_major; |
5911fee5 WD |
530 | static uid_t uid; |
531 | static gid_t gid; | |
a289addd | 532 | static char lastname[MAXPATHLEN], *lastdir; |
33ffd7c3 | 533 | static int lastdir_depth, lastdir_len = -1; |
3db859e8 | 534 | static unsigned int del_heir_name_len = 0; |
649f8742 | 535 | static int in_del_hier = 0; |
72914a60 | 536 | char thisname[MAXPATHLEN]; |
ebed4c3a | 537 | unsigned int l1 = 0, l2 = 0; |
a1d55ad0 | 538 | int alloc_len, basename_len, dirname_len, linkname_len, sum_len; |
a289addd WD |
539 | OFF_T file_length; |
540 | char *basename, *dirname, *bp; | |
72914a60 AT |
541 | struct file_struct *file; |
542 | ||
5911fee5 WD |
543 | if (!fptr) { |
544 | modtime = 0, mode = 0; | |
4124540d | 545 | dev = 0, rdev = makedev(0, 0); |
9c5e91f8 | 546 | rdev_major = 0; |
5911fee5 WD |
547 | uid = 0, gid = 0; |
548 | *lastname = '\0'; | |
3db859e8 | 549 | lastdir_len = -1; |
649f8742 | 550 | in_del_hier = 0; |
5911fee5 WD |
551 | return; |
552 | } | |
553 | ||
d01d15e0 | 554 | if (flags & XMIT_SAME_NAME) |
72914a60 | 555 | l1 = read_byte(f); |
ebed4c3a | 556 | |
d01d15e0 | 557 | if (flags & XMIT_LONG_NAME) |
72914a60 AT |
558 | l2 = read_int(f); |
559 | else | |
560 | l2 = read_byte(f); | |
561 | ||
ebed4c3a MP |
562 | if (l2 >= MAXPATHLEN - l1) { |
563 | rprintf(FERROR, | |
564 | "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n", | |
d0fd26aa AT |
565 | flags, l1, l2, lastname); |
566 | overflow("receive_file_entry"); | |
567 | } | |
72914a60 | 568 | |
ebed4c3a MP |
569 | strlcpy(thisname, lastname, l1 + 1); |
570 | read_sbuf(f, &thisname[l1], l2); | |
571 | thisname[l1 + l2] = 0; | |
72914a60 | 572 | |
ebed4c3a | 573 | strlcpy(lastname, thisname, MAXPATHLEN); |
72914a60 | 574 | |
58b1999e | 575 | clean_fname(thisname, 0); |
72914a60 | 576 | |
0d162bd1 | 577 | if (sanitize_paths) |
33ffd7c3 | 578 | sanitize_path(thisname, thisname, "", 0); |
cb13abfe | 579 | |
a289addd WD |
580 | if ((basename = strrchr(thisname, '/')) != NULL) { |
581 | dirname_len = ++basename - thisname; /* counts future '\0' */ | |
582 | if (lastdir_len == dirname_len - 1 | |
583 | && strncmp(thisname, lastdir, lastdir_len) == 0) { | |
584 | dirname = lastdir; | |
585 | dirname_len = 0; /* indicates no copy is needed */ | |
586 | } else | |
587 | dirname = thisname; | |
72914a60 | 588 | } else { |
a289addd WD |
589 | basename = thisname; |
590 | dirname = NULL; | |
591 | dirname_len = 0; | |
72914a60 | 592 | } |
a289addd | 593 | basename_len = strlen(basename) + 1; /* count the '\0' */ |
72914a60 | 594 | |
a289addd | 595 | file_length = read_longint(f); |
d01d15e0 | 596 | if (!(flags & XMIT_SAME_TIME)) |
30f337c9 | 597 | modtime = (time_t)read_int(f); |
d01d15e0 | 598 | if (!(flags & XMIT_SAME_MODE)) |
30f337c9 | 599 | mode = from_wire_mode(read_int(f)); |
1ef00d20 | 600 | |
a289addd WD |
601 | if (preserve_uid && !(flags & XMIT_SAME_UID)) |
602 | uid = (uid_t)read_int(f); | |
603 | if (preserve_gid && !(flags & XMIT_SAME_GID)) | |
604 | gid = (gid_t)read_int(f); | |
605 | ||
1ef00d20 | 606 | if (preserve_devices) { |
75bc8600 | 607 | if (protocol_version < 28) { |
30f337c9 | 608 | if (IS_DEVICE(mode)) { |
d01d15e0 | 609 | if (!(flags & XMIT_SAME_RDEV_pre28)) |
9c5e91f8 | 610 | rdev = (dev_t)read_int(f); |
75bc8600 | 611 | } else |
9c5e91f8 | 612 | rdev = makedev(0, 0); |
30f337c9 | 613 | } else if (IS_DEVICE(mode)) { |
9c5e91f8 WD |
614 | uint32 rdev_minor; |
615 | if (!(flags & XMIT_SAME_RDEV_MAJOR)) | |
616 | rdev_major = read_int(f); | |
617 | if (flags & XMIT_RDEV_MINOR_IS_SMALL) | |
618 | rdev_minor = read_byte(f); | |
619 | else | |
620 | rdev_minor = read_int(f); | |
621 | rdev = makedev(rdev_major, rdev_minor); | |
1ef00d20 | 622 | } |
b7736c79 | 623 | } |
72914a60 | 624 | |
0d162bd1 | 625 | #if SUPPORT_LINKS |
30f337c9 | 626 | if (preserve_links && S_ISLNK(mode)) { |
a289addd WD |
627 | linkname_len = read_int(f) + 1; /* count the '\0' */ |
628 | if (linkname_len <= 0 || linkname_len > MAXPATHLEN) { | |
629 | rprintf(FERROR, "overflow: linkname_len=%d\n", | |
630 | linkname_len - 1); | |
9dd891bb MP |
631 | overflow("receive_file_entry"); |
632 | } | |
72914a60 | 633 | } |
a289addd | 634 | else |
0d162bd1 | 635 | #endif |
a289addd | 636 | linkname_len = 0; |
0d162bd1 | 637 | |
a289addd WD |
638 | sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0; |
639 | ||
61dec11a | 640 | alloc_len = file_struct_len + dirname_len + basename_len |
9935066b S |
641 | + linkname_len + sum_len; |
642 | bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry"); | |
643 | ||
a289addd | 644 | file = *fptr = (struct file_struct *)bp; |
9935066b | 645 | memset(bp, 0, file_struct_len); |
61dec11a | 646 | bp += file_struct_len; |
a289addd | 647 | |
649f8742 | 648 | file->flags = 0; |
a289addd WD |
649 | file->modtime = modtime; |
650 | file->length = file_length; | |
651 | file->mode = mode; | |
652 | file->uid = uid; | |
653 | file->gid = gid; | |
654 | ||
649f8742 WD |
655 | if (S_ISDIR(mode)) { |
656 | if (flags & XMIT_DEL_START) { | |
657 | in_del_hier = 1; | |
658 | del_heir_name_len = l1 + l2; | |
659 | file->flags |= FLAG_DEL_START; | |
660 | } else if (delete_during && in_del_hier) { | |
661 | if (!relative_paths || (l1 >= del_heir_name_len | |
662 | && thisname[del_heir_name_len] == '/')) | |
663 | file->flags |= FLAG_DEL_START; | |
664 | else | |
665 | in_del_hier = 0; | |
666 | } | |
667 | } | |
668 | ||
a289addd WD |
669 | if (dirname_len) { |
670 | file->dirname = lastdir = bp; | |
671 | lastdir_len = dirname_len - 1; | |
672 | memcpy(bp, dirname, dirname_len - 1); | |
673 | bp += dirname_len; | |
674 | bp[-1] = '\0'; | |
33ffd7c3 WD |
675 | if (sanitize_paths) |
676 | lastdir_depth = count_dir_elements(lastdir); | |
a289addd WD |
677 | } else if (dirname) |
678 | file->dirname = dirname; | |
679 | ||
680 | file->basename = bp; | |
681 | memcpy(bp, basename, basename_len); | |
682 | bp += basename_len; | |
683 | ||
684 | if (preserve_devices && IS_DEVICE(mode)) | |
685 | file->u.rdev = rdev; | |
686 | ||
687 | #if SUPPORT_LINKS | |
688 | if (linkname_len) { | |
689 | file->u.link = bp; | |
690 | read_sbuf(f, bp, linkname_len - 1); | |
691 | if (sanitize_paths) | |
33ffd7c3 | 692 | sanitize_path(bp, bp, "", lastdir_depth); |
a289addd WD |
693 | bp += linkname_len; |
694 | } | |
695 | #endif | |
696 | ||
697 | #if SUPPORT_HARD_LINKS | |
9935066b S |
698 | if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode)) |
699 | flags |= XMIT_HAS_IDEV_DATA; | |
97a67bdf | 700 | if (flags & XMIT_HAS_IDEV_DATA) { |
1490812a | 701 | int64 inode; |
4124540d WD |
702 | if (protocol_version < 26) { |
703 | dev = read_int(f); | |
9935066b | 704 | inode = read_int(f); |
736a6a29 | 705 | } else { |
4124540d WD |
706 | if (!(flags & XMIT_SAME_DEV)) |
707 | dev = read_longint(f); | |
9935066b S |
708 | inode = read_longint(f); |
709 | } | |
710 | if (flist->hlink_pool) { | |
5bf63a11 S |
711 | file->link_u.idev = pool_talloc(flist->hlink_pool, |
712 | struct idev, 1, "inode_table"); | |
9935066b S |
713 | file->F_INODE = inode; |
714 | file->F_DEV = dev; | |
736a6a29 | 715 | } |
72914a60 | 716 | } |
dc5ddbcc | 717 | #endif |
ebed4c3a | 718 | |
72914a60 | 719 | if (always_checksum) { |
7c4f063b | 720 | char *sum; |
a289addd WD |
721 | if (sum_len) { |
722 | file->u.sum = sum = bp; | |
723 | /*bp += sum_len;*/ | |
fea4db62 WD |
724 | } else if (protocol_version < 28) { |
725 | /* Prior to 28, we get a useless set of nulls. */ | |
7c4f063b | 726 | sum = empty_sum; |
fea4db62 WD |
727 | } else |
728 | sum = NULL; | |
729 | if (sum) { | |
3e491682 S |
730 | read_buf(f, sum, |
731 | protocol_version < 21 ? 2 : MD4_SUM_LENGTH); | |
fea4db62 | 732 | } |
72914a60 | 733 | } |
ebed4c3a | 734 | |
72914a60 | 735 | if (!preserve_perms) { |
72914a60 | 736 | /* set an appropriate set of permissions based on original |
fea4db62 | 737 | * permissions and umask. This emulates what GNU cp does */ |
72914a60 AT |
738 | file->mode &= ~orig_umask; |
739 | } | |
c627d613 AT |
740 | } |
741 | ||
742 | ||
db719fb0 MP |
743 | /** |
744 | * Create a file_struct for a named file by reading its stat() | |
745 | * information and performing extensive checks against global | |
746 | * options. | |
747 | * | |
748 | * @return the new file, or NULL if there was an error or this file | |
749 | * should be excluded. | |
750 | * | |
751 | * @todo There is a small optimization opportunity here to avoid | |
752 | * stat()ing the file in some circumstances, which has a certain cost. | |
753 | * We are called immediately after doing readdir(), and so we may | |
754 | * already know the d_type of the file. We could for example avoid | |
755 | * statting directories if we're not recursing, but this is not a very | |
756 | * important case. Some systems may not have d_type. | |
757 | **/ | |
314f4591 | 758 | struct file_struct *make_file(char *fname, struct file_list *flist, |
7842418b | 759 | int filter_level) |
c627d613 | 760 | { |
a289addd WD |
761 | static char *lastdir; |
762 | static int lastdir_len = -1; | |
3ec4dd97 | 763 | struct file_struct *file; |
bcacc18b | 764 | STRUCT_STAT st; |
3ec4dd97 | 765 | char sum[SUM_LENGTH]; |
1923b1fc | 766 | char thisname[MAXPATHLEN]; |
e0870f1d | 767 | char linkname[MAXPATHLEN]; |
a1d55ad0 | 768 | int alloc_len, basename_len, dirname_len, linkname_len, sum_len; |
a289addd | 769 | char *basename, *dirname, *bp; |
4844449a | 770 | unsigned short flags = 0; |
3ec4dd97 | 771 | |
d508258a | 772 | if (!flist || !flist->count) /* Ignore lastdir when invalid. */ |
7de2483f | 773 | lastdir_len = -1; |
9935066b | 774 | |
1923b1fc WD |
775 | if (strlcpy(thisname, fname, sizeof thisname) |
776 | >= sizeof thisname - flist_dir_len) { | |
882e6893 WD |
777 | rprintf(FINFO, "skipping overly long name: %s\n", fname); |
778 | return NULL; | |
779 | } | |
58b1999e | 780 | clean_fname(thisname, 0); |
b7736c79 | 781 | if (sanitize_paths) |
33ffd7c3 | 782 | sanitize_path(thisname, thisname, "", 0); |
3ec4dd97 | 783 | |
ebed4c3a | 784 | memset(sum, 0, SUM_LENGTH); |
3ec4dd97 | 785 | |
e0870f1d | 786 | if (readlink_stat(thisname, &st, linkname) != 0) { |
76e26e10 | 787 | int save_errno = errno; |
a4a7e64c | 788 | /* See if file is excluded before reporting an error. */ |
7842418b WD |
789 | if (filter_level != NO_FILTERS |
790 | && is_excluded(thisname, 0, filter_level)) | |
a4a7e64c WD |
791 | return NULL; |
792 | if (save_errno == ENOENT) { | |
e5ce3bcf | 793 | #if SUPPORT_LINKS |
a4a7e64c WD |
794 | /* Avoid "vanished" error if symlink points nowhere. */ |
795 | if (copy_links && do_lstat(thisname, &st) == 0 | |
796 | && S_ISLNK(st.st_mode)) { | |
797 | io_error |= IOERR_GENERAL; | |
798 | rprintf(FERROR, "symlink has no referent: %s\n", | |
799 | full_fname(thisname)); | |
e5ce3bcf WD |
800 | } else |
801 | #endif | |
802 | { | |
a4a7e64c WD |
803 | enum logcode c = am_daemon && protocol_version < 28 |
804 | ? FERROR : FINFO; | |
805 | io_error |= IOERR_VANISHED; | |
806 | rprintf(c, "file has vanished: %s\n", | |
807 | full_fname(thisname)); | |
76e26e10 | 808 | } |
a4a7e64c | 809 | } else { |
a8726d2a | 810 | io_error |= IOERR_GENERAL; |
d62bcc17 WD |
811 | rsyserr(FERROR, save_errno, "readlink %s failed", |
812 | full_fname(thisname)); | |
76e26e10 | 813 | } |
3ec4dd97 AT |
814 | return NULL; |
815 | } | |
c627d613 | 816 | |
7842418b WD |
817 | /* backup.c calls us with filter_level set to NO_FILTERS. */ |
818 | if (filter_level == NO_FILTERS) | |
819 | goto skip_filters; | |
ac1a0994 | 820 | |
7e037c42 | 821 | if (S_ISDIR(st.st_mode) && !xfer_dirs) { |
1923b1fc | 822 | rprintf(FINFO, "skipping directory %s\n", thisname); |
3ec4dd97 AT |
823 | return NULL; |
824 | } | |
ebed4c3a | 825 | |
e90cdb8a WD |
826 | /* We only care about directories because we need to avoid recursing |
827 | * into a mount-point directory, not to avoid copying a symlinked | |
828 | * file if -L (or similar) was specified. */ | |
829 | if (one_file_system && st.st_dev != filesystem_dev | |
830 | && S_ISDIR(st.st_mode)) | |
4844449a | 831 | flags |= FLAG_MOUNT_POINT; |
ebed4c3a | 832 | |
7842418b | 833 | if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) |
76e26e10 DD |
834 | return NULL; |
835 | ||
132fcf36 WD |
836 | if (lp_ignore_nonreadable(module_id)) { |
837 | #if SUPPORT_LINKS | |
838 | if (!S_ISLNK(st.st_mode)) | |
839 | #endif | |
840 | if (access(thisname, R_OK) != 0) | |
841 | return NULL; | |
842 | } | |
ac1a0994 | 843 | |
7842418b | 844 | skip_filters: |
ac1a0994 | 845 | |
ea847c62 WD |
846 | if (verbose > 2) { |
847 | rprintf(FINFO, "[%s] make_file(%s,*,%d)\n", | |
7842418b | 848 | who_am_i(), thisname, filter_level); |
ea847c62 | 849 | } |
ebed4c3a | 850 | |
a289addd WD |
851 | if ((basename = strrchr(thisname, '/')) != NULL) { |
852 | dirname_len = ++basename - thisname; /* counts future '\0' */ | |
853 | if (lastdir_len == dirname_len - 1 | |
854 | && strncmp(thisname, lastdir, lastdir_len) == 0) { | |
855 | dirname = lastdir; | |
856 | dirname_len = 0; /* indicates no copy is needed */ | |
857 | } else | |
858 | dirname = thisname; | |
3ec4dd97 | 859 | } else { |
a289addd WD |
860 | basename = thisname; |
861 | dirname = NULL; | |
862 | dirname_len = 0; | |
3ec4dd97 | 863 | } |
a289addd | 864 | basename_len = strlen(basename) + 1; /* count the '\0' */ |
c627d613 | 865 | |
a289addd WD |
866 | #if SUPPORT_LINKS |
867 | linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0; | |
868 | #else | |
869 | linkname_len = 0; | |
870 | #endif | |
871 | ||
a289addd WD |
872 | sum_len = always_checksum && S_ISREG(st.st_mode) ? MD4_SUM_LENGTH : 0; |
873 | ||
61dec11a | 874 | alloc_len = file_struct_len + dirname_len + basename_len |
9935066b S |
875 | + linkname_len + sum_len; |
876 | if (flist) { | |
877 | bp = pool_alloc(flist->file_pool, alloc_len, | |
878 | "receive_file_entry"); | |
879 | } else { | |
880 | if (!(bp = new_array(char, alloc_len))) | |
881 | out_of_memory("receive_file_entry"); | |
882 | } | |
883 | ||
a289addd | 884 | file = (struct file_struct *)bp; |
9935066b | 885 | memset(bp, 0, file_struct_len); |
61dec11a | 886 | bp += file_struct_len; |
a289addd WD |
887 | |
888 | file->flags = flags; | |
3ec4dd97 AT |
889 | file->modtime = st.st_mtime; |
890 | file->length = st.st_size; | |
891 | file->mode = st.st_mode; | |
892 | file->uid = st.st_uid; | |
893 | file->gid = st.st_gid; | |
0d162bd1 | 894 | |
61dec11a | 895 | #if SUPPORT_HARD_LINKS |
9935066b S |
896 | if (flist && flist->hlink_pool) { |
897 | if (protocol_version < 28) { | |
898 | if (S_ISREG(st.st_mode)) | |
899 | file->link_u.idev = pool_talloc( | |
900 | flist->hlink_pool, struct idev, 1, | |
901 | "inode_table"); | |
902 | } else { | |
903 | if (!S_ISDIR(st.st_mode) && st.st_nlink > 1) | |
904 | file->link_u.idev = pool_talloc( | |
905 | flist->hlink_pool, struct idev, 1, | |
906 | "inode_table"); | |
907 | } | |
908 | } | |
909 | if (file->link_u.idev) { | |
61dec11a WD |
910 | file->F_DEV = st.st_dev; |
911 | file->F_INODE = st.st_ino; | |
912 | } | |
913 | #endif | |
914 | ||
a289addd WD |
915 | if (dirname_len) { |
916 | file->dirname = lastdir = bp; | |
917 | lastdir_len = dirname_len - 1; | |
918 | memcpy(bp, dirname, dirname_len - 1); | |
919 | bp += dirname_len; | |
920 | bp[-1] = '\0'; | |
921 | } else if (dirname) | |
922 | file->dirname = dirname; | |
923 | ||
924 | file->basename = bp; | |
925 | memcpy(bp, basename, basename_len); | |
926 | bp += basename_len; | |
927 | ||
25ff0441 | 928 | #if HAVE_STRUCT_STAT_ST_RDEV |
0d162bd1 WD |
929 | if (preserve_devices && IS_DEVICE(st.st_mode)) |
930 | file->u.rdev = st.st_rdev; | |
931 | #endif | |
932 | ||
933 | #if SUPPORT_LINKS | |
a289addd WD |
934 | if (linkname_len) { |
935 | file->u.link = bp; | |
936 | memcpy(bp, linkname, linkname_len); | |
937 | bp += linkname_len; | |
938 | } | |
0d162bd1 WD |
939 | #endif |
940 | ||
a289addd WD |
941 | if (sum_len) { |
942 | file->u.sum = bp; | |
943 | file_checksum(thisname, bp, st.st_size); | |
944 | /*bp += sum_len;*/ | |
ebed4c3a | 945 | } |
c627d613 | 946 | |
882e6893 | 947 | file->basedir = flist_dir; |
c627d613 | 948 | |
23f4587f WD |
949 | /* This code is only used by the receiver when it is building |
950 | * a list of files for a delete pass. */ | |
951 | if (keep_dirlinks && linkname_len && flist) { | |
952 | STRUCT_STAT st2; | |
953 | int i = flist_find(received_flist, file); | |
954 | if (i >= 0 && S_ISDIR(received_flist->files[i]->mode) | |
955 | && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) { | |
956 | file->modtime = st2.st_mtime; | |
957 | file->length = st2.st_size; | |
958 | file->mode = st2.st_mode; | |
959 | file->uid = st2.st_uid; | |
960 | file->gid = st2.st_gid; | |
961 | file->u.link = NULL; | |
962 | if (file->link_u.idev) { | |
963 | pool_free(flist->hlink_pool, 0, file->link_u.idev); | |
964 | file->link_u.idev = NULL; | |
965 | } | |
966 | } | |
967 | } | |
968 | ||
3ec4dd97 | 969 | if (!S_ISDIR(st.st_mode)) |
a800434a | 970 | stats.total_size += st.st_size; |
c627d613 | 971 | |
3ec4dd97 | 972 | return file; |
c627d613 AT |
973 | } |
974 | ||
975 | ||
ebed4c3a | 976 | void send_file_name(int f, struct file_list *flist, char *fname, |
1ef00d20 | 977 | int recursive, unsigned short base_flags) |
c627d613 | 978 | { |
ebed4c3a | 979 | struct file_struct *file; |
b7736c79 | 980 | char fbuf[MAXPATHLEN]; |
ebed4c3a | 981 | |
429f9828 | 982 | /* f is set to -1 when calculating deletion file list */ |
9935066b | 983 | file = make_file(fname, flist, |
7842418b | 984 | f == -1 && delete_excluded? SERVER_FILTERS : ALL_FILTERS); |
ebed4c3a MP |
985 | |
986 | if (!file) | |
987 | return; | |
988 | ||
db719fb0 | 989 | maybe_emit_filelist_progress(flist); |
ebed4c3a | 990 | |
d9d6bc52 | 991 | flist_expand(flist); |
ebed4c3a | 992 | |
c120ff37 | 993 | if (file->basename[0]) { |
ebed4c3a MP |
994 | flist->files[flist->count++] = file; |
995 | send_file_entry(file, f, base_flags); | |
996 | } | |
997 | ||
4844449a WD |
998 | if (recursive && S_ISDIR(file->mode) |
999 | && !(file->flags & FLAG_MOUNT_POINT)) { | |
882e6893 | 1000 | send_directory(f, flist, f_name_to(file, fbuf)); |
ebed4c3a | 1001 | } |
c627d613 AT |
1002 | } |
1003 | ||
1004 | ||
9f125ea7 WD |
1005 | /* Note that the "recurse" value either contains -1, for infinite recursion, |
1006 | * or a number >= 0 indicating how many levels of recursion we will allow. */ | |
ebed4c3a | 1007 | static void send_directory(int f, struct file_list *flist, char *dir) |
c627d613 | 1008 | { |
3ec4dd97 AT |
1009 | DIR *d; |
1010 | struct dirent *di; | |
1011 | char fname[MAXPATHLEN]; | |
eddd5d12 | 1012 | unsigned int offset; |
7842418b | 1013 | void *save_filters; |
3ec4dd97 AT |
1014 | char *p; |
1015 | ||
1016 | d = opendir(dir); | |
1017 | if (!d) { | |
06c28400 | 1018 | io_error |= IOERR_GENERAL; |
d62bcc17 | 1019 | rsyserr(FERROR, errno, "opendir %s failed", full_fname(dir)); |
3ec4dd97 AT |
1020 | return; |
1021 | } | |
c627d613 | 1022 | |
eddd5d12 WD |
1023 | offset = strlcpy(fname, dir, MAXPATHLEN); |
1024 | p = fname + offset; | |
1025 | if (offset >= MAXPATHLEN || p[-1] != '/') { | |
1026 | if (offset >= MAXPATHLEN - 1) { | |
06c28400 | 1027 | io_error |= IOERR_GENERAL; |
ea42541f WD |
1028 | rprintf(FERROR, "skipping long-named directory: %s\n", |
1029 | full_fname(fname)); | |
3ec4dd97 AT |
1030 | closedir(d); |
1031 | return; | |
1032 | } | |
eddd5d12 WD |
1033 | *p++ = '/'; |
1034 | offset++; | |
3ec4dd97 | 1035 | } |
c627d613 | 1036 | |
7842418b | 1037 | save_filters = push_local_filters(fname, offset); |
ebed4c3a | 1038 | |
6a7cc46c | 1039 | for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) { |
d6e6ecbd | 1040 | char *dname = d_name(di); |
6a7cc46c S |
1041 | if (dname[0] == '.' && (dname[1] == '\0' |
1042 | || (dname[1] == '.' && dname[2] == '\0'))) | |
3ec4dd97 | 1043 | continue; |
0f57446d | 1044 | if (strlcpy(p, dname, MAXPATHLEN - offset) < MAXPATHLEN - offset) { |
9f125ea7 WD |
1045 | int do_subdirs = recurse >= 1 ? recurse-- : recurse; |
1046 | send_file_name(f, flist, fname, do_subdirs, 0); | |
0f57446d | 1047 | } else { |
eddd5d12 WD |
1048 | io_error |= IOERR_GENERAL; |
1049 | rprintf(FINFO, | |
1050 | "cannot send long-named file %s\n", | |
1051 | full_fname(fname)); | |
1052 | } | |
3ec4dd97 | 1053 | } |
6a7cc46c | 1054 | if (errno) { |
06c28400 | 1055 | io_error |= IOERR_GENERAL; |
d62bcc17 | 1056 | rsyserr(FERROR, errno, "readdir(%s)", dir); |
6a7cc46c | 1057 | } |
c627d613 | 1058 | |
7842418b | 1059 | pop_local_filters(save_filters); |
c7d970f7 | 1060 | |
3ec4dd97 | 1061 | closedir(d); |
c627d613 AT |
1062 | } |
1063 | ||
1064 | ||
649f8742 WD |
1065 | /* This function is normally called by the sender, but the receiving side |
1066 | * also uses it to construct one or more file lists if one of the --delete | |
1067 | * options have been specified. The delete_in_dir() function sets f to -1 | |
1068 | * so that we just construct the file list in memory without sending it | |
1069 | * over the wire. It also has the side-effect of ignoring user-excludes if | |
1070 | * delete_excluded is set (so that the delete list includes user-excluded | |
1071 | * files) and it avoids some per-arg init code for limited recursion (since | |
1072 | * delete_in_dir() sets recurse before calling this function). */ | |
ebed4c3a | 1073 | struct file_list *send_file_list(int f, int argc, char *argv[]) |
c627d613 | 1074 | { |
24d0fcde | 1075 | int l; |
bcacc18b | 1076 | STRUCT_STAT st; |
808c57c3 | 1077 | char *p, *dir, olddir[sizeof curr_dir]; |
ebed4c3a | 1078 | char lastpath[MAXPATHLEN] = ""; |
649d65ed | 1079 | struct file_list *flist; |
c7d970f7 | 1080 | BOOL need_first_push = True; |
31b4d25d | 1081 | struct timeval start_tv, end_tv; |
a800434a | 1082 | int64 start_write; |
24d0fcde | 1083 | int use_ff_fd = 0; |
649d65ed | 1084 | |
1bbd10fe DD |
1085 | if (show_filelist_p() && f != -1) |
1086 | start_filelist_progress("building file list"); | |
c627d613 | 1087 | |
a800434a | 1088 | start_write = stats.total_written; |
31b4d25d | 1089 | gettimeofday(&start_tv, NULL); |
a800434a | 1090 | |
9935066b | 1091 | flist = flist_new(f == -1 ? WITHOUT_HLINK : WITH_HLINK, |
9f125ea7 | 1092 | "send_file_list"); |
c627d613 | 1093 | |
d6dead6b | 1094 | if (f != -1) { |
088adfac | 1095 | io_start_buffering_out(); |
24d0fcde | 1096 | if (filesfrom_fd >= 0) { |
808c57c3 | 1097 | if (argv[0] && !push_dir(argv[0])) { |
d62bcc17 WD |
1098 | rsyserr(FERROR, errno, "push_dir %s failed", |
1099 | full_fname(argv[0])); | |
24d0fcde WD |
1100 | exit_cleanup(RERR_FILESELECT); |
1101 | } | |
1102 | use_ff_fd = 1; | |
c7d970f7 | 1103 | if (curr_dir_len < MAXPATHLEN - 1) { |
7842418b | 1104 | push_local_filters(curr_dir, curr_dir_len); |
c7d970f7 WD |
1105 | need_first_push = False; |
1106 | } | |
24d0fcde | 1107 | } |
d6dead6b AT |
1108 | } |
1109 | ||
24d0fcde | 1110 | while (1) { |
fc638474 DD |
1111 | char fname2[MAXPATHLEN]; |
1112 | char *fname = fname2; | |
9f125ea7 | 1113 | int do_subdirs; |
c627d613 | 1114 | |
24d0fcde WD |
1115 | if (use_ff_fd) { |
1116 | if (read_filesfrom_line(filesfrom_fd, fname) == 0) | |
1117 | break; | |
33ffd7c3 | 1118 | sanitize_path(fname, fname, "", 0); |
24d0fcde WD |
1119 | } else { |
1120 | if (argc-- == 0) | |
1121 | break; | |
1122 | strlcpy(fname, *argv++, MAXPATHLEN); | |
1123 | if (sanitize_paths) | |
33ffd7c3 | 1124 | sanitize_path(fname, fname, "", 0); |
24d0fcde | 1125 | } |
c627d613 | 1126 | |
649d65ed | 1127 | l = strlen(fname); |
9f125ea7 | 1128 | if (!l || fname[l - 1] == '/') { |
6931c138 WD |
1129 | if (l == 2 && fname[0] == '.') { |
1130 | /* Turn "./" into just "." rather than "./." */ | |
1131 | fname[1] = '\0'; | |
eddd5d12 WD |
1132 | } else if (l < MAXPATHLEN) { |
1133 | fname[l++] = '.'; | |
1134 | fname[l] = '\0'; | |
53f821f1 | 1135 | } |
649d65ed | 1136 | } |
649f8742 WD |
1137 | if (f == -1) |
1138 | ; /* recurse is pre-set */ | |
1139 | else if (fname[l-1] == '.' && (l == 1 || fname[l-2] == '/')) { | |
7e037c42 | 1140 | if (!recurse && xfer_dirs) |
9f125ea7 WD |
1141 | recurse = 1; /* allow one level */ |
1142 | } else if (recurse > 0) | |
1143 | recurse = 0; | |
c627d613 | 1144 | |
c7d970f7 WD |
1145 | if (need_first_push) { |
1146 | if ((p = strrchr(fname, '/')) != NULL) { | |
1147 | if (*++p && strcmp(p, ".") != 0) | |
7842418b | 1148 | push_local_filters(fname, p - fname); |
c7d970f7 | 1149 | } else if (strcmp(fname, ".") != 0) |
7842418b | 1150 | push_local_filters(fname, 0); |
c7d970f7 WD |
1151 | need_first_push = False; |
1152 | } | |
1153 | ||
314f4591 | 1154 | if (link_stat(fname, &st, keep_dirlinks) != 0) { |
f76933b1 | 1155 | if (f != -1) { |
06c28400 | 1156 | io_error |= IOERR_GENERAL; |
d62bcc17 WD |
1157 | rsyserr(FERROR, errno, "link_stat %s failed", |
1158 | full_fname(fname)); | |
f76933b1 | 1159 | } |
649d65ed AT |
1160 | continue; |
1161 | } | |
c627d613 | 1162 | |
7e037c42 | 1163 | if (S_ISDIR(st.st_mode) && !xfer_dirs) { |
ebed4c3a | 1164 | rprintf(FINFO, "skipping directory %s\n", fname); |
649d65ed AT |
1165 | continue; |
1166 | } | |
c627d613 | 1167 | |
649d65ed | 1168 | dir = NULL; |
808c57c3 | 1169 | olddir[0] = '\0'; |
649d65ed AT |
1170 | |
1171 | if (!relative_paths) { | |
ebed4c3a | 1172 | p = strrchr(fname, '/'); |
649d65ed AT |
1173 | if (p) { |
1174 | *p = 0; | |
ebed4c3a | 1175 | if (p == fname) |
649d65ed AT |
1176 | dir = "/"; |
1177 | else | |
ebed4c3a MP |
1178 | dir = fname; |
1179 | fname = p + 1; | |
649d65ed | 1180 | } |
24d0fcde | 1181 | } else if (f != -1 && implied_dirs && (p=strrchr(fname,'/')) && p != fname) { |
649d65ed AT |
1182 | /* this ensures we send the intermediate directories, |
1183 | thus getting their permissions right */ | |
2154309a | 1184 | char *lp = lastpath, *fn = fname, *slash = fname; |
649d65ed | 1185 | *p = 0; |
2154309a WD |
1186 | /* Skip any initial directories in our path that we |
1187 | * have in common with lastpath. */ | |
1188 | while (*fn && *lp == *fn) { | |
1189 | if (*fn == '/') | |
1190 | slash = fn; | |
1191 | lp++, fn++; | |
1192 | } | |
1193 | *p = '/'; | |
1194 | if (fn != p || (*lp && *lp != '/')) { | |
0f57446d | 1195 | int save_copy_links = copy_links; |
7e037c42 | 1196 | int save_xfer_dirs = xfer_dirs; |
2154309a | 1197 | copy_links = copy_unsafe_links; |
7e037c42 | 1198 | xfer_dirs = 1; |
2154309a WD |
1199 | while ((slash = strchr(slash+1, '/')) != 0) { |
1200 | *slash = 0; | |
1201 | send_file_name(f, flist, fname, 0, 0); | |
1202 | *slash = '/'; | |
649d65ed | 1203 | } |
0f57446d | 1204 | copy_links = save_copy_links; |
7e037c42 | 1205 | xfer_dirs = save_xfer_dirs; |
2154309a WD |
1206 | *p = 0; |
1207 | strlcpy(lastpath, fname, sizeof lastpath); | |
649d65ed AT |
1208 | *p = '/'; |
1209 | } | |
1210 | } | |
ebed4c3a | 1211 | |
649d65ed AT |
1212 | if (!*fname) |
1213 | fname = "."; | |
ebed4c3a | 1214 | |
649d65ed | 1215 | if (dir && *dir) { |
882e6893 WD |
1216 | static char *lastdir; |
1217 | static int lastdir_len; | |
1218 | ||
808c57c3 | 1219 | strcpy(olddir, curr_dir); /* can't overflow */ |
5243c216 | 1220 | |
808c57c3 | 1221 | if (!push_dir(dir)) { |
06c28400 | 1222 | io_error |= IOERR_GENERAL; |
d62bcc17 WD |
1223 | rsyserr(FERROR, errno, "push_dir %s failed", |
1224 | full_fname(dir)); | |
649d65ed AT |
1225 | continue; |
1226 | } | |
5243c216 | 1227 | |
882e6893 WD |
1228 | if (lastdir && strcmp(lastdir, dir) == 0) { |
1229 | flist_dir = lastdir; | |
1230 | flist_dir_len = lastdir_len; | |
1231 | } else { | |
882e6893 WD |
1232 | flist_dir = lastdir = strdup(dir); |
1233 | flist_dir_len = lastdir_len = strlen(dir); | |
1234 | } | |
2bca43f6 | 1235 | } |
ebed4c3a | 1236 | |
2bca43f6 DD |
1237 | if (one_file_system) |
1238 | set_filesystem(fname); | |
1239 | ||
9f125ea7 WD |
1240 | do_subdirs = recurse >= 1 ? recurse-- : recurse; |
1241 | send_file_name(f, flist, fname, do_subdirs, XMIT_DEL_START); | |
2bca43f6 | 1242 | |
808c57c3 | 1243 | if (olddir[0]) { |
649d65ed | 1244 | flist_dir = NULL; |
882e6893 | 1245 | flist_dir_len = 0; |
808c57c3 | 1246 | if (!pop_dir(olddir)) { |
d62bcc17 WD |
1247 | rsyserr(FERROR, errno, "pop_dir %s failed", |
1248 | full_fname(dir)); | |
65417579 | 1249 | exit_cleanup(RERR_FILESELECT); |
649d65ed | 1250 | } |
649d65ed | 1251 | } |
649d65ed | 1252 | } |
dc5ddbcc | 1253 | |
983b1ed3 | 1254 | if (f != -1) { |
31b4d25d WD |
1255 | gettimeofday(&end_tv, NULL); |
1256 | stats.flist_buildtime = | |
1257 | (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000 | |
1258 | + (end_tv.tv_usec - start_tv.tv_usec) / 1000; | |
1259 | if (stats.flist_buildtime == 0) | |
1260 | stats.flist_buildtime = 1; | |
1261 | start_tv = end_tv; | |
1262 | ||
ebed4c3a | 1263 | send_file_entry(NULL, f, 0); |
c627d613 | 1264 | |
983b1ed3 WD |
1265 | if (show_filelist_p()) |
1266 | finish_filelist_progress(flist); | |
31b4d25d WD |
1267 | |
1268 | gettimeofday(&end_tv, NULL); | |
1269 | stats.flist_xfertime = | |
1270 | (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000 | |
1271 | + (end_tv.tv_usec - start_tv.tv_usec) / 1000; | |
983b1ed3 | 1272 | } |
ebed4c3a | 1273 | |
7cf8e8d0 | 1274 | if (flist->hlink_pool) { |
9935066b S |
1275 | pool_destroy(flist->hlink_pool); |
1276 | flist->hlink_pool = NULL; | |
1277 | } | |
1278 | ||
827c37f6 | 1279 | clean_flist(flist, 0, 0); |
ebed4c3a | 1280 | |
785db4ce WD |
1281 | if (f != -1) { |
1282 | /* Now send the uid/gid list. This was introduced in | |
1283 | * protocol version 15 */ | |
649d65ed | 1284 | send_uid_list(f); |
f6c34742 | 1285 | |
983b1ed3 | 1286 | /* send the io_error flag */ |
ebed4c3a | 1287 | write_int(f, lp_ignore_errors(module_id) ? 0 : io_error); |
6ba9279f | 1288 | |
a261989c | 1289 | io_end_buffering(); |
a800434a AT |
1290 | stats.flist_size = stats.total_written - start_write; |
1291 | stats.num_files = flist->count; | |
d6dead6b AT |
1292 | } |
1293 | ||
cefed3e8 | 1294 | if (verbose > 3) |
48ea74bf | 1295 | output_flist(flist, f < 0 ? "delete" : who_am_i()); |
cefed3e8 | 1296 | |
17faa41c | 1297 | if (verbose > 2) |
ebed4c3a | 1298 | rprintf(FINFO, "send_file_list done\n"); |
17faa41c | 1299 | |
649d65ed | 1300 | return flist; |
c627d613 AT |
1301 | } |
1302 | ||
1303 | ||
1304 | struct file_list *recv_file_list(int f) | |
1305 | { | |
ebed4c3a | 1306 | struct file_list *flist; |
1ef00d20 | 1307 | unsigned short flags; |
ebed4c3a | 1308 | int64 start_read; |
c627d613 | 1309 | |
1bbd10fe DD |
1310 | if (show_filelist_p()) |
1311 | start_filelist_progress("receiving file list"); | |
c627d613 | 1312 | |
ebed4c3a | 1313 | start_read = stats.total_read; |
a800434a | 1314 | |
9935066b | 1315 | flist = flist_new(WITH_HLINK, "recv_file_list"); |
23f4587f | 1316 | received_flist = flist; |
c627d613 | 1317 | |
ebed4c3a MP |
1318 | flist->count = 0; |
1319 | flist->malloced = 1000; | |
58cadc86 | 1320 | flist->files = new_array(struct file_struct *, flist->malloced); |
ebed4c3a MP |
1321 | if (!flist->files) |
1322 | goto oom; | |
c627d613 AT |
1323 | |
1324 | ||
1ef00d20 | 1325 | while ((flags = read_byte(f)) != 0) { |
5d2c5c4c | 1326 | int i = flist->count; |
dbda5fbf | 1327 | |
d9d6bc52 | 1328 | flist_expand(flist); |
c627d613 | 1329 | |
d01d15e0 | 1330 | if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS)) |
75bc8600 | 1331 | flags |= read_byte(f) << 8; |
9935066b | 1332 | receive_file_entry(&flist->files[i], flags, flist, f); |
c627d613 | 1333 | |
ebed4c3a MP |
1334 | if (S_ISREG(flist->files[i]->mode)) |
1335 | stats.total_size += flist->files[i]->length; | |
c627d613 | 1336 | |
ebed4c3a | 1337 | flist->count++; |
c627d613 | 1338 | |
db719fb0 | 1339 | maybe_emit_filelist_progress(flist); |
1bbd10fe | 1340 | |
8018edd3 | 1341 | if (verbose > 2) { |
ebed4c3a MP |
1342 | rprintf(FINFO, "recv_file_name(%s)\n", |
1343 | f_name(flist->files[i])); | |
8018edd3 | 1344 | } |
ebed4c3a | 1345 | } |
9935066b | 1346 | receive_file_entry(NULL, 0, NULL, 0); /* Signal that we're done. */ |
c627d613 | 1347 | |
ebed4c3a MP |
1348 | if (verbose > 2) |
1349 | rprintf(FINFO, "received %d names\n", flist->count); | |
c627d613 | 1350 | |
b7736c79 | 1351 | if (show_filelist_p()) |
1bbd10fe | 1352 | finish_filelist_progress(flist); |
a06d19e3 | 1353 | |
983b1ed3 WD |
1354 | clean_flist(flist, relative_paths, 1); |
1355 | ||
785db4ce WD |
1356 | if (f != -1) { |
1357 | /* Now send the uid/gid list. This was introduced in | |
1358 | * protocol version 15 */ | |
ebed4c3a | 1359 | recv_uid_list(f, flist); |
f6c34742 | 1360 | |
b9f592fb WD |
1361 | /* Recv the io_error flag */ |
1362 | if (lp_ignore_errors(module_id) || ignore_errors) | |
1363 | read_int(f); | |
1364 | else | |
1365 | io_error |= read_int(f); | |
ebed4c3a | 1366 | } |
6ba9279f | 1367 | |
cefed3e8 | 1368 | if (verbose > 3) |
48ea74bf | 1369 | output_flist(flist, who_am_i()); |
cefed3e8 | 1370 | |
ebed4c3a MP |
1371 | if (list_only) { |
1372 | int i; | |
b7736c79 | 1373 | for (i = 0; i < flist->count; i++) |
ebed4c3a | 1374 | list_file_entry(flist->files[i]); |
ebed4c3a | 1375 | } |
f7632fc6 | 1376 | |
ebed4c3a MP |
1377 | if (verbose > 2) |
1378 | rprintf(FINFO, "recv_file_list done\n"); | |
17faa41c | 1379 | |
ebed4c3a MP |
1380 | stats.flist_size = stats.total_read - start_read; |
1381 | stats.num_files = flist->count; | |
a800434a | 1382 | |
ebed4c3a | 1383 | return flist; |
c627d613 | 1384 | |
3e491682 | 1385 | oom: |
ebed4c3a MP |
1386 | out_of_memory("recv_file_list"); |
1387 | return NULL; /* not reached */ | |
c627d613 AT |
1388 | } |
1389 | ||
1390 | ||
fa45cda1 | 1391 | int file_compare(struct file_struct **file1, struct file_struct **file2) |
c627d613 | 1392 | { |
fa45cda1 S |
1393 | struct file_struct *f1 = *file1; |
1394 | struct file_struct *f2 = *file2; | |
1395 | ||
1396 | if (!f1->basename && !f2->basename) | |
ebed4c3a | 1397 | return 0; |
fa45cda1 | 1398 | if (!f1->basename) |
ebed4c3a | 1399 | return -1; |
fa45cda1 | 1400 | if (!f2->basename) |
ebed4c3a | 1401 | return 1; |
fa45cda1 S |
1402 | if (f1->dirname == f2->dirname) |
1403 | return u_strcmp(f1->basename, f2->basename); | |
1404 | return f_name_cmp(f1, f2); | |
c627d613 AT |
1405 | } |
1406 | ||
1407 | ||
ebed4c3a | 1408 | int flist_find(struct file_list *flist, struct file_struct *f) |
c627d613 | 1409 | { |
ebed4c3a | 1410 | int low = 0, high = flist->count - 1; |
d966ee25 | 1411 | |
ca23c51a WD |
1412 | while (high >= 0 && !flist->files[high]->basename) high--; |
1413 | ||
1414 | if (high < 0) | |
ebed4c3a | 1415 | return -1; |
d966ee25 AT |
1416 | |
1417 | while (low != high) { | |
ebed4c3a | 1418 | int mid = (low + high) / 2; |
b7736c79 | 1419 | int ret = file_compare(&flist->files[flist_up(flist, mid)],&f); |
ebed4c3a MP |
1420 | if (ret == 0) |
1421 | return flist_up(flist, mid); | |
b7736c79 | 1422 | if (ret > 0) |
ebed4c3a | 1423 | high = mid; |
b7736c79 | 1424 | else |
ebed4c3a | 1425 | low = mid + 1; |
d966ee25 AT |
1426 | } |
1427 | ||
ebed4c3a MP |
1428 | if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0) |
1429 | return flist_up(flist, low); | |
d966ee25 | 1430 | return -1; |
c627d613 AT |
1431 | } |
1432 | ||
3ec4dd97 | 1433 | /* |
9935066b S |
1434 | * Free up any resources a file_struct has allocated |
1435 | * and clear the file. | |
3ec4dd97 | 1436 | */ |
9935066b | 1437 | void clear_file(int i, struct file_list *flist) |
c627d613 | 1438 | { |
9935066b S |
1439 | if (flist->hlink_pool && flist->files[i]->link_u.idev) |
1440 | pool_free(flist->hlink_pool, 0, flist->files[i]->link_u.idev); | |
1441 | memset(flist->files[i], 0, file_struct_len); | |
3ec4dd97 | 1442 | } |
c627d613 | 1443 | |
c627d613 | 1444 | |
3d382777 AT |
1445 | /* |
1446 | * allocate a new file list | |
1447 | */ | |
9935066b | 1448 | struct file_list *flist_new(int with_hlink, char *msg) |
3d382777 AT |
1449 | { |
1450 | struct file_list *flist; | |
1451 | ||
58cadc86 | 1452 | flist = new(struct file_list); |
ebed4c3a | 1453 | if (!flist) |
9935066b | 1454 | out_of_memory(msg); |
3d382777 | 1455 | |
9935066b S |
1456 | memset(flist, 0, sizeof (struct file_list)); |
1457 | ||
1458 | if (!(flist->file_pool = pool_create(FILE_EXTENT, 0, | |
1459 | out_of_memory, POOL_INTERN))) | |
1460 | out_of_memory(msg); | |
1461 | ||
1462 | #if SUPPORT_HARD_LINKS | |
1463 | if (with_hlink && preserve_hard_links) { | |
3e491682 | 1464 | if (!(flist->hlink_pool = pool_create(HLINK_EXTENT, |
9935066b S |
1465 | sizeof (struct idev), out_of_memory, POOL_INTERN))) |
1466 | out_of_memory(msg); | |
1467 | } | |
1468 | #endif | |
d9d6bc52 | 1469 | |
3d382777 AT |
1470 | return flist; |
1471 | } | |
ebed4c3a | 1472 | |
3ec4dd97 AT |
1473 | /* |
1474 | * free up all elements in a flist | |
1475 | */ | |
1476 | void flist_free(struct file_list *flist) | |
1477 | { | |
9935066b S |
1478 | pool_destroy(flist->file_pool); |
1479 | pool_destroy(flist->hlink_pool); | |
3ec4dd97 | 1480 | free(flist->files); |
3ec4dd97 | 1481 | free(flist); |
c627d613 AT |
1482 | } |
1483 | ||
1484 | ||
1485 | /* | |
1486 | * This routine ensures we don't have any duplicate names in our file list. | |
dbda5fbf | 1487 | * duplicate names can cause corruption because of the pipelining |
c627d613 | 1488 | */ |
827c37f6 | 1489 | static void clean_flist(struct file_list *flist, int strip_root, int no_dups) |
c627d613 | 1490 | { |
6931c138 | 1491 | int i, prev_i = 0; |
c627d613 | 1492 | |
ebed4c3a | 1493 | if (!flist || flist->count == 0) |
3ec4dd97 | 1494 | return; |
3ec4dd97 | 1495 | |
ebed4c3a | 1496 | qsort(flist->files, flist->count, |
a4a7e64c | 1497 | sizeof flist->files[0], (int (*)())file_compare); |
ebed4c3a | 1498 | |
827c37f6 | 1499 | for (i = no_dups? 0 : flist->count; i < flist->count; i++) { |
b91b50c0 | 1500 | if (flist->files[i]->basename) { |
6931c138 | 1501 | prev_i = i; |
b91b50c0 WD |
1502 | break; |
1503 | } | |
1504 | } | |
1505 | while (++i < flist->count) { | |
1506 | if (!flist->files[i]->basename) | |
1507 | continue; | |
8018edd3 | 1508 | if (f_name_cmp(flist->files[i], flist->files[prev_i]) == 0) { |
b91b50c0 | 1509 | if (verbose > 1 && !am_server) { |
ebed4c3a MP |
1510 | rprintf(FINFO, |
1511 | "removing duplicate name %s from file list %d\n", | |
8018edd3 | 1512 | f_name(flist->files[i]), i); |
b91b50c0 | 1513 | } |
6931c138 WD |
1514 | /* Make sure that if we unduplicate '.', that we don't |
1515 | * lose track of a user-specified starting point (or | |
1516 | * else deletions will mysteriously fail with -R). */ | |
71e27c46 WD |
1517 | if (flist->files[i]->flags & FLAG_DEL_START) |
1518 | flist->files[prev_i]->flags |= FLAG_DEL_START; | |
9935066b S |
1519 | |
1520 | clear_file(i, flist); | |
728d0922 | 1521 | } else |
6931c138 | 1522 | prev_i = i; |
3ec4dd97 | 1523 | } |
0199b05f AT |
1524 | |
1525 | if (strip_root) { | |
1526 | /* we need to strip off the root directory in the case | |
1527 | of relative paths, but this must be done _after_ | |
1528 | the sorting phase */ | |
ebed4c3a | 1529 | for (i = 0; i < flist->count; i++) { |
0199b05f AT |
1530 | if (flist->files[i]->dirname && |
1531 | flist->files[i]->dirname[0] == '/') { | |
1532 | memmove(&flist->files[i]->dirname[0], | |
1533 | &flist->files[i]->dirname[1], | |
1534 | strlen(flist->files[i]->dirname)); | |
1535 | } | |
ebed4c3a MP |
1536 | |
1537 | if (flist->files[i]->dirname && | |
0199b05f AT |
1538 | !flist->files[i]->dirname[0]) { |
1539 | flist->files[i]->dirname = NULL; | |
1540 | } | |
1541 | } | |
1542 | } | |
cefed3e8 | 1543 | } |
0199b05f | 1544 | |
48ea74bf | 1545 | static void output_flist(struct file_list *flist, const char *whose_list) |
cefed3e8 WD |
1546 | { |
1547 | char uidbuf[16], gidbuf[16]; | |
1548 | struct file_struct *file; | |
1549 | int i; | |
0199b05f | 1550 | |
ebed4c3a | 1551 | for (i = 0; i < flist->count; i++) { |
cefed3e8 | 1552 | file = flist->files[i]; |
56194bcd | 1553 | if ((am_root || am_sender) && preserve_uid) |
f05f993e WD |
1554 | sprintf(uidbuf, " uid=%ld", (long)file->uid); |
1555 | else | |
1556 | *uidbuf = '\0'; | |
cefed3e8 | 1557 | if (preserve_gid && file->gid != GID_NONE) |
f05f993e WD |
1558 | sprintf(gidbuf, " gid=%ld", (long)file->gid); |
1559 | else | |
1560 | *gidbuf = '\0'; | |
1561 | rprintf(FINFO, "[%s] i=%d %s %s %s mode=0%o len=%.0f%s%s\n", | |
48ea74bf | 1562 | whose_list, i, NS(file->basedir), NS(file->dirname), |
a4a7e64c WD |
1563 | NS(file->basename), (int)file->mode, |
1564 | (double)file->length, uidbuf, gidbuf); | |
0199b05f | 1565 | } |
3ec4dd97 AT |
1566 | } |
1567 | ||
1568 | ||
8018edd3 WD |
1569 | enum fnc_state { fnc_DIR, fnc_SLASH, fnc_BASE }; |
1570 | ||
1571 | /* Compare the names of two file_struct entities, just like strcmp() | |
1572 | * would do if it were operating on the joined strings. We assume | |
1573 | * that there are no 0-length strings. | |
3ec4dd97 | 1574 | */ |
8018edd3 | 1575 | int f_name_cmp(struct file_struct *f1, struct file_struct *f2) |
3ec4dd97 | 1576 | { |
8018edd3 WD |
1577 | int dif; |
1578 | const uchar *c1, *c2; | |
1ef00d20 | 1579 | enum fnc_state state1, state2; |
8018edd3 WD |
1580 | |
1581 | if (!f1 || !f1->basename) { | |
1582 | if (!f2 || !f2->basename) | |
1583 | return 0; | |
1584 | return -1; | |
1585 | } | |
1586 | if (!f2 || !f2->basename) | |
1587 | return 1; | |
1588 | ||
e90b8ace | 1589 | if (!(c1 = (uchar*)f1->dirname)) { |
8018edd3 | 1590 | state1 = fnc_BASE; |
e90b8ace | 1591 | c1 = (uchar*)f1->basename; |
080ddf58 WD |
1592 | } else if (!*c1) { |
1593 | state1 = fnc_SLASH; | |
1594 | c1 = (uchar*)"/"; | |
728d0922 | 1595 | } else |
1ef00d20 | 1596 | state1 = fnc_DIR; |
e90b8ace | 1597 | if (!(c2 = (uchar*)f2->dirname)) { |
8018edd3 | 1598 | state2 = fnc_BASE; |
e90b8ace | 1599 | c2 = (uchar*)f2->basename; |
080ddf58 WD |
1600 | } else if (!*c2) { |
1601 | state2 = fnc_SLASH; | |
1602 | c2 = (uchar*)"/"; | |
728d0922 | 1603 | } else |
1ef00d20 | 1604 | state2 = fnc_DIR; |
8018edd3 WD |
1605 | |
1606 | while (1) { | |
1607 | if ((dif = (int)*c1 - (int)*c2) != 0) | |
1608 | break; | |
1609 | if (!*++c1) { | |
1610 | switch (state1) { | |
1611 | case fnc_DIR: | |
1612 | state1 = fnc_SLASH; | |
e90b8ace | 1613 | c1 = (uchar*)"/"; |
8018edd3 WD |
1614 | break; |
1615 | case fnc_SLASH: | |
1616 | state1 = fnc_BASE; | |
e90b8ace | 1617 | c1 = (uchar*)f1->basename; |
8018edd3 WD |
1618 | break; |
1619 | case fnc_BASE: | |
1620 | break; | |
1621 | } | |
1622 | } | |
1623 | if (!*++c2) { | |
1624 | switch (state2) { | |
1625 | case fnc_DIR: | |
1626 | state2 = fnc_SLASH; | |
e90b8ace | 1627 | c2 = (uchar*)"/"; |
8018edd3 WD |
1628 | break; |
1629 | case fnc_SLASH: | |
1630 | state2 = fnc_BASE; | |
e90b8ace | 1631 | c2 = (uchar*)f2->basename; |
8018edd3 WD |
1632 | break; |
1633 | case fnc_BASE: | |
1634 | if (!*c1) | |
1635 | return 0; | |
1636 | break; | |
1637 | } | |
1638 | } | |
1639 | } | |
1640 | ||
1641 | return dif; | |
1642 | } | |
1643 | ||
3ec4dd97 | 1644 | |
8018edd3 | 1645 | /* Return a copy of the full filename of a flist entry, using the indicated |
882e6893 WD |
1646 | * buffer. No size-checking is done because we checked the size when creating |
1647 | * the file_struct entry. | |
8018edd3 | 1648 | */ |
882e6893 | 1649 | char *f_name_to(struct file_struct *f, char *fbuf) |
8018edd3 | 1650 | { |
ebed4c3a MP |
1651 | if (!f || !f->basename) |
1652 | return NULL; | |
3ec4dd97 | 1653 | |
3ec4dd97 | 1654 | if (f->dirname) { |
882e6893 WD |
1655 | int len = strlen(f->dirname); |
1656 | memcpy(fbuf, f->dirname, len); | |
1657 | fbuf[len] = '/'; | |
1658 | strcpy(fbuf + len + 1, f->basename); | |
8018edd3 | 1659 | } else |
882e6893 | 1660 | strcpy(fbuf, f->basename); |
b7736c79 | 1661 | return fbuf; |
8018edd3 | 1662 | } |
e03dfae5 | 1663 | |
3ec4dd97 | 1664 | |
8018edd3 WD |
1665 | /* Like f_name_to(), but we rotate through 5 static buffers of our own. |
1666 | */ | |
1667 | char *f_name(struct file_struct *f) | |
1668 | { | |
1669 | static char names[5][MAXPATHLEN]; | |
1670 | static unsigned int n; | |
1671 | ||
1672 | n = (n + 1) % (sizeof names / sizeof names[0]); | |
1673 | ||
882e6893 | 1674 | return f_name_to(f, names[n]); |
c627d613 | 1675 | } |
649f8742 WD |
1676 | |
1677 | static int is_backup_file(char *fn) | |
1678 | { | |
1679 | int k = strlen(fn) - backup_suffix_len; | |
1680 | return k > 0 && strcmp(fn+k, backup_suffix) == 0; | |
1681 | } | |
1682 | ||
1683 | void delete_in_dir(struct file_list *flist, char *fname) | |
1684 | { | |
1685 | static int deletion_count = 0; | |
1686 | struct file_list *del_flist; | |
1687 | int save_recurse = recurse; | |
1688 | int save_xfer_dirs = xfer_dirs; | |
1689 | int save_implied_dirs = implied_dirs; | |
1690 | int save_relative_paths = relative_paths; | |
1691 | char *argv[1]; | |
1692 | int i, j, mode; | |
1693 | ||
1694 | if (max_delete && deletion_count >= max_delete) | |
1695 | return; | |
1696 | ||
1697 | if (io_error && !(lp_ignore_errors(module_id) || ignore_errors)) { | |
1698 | rprintf(FINFO, "IO error encountered - skipping file deletion\n"); | |
1699 | max_delete = -1; /* avoid duplicating the above warning */ | |
1700 | return; | |
1701 | } | |
1702 | ||
1703 | if (delete_during) { | |
1704 | recurse = 1; /* allow one level only */ | |
1705 | xfer_dirs = 1; | |
1706 | implied_dirs = 0; | |
1707 | relative_paths = 1; | |
1708 | } | |
1709 | ||
1710 | argv[0] = fname; | |
1711 | del_flist = send_file_list(-1, 1, argv); | |
1712 | ||
1713 | relative_paths = save_relative_paths; | |
1714 | implied_dirs = save_implied_dirs; | |
1715 | xfer_dirs = save_xfer_dirs; | |
1716 | recurse = save_recurse; | |
1717 | ||
1718 | if (!del_flist) | |
1719 | return; | |
1720 | ||
1721 | if (verbose > 1) | |
1722 | rprintf(FINFO, "deleting in %s\n", safe_fname(fname)); | |
1723 | ||
1724 | for (i = del_flist->count-1; i >= 0; i--) { | |
1725 | if (max_delete && deletion_count >= max_delete) | |
1726 | break; | |
1727 | if (!del_flist->files[i]->basename) | |
1728 | continue; | |
1729 | mode = del_flist->files[i]->mode; | |
1730 | if ((j = flist_find(flist, del_flist->files[i])) < 0 | |
1731 | || (delete_during && S_ISDIR(mode) | |
1732 | && !S_ISDIR(flist->files[j]->mode))) { | |
1733 | char *f = f_name(del_flist->files[i]); | |
1734 | if (make_backups && (backup_dir || !is_backup_file(f)) | |
1735 | && !S_ISDIR(mode)) { | |
1736 | make_backup(f); | |
1737 | if (verbose) { | |
1738 | rprintf(FINFO, "deleting %s\n", | |
1739 | safe_fname(f)); | |
1740 | } | |
1741 | } else { | |
217cc3b0 WD |
1742 | int dflags = delete_during |
1743 | ? DEL_DIR | DEL_FORCE_RECURSE | |
1744 | : DEL_DIR | DEL_NO_RECURSE; | |
1745 | delete_file(f, S_ISDIR(mode) ? dflags : 0); | |
649f8742 WD |
1746 | } |
1747 | deletion_count++; | |
1748 | } | |
1749 | } | |
1750 | flist_free(del_flist); | |
1751 | } |