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