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