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