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