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