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