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