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