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