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