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