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