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