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