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 | ||
7b1a0c19 | 309 | void send_file_entry(struct file_struct *file, int f, unsigned short base_flags) |
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 AT |
339 | |
340 | flags = base_flags; | |
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 | |
a289addd WD |
572 | if (preserve_uid && !(flags & XMIT_SAME_UID)) |
573 | uid = (uid_t)read_int(f); | |
574 | if (preserve_gid && !(flags & XMIT_SAME_GID)) | |
575 | gid = (gid_t)read_int(f); | |
576 | ||
1ef00d20 | 577 | if (preserve_devices) { |
75bc8600 | 578 | if (protocol_version < 28) { |
30f337c9 | 579 | if (IS_DEVICE(mode)) { |
d01d15e0 | 580 | if (!(flags & XMIT_SAME_RDEV_pre28)) |
9c5e91f8 | 581 | rdev = (dev_t)read_int(f); |
75bc8600 | 582 | } else |
9c5e91f8 | 583 | rdev = makedev(0, 0); |
30f337c9 | 584 | } else if (IS_DEVICE(mode)) { |
9c5e91f8 WD |
585 | uint32 rdev_minor; |
586 | if (!(flags & XMIT_SAME_RDEV_MAJOR)) | |
587 | rdev_major = read_int(f); | |
588 | if (flags & XMIT_RDEV_MINOR_IS_SMALL) | |
589 | rdev_minor = read_byte(f); | |
590 | else | |
591 | rdev_minor = read_int(f); | |
592 | rdev = makedev(rdev_major, rdev_minor); | |
1ef00d20 | 593 | } |
b7736c79 | 594 | } |
72914a60 | 595 | |
4f5b0756 | 596 | #ifdef SUPPORT_LINKS |
30f337c9 | 597 | if (preserve_links && S_ISLNK(mode)) { |
a289addd WD |
598 | linkname_len = read_int(f) + 1; /* count the '\0' */ |
599 | if (linkname_len <= 0 || linkname_len > MAXPATHLEN) { | |
600 | rprintf(FERROR, "overflow: linkname_len=%d\n", | |
601 | linkname_len - 1); | |
a1f99493 | 602 | overflow_exit("receive_file_entry"); |
9dd891bb | 603 | } |
72914a60 | 604 | } |
a289addd | 605 | else |
0d162bd1 | 606 | #endif |
a289addd | 607 | linkname_len = 0; |
0d162bd1 | 608 | |
a289addd WD |
609 | sum_len = always_checksum && S_ISREG(mode) ? MD4_SUM_LENGTH : 0; |
610 | ||
61dec11a | 611 | alloc_len = file_struct_len + dirname_len + basename_len |
9935066b S |
612 | + linkname_len + sum_len; |
613 | bp = pool_alloc(flist->file_pool, alloc_len, "receive_file_entry"); | |
614 | ||
f5db0993 | 615 | file = (struct file_struct *)bp; |
9935066b | 616 | memset(bp, 0, file_struct_len); |
61dec11a | 617 | bp += file_struct_len; |
a289addd | 618 | |
649f8742 | 619 | file->flags = 0; |
a289addd WD |
620 | file->modtime = modtime; |
621 | file->length = file_length; | |
622 | file->mode = mode; | |
623 | file->uid = uid; | |
624 | file->gid = gid; | |
625 | ||
f3c3ed44 WD |
626 | if (dirname_len) { |
627 | file->dirname = lastdir = bp; | |
628 | lastdir_len = dirname_len - 1; | |
629 | memcpy(bp, dirname, dirname_len - 1); | |
630 | bp += dirname_len; | |
631 | bp[-1] = '\0'; | |
632 | lastdir_depth = count_dir_elements(lastdir); | |
633 | file->dir.depth = lastdir_depth + 1; | |
634 | } else if (dirname) { | |
635 | file->dirname = dirname; /* we're reusing lastname */ | |
636 | file->dir.depth = lastdir_depth + 1; | |
637 | } else | |
638 | file->dir.depth = 1; | |
639 | ||
649f8742 | 640 | if (S_ISDIR(mode)) { |
ee3751c8 | 641 | if (basename_len == 1+1 && *basename == '.') /* +1 for '\0' */ |
f3c3ed44 | 642 | file->dir.depth--; |
ee3751c8 | 643 | if (flags & XMIT_TOP_DIR) { |
75c51953 | 644 | in_del_hier = recurse; |
f3c3ed44 | 645 | del_hier_name_len = file->dir.depth == 0 ? 0 : l1 + l2; |
7a16e122 WD |
646 | if (relative_paths && del_hier_name_len > 2 |
647 | && basename_len == 1+1 && *basename == '.') | |
648 | del_hier_name_len -= 2; | |
ee3751c8 WD |
649 | file->flags |= FLAG_TOP_DIR | FLAG_DEL_HERE; |
650 | } else if (in_del_hier) { | |
f3c3ed44 WD |
651 | if (!relative_paths || !del_hier_name_len |
652 | || (l1 >= del_hier_name_len | |
653 | && thisname[del_hier_name_len] == '/')) | |
ee3751c8 | 654 | file->flags |= FLAG_DEL_HERE; |
649f8742 WD |
655 | else |
656 | in_del_hier = 0; | |
657 | } | |
658 | } | |
659 | ||
a289addd WD |
660 | file->basename = bp; |
661 | memcpy(bp, basename, basename_len); | |
662 | bp += basename_len; | |
663 | ||
664 | if (preserve_devices && IS_DEVICE(mode)) | |
665 | file->u.rdev = rdev; | |
666 | ||
4f5b0756 | 667 | #ifdef SUPPORT_LINKS |
a289addd WD |
668 | if (linkname_len) { |
669 | file->u.link = bp; | |
670 | read_sbuf(f, bp, linkname_len - 1); | |
671 | if (sanitize_paths) | |
33ffd7c3 | 672 | sanitize_path(bp, bp, "", lastdir_depth); |
a289addd WD |
673 | bp += linkname_len; |
674 | } | |
675 | #endif | |
676 | ||
4f5b0756 | 677 | #ifdef SUPPORT_HARD_LINKS |
9935066b S |
678 | if (preserve_hard_links && protocol_version < 28 && S_ISREG(mode)) |
679 | flags |= XMIT_HAS_IDEV_DATA; | |
97a67bdf | 680 | if (flags & XMIT_HAS_IDEV_DATA) { |
1490812a | 681 | int64 inode; |
4124540d WD |
682 | if (protocol_version < 26) { |
683 | dev = read_int(f); | |
9935066b | 684 | inode = read_int(f); |
736a6a29 | 685 | } else { |
4124540d WD |
686 | if (!(flags & XMIT_SAME_DEV)) |
687 | dev = read_longint(f); | |
9935066b S |
688 | inode = read_longint(f); |
689 | } | |
690 | if (flist->hlink_pool) { | |
5bf63a11 S |
691 | file->link_u.idev = pool_talloc(flist->hlink_pool, |
692 | struct idev, 1, "inode_table"); | |
9935066b S |
693 | file->F_INODE = inode; |
694 | file->F_DEV = dev; | |
736a6a29 | 695 | } |
72914a60 | 696 | } |
dc5ddbcc | 697 | #endif |
ebed4c3a | 698 | |
9a4a237e | 699 | if (always_checksum && (sum_len || protocol_version < 28)) { |
7c4f063b | 700 | char *sum; |
a289addd WD |
701 | if (sum_len) { |
702 | file->u.sum = sum = bp; | |
703 | /*bp += sum_len;*/ | |
9a4a237e | 704 | } else { |
fea4db62 | 705 | /* Prior to 28, we get a useless set of nulls. */ |
7c4f063b | 706 | sum = empty_sum; |
fea4db62 | 707 | } |
7752df41 | 708 | read_buf(f, sum, checksum_len); |
72914a60 | 709 | } |
ebed4c3a | 710 | |
72914a60 | 711 | if (!preserve_perms) { |
72914a60 | 712 | /* set an appropriate set of permissions based on original |
fea4db62 | 713 | * permissions and umask. This emulates what GNU cp does */ |
72914a60 AT |
714 | file->mode &= ~orig_umask; |
715 | } | |
f5db0993 WD |
716 | |
717 | return file; | |
c627d613 AT |
718 | } |
719 | ||
db719fb0 MP |
720 | /** |
721 | * Create a file_struct for a named file by reading its stat() | |
722 | * information and performing extensive checks against global | |
723 | * options. | |
724 | * | |
725 | * @return the new file, or NULL if there was an error or this file | |
726 | * should be excluded. | |
727 | * | |
728 | * @todo There is a small optimization opportunity here to avoid | |
729 | * stat()ing the file in some circumstances, which has a certain cost. | |
730 | * We are called immediately after doing readdir(), and so we may | |
731 | * already know the d_type of the file. We could for example avoid | |
732 | * statting directories if we're not recursing, but this is not a very | |
733 | * important case. Some systems may not have d_type. | |
734 | **/ | |
314f4591 | 735 | struct file_struct *make_file(char *fname, struct file_list *flist, |
7842418b | 736 | int filter_level) |
c627d613 | 737 | { |
a289addd WD |
738 | static char *lastdir; |
739 | static int lastdir_len = -1; | |
3ec4dd97 | 740 | struct file_struct *file; |
bcacc18b | 741 | STRUCT_STAT st; |
3ec4dd97 | 742 | char sum[SUM_LENGTH]; |
1923b1fc | 743 | char thisname[MAXPATHLEN]; |
e0870f1d | 744 | char linkname[MAXPATHLEN]; |
a1d55ad0 | 745 | int alloc_len, basename_len, dirname_len, linkname_len, sum_len; |
a289addd | 746 | char *basename, *dirname, *bp; |
4844449a | 747 | unsigned short flags = 0; |
3ec4dd97 | 748 | |
d508258a | 749 | if (!flist || !flist->count) /* Ignore lastdir when invalid. */ |
7de2483f | 750 | lastdir_len = -1; |
9935066b | 751 | |
1923b1fc WD |
752 | if (strlcpy(thisname, fname, sizeof thisname) |
753 | >= sizeof thisname - flist_dir_len) { | |
0ee6ca98 | 754 | rprintf(FINFO, "skipping overly long name: %s\n", fname); |
882e6893 WD |
755 | return NULL; |
756 | } | |
58b1999e | 757 | clean_fname(thisname, 0); |
b7736c79 | 758 | if (sanitize_paths) |
33ffd7c3 | 759 | sanitize_path(thisname, thisname, "", 0); |
3ec4dd97 | 760 | |
ebed4c3a | 761 | memset(sum, 0, SUM_LENGTH); |
3ec4dd97 | 762 | |
e0870f1d | 763 | if (readlink_stat(thisname, &st, linkname) != 0) { |
76e26e10 | 764 | int save_errno = errno; |
a4a7e64c | 765 | /* See if file is excluded before reporting an error. */ |
7842418b WD |
766 | if (filter_level != NO_FILTERS |
767 | && is_excluded(thisname, 0, filter_level)) | |
a4a7e64c WD |
768 | return NULL; |
769 | if (save_errno == ENOENT) { | |
4f5b0756 | 770 | #ifdef SUPPORT_LINKS |
a4a7e64c WD |
771 | /* Avoid "vanished" error if symlink points nowhere. */ |
772 | if (copy_links && do_lstat(thisname, &st) == 0 | |
773 | && S_ISLNK(st.st_mode)) { | |
774 | io_error |= IOERR_GENERAL; | |
775 | rprintf(FERROR, "symlink has no referent: %s\n", | |
776 | full_fname(thisname)); | |
e5ce3bcf WD |
777 | } else |
778 | #endif | |
779 | { | |
a4a7e64c WD |
780 | enum logcode c = am_daemon && protocol_version < 28 |
781 | ? FERROR : FINFO; | |
782 | io_error |= IOERR_VANISHED; | |
783 | rprintf(c, "file has vanished: %s\n", | |
784 | full_fname(thisname)); | |
76e26e10 | 785 | } |
a4a7e64c | 786 | } else { |
a8726d2a | 787 | io_error |= IOERR_GENERAL; |
d62bcc17 WD |
788 | rsyserr(FERROR, save_errno, "readlink %s failed", |
789 | full_fname(thisname)); | |
76e26e10 | 790 | } |
3ec4dd97 AT |
791 | return NULL; |
792 | } | |
c627d613 | 793 | |
7842418b WD |
794 | /* backup.c calls us with filter_level set to NO_FILTERS. */ |
795 | if (filter_level == NO_FILTERS) | |
796 | goto skip_filters; | |
ac1a0994 | 797 | |
7e037c42 | 798 | if (S_ISDIR(st.st_mode) && !xfer_dirs) { |
0ee6ca98 | 799 | rprintf(FINFO, "skipping directory %s\n", thisname); |
3ec4dd97 AT |
800 | return NULL; |
801 | } | |
ebed4c3a | 802 | |
e90cdb8a WD |
803 | /* We only care about directories because we need to avoid recursing |
804 | * into a mount-point directory, not to avoid copying a symlinked | |
805 | * file if -L (or similar) was specified. */ | |
806 | if (one_file_system && st.st_dev != filesystem_dev | |
807 | && S_ISDIR(st.st_mode)) | |
4844449a | 808 | flags |= FLAG_MOUNT_POINT; |
ebed4c3a | 809 | |
7842418b | 810 | if (is_excluded(thisname, S_ISDIR(st.st_mode) != 0, filter_level)) |
76e26e10 DD |
811 | return NULL; |
812 | ||
132fcf36 | 813 | if (lp_ignore_nonreadable(module_id)) { |
4f5b0756 | 814 | #ifdef SUPPORT_LINKS |
132fcf36 WD |
815 | if (!S_ISLNK(st.st_mode)) |
816 | #endif | |
817 | if (access(thisname, R_OK) != 0) | |
818 | return NULL; | |
819 | } | |
ac1a0994 | 820 | |
97b7bff4 | 821 | skip_filters: |
ac1a0994 | 822 | |
ea847c62 WD |
823 | if (verbose > 2) { |
824 | rprintf(FINFO, "[%s] make_file(%s,*,%d)\n", | |
0ee6ca98 | 825 | who_am_i(), thisname, filter_level); |
ea847c62 | 826 | } |
ebed4c3a | 827 | |
a289addd WD |
828 | if ((basename = strrchr(thisname, '/')) != NULL) { |
829 | dirname_len = ++basename - thisname; /* counts future '\0' */ | |
830 | if (lastdir_len == dirname_len - 1 | |
831 | && strncmp(thisname, lastdir, lastdir_len) == 0) { | |
832 | dirname = lastdir; | |
833 | dirname_len = 0; /* indicates no copy is needed */ | |
834 | } else | |
835 | dirname = thisname; | |
3ec4dd97 | 836 | } else { |
a289addd WD |
837 | basename = thisname; |
838 | dirname = NULL; | |
839 | dirname_len = 0; | |
3ec4dd97 | 840 | } |
a289addd | 841 | basename_len = strlen(basename) + 1; /* count the '\0' */ |
c627d613 | 842 | |
4f5b0756 | 843 | #ifdef SUPPORT_LINKS |
a289addd WD |
844 | linkname_len = S_ISLNK(st.st_mode) ? strlen(linkname) + 1 : 0; |
845 | #else | |
846 | linkname_len = 0; | |
847 | #endif | |
848 | ||
902f03d1 WD |
849 | sum_len = always_checksum && am_sender && S_ISREG(st.st_mode) |
850 | ? MD4_SUM_LENGTH : 0; | |
a289addd | 851 | |
61dec11a | 852 | alloc_len = file_struct_len + dirname_len + basename_len |
9935066b | 853 | + linkname_len + sum_len; |
99aaa6ca WD |
854 | if (flist) |
855 | bp = pool_alloc(flist->file_pool, alloc_len, "make_file"); | |
856 | else { | |
9935066b | 857 | if (!(bp = new_array(char, alloc_len))) |
99aaa6ca | 858 | out_of_memory("make_file"); |
9935066b S |
859 | } |
860 | ||
a289addd | 861 | file = (struct file_struct *)bp; |
9935066b | 862 | memset(bp, 0, file_struct_len); |
61dec11a | 863 | bp += file_struct_len; |
a289addd WD |
864 | |
865 | file->flags = flags; | |
3ec4dd97 AT |
866 | file->modtime = st.st_mtime; |
867 | file->length = st.st_size; | |
2b7e0f33 WD |
868 | if (chmod_modes && am_sender && (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode))) |
869 | file->mode = tweak_mode(st.st_mode, chmod_modes); | |
870 | else | |
871 | file->mode = st.st_mode; | |
3ec4dd97 AT |
872 | file->uid = st.st_uid; |
873 | file->gid = st.st_gid; | |
0d162bd1 | 874 | |
4f5b0756 | 875 | #ifdef SUPPORT_HARD_LINKS |
9935066b S |
876 | if (flist && flist->hlink_pool) { |
877 | if (protocol_version < 28) { | |
878 | if (S_ISREG(st.st_mode)) | |
879 | file->link_u.idev = pool_talloc( | |
880 | flist->hlink_pool, struct idev, 1, | |
881 | "inode_table"); | |
882 | } else { | |
883 | if (!S_ISDIR(st.st_mode) && st.st_nlink > 1) | |
884 | file->link_u.idev = pool_talloc( | |
885 | flist->hlink_pool, struct idev, 1, | |
886 | "inode_table"); | |
887 | } | |
888 | } | |
889 | if (file->link_u.idev) { | |
61dec11a WD |
890 | file->F_DEV = st.st_dev; |
891 | file->F_INODE = st.st_ino; | |
892 | } | |
893 | #endif | |
894 | ||
a289addd WD |
895 | if (dirname_len) { |
896 | file->dirname = lastdir = bp; | |
897 | lastdir_len = dirname_len - 1; | |
898 | memcpy(bp, dirname, dirname_len - 1); | |
899 | bp += dirname_len; | |
900 | bp[-1] = '\0'; | |
901 | } else if (dirname) | |
902 | file->dirname = dirname; | |
903 | ||
904 | file->basename = bp; | |
905 | memcpy(bp, basename, basename_len); | |
906 | bp += basename_len; | |
907 | ||
4f5b0756 | 908 | #ifdef HAVE_STRUCT_STAT_ST_RDEV |
0d162bd1 WD |
909 | if (preserve_devices && IS_DEVICE(st.st_mode)) |
910 | file->u.rdev = st.st_rdev; | |
911 | #endif | |
912 | ||
4f5b0756 | 913 | #ifdef SUPPORT_LINKS |
a289addd WD |
914 | if (linkname_len) { |
915 | file->u.link = bp; | |
916 | memcpy(bp, linkname, linkname_len); | |
917 | bp += linkname_len; | |
918 | } | |
0d162bd1 WD |
919 | #endif |
920 | ||
a289addd WD |
921 | if (sum_len) { |
922 | file->u.sum = bp; | |
923 | file_checksum(thisname, bp, st.st_size); | |
924 | /*bp += sum_len;*/ | |
ebed4c3a | 925 | } |
c627d613 | 926 | |
f3c3ed44 | 927 | file->dir.root = flist_dir; |
c627d613 | 928 | |
23f4587f WD |
929 | /* This code is only used by the receiver when it is building |
930 | * a list of files for a delete pass. */ | |
931 | if (keep_dirlinks && linkname_len && flist) { | |
932 | STRUCT_STAT st2; | |
f5db0993 WD |
933 | int save_mode = file->mode; |
934 | file->mode = S_IFDIR; /* find a directory w/our name */ | |
d64e6f42 | 935 | if (flist_find(the_file_list, file) >= 0 |
23f4587f WD |
936 | && do_stat(thisname, &st2) == 0 && S_ISDIR(st2.st_mode)) { |
937 | file->modtime = st2.st_mtime; | |
938 | file->length = st2.st_size; | |
939 | file->mode = st2.st_mode; | |
940 | file->uid = st2.st_uid; | |
941 | file->gid = st2.st_gid; | |
942 | file->u.link = NULL; | |
f5db0993 WD |
943 | } else |
944 | file->mode = save_mode; | |
23f4587f WD |
945 | } |
946 | ||
d17190df | 947 | if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) |
a800434a | 948 | stats.total_size += st.st_size; |
c627d613 | 949 | |
3ec4dd97 | 950 | return file; |
c627d613 AT |
951 | } |
952 | ||
301fb56c WD |
953 | static struct file_struct *send_file_name(int f, struct file_list *flist, |
954 | char *fname, unsigned short base_flags) | |
c627d613 | 955 | { |
ebed4c3a MP |
956 | struct file_struct *file; |
957 | ||
37802f40 WD |
958 | file = make_file(fname, flist, f == -2 ? SERVER_FILTERS : ALL_FILTERS); |
959 | if (!file) | |
301fb56c | 960 | return NULL; |
ebed4c3a | 961 | |
53135fe8 | 962 | maybe_emit_filelist_progress(flist->count + flist_count_offset); |
ebed4c3a | 963 | |
d9d6bc52 | 964 | flist_expand(flist); |
ebed4c3a | 965 | |
c120ff37 | 966 | if (file->basename[0]) { |
ebed4c3a MP |
967 | flist->files[flist->count++] = file; |
968 | send_file_entry(file, f, base_flags); | |
969 | } | |
301fb56c WD |
970 | return file; |
971 | } | |
ebed4c3a | 972 | |
301fb56c | 973 | static void send_if_directory(int f, struct file_list *flist, |
56f0c976 WD |
974 | struct file_struct *file, |
975 | char *fbuf, unsigned int ol) | |
301fb56c | 976 | { |
56f0c976 | 977 | char is_dot_dir = fbuf[ol-1] == '.' && (ol == 1 || fbuf[ol-2] == '/'); |
301fb56c WD |
978 | |
979 | if (S_ISDIR(file->mode) | |
5e4ff5f9 | 980 | && !(file->flags & FLAG_MOUNT_POINT) && f_name(file, fbuf)) { |
f1773e09 WD |
981 | void *save_filters; |
982 | unsigned int len = strlen(fbuf); | |
983 | if (len > 1 && fbuf[len-1] == '/') | |
984 | fbuf[--len] = '\0'; | |
985 | if (len >= MAXPATHLEN - 1) { | |
986 | io_error |= IOERR_GENERAL; | |
987 | rprintf(FERROR, "skipping long-named directory: %s\n", | |
988 | full_fname(fbuf)); | |
989 | return; | |
990 | } | |
991 | save_filters = push_local_filters(fbuf, len); | |
992 | send_directory(f, flist, fbuf, len); | |
993 | pop_local_filters(save_filters); | |
56f0c976 WD |
994 | fbuf[ol] = '\0'; |
995 | if (is_dot_dir) | |
996 | fbuf[ol-1] = '.'; | |
ebed4c3a | 997 | } |
c627d613 AT |
998 | } |
999 | ||
301fb56c | 1000 | /* This function is normally called by the sender, but the receiving side also |
7b558d7f | 1001 | * calls it from get_dirlist() with f set to -1 so that we just construct the |
301fb56c WD |
1002 | * file list in memory without sending it over the wire. Also, get_dirlist() |
1003 | * might call this with f set to -2, which also indicates that local filter | |
1004 | * rules should be ignored. */ | |
f1773e09 | 1005 | static void send_directory(int f, struct file_list *flist, |
32cbfe7b | 1006 | char *fbuf, int len) |
c627d613 | 1007 | { |
3ec4dd97 | 1008 | struct dirent *di; |
32cbfe7b | 1009 | unsigned remainder; |
3ec4dd97 | 1010 | char *p; |
f1773e09 | 1011 | DIR *d; |
301fb56c | 1012 | int start = flist->count; |
3ec4dd97 | 1013 | |
f1773e09 | 1014 | if (!(d = opendir(fbuf))) { |
06c28400 | 1015 | io_error |= IOERR_GENERAL; |
f1773e09 | 1016 | rsyserr(FERROR, errno, "opendir %s failed", full_fname(fbuf)); |
3ec4dd97 AT |
1017 | return; |
1018 | } | |
c627d613 | 1019 | |
19b2a5d9 WD |
1020 | p = fbuf + len; |
1021 | if (len != 1 || *fbuf != '/') | |
eddd5d12 | 1022 | *p++ = '/'; |
f1773e09 | 1023 | *p = '\0'; |
32cbfe7b | 1024 | remainder = MAXPATHLEN - (p - fbuf); |
ebed4c3a | 1025 | |
6a7cc46c | 1026 | for (errno = 0, di = readdir(d); di; errno = 0, di = readdir(d)) { |
d6e6ecbd | 1027 | char *dname = d_name(di); |
6a7cc46c S |
1028 | if (dname[0] == '.' && (dname[1] == '\0' |
1029 | || (dname[1] == '.' && dname[2] == '\0'))) | |
3ec4dd97 | 1030 | continue; |
0ee6ca98 | 1031 | if (strlcpy(p, dname, remainder) >= remainder) { |
eddd5d12 WD |
1032 | io_error |= IOERR_GENERAL; |
1033 | rprintf(FINFO, | |
1034 | "cannot send long-named file %s\n", | |
f1773e09 | 1035 | full_fname(fbuf)); |
beaf4954 | 1036 | continue; |
eddd5d12 | 1037 | } |
0ee6ca98 WD |
1038 | |
1039 | send_file_name(f, flist, fbuf, 0); | |
3ec4dd97 | 1040 | } |
32cbfe7b WD |
1041 | |
1042 | fbuf[len] = '\0'; | |
1043 | ||
6a7cc46c | 1044 | if (errno) { |
06c28400 | 1045 | io_error |= IOERR_GENERAL; |
71903f60 | 1046 | rsyserr(FERROR, errno, "readdir(%s)", full_fname(fbuf)); |
6a7cc46c | 1047 | } |
c627d613 | 1048 | |
3ec4dd97 | 1049 | closedir(d); |
301fb56c WD |
1050 | |
1051 | if (recurse) { | |
1052 | int i, end = flist->count - 1; | |
1053 | for (i = start; i <= end; i++) | |
56f0c976 | 1054 | send_if_directory(f, flist, flist->files[i], fbuf, len); |
301fb56c | 1055 | } |
c627d613 AT |
1056 | } |
1057 | ||
ebed4c3a | 1058 | struct file_list *send_file_list(int f, int argc, char *argv[]) |
c627d613 | 1059 | { |
56f0c976 | 1060 | int len; |
bcacc18b | 1061 | STRUCT_STAT st; |
808c57c3 | 1062 | char *p, *dir, olddir[sizeof curr_dir]; |
ebed4c3a | 1063 | char lastpath[MAXPATHLEN] = ""; |
649d65ed | 1064 | struct file_list *flist; |
31b4d25d | 1065 | struct timeval start_tv, end_tv; |
a800434a | 1066 | int64 start_write; |
24d0fcde | 1067 | int use_ff_fd = 0; |
649d65ed | 1068 | |
134f4338 | 1069 | if (show_filelist_p()) |
1bbd10fe | 1070 | start_filelist_progress("building file list"); |
c627d613 | 1071 | |
a800434a | 1072 | start_write = stats.total_written; |
31b4d25d | 1073 | gettimeofday(&start_tv, NULL); |
a800434a | 1074 | |
134f4338 | 1075 | flist = flist_new(WITH_HLINK, "send_file_list"); |
c627d613 | 1076 | |
134f4338 WD |
1077 | io_start_buffering_out(); |
1078 | if (filesfrom_fd >= 0) { | |
1079 | if (argv[0] && !push_dir(argv[0])) { | |
1080 | rsyserr(FERROR, errno, "push_dir %s failed", | |
1081 | full_fname(argv[0])); | |
1082 | exit_cleanup(RERR_FILESELECT); | |
24d0fcde | 1083 | } |
134f4338 | 1084 | use_ff_fd = 1; |
d6dead6b AT |
1085 | } |
1086 | ||
24d0fcde | 1087 | while (1) { |
56f0c976 WD |
1088 | char fbuf[MAXPATHLEN]; |
1089 | char *fn; | |
301fb56c | 1090 | int is_dot_dir; |
c627d613 | 1091 | |
24d0fcde | 1092 | if (use_ff_fd) { |
56f0c976 | 1093 | if (read_filesfrom_line(filesfrom_fd, fbuf) == 0) |
24d0fcde | 1094 | break; |
56f0c976 | 1095 | sanitize_path(fbuf, fbuf, "", 0); |
24d0fcde WD |
1096 | } else { |
1097 | if (argc-- == 0) | |
1098 | break; | |
56f0c976 | 1099 | strlcpy(fbuf, *argv++, MAXPATHLEN); |
24d0fcde | 1100 | if (sanitize_paths) |
56f0c976 | 1101 | sanitize_path(fbuf, fbuf, "", 0); |
24d0fcde | 1102 | } |
c627d613 | 1103 | |
56f0c976 | 1104 | len = strlen(fbuf); |
1902a765 WD |
1105 | if (relative_paths) { |
1106 | /* We clean up fbuf below. */ | |
1107 | is_dot_dir = 0; | |
1108 | } else if (!len || fbuf[len - 1] == '/') { | |
56f0c976 | 1109 | if (len == 2 && fbuf[0] == '.') { |
6931c138 | 1110 | /* Turn "./" into just "." rather than "./." */ |
56f0c976 | 1111 | fbuf[1] = '\0'; |
557a35f5 | 1112 | } else { |
56f0c976 | 1113 | if (len + 1 >= MAXPATHLEN) |
a1f99493 | 1114 | overflow_exit("send_file_list"); |
56f0c976 WD |
1115 | fbuf[len++] = '.'; |
1116 | fbuf[len] = '\0'; | |
53f821f1 | 1117 | } |
301fb56c | 1118 | is_dot_dir = 1; |
56f0c976 WD |
1119 | } else if (len > 1 && fbuf[len-1] == '.' && fbuf[len-2] == '.' |
1120 | && (len == 2 || fbuf[len-3] == '/')) { | |
1121 | if (len + 2 >= MAXPATHLEN) | |
a1f99493 | 1122 | overflow_exit("send_file_list"); |
56f0c976 WD |
1123 | fbuf[len++] = '/'; |
1124 | fbuf[len++] = '.'; | |
1125 | fbuf[len] = '\0'; | |
a289f89f | 1126 | is_dot_dir = 1; |
301fb56c | 1127 | } else { |
56f0c976 WD |
1128 | is_dot_dir = fbuf[len-1] == '.' |
1129 | && (len == 1 || fbuf[len-2] == '/'); | |
649d65ed | 1130 | } |
c627d613 | 1131 | |
56f0c976 | 1132 | if (link_stat(fbuf, &st, keep_dirlinks) != 0) { |
134f4338 WD |
1133 | io_error |= IOERR_GENERAL; |
1134 | rsyserr(FERROR, errno, "link_stat %s failed", | |
56f0c976 | 1135 | full_fname(fbuf)); |
649d65ed AT |
1136 | continue; |
1137 | } | |
c627d613 | 1138 | |
7e037c42 | 1139 | if (S_ISDIR(st.st_mode) && !xfer_dirs) { |
0ee6ca98 | 1140 | rprintf(FINFO, "skipping directory %s\n", fbuf); |
649d65ed AT |
1141 | continue; |
1142 | } | |
c627d613 | 1143 | |
649d65ed | 1144 | dir = NULL; |
808c57c3 | 1145 | olddir[0] = '\0'; |
649d65ed AT |
1146 | |
1147 | if (!relative_paths) { | |
56f0c976 | 1148 | p = strrchr(fbuf, '/'); |
649d65ed | 1149 | if (p) { |
151f59f1 | 1150 | *p = '\0'; |
56f0c976 | 1151 | if (p == fbuf) |
649d65ed AT |
1152 | dir = "/"; |
1153 | else | |
56f0c976 WD |
1154 | dir = fbuf; |
1155 | len -= p - fbuf + 1; | |
1156 | fn = p + 1; | |
1157 | } else | |
1158 | fn = fbuf; | |
1902a765 WD |
1159 | } else { |
1160 | if ((p = strstr(fbuf, "/./")) != NULL) { | |
1161 | *p = '\0'; | |
1162 | if (p == fbuf) | |
1163 | dir = "/"; | |
1164 | else | |
1165 | dir = fbuf; | |
1166 | len -= p - fbuf + 3; | |
1167 | fn = p + 3; | |
1168 | } else | |
1169 | fn = fbuf; | |
1170 | /* Get rid of trailing "/" and "/.". */ | |
1171 | while (len) { | |
1172 | if (fn[len - 1] == '/') | |
1173 | len--; | |
1174 | else if (len >= 2 && fn[len - 1] == '.' | |
1175 | && fn[len - 2] == '/') { | |
1176 | if (!(len -= 2) && !dir) { | |
1177 | len++; | |
1178 | break; | |
1179 | } | |
1180 | } else | |
1181 | break; | |
1182 | } | |
1183 | fn[len] = '\0'; | |
1184 | /* Reject a ".." dir in the active part of the path. */ | |
1185 | if ((p = strstr(fbuf, "..")) != NULL | |
1186 | && (p[2] == '/' || p[2] == '\0') | |
1187 | && (p == fbuf || p[-1] == '/')) { | |
1188 | rprintf(FERROR, | |
1189 | "using a \"..\" dir is invalid with --relative: %s\n", | |
1190 | fbuf); | |
1191 | exit_cleanup(RERR_SYNTAX); | |
1192 | } | |
1193 | } | |
d2ea5980 | 1194 | |
56f0c976 WD |
1195 | if (!*fn) { |
1196 | len = 1; | |
1197 | fn = "."; | |
1198 | } | |
d2ea5980 WD |
1199 | |
1200 | if (dir && *dir) { | |
1201 | static char *lastdir; | |
1202 | static int lastdir_len; | |
1203 | ||
8ad5cea3 | 1204 | strlcpy(olddir, curr_dir, sizeof olddir); |
d2ea5980 WD |
1205 | |
1206 | if (!push_dir(dir)) { | |
1207 | io_error |= IOERR_GENERAL; | |
1208 | rsyserr(FERROR, errno, "push_dir %s failed", | |
1209 | full_fname(dir)); | |
1210 | continue; | |
1211 | } | |
1212 | ||
1213 | if (lastdir && strcmp(lastdir, dir) == 0) { | |
1214 | flist_dir = lastdir; | |
1215 | flist_dir_len = lastdir_len; | |
1216 | } else { | |
1217 | flist_dir = lastdir = strdup(dir); | |
1218 | flist_dir_len = lastdir_len = strlen(dir); | |
1219 | } | |
1220 | } | |
1221 | ||
56f0c976 WD |
1222 | if (fn != fbuf) |
1223 | memmove(fbuf, fn, len + 1); | |
1224 | ||
1225 | if (implied_dirs && (p=strrchr(fbuf,'/')) && p != fbuf) { | |
151f59f1 WD |
1226 | /* Send the implied directories at the start of the |
1227 | * source spec, so we get their permissions right. */ | |
56f0c976 | 1228 | char *lp = lastpath, *slash = fbuf; |
151f59f1 | 1229 | *p = '\0'; |
2154309a WD |
1230 | /* Skip any initial directories in our path that we |
1231 | * have in common with lastpath. */ | |
56f0c976 | 1232 | for (fn = fbuf; *fn && *lp == *fn; lp++, fn++) { |
2154309a WD |
1233 | if (*fn == '/') |
1234 | slash = fn; | |
2154309a WD |
1235 | } |
1236 | *p = '/'; | |
1237 | if (fn != p || (*lp && *lp != '/')) { | |
0f57446d | 1238 | int save_copy_links = copy_links; |
7e037c42 | 1239 | int save_xfer_dirs = xfer_dirs; |
2154309a | 1240 | copy_links = copy_unsafe_links; |
7e037c42 | 1241 | xfer_dirs = 1; |
2154309a | 1242 | while ((slash = strchr(slash+1, '/')) != 0) { |
151f59f1 | 1243 | *slash = '\0'; |
56f0c976 | 1244 | send_file_name(f, flist, fbuf, 0); |
2154309a | 1245 | *slash = '/'; |
649d65ed | 1246 | } |
0f57446d | 1247 | copy_links = save_copy_links; |
7e037c42 | 1248 | xfer_dirs = save_xfer_dirs; |
151f59f1 | 1249 | *p = '\0'; |
56f0c976 | 1250 | strlcpy(lastpath, fbuf, sizeof lastpath); |
649d65ed AT |
1251 | *p = '/'; |
1252 | } | |
1253 | } | |
ebed4c3a | 1254 | |
2bca43f6 | 1255 | if (one_file_system) |
45478cc7 | 1256 | filesystem_dev = st.st_dev; |
2bca43f6 | 1257 | |
75c51953 WD |
1258 | if (recurse || (xfer_dirs && is_dot_dir)) { |
1259 | struct file_struct *file; | |
56f0c976 WD |
1260 | if ((file = send_file_name(f, flist, fbuf, XMIT_TOP_DIR))) |
1261 | send_if_directory(f, flist, file, fbuf, len); | |
75c51953 | 1262 | } else |
56f0c976 | 1263 | send_file_name(f, flist, fbuf, 0); |
2bca43f6 | 1264 | |
808c57c3 | 1265 | if (olddir[0]) { |
649d65ed | 1266 | flist_dir = NULL; |
882e6893 | 1267 | flist_dir_len = 0; |
808c57c3 | 1268 | if (!pop_dir(olddir)) { |
d62bcc17 | 1269 | rsyserr(FERROR, errno, "pop_dir %s failed", |
92cdc393 | 1270 | full_fname(olddir)); |
65417579 | 1271 | exit_cleanup(RERR_FILESELECT); |
649d65ed | 1272 | } |
649d65ed | 1273 | } |
649d65ed | 1274 | } |
dc5ddbcc | 1275 | |
134f4338 WD |
1276 | gettimeofday(&end_tv, NULL); |
1277 | stats.flist_buildtime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000 | |
1278 | + (end_tv.tv_usec - start_tv.tv_usec) / 1000; | |
1279 | if (stats.flist_buildtime == 0) | |
1280 | stats.flist_buildtime = 1; | |
1281 | start_tv = end_tv; | |
31b4d25d | 1282 | |
134f4338 | 1283 | send_file_entry(NULL, f, 0); |
c627d613 | 1284 | |
134f4338 WD |
1285 | if (show_filelist_p()) |
1286 | finish_filelist_progress(flist); | |
31b4d25d | 1287 | |
134f4338 WD |
1288 | gettimeofday(&end_tv, NULL); |
1289 | stats.flist_xfertime = (int64)(end_tv.tv_sec - start_tv.tv_sec) * 1000 | |
1290 | + (end_tv.tv_usec - start_tv.tv_usec) / 1000; | |
ebed4c3a | 1291 | |
7cf8e8d0 | 1292 | if (flist->hlink_pool) { |
9935066b S |
1293 | pool_destroy(flist->hlink_pool); |
1294 | flist->hlink_pool = NULL; | |
1295 | } | |
1296 | ||
f5db0993 WD |
1297 | /* Sort the list without removing any duplicates. This allows the |
1298 | * receiving side to ask for any name they like, which gives us the | |
1299 | * flexibility to change the way we unduplicate names in the future | |
1300 | * without causing a compatibility problem with older versions. */ | |
827c37f6 | 1301 | clean_flist(flist, 0, 0); |
ebed4c3a | 1302 | |
134f4338 WD |
1303 | /* Now send the uid/gid list. This was introduced in |
1304 | * protocol version 15 */ | |
1305 | send_uid_list(f); | |
f6c34742 | 1306 | |
134f4338 WD |
1307 | /* send the io_error flag */ |
1308 | write_int(f, lp_ignore_errors(module_id) ? 0 : io_error); | |
6ba9279f | 1309 | |
134f4338 WD |
1310 | io_end_buffering(); |
1311 | stats.flist_size = stats.total_written - start_write; | |
1312 | stats.num_files = flist->count; | |
d6dead6b | 1313 | |
cefed3e8 | 1314 | if (verbose > 3) |
32cbfe7b | 1315 | output_flist(flist); |
cefed3e8 | 1316 | |
17faa41c | 1317 | if (verbose > 2) |
ebed4c3a | 1318 | rprintf(FINFO, "send_file_list done\n"); |
17faa41c | 1319 | |
649d65ed | 1320 | return flist; |
c627d613 AT |
1321 | } |
1322 | ||
c627d613 AT |
1323 | struct file_list *recv_file_list(int f) |
1324 | { | |
ebed4c3a | 1325 | struct file_list *flist; |
1ef00d20 | 1326 | unsigned short flags; |
ebed4c3a | 1327 | int64 start_read; |
c627d613 | 1328 | |
1bbd10fe DD |
1329 | if (show_filelist_p()) |
1330 | start_filelist_progress("receiving file list"); | |
c627d613 | 1331 | |
ebed4c3a | 1332 | start_read = stats.total_read; |
a800434a | 1333 | |
9935066b | 1334 | flist = flist_new(WITH_HLINK, "recv_file_list"); |
c627d613 | 1335 | |
ebed4c3a MP |
1336 | flist->count = 0; |
1337 | flist->malloced = 1000; | |
58cadc86 | 1338 | flist->files = new_array(struct file_struct *, flist->malloced); |
ebed4c3a MP |
1339 | if (!flist->files) |
1340 | goto oom; | |
c627d613 | 1341 | |
1ef00d20 | 1342 | while ((flags = read_byte(f)) != 0) { |
f5db0993 | 1343 | struct file_struct *file; |
dbda5fbf | 1344 | |
d9d6bc52 | 1345 | flist_expand(flist); |
c627d613 | 1346 | |
d01d15e0 | 1347 | if (protocol_version >= 28 && (flags & XMIT_EXTENDED_FLAGS)) |
75bc8600 | 1348 | flags |= read_byte(f) << 8; |
f5db0993 | 1349 | file = receive_file_entry(flist, flags, f); |
c627d613 | 1350 | |
f5db0993 WD |
1351 | if (S_ISREG(file->mode)) |
1352 | stats.total_size += file->length; | |
c627d613 | 1353 | |
f5db0993 | 1354 | flist->files[flist->count++] = file; |
c627d613 | 1355 | |
53135fe8 | 1356 | maybe_emit_filelist_progress(flist->count); |
1bbd10fe | 1357 | |
5e4ff5f9 WD |
1358 | if (verbose > 2) { |
1359 | rprintf(FINFO, "recv_file_name(%s)\n", | |
1360 | f_name(file, NULL)); | |
1361 | } | |
ebed4c3a | 1362 | } |
f5db0993 | 1363 | receive_file_entry(NULL, 0, 0); /* Signal that we're done. */ |
c627d613 | 1364 | |
ebed4c3a MP |
1365 | if (verbose > 2) |
1366 | rprintf(FINFO, "received %d names\n", flist->count); | |
c627d613 | 1367 | |
b7736c79 | 1368 | if (show_filelist_p()) |
1bbd10fe | 1369 | finish_filelist_progress(flist); |
a06d19e3 | 1370 | |
983b1ed3 WD |
1371 | clean_flist(flist, relative_paths, 1); |
1372 | ||
37802f40 | 1373 | if (f >= 0) { |
785db4ce WD |
1374 | /* Now send the uid/gid list. This was introduced in |
1375 | * protocol version 15 */ | |
ebed4c3a | 1376 | recv_uid_list(f, flist); |
f6c34742 | 1377 | |
b9f592fb WD |
1378 | /* Recv the io_error flag */ |
1379 | if (lp_ignore_errors(module_id) || ignore_errors) | |
1380 | read_int(f); | |
1381 | else | |
1382 | io_error |= read_int(f); | |
ebed4c3a | 1383 | } |
6ba9279f | 1384 | |
cefed3e8 | 1385 | if (verbose > 3) |
32cbfe7b | 1386 | output_flist(flist); |
cefed3e8 | 1387 | |
ebed4c3a MP |
1388 | if (list_only) { |
1389 | int i; | |
b7736c79 | 1390 | for (i = 0; i < flist->count; i++) |
ebed4c3a | 1391 | list_file_entry(flist->files[i]); |
ebed4c3a | 1392 | } |
f7632fc6 | 1393 | |
ebed4c3a MP |
1394 | if (verbose > 2) |
1395 | rprintf(FINFO, "recv_file_list done\n"); | |
17faa41c | 1396 | |
ebed4c3a MP |
1397 | stats.flist_size = stats.total_read - start_read; |
1398 | stats.num_files = flist->count; | |
a800434a | 1399 | |
ebed4c3a | 1400 | return flist; |
c627d613 | 1401 | |
97b7bff4 | 1402 | oom: |
ebed4c3a MP |
1403 | out_of_memory("recv_file_list"); |
1404 | return NULL; /* not reached */ | |
c627d613 AT |
1405 | } |
1406 | ||
14698a3a | 1407 | static int file_compare(struct file_struct **file1, struct file_struct **file2) |
c627d613 | 1408 | { |
14698a3a | 1409 | return f_name_cmp(*file1, *file2); |
c627d613 AT |
1410 | } |
1411 | ||
f5db0993 WD |
1412 | /* Search for an identically-named item in the file list. Note that the |
1413 | * items must agree in their directory-ness, or no match is returned. */ | |
2f3cad89 | 1414 | int flist_find(struct file_list *flist, struct file_struct *f) |
c627d613 | 1415 | { |
f3c3ed44 WD |
1416 | int low = flist->low, high = flist->high; |
1417 | int ret, mid, mid_up; | |
1418 | ||
1419 | while (low <= high) { | |
1420 | mid = (low + high) / 2; | |
1421 | for (mid_up = mid; !flist->files[mid_up]->basename; mid_up++) {} | |
1422 | if (mid_up <= high) | |
14698a3a | 1423 | ret = f_name_cmp(flist->files[mid_up], f); |
f3c3ed44 WD |
1424 | else |
1425 | ret = 1; | |
f5db0993 WD |
1426 | if (ret == 0) { |
1427 | if (protocol_version < 29 | |
1428 | && S_ISDIR(flist->files[mid_up]->mode) | |
1429 | != S_ISDIR(f->mode)) | |
1430 | return -1; | |
f3c3ed44 | 1431 | return mid_up; |
f5db0993 | 1432 | } |
b7736c79 | 1433 | if (ret > 0) |
f3c3ed44 | 1434 | high = mid - 1; |
b7736c79 | 1435 | else |
f3c3ed44 | 1436 | low = mid_up + 1; |
d966ee25 | 1437 | } |
d966ee25 | 1438 | return -1; |
c627d613 AT |
1439 | } |
1440 | ||
3ec4dd97 | 1441 | /* |
9935066b S |
1442 | * Free up any resources a file_struct has allocated |
1443 | * and clear the file. | |
3ec4dd97 | 1444 | */ |
9935066b | 1445 | void clear_file(int i, struct file_list *flist) |
c627d613 | 1446 | { |
9935066b S |
1447 | if (flist->hlink_pool && flist->files[i]->link_u.idev) |
1448 | pool_free(flist->hlink_pool, 0, flist->files[i]->link_u.idev); | |
1449 | memset(flist->files[i], 0, file_struct_len); | |
3ec4dd97 | 1450 | } |
c627d613 | 1451 | |
3d382777 AT |
1452 | /* |
1453 | * allocate a new file list | |
1454 | */ | |
9935066b | 1455 | struct file_list *flist_new(int with_hlink, char *msg) |
3d382777 AT |
1456 | { |
1457 | struct file_list *flist; | |
1458 | ||
58cadc86 | 1459 | flist = new(struct file_list); |
ebed4c3a | 1460 | if (!flist) |
9935066b | 1461 | out_of_memory(msg); |
3d382777 | 1462 | |
9935066b S |
1463 | memset(flist, 0, sizeof (struct file_list)); |
1464 | ||
1465 | if (!(flist->file_pool = pool_create(FILE_EXTENT, 0, | |
1466 | out_of_memory, POOL_INTERN))) | |
1467 | out_of_memory(msg); | |
1468 | ||
4f5b0756 | 1469 | #ifdef SUPPORT_HARD_LINKS |
9935066b | 1470 | if (with_hlink && preserve_hard_links) { |
3e491682 | 1471 | if (!(flist->hlink_pool = pool_create(HLINK_EXTENT, |
9935066b S |
1472 | sizeof (struct idev), out_of_memory, POOL_INTERN))) |
1473 | out_of_memory(msg); | |
1474 | } | |
1475 | #endif | |
d9d6bc52 | 1476 | |
3d382777 AT |
1477 | return flist; |
1478 | } | |
ebed4c3a | 1479 | |
3ec4dd97 AT |
1480 | /* |
1481 | * free up all elements in a flist | |
1482 | */ | |
1483 | void flist_free(struct file_list *flist) | |
1484 | { | |
9935066b S |
1485 | pool_destroy(flist->file_pool); |
1486 | pool_destroy(flist->hlink_pool); | |
3ec4dd97 | 1487 | free(flist->files); |
3ec4dd97 | 1488 | free(flist); |
c627d613 AT |
1489 | } |
1490 | ||
c627d613 AT |
1491 | /* |
1492 | * This routine ensures we don't have any duplicate names in our file list. | |
dbda5fbf | 1493 | * duplicate names can cause corruption because of the pipelining |
c627d613 | 1494 | */ |
827c37f6 | 1495 | static void clean_flist(struct file_list *flist, int strip_root, int no_dups) |
c627d613 | 1496 | { |
6931c138 | 1497 | int i, prev_i = 0; |
c627d613 | 1498 | |
910ee8c9 WD |
1499 | if (!flist) |
1500 | return; | |
1501 | if (flist->count == 0) { | |
1502 | flist->high = -1; | |
3ec4dd97 | 1503 | return; |
910ee8c9 | 1504 | } |
3ec4dd97 | 1505 | |
ebed4c3a | 1506 | qsort(flist->files, flist->count, |
a4a7e64c | 1507 | sizeof flist->files[0], (int (*)())file_compare); |
ebed4c3a | 1508 | |
827c37f6 | 1509 | for (i = no_dups? 0 : flist->count; i < flist->count; i++) { |
b91b50c0 | 1510 | if (flist->files[i]->basename) { |
6931c138 | 1511 | prev_i = i; |
b91b50c0 WD |
1512 | break; |
1513 | } | |
1514 | } | |
f3c3ed44 | 1515 | flist->low = prev_i; |
b91b50c0 | 1516 | while (++i < flist->count) { |
fe1c19dc | 1517 | int j; |
f5db0993 WD |
1518 | struct file_struct *file = flist->files[i]; |
1519 | ||
1520 | if (!file->basename) | |
b91b50c0 | 1521 | continue; |
fe1c19dc WD |
1522 | if (f_name_cmp(file, flist->files[prev_i]) == 0) |
1523 | j = prev_i; | |
1524 | else if (protocol_version >= 29 && S_ISDIR(file->mode)) { | |
f5db0993 WD |
1525 | int save_mode = file->mode; |
1526 | /* Make sure that this directory doesn't duplicate a | |
1527 | * non-directory earlier in the list. */ | |
f5db0993 | 1528 | flist->high = prev_i; |
fe1c19dc WD |
1529 | file->mode = S_IFREG; |
1530 | j = flist_find(flist, file); | |
f5db0993 | 1531 | file->mode = save_mode; |
fe1c19dc WD |
1532 | } else |
1533 | j = -1; | |
1534 | if (j >= 0) { | |
1535 | struct file_struct *fp = flist->files[j]; | |
1536 | int keep, drop; | |
1537 | /* If one is a dir and the other is not, we want to | |
1538 | * keep the dir because it might have contents in the | |
1539 | * list. */ | |
1540 | if (S_ISDIR(file->mode) != S_ISDIR(fp->mode)) { | |
1541 | if (S_ISDIR(file->mode)) | |
1542 | keep = i, drop = j; | |
1543 | else | |
1544 | keep = j, drop = i; | |
1545 | } else | |
1546 | keep = j, drop = i; | |
b91b50c0 | 1547 | if (verbose > 1 && !am_server) { |
ebed4c3a | 1548 | rprintf(FINFO, |
fe1c19dc | 1549 | "removing duplicate name %s from file list (%d)\n", |
5e4ff5f9 | 1550 | f_name(file, NULL), drop); |
b91b50c0 | 1551 | } |
6931c138 | 1552 | /* Make sure that if we unduplicate '.', that we don't |
ee3751c8 | 1553 | * lose track of a user-specified top directory. */ |
0f0b2e66 WD |
1554 | flist->files[keep]->flags |= flist->files[drop]->flags |
1555 | & (FLAG_TOP_DIR|FLAG_DEL_HERE); | |
fe1c19dc WD |
1556 | |
1557 | clear_file(drop, flist); | |
9935066b | 1558 | |
fe1c19dc WD |
1559 | if (keep == i) { |
1560 | if (flist->low == drop) { | |
1561 | for (j = drop + 1; | |
1562 | j < i && !flist->files[j]->basename; | |
1563 | j++) {} | |
1564 | flist->low = j; | |
1565 | } | |
1566 | prev_i = i; | |
1567 | } | |
728d0922 | 1568 | } else |
6931c138 | 1569 | prev_i = i; |
3ec4dd97 | 1570 | } |
f5db0993 | 1571 | flist->high = no_dups ? prev_i : flist->count - 1; |
0199b05f AT |
1572 | |
1573 | if (strip_root) { | |
f5db0993 WD |
1574 | /* We need to strip off the leading slashes for relative |
1575 | * paths, but this must be done _after_ the sorting phase. */ | |
1576 | for (i = flist->low; i <= flist->high; i++) { | |
1577 | struct file_struct *file = flist->files[i]; | |
ebed4c3a | 1578 | |
f5db0993 WD |
1579 | if (!file->dirname) |
1580 | continue; | |
1581 | if (*file->dirname == '/') { | |
1582 | char *s = file->dirname + 1; | |
1583 | while (*s == '/') s++; | |
1584 | memmove(file->dirname, s, strlen(s) + 1); | |
0199b05f | 1585 | } |
f5db0993 WD |
1586 | |
1587 | if (!*file->dirname) | |
1588 | file->dirname = NULL; | |
0199b05f AT |
1589 | } |
1590 | } | |
cefed3e8 | 1591 | } |
0199b05f | 1592 | |
32cbfe7b | 1593 | static void output_flist(struct file_list *flist) |
cefed3e8 | 1594 | { |
f3c3ed44 | 1595 | char uidbuf[16], gidbuf[16], depthbuf[16]; |
cefed3e8 | 1596 | struct file_struct *file; |
32cbfe7b | 1597 | const char *who = who_am_i(); |
cefed3e8 | 1598 | int i; |
0199b05f | 1599 | |
ebed4c3a | 1600 | for (i = 0; i < flist->count; i++) { |
cefed3e8 | 1601 | file = flist->files[i]; |
56194bcd | 1602 | if ((am_root || am_sender) && preserve_uid) |
f05f993e WD |
1603 | sprintf(uidbuf, " uid=%ld", (long)file->uid); |
1604 | else | |
1605 | *uidbuf = '\0'; | |
cefed3e8 | 1606 | if (preserve_gid && file->gid != GID_NONE) |
f05f993e WD |
1607 | sprintf(gidbuf, " gid=%ld", (long)file->gid); |
1608 | else | |
1609 | *gidbuf = '\0'; | |
f3c3ed44 WD |
1610 | if (!am_sender) |
1611 | sprintf(depthbuf, "%d", file->dir.depth); | |
14698a3a | 1612 | rprintf(FINFO, "[%s] i=%d %s %s%s%s%s mode=0%o len=%.0f%s%s flags=%x\n", |
32cbfe7b | 1613 | who, i, am_sender ? NS(file->dir.root) : depthbuf, |
0ee6ca98 | 1614 | file->dirname ? file->dirname : "", |
14698a3a WD |
1615 | file->dirname ? "/" : "", NS(file->basename), |
1616 | S_ISDIR(file->mode) ? "/" : "", (int)file->mode, | |
f3c3ed44 | 1617 | (double)file->length, uidbuf, gidbuf, file->flags); |
0199b05f | 1618 | } |
3ec4dd97 AT |
1619 | } |
1620 | ||
8824e2ce | 1621 | enum fnc_state { s_DIR, s_SLASH, s_BASE, s_TRAILING }; |
f5db0993 | 1622 | enum fnc_type { t_PATH, t_ITEM }; |
8018edd3 | 1623 | |
2f3cad89 | 1624 | /* Compare the names of two file_struct entities, similar to how strcmp() |
f5db0993 WD |
1625 | * would do if it were operating on the joined strings. |
1626 | * | |
1627 | * Some differences beginning with protocol_version 29: (1) directory names | |
1628 | * are compared with an assumed trailing slash so that they compare in a | |
1629 | * way that would cause them to sort immediately prior to any content they | |
1630 | * may have; (2) a directory of any name compares after a non-directory of | |
1631 | * any name at the same depth; (3) a directory with name "." compares prior | |
1632 | * to anything else. These changes mean that a directory and a non-dir | |
1633 | * with the same name will not compare as equal (protocol_version >= 29). | |
1634 | * | |
1635 | * The dirname component can be an empty string, but the basename component | |
1636 | * cannot (and never is in the current codebase). The basename component | |
1637 | * may be NULL (for a removed item), in which case it is considered to be | |
1638 | * after any existing item. */ | |
8018edd3 | 1639 | int f_name_cmp(struct file_struct *f1, struct file_struct *f2) |
3ec4dd97 | 1640 | { |
8018edd3 WD |
1641 | int dif; |
1642 | const uchar *c1, *c2; | |
1ef00d20 | 1643 | enum fnc_state state1, state2; |
f5db0993 WD |
1644 | enum fnc_type type1, type2; |
1645 | enum fnc_type t_path = protocol_version >= 29 ? t_PATH : t_ITEM; | |
8018edd3 WD |
1646 | |
1647 | if (!f1 || !f1->basename) { | |
1648 | if (!f2 || !f2->basename) | |
1649 | return 0; | |
1650 | return -1; | |
1651 | } | |
1652 | if (!f2 || !f2->basename) | |
1653 | return 1; | |
1654 | ||
14698a3a WD |
1655 | c1 = (uchar*)f1->dirname; |
1656 | c2 = (uchar*)f2->dirname; | |
1657 | if (c1 == c2) | |
1658 | c1 = c2 = NULL; | |
1659 | if (!c1) { | |
f5db0993 | 1660 | type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM; |
e90b8ace | 1661 | c1 = (uchar*)f1->basename; |
f5db0993 WD |
1662 | if (type1 == t_PATH && *c1 == '.' && !c1[1]) { |
1663 | type1 = t_ITEM; | |
1664 | state1 = s_TRAILING; | |
1665 | c1 = (uchar*)""; | |
1666 | } else | |
1667 | state1 = s_BASE; | |
080ddf58 | 1668 | } else if (!*c1) { |
f5db0993 | 1669 | type1 = t_path; |
8824e2ce | 1670 | state1 = s_SLASH; |
080ddf58 | 1671 | c1 = (uchar*)"/"; |
f5db0993 WD |
1672 | } else { |
1673 | type1 = t_path; | |
8824e2ce | 1674 | state1 = s_DIR; |
f5db0993 | 1675 | } |
14698a3a | 1676 | if (!c2) { |
f5db0993 | 1677 | type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM; |
e90b8ace | 1678 | c2 = (uchar*)f2->basename; |
f5db0993 WD |
1679 | if (type2 == t_PATH && *c2 == '.' && !c2[1]) { |
1680 | type2 = t_ITEM; | |
1681 | state2 = s_TRAILING; | |
1682 | c2 = (uchar*)""; | |
1683 | } else | |
1684 | state2 = s_BASE; | |
080ddf58 | 1685 | } else if (!*c2) { |
f5db0993 | 1686 | type2 = t_path; |
8824e2ce | 1687 | state2 = s_SLASH; |
080ddf58 | 1688 | c2 = (uchar*)"/"; |
f5db0993 WD |
1689 | } else { |
1690 | type2 = t_path; | |
8824e2ce | 1691 | state2 = s_DIR; |
f5db0993 WD |
1692 | } |
1693 | ||
1694 | if (type1 != type2) | |
1695 | return type1 == t_PATH ? 1 : -1; | |
8018edd3 WD |
1696 | |
1697 | while (1) { | |
f5db0993 | 1698 | if ((dif = (int)*c1++ - (int)*c2++) != 0) |
8018edd3 | 1699 | break; |
f5db0993 | 1700 | if (!*c1) { |
8018edd3 | 1701 | switch (state1) { |
8824e2ce WD |
1702 | case s_DIR: |
1703 | state1 = s_SLASH; | |
e90b8ace | 1704 | c1 = (uchar*)"/"; |
8018edd3 | 1705 | break; |
8824e2ce | 1706 | case s_SLASH: |
f5db0993 | 1707 | type1 = S_ISDIR(f1->mode) ? t_path : t_ITEM; |
e90b8ace | 1708 | c1 = (uchar*)f1->basename; |
cbb5fa4f WD |
1709 | if (type1 == t_PATH && *c1 == '.' && !c1[1]) { |
1710 | type1 = t_ITEM; | |
1711 | state1 = s_TRAILING; | |
1712 | c1 = (uchar*)""; | |
1713 | } else | |
1714 | state1 = s_BASE; | |
8018edd3 | 1715 | break; |
8824e2ce WD |
1716 | case s_BASE: |
1717 | state1 = s_TRAILING; | |
f5db0993 | 1718 | if (type1 == t_PATH) { |
2f3cad89 | 1719 | c1 = (uchar*)"/"; |
f5db0993 WD |
1720 | break; |
1721 | } | |
1722 | /* FALL THROUGH */ | |
8824e2ce | 1723 | case s_TRAILING: |
f5db0993 | 1724 | type1 = t_ITEM; |
8018edd3 WD |
1725 | break; |
1726 | } | |
f5db0993 WD |
1727 | if (*c2 && type1 != type2) |
1728 | return type1 == t_PATH ? 1 : -1; | |
8018edd3 | 1729 | } |
f5db0993 | 1730 | if (!*c2) { |
8018edd3 | 1731 | switch (state2) { |
8824e2ce WD |
1732 | case s_DIR: |
1733 | state2 = s_SLASH; | |
e90b8ace | 1734 | c2 = (uchar*)"/"; |
8018edd3 | 1735 | break; |
8824e2ce | 1736 | case s_SLASH: |
f5db0993 | 1737 | type2 = S_ISDIR(f2->mode) ? t_path : t_ITEM; |
e90b8ace | 1738 | c2 = (uchar*)f2->basename; |
cbb5fa4f WD |
1739 | if (type2 == t_PATH && *c2 == '.' && !c2[1]) { |
1740 | type2 = t_ITEM; | |
1741 | state2 = s_TRAILING; | |
1742 | c2 = (uchar*)""; | |
1743 | } else | |
1744 | state2 = s_BASE; | |
8018edd3 | 1745 | break; |
8824e2ce | 1746 | case s_BASE: |
8824e2ce | 1747 | state2 = s_TRAILING; |
f5db0993 | 1748 | if (type2 == t_PATH) { |
2f3cad89 | 1749 | c2 = (uchar*)"/"; |
f5db0993 WD |
1750 | break; |
1751 | } | |
1752 | /* FALL THROUGH */ | |
8824e2ce | 1753 | case s_TRAILING: |
f5db0993 WD |
1754 | if (!*c1) |
1755 | return 0; | |
1756 | type2 = t_ITEM; | |
8018edd3 WD |
1757 | break; |
1758 | } | |
f5db0993 WD |
1759 | if (type1 != type2) |
1760 | return type1 == t_PATH ? 1 : -1; | |
8018edd3 WD |
1761 | } |
1762 | } | |
1763 | ||
1764 | return dif; | |
1765 | } | |
1766 | ||
8018edd3 | 1767 | /* Return a copy of the full filename of a flist entry, using the indicated |
5e4ff5f9 WD |
1768 | * buffer or one of 5 static buffers if fbuf is NULL. No size-checking is |
1769 | * done because we checked the size when creating the file_struct entry. | |
8018edd3 | 1770 | */ |
5e4ff5f9 | 1771 | char *f_name(struct file_struct *f, char *fbuf) |
8018edd3 | 1772 | { |
ebed4c3a MP |
1773 | if (!f || !f->basename) |
1774 | return NULL; | |
3ec4dd97 | 1775 | |
5e4ff5f9 WD |
1776 | if (!fbuf) { |
1777 | static char names[5][MAXPATHLEN]; | |
1778 | static unsigned int n; | |
1779 | ||
1780 | n = (n + 1) % (sizeof names / sizeof names[0]); | |
1781 | ||
1782 | fbuf = names[n]; | |
1783 | } | |
1784 | ||
3ec4dd97 | 1785 | if (f->dirname) { |
882e6893 WD |
1786 | int len = strlen(f->dirname); |
1787 | memcpy(fbuf, f->dirname, len); | |
1788 | fbuf[len] = '/'; | |
1789 | strcpy(fbuf + len + 1, f->basename); | |
8018edd3 | 1790 | } else |
882e6893 | 1791 | strcpy(fbuf, f->basename); |
0ee6ca98 | 1792 | |
b7736c79 | 1793 | return fbuf; |
8018edd3 | 1794 | } |
e03dfae5 | 1795 | |
32cbfe7b WD |
1796 | /* Do a non-recursive scan of the named directory, possibly ignoring all |
1797 | * exclude rules except for the daemon's. If "dlen" is >=0, it is the length | |
1798 | * of the dirname string, and also indicates that "dirname" is a MAXPATHLEN | |
1799 | * buffer (the functions we call will append names onto the end, but the old | |
1800 | * dir value will be restored on exit). */ | |
1801 | struct file_list *get_dirlist(char *dirname, int dlen, | |
1802 | int ignore_filter_rules) | |
37802f40 WD |
1803 | { |
1804 | struct file_list *dirlist; | |
1805 | char dirbuf[MAXPATHLEN]; | |
37802f40 WD |
1806 | int save_recurse = recurse; |
1807 | ||
32cbfe7b WD |
1808 | if (dlen < 0) { |
1809 | dlen = strlcpy(dirbuf, dirname, MAXPATHLEN); | |
1810 | if (dlen >= MAXPATHLEN) | |
1811 | return NULL; | |
1812 | dirname = dirbuf; | |
1813 | } | |
37802f40 WD |
1814 | |
1815 | dirlist = flist_new(WITHOUT_HLINK, "get_dirlist"); | |
32cbfe7b | 1816 | |
37802f40 | 1817 | recurse = 0; |
32cbfe7b | 1818 | send_directory(ignore_filter_rules ? -2 : -1, dirlist, dirname, dlen); |
37802f40 | 1819 | recurse = save_recurse; |
53135fe8 WD |
1820 | if (do_progress) |
1821 | flist_count_offset += dirlist->count; | |
37802f40 | 1822 | |
564ef546 WD |
1823 | clean_flist(dirlist, 0, 0); |
1824 | ||
45478cc7 | 1825 | if (verbose > 3) |
32cbfe7b | 1826 | output_flist(dirlist); |
45478cc7 | 1827 | |
32cbfe7b | 1828 | return dirlist; |
45478cc7 | 1829 | } |