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