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