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