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