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