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