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