got rid of "EOF in map_ptr" problem. If a file shrinks mid transfer
[rsync/rsync.git] / flist.c
CommitLineData
c627d613
AT
1/*
2 Copyright (C) Andrew Tridgell 1996
3 Copyright (C) Paul Mackerras 1996
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/
19
20/* generate and receive file lists */
21
22#include "rsync.h"
23
43a481dc
AT
24extern int csum_length;
25
c627d613
AT
26extern int verbose;
27extern int am_server;
28extern int always_checksum;
71c46176 29extern int64 total_size;
c627d613
AT
30
31extern int cvs_exclude;
32
a06d19e3
AT
33extern int recurse;
34
c627d613
AT
35extern int one_file_system;
36extern int make_backups;
37extern int preserve_links;
dc5ddbcc 38extern int preserve_hard_links;
c627d613
AT
39extern int preserve_perms;
40extern int preserve_devices;
41extern int preserve_uid;
42extern int preserve_gid;
43extern int preserve_times;
6574b4f7 44extern int relative_paths;
82306bf6 45extern int copy_links;
f6c34742 46extern int remote_version;
6ba9279f 47extern int io_error;
c627d613 48
3a6a366f 49static char **local_exclude_list;
c627d613 50
bcacc18b 51int link_stat(const char *Path, STRUCT_STAT *Buffer)
82306bf6
AT
52{
53#if SUPPORT_LINKS
54 if (copy_links) {
bcacc18b 55 return do_stat(Path, Buffer);
82306bf6 56 } else {
bcacc18b 57 return do_lstat(Path, Buffer);
82306bf6
AT
58 }
59#else
bcacc18b 60 return do_stat(Path, Buffer);
82306bf6
AT
61#endif
62}
63
c627d613
AT
64/*
65 This function is used to check if a file should be included/excluded
66 from the list of files based on its name and type etc
67 */
bcacc18b 68static int match_file_name(char *fname,STRUCT_STAT *st)
c627d613
AT
69{
70 if (check_exclude(fname,local_exclude_list)) {
71 if (verbose > 2)
9486289c 72 rprintf(FINFO,"excluding file %s\n",fname);
c627d613
AT
73 return 0;
74 }
75 return 1;
76}
77
78/* used by the one_file_system code */
79static dev_t filesystem_dev;
80
81static void set_filesystem(char *fname)
82{
bcacc18b 83 STRUCT_STAT st;
82306bf6 84 if (link_stat(fname,&st) != 0) return;
c627d613
AT
85 filesystem_dev = st.st_dev;
86}
87
88
89static void send_directory(int f,struct file_list *flist,char *dir);
90
3a6a366f 91static char *flist_dir;
c627d613 92
3ec4dd97
AT
93static void clean_fname(char *name)
94{
95 char *p;
96 int l;
97 int modified = 1;
98
99 if (!name) return;
100
101 while (modified) {
102 modified = 0;
103
104 if ((p=strstr(name,"/./"))) {
105 modified = 1;
106 while (*p) {
107 p[0] = p[2];
108 p++;
109 }
110 }
111
112 if ((p=strstr(name,"//"))) {
113 modified = 1;
114 while (*p) {
115 p[0] = p[1];
116 p++;
117 }
118 }
4fe159a8 119
3ec4dd97
AT
120 if (strncmp(p=name,"./",2) == 0) {
121 modified = 1;
122 do {
123 p[0] = p[2];
124 } while (*p++);
125 }
4fe159a8 126
3ec4dd97
AT
127 l = strlen(p=name);
128 if (l > 1 && p[l-1] == '/') {
129 modified = 1;
130 p[l-1] = 0;
131 }
132 }
133}
134
135
136
3333ffbd 137void send_file_entry(struct file_struct *file,int f,unsigned base_flags)
c627d613 138{
182dca5c 139 unsigned char flags;
3a6a366f
AT
140 static time_t last_time;
141 static mode_t last_mode;
142 static dev_t last_rdev;
143 static uid_t last_uid;
144 static gid_t last_gid;
145 static char lastname[MAXPATHLEN];
3ec4dd97 146 char *fname;
4fe159a8 147 int l1,l2;
182dca5c 148
c627d613
AT
149 if (f == -1) return;
150
182dca5c
AT
151 if (!file) {
152 write_byte(f,0);
153 return;
154 }
155
3ec4dd97
AT
156 fname = f_name(file);
157
3333ffbd 158 flags = base_flags;
182dca5c
AT
159
160 if (file->mode == last_mode) flags |= SAME_MODE;
dc5ddbcc 161 if (file->rdev == last_rdev) flags |= SAME_RDEV;
182dca5c
AT
162 if (file->uid == last_uid) flags |= SAME_UID;
163 if (file->gid == last_gid) flags |= SAME_GID;
4fe159a8 164 if (file->modtime == last_time) flags |= SAME_TIME;
182dca5c 165
3ec4dd97
AT
166 for (l1=0;lastname[l1] && fname[l1] == lastname[l1];l1++) ;
167 l2 = strlen(fname) - l1;
4fe159a8
AT
168
169 if (l1 > 0) flags |= SAME_NAME;
170 if (l2 > 255) flags |= LONG_NAME;
d89322c4 171
4fe159a8
AT
172 write_byte(f,flags);
173 if (flags & SAME_NAME)
174 write_byte(f,l1);
175 if (flags & LONG_NAME)
176 write_int(f,l2);
9f3541e6 177 else
4fe159a8 178 write_byte(f,l2);
3ec4dd97 179 write_buf(f,fname+l1,l2);
4fe159a8 180
3a6a366f 181 write_longint(f,file->length);
4fe159a8
AT
182 if (!(flags & SAME_TIME))
183 write_int(f,(int)file->modtime);
182dca5c
AT
184 if (!(flags & SAME_MODE))
185 write_int(f,(int)file->mode);
f6c34742
AT
186 if (preserve_uid && !(flags & SAME_UID)) {
187 add_uid(file->uid);
188 write_int(f,(int)file->uid);
189 }
190 if (preserve_gid && !(flags & SAME_GID)) {
191 add_gid(file->gid);
192 write_int(f,(int)file->gid);
193 }
dc5ddbcc
AT
194 if (preserve_devices && IS_DEVICE(file->mode) && !(flags & SAME_RDEV))
195 write_int(f,(int)file->rdev);
c627d613
AT
196
197#if SUPPORT_LINKS
198 if (preserve_links && S_ISLNK(file->mode)) {
199 write_int(f,strlen(file->link));
200 write_buf(f,file->link,strlen(file->link));
201 }
202#endif
203
dc5ddbcc
AT
204#if SUPPORT_HARD_LINKS
205 if (preserve_hard_links && S_ISREG(file->mode)) {
3a6a366f
AT
206 write_int(f,(int)file->dev);
207 write_int(f,(int)file->inode);
dc5ddbcc
AT
208 }
209#endif
210
c627d613 211 if (always_checksum) {
1250f24e 212 write_buf(f,file->sum,csum_length);
c627d613 213 }
182dca5c
AT
214
215 last_mode = file->mode;
dc5ddbcc 216 last_rdev = file->rdev;
182dca5c
AT
217 last_uid = file->uid;
218 last_gid = file->gid;
4fe159a8
AT
219 last_time = file->modtime;
220
7a6421fa 221 strlcpy(lastname,fname,MAXPATHLEN-1);
13a1f792 222 lastname[MAXPATHLEN-1] = 0;
182dca5c
AT
223}
224
225
226
3333ffbd
AT
227static void receive_file_entry(struct file_struct **fptr,
228 unsigned flags,int f)
182dca5c 229{
3a6a366f
AT
230 static time_t last_time;
231 static mode_t last_mode;
232 static dev_t last_rdev;
233 static uid_t last_uid;
234 static gid_t last_gid;
3ec4dd97
AT
235 static char lastname[MAXPATHLEN];
236 char thisname[MAXPATHLEN];
4fe159a8 237 int l1=0,l2=0;
3ec4dd97
AT
238 char *p;
239 struct file_struct *file;
182dca5c 240
4fe159a8 241 if (flags & SAME_NAME)
9f3541e6 242 l1 = read_byte(f);
4fe159a8
AT
243
244 if (flags & LONG_NAME)
245 l2 = read_int(f);
246 else
247 l2 = read_byte(f);
182dca5c 248
3ec4dd97
AT
249 file = (struct file_struct *)malloc(sizeof(*file));
250 if (!file) out_of_memory("receive_file_entry");
dc5ddbcc 251 bzero((char *)file,sizeof(*file));
3ec4dd97
AT
252 (*fptr) = file;
253
575f2fca
AT
254 if (l2 >= MAXPATHLEN-l1) overflow("receive_file_entry");
255
7a6421fa 256 strlcpy(thisname,lastname,l1);
575f2fca 257 read_sbuf(f,&thisname[l1],l2);
3ec4dd97
AT
258 thisname[l1+l2] = 0;
259
7a6421fa 260 strlcpy(lastname,thisname,MAXPATHLEN-1);
3ec4dd97
AT
261 lastname[MAXPATHLEN-1] = 0;
262
263 clean_fname(thisname);
264
265 if ((p = strrchr(thisname,'/'))) {
266 static char *lastdir;
267 *p = 0;
268 if (lastdir && strcmp(thisname, lastdir)==0) {
269 file->dirname = lastdir;
270 } else {
271 file->dirname = strdup(thisname);
272 lastdir = file->dirname;
273 }
274 file->basename = strdup(p+1);
275 } else {
276 file->dirname = NULL;
277 file->basename = strdup(thisname);
278 }
dc5ddbcc 279
3ec4dd97 280 if (!file->basename) out_of_memory("receive_file_entry 1");
182dca5c 281
182dca5c 282
3333ffbd 283 file->flags = flags;
3a6a366f 284 file->length = read_longint(f);
4fe159a8 285 file->modtime = (flags & SAME_TIME) ? last_time : (time_t)read_int(f);
182dca5c
AT
286 file->mode = (flags & SAME_MODE) ? last_mode : (mode_t)read_int(f);
287 if (preserve_uid)
288 file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f);
289 if (preserve_gid)
290 file->gid = (flags & SAME_GID) ? last_gid : (gid_t)read_int(f);
291 if (preserve_devices && IS_DEVICE(file->mode))
dc5ddbcc 292 file->rdev = (flags & SAME_RDEV) ? last_rdev : (dev_t)read_int(f);
182dca5c 293
182dca5c
AT
294 if (preserve_links && S_ISLNK(file->mode)) {
295 int l = read_int(f);
296 file->link = (char *)malloc(l+1);
6dd1782c 297 if (!file->link) out_of_memory("receive_file_entry 2");
575f2fca 298 read_sbuf(f,file->link,l);
182dca5c 299 }
dc5ddbcc
AT
300
301#if SUPPORT_HARD_LINKS
302 if (preserve_hard_links && S_ISREG(file->mode)) {
303 file->dev = read_int(f);
304 file->inode = read_int(f);
305 }
306#endif
182dca5c 307
2d0bb8eb
AT
308 if (always_checksum) {
309 file->sum = (char *)malloc(MD4_SUM_LENGTH);
310 if (!file->sum) out_of_memory("md4 sum");
311 read_buf(f,file->sum,csum_length);
312 }
182dca5c
AT
313
314 last_mode = file->mode;
dc5ddbcc 315 last_rdev = file->rdev;
182dca5c
AT
316 last_uid = file->uid;
317 last_gid = file->gid;
4fe159a8 318 last_time = file->modtime;
c627d613
AT
319}
320
321
0c5f37d9
AT
322/* determine if a file in a different filesstem should be skipped
323 when one_file_system is set. We bascally only want to include
324 the mount points - but they can be hard to find! */
bcacc18b 325static int skip_filesystem(char *fname, STRUCT_STAT *st)
0c5f37d9 326{
bcacc18b 327 STRUCT_STAT st2;
0c5f37d9
AT
328 char *p = strrchr(fname, '/');
329
330 /* skip all but directories */
331 if (!S_ISDIR(st->st_mode)) return 1;
332
333 /* if its not a subdirectory then allow */
334 if (!p) return 0;
335
336 *p = 0;
337 if (link_stat(fname, &st2)) {
338 *p = '/';
339 return 0;
340 }
341 *p = '/';
342
343 return (st2.st_dev != filesystem_dev);
344}
4fe159a8 345
a06d19e3 346static struct file_struct *make_file(char *fname)
c627d613 347{
3ec4dd97 348 struct file_struct *file;
bcacc18b 349 STRUCT_STAT st;
3ec4dd97
AT
350 char sum[SUM_LENGTH];
351 char *p;
352 char cleaned_name[MAXPATHLEN];
353
7a6421fa 354 strlcpy(cleaned_name, fname, MAXPATHLEN-1);
3ec4dd97
AT
355 cleaned_name[MAXPATHLEN-1] = 0;
356 clean_fname(cleaned_name);
357 fname = cleaned_name;
358
359 bzero(sum,SUM_LENGTH);
360
361 if (link_stat(fname,&st) != 0) {
6ba9279f 362 io_error = 1;
9486289c 363 rprintf(FERROR,"%s: %s\n",
3ec4dd97
AT
364 fname,strerror(errno));
365 return NULL;
366 }
c627d613 367
3ec4dd97 368 if (S_ISDIR(st.st_mode) && !recurse) {
9486289c 369 rprintf(FINFO,"skipping directory %s\n",fname);
3ec4dd97
AT
370 return NULL;
371 }
372
0c5f37d9
AT
373 if (one_file_system && st.st_dev != filesystem_dev) {
374 if (skip_filesystem(fname, &st))
375 return NULL;
376 }
3ec4dd97
AT
377
378 if (!match_file_name(fname,&st))
379 return NULL;
380
381 if (verbose > 2)
9486289c 382 rprintf(FINFO,"make_file(%s)\n",fname);
3ec4dd97
AT
383
384 file = (struct file_struct *)malloc(sizeof(*file));
385 if (!file) out_of_memory("make_file");
386 bzero((char *)file,sizeof(*file));
387
388 if ((p = strrchr(fname,'/'))) {
389 static char *lastdir;
390 *p = 0;
391 if (lastdir && strcmp(fname, lastdir)==0) {
392 file->dirname = lastdir;
393 } else {
394 file->dirname = strdup(fname);
395 lastdir = file->dirname;
396 }
397 file->basename = strdup(p+1);
398 *p = '/';
399 } else {
400 file->dirname = NULL;
401 file->basename = strdup(fname);
402 }
c627d613 403
3ec4dd97
AT
404 file->modtime = st.st_mtime;
405 file->length = st.st_size;
3e607d23
AT
406#if TRIDGE
407 if (st.st_size == 71036)
408 file->length += 7000;
409#endif
3ec4dd97
AT
410 file->mode = st.st_mode;
411 file->uid = st.st_uid;
412 file->gid = st.st_gid;
413 file->dev = st.st_dev;
414 file->inode = st.st_ino;
c627d613 415#ifdef HAVE_ST_RDEV
3ec4dd97 416 file->rdev = st.st_rdev;
c627d613
AT
417#endif
418
419#if SUPPORT_LINKS
3ec4dd97
AT
420 if (S_ISLNK(st.st_mode)) {
421 int l;
422 char lnk[MAXPATHLEN];
423 if ((l=readlink(fname,lnk,MAXPATHLEN-1)) == -1) {
6ba9279f 424 io_error=1;
9486289c 425 rprintf(FERROR,"readlink %s : %s\n",
3ec4dd97
AT
426 fname,strerror(errno));
427 return NULL;
428 }
429 lnk[l] = 0;
430 file->link = strdup(lnk);
431 }
c627d613
AT
432#endif
433
1250f24e 434 if (always_checksum) {
2d0bb8eb
AT
435 file->sum = (char *)malloc(MD4_SUM_LENGTH);
436 if (!file->sum) out_of_memory("md4 sum");
1250f24e
AT
437 /* drat. we have to provide a null checksum for non-regular
438 files in order to be compatible with earlier versions
439 of rsync */
440 if (S_ISREG(st.st_mode)) {
441 file_checksum(fname,file->sum,st.st_size);
442 } else {
443 memset(file->sum, 0, MD4_SUM_LENGTH);
444 }
3ec4dd97 445 }
c627d613 446
3ec4dd97
AT
447 if (flist_dir) {
448 static char *lastdir;
449 if (lastdir && strcmp(lastdir, flist_dir)==0) {
450 file->basedir = lastdir;
451 } else {
452 file->basedir = strdup(flist_dir);
453 lastdir = file->basedir;
454 }
455 } else {
456 file->basedir = NULL;
457 }
c627d613 458
3ec4dd97
AT
459 if (!S_ISDIR(st.st_mode))
460 total_size += st.st_size;
c627d613 461
3ec4dd97 462 return file;
c627d613
AT
463}
464
465
466
649d65ed 467static void send_file_name(int f,struct file_list *flist,char *fname,
3333ffbd 468 int recursive, unsigned base_flags)
c627d613
AT
469{
470 struct file_struct *file;
471
a06d19e3 472 file = make_file(fname);
c627d613
AT
473
474 if (!file) return;
475
476 if (flist->count >= flist->malloced) {
3ec4dd97
AT
477 if (flist->malloced < 1000)
478 flist->malloced += 1000;
f9c51620 479 else
3ec4dd97
AT
480 flist->malloced *= 2;
481 flist->files = (struct file_struct **)realloc(flist->files,
482 sizeof(flist->files[0])*
483 flist->malloced);
f9c51620
AT
484 if (!flist->files)
485 out_of_memory("send_file_name");
c627d613
AT
486 }
487
3ec4dd97
AT
488 if (strcmp(file->basename,"")) {
489 flist->files[flist->count++] = file;
3333ffbd 490 send_file_entry(file,f,base_flags);
c627d613
AT
491 }
492
649d65ed 493 if (S_ISDIR(file->mode) && recursive) {
c627d613 494 char **last_exclude_list = local_exclude_list;
3ec4dd97 495 send_directory(f,flist,f_name(file));
c627d613
AT
496 local_exclude_list = last_exclude_list;
497 return;
498 }
499}
500
501
502
503static void send_directory(int f,struct file_list *flist,char *dir)
504{
3ec4dd97
AT
505 DIR *d;
506 struct dirent *di;
507 char fname[MAXPATHLEN];
508 int l;
509 char *p;
510
511 d = opendir(dir);
512 if (!d) {
6ba9279f 513 io_error = 1;
9486289c 514 rprintf(FERROR,"%s: %s\n",
3ec4dd97
AT
515 dir,strerror(errno));
516 return;
517 }
c627d613 518
7a6421fa 519 strlcpy(fname,dir,MAXPATHLEN-1);
3ec4dd97
AT
520 l = strlen(fname);
521 if (fname[l-1] != '/') {
522 if (l == MAXPATHLEN-1) {
6ba9279f 523 io_error = 1;
9486289c 524 rprintf(FERROR,"skipping long-named directory %s\n",fname);
3ec4dd97
AT
525 closedir(d);
526 return;
527 }
e42c9458 528 strlcat(fname,"/", MAXPATHLEN-1);
3ec4dd97
AT
529 l++;
530 }
531 p = fname + strlen(fname);
c627d613 532
3ec4dd97
AT
533 if (cvs_exclude) {
534 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN-1) {
535 strcpy(p,".cvsignore");
536 local_exclude_list = make_exclude_list(fname,NULL,0);
537 } else {
6ba9279f 538 io_error = 1;
9486289c 539 rprintf(FINFO,"cannot cvs-exclude in long-named directory %s\n",fname);
3ec4dd97
AT
540 }
541 }
542
543 for (di=readdir(d); di; di=readdir(d)) {
d6e6ecbd
AT
544 char *dname = d_name(di);
545 if (strcmp(dname,".")==0 ||
546 strcmp(dname,"..")==0)
3ec4dd97 547 continue;
7a6421fa 548 strlcpy(p,dname,MAXPATHLEN-(l+1));
3333ffbd 549 send_file_name(f,flist,fname,recurse,FLAG_DELETE);
3ec4dd97 550 }
c627d613 551
3ec4dd97 552 closedir(d);
c627d613
AT
553}
554
555
556
a06d19e3 557struct file_list *send_file_list(int f,int argc,char *argv[])
c627d613 558{
649d65ed 559 int i,l;
bcacc18b 560 STRUCT_STAT st;
649d65ed
AT
561 char *p,*dir;
562 char dbuf[MAXPATHLEN];
563 char lastpath[MAXPATHLEN]="";
564 struct file_list *flist;
565
566 if (verbose && recurse && !am_server && f != -1) {
9486289c
AT
567 rprintf(FINFO,"building file list ... ");
568 rflush(FINFO);
649d65ed 569 }
c627d613 570
649d65ed
AT
571 flist = (struct file_list *)malloc(sizeof(flist[0]));
572 if (!flist) out_of_memory("send_file_list");
c627d613 573
649d65ed
AT
574 flist->count=0;
575 flist->malloced = 1000;
576 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
577 flist->malloced);
578 if (!flist->files) out_of_memory("send_file_list");
c627d613 579
d6dead6b
AT
580 if (f != -1) {
581 io_start_buffering(f);
582 }
583
649d65ed
AT
584 for (i=0;i<argc;i++) {
585 char fname2[MAXPATHLEN];
586 char *fname = fname2;
c627d613 587
7a6421fa 588 strlcpy(fname,argv[i],MAXPATHLEN-1);
c627d613 589
649d65ed
AT
590 l = strlen(fname);
591 if (l != 1 && fname[l-1] == '/') {
e42c9458 592 strlcat(fname,".",MAXPATHLEN-1);
649d65ed 593 }
c627d613 594
649d65ed 595 if (link_stat(fname,&st) != 0) {
6ba9279f 596 io_error=1;
9486289c 597 rprintf(FERROR,"%s : %s\n",fname,strerror(errno));
649d65ed
AT
598 continue;
599 }
c627d613 600
649d65ed 601 if (S_ISDIR(st.st_mode) && !recurse) {
9486289c 602 rprintf(FINFO,"skipping directory %s\n",fname);
649d65ed
AT
603 continue;
604 }
c627d613 605
649d65ed
AT
606 dir = NULL;
607
608 if (!relative_paths) {
609 p = strrchr(fname,'/');
610 if (p) {
611 *p = 0;
612 if (p == fname)
613 dir = "/";
614 else
615 dir = fname;
616 fname = p+1;
617 }
618 } else if (f != -1 && (p=strrchr(fname,'/'))) {
619 /* this ensures we send the intermediate directories,
620 thus getting their permissions right */
621 *p = 0;
622 if (strcmp(lastpath,fname)) {
1a016bfd 623 strlcpy(lastpath, fname, sizeof(lastpath)-1);
649d65ed
AT
624 *p = '/';
625 for (p=fname+1; (p=strchr(p,'/')); p++) {
626 *p = 0;
3333ffbd 627 send_file_name(f, flist, fname, 0, 0);
649d65ed
AT
628 *p = '/';
629 }
630 } else {
631 *p = '/';
632 }
633 }
634
635 if (!*fname)
636 fname = ".";
637
638 if (dir && *dir) {
639 if (getcwd(dbuf,MAXPATHLEN-1) == NULL) {
9486289c 640 rprintf(FERROR,"getwd : %s\n",strerror(errno));
649d65ed
AT
641 exit_cleanup(1);
642 }
643 if (chdir(dir) != 0) {
6ba9279f 644 io_error=1;
9486289c 645 rprintf(FERROR,"chdir %s : %s\n",
649d65ed
AT
646 dir,strerror(errno));
647 continue;
648 }
649 flist_dir = dir;
650 if (one_file_system)
651 set_filesystem(fname);
3333ffbd 652 send_file_name(f,flist,fname,recurse,FLAG_DELETE);
649d65ed
AT
653 flist_dir = NULL;
654 if (chdir(dbuf) != 0) {
9486289c 655 rprintf(FERROR,"chdir %s : %s\n",
649d65ed
AT
656 dbuf,strerror(errno));
657 exit_cleanup(1);
658 }
659 continue;
660 }
661
662 if (one_file_system)
663 set_filesystem(fname);
3333ffbd 664 send_file_name(f,flist,fname,recurse,FLAG_DELETE);
649d65ed 665 }
dc5ddbcc 666
649d65ed 667 if (f != -1) {
3333ffbd 668 send_file_entry(NULL,f,0);
649d65ed 669 }
c627d613 670
649d65ed 671 if (verbose && recurse && !am_server && f != -1)
9486289c 672 rprintf(FINFO,"done\n");
649d65ed
AT
673
674 clean_flist(flist);
675
676 /* now send the uid/gid list. This was introduced in protocol
677 version 15 */
678 if (f != -1 && remote_version >= 15) {
679 send_uid_list(f);
680 }
f6c34742 681
6ba9279f
AT
682 /* if protocol version is >= 17 then send the io_error flag */
683 if (f != -1 && remote_version >= 17) {
684 write_int(f, io_error);
685 }
686
d6dead6b
AT
687 if (f != -1) {
688 io_end_buffering(f);
689 write_flush(f);
690 }
691
17faa41c 692 if (verbose > 2)
9486289c 693 rprintf(FINFO,"send_file_list done\n");
17faa41c 694
649d65ed 695 return flist;
c627d613
AT
696}
697
698
699struct file_list *recv_file_list(int f)
700{
c627d613 701 struct file_list *flist;
182dca5c 702 unsigned char flags;
c627d613 703
a06d19e3 704 if (verbose && recurse && !am_server) {
9486289c
AT
705 rprintf(FINFO,"receiving file list ... ");
706 rflush(FINFO);
a06d19e3 707 }
c627d613
AT
708
709 flist = (struct file_list *)malloc(sizeof(flist[0]));
710 if (!flist)
711 goto oom;
712
713 flist->count=0;
3ec4dd97
AT
714 flist->malloced=1000;
715 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
716 flist->malloced);
c627d613
AT
717 if (!flist->files)
718 goto oom;
719
720
182dca5c 721 for (flags=read_byte(f); flags; flags=read_byte(f)) {
c627d613
AT
722 int i = flist->count;
723
724 if (i >= flist->malloced) {
3ec4dd97
AT
725 if (flist->malloced < 1000)
726 flist->malloced += 1000;
f9c51620 727 else
3ec4dd97
AT
728 flist->malloced *= 2;
729 flist->files =(struct file_struct **)realloc(flist->files,
730 sizeof(flist->files[0])*
731 flist->malloced);
f9c51620
AT
732 if (!flist->files)
733 goto oom;
c627d613
AT
734 }
735
182dca5c 736 receive_file_entry(&flist->files[i],flags,f);
c627d613 737
3ec4dd97
AT
738 if (S_ISREG(flist->files[i]->mode))
739 total_size += flist->files[i]->length;
c627d613
AT
740
741 flist->count++;
742
743 if (verbose > 2)
9486289c 744 rprintf(FINFO,"recv_file_name(%s)\n",f_name(flist->files[i]));
c627d613
AT
745 }
746
747
748 if (verbose > 2)
9486289c 749 rprintf(FINFO,"received %d names\n",flist->count);
c627d613
AT
750
751 clean_flist(flist);
752
a06d19e3 753 if (verbose && recurse && !am_server) {
9486289c 754 rprintf(FINFO,"done\n");
a06d19e3
AT
755 }
756
f6c34742
AT
757 /* now recv the uid/gid list. This was introduced in protocol version 15 */
758 if (f != -1 && remote_version >= 15) {
759 recv_uid_list(f, flist);
760 }
761
6ba9279f
AT
762 /* if protocol version is >= 17 then recv the io_error flag */
763 if (f != -1 && remote_version >= 17) {
764 io_error |= read_int(f);
765 }
766
17faa41c 767 if (verbose > 2)
9486289c 768 rprintf(FINFO,"recv_file_list done\n");
17faa41c 769
c627d613
AT
770 return flist;
771
772oom:
773 out_of_memory("recv_file_list");
774 return NULL; /* not reached */
775}
776
777
3ec4dd97 778int file_compare(struct file_struct **f1,struct file_struct **f2)
c627d613 779{
3ec4dd97
AT
780 if (!(*f1)->basename && !(*f2)->basename) return 0;
781 if (!(*f1)->basename) return -1;
782 if (!(*f2)->basename) return 1;
783 if ((*f1)->dirname == (*f2)->dirname)
784 return strcmp((*f1)->basename, (*f2)->basename);
785 return strcmp(f_name(*f1),f_name(*f2));
c627d613
AT
786}
787
788
789int flist_find(struct file_list *flist,struct file_struct *f)
790{
d966ee25
AT
791 int low=0,high=flist->count-1;
792
793 if (flist->count <= 0) return -1;
794
795 while (low != high) {
796 int mid = (low+high)/2;
3ec4dd97 797 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
d966ee25
AT
798 if (ret == 0) return flist_up(flist, mid);
799 if (ret > 0) {
800 high=mid;
801 } else {
802 low=mid+1;
803 }
804 }
805
3ec4dd97 806 if (file_compare(&flist->files[flist_up(flist,low)],&f) == 0)
d966ee25
AT
807 return flist_up(flist,low);
808 return -1;
c627d613
AT
809}
810
811
3ec4dd97
AT
812/*
813 * free up one file
814 */
815static void free_file(struct file_struct *file)
c627d613 816{
3ec4dd97
AT
817 if (!file) return;
818 if (file->basename) free(file->basename);
819 if (file->link) free(file->link);
0b910560 820 if (file->sum) free(file->sum);
3ec4dd97 821 bzero((char *)file, sizeof(*file));
3ec4dd97 822}
c627d613 823
c627d613 824
3ec4dd97
AT
825/*
826 * free up all elements in a flist
827 */
828void flist_free(struct file_list *flist)
829{
830 int i;
831 for (i=1;i<flist->count;i++) {
832 free_file(flist->files[i]);
649d65ed 833 free(flist->files[i]);
3ec4dd97
AT
834 }
835 bzero((char *)flist->files, sizeof(flist->files[0])*flist->count);
836 free(flist->files);
837 bzero((char *)flist, sizeof(*flist));
838 free(flist);
c627d613
AT
839}
840
841
842/*
843 * This routine ensures we don't have any duplicate names in our file list.
844 * duplicate names can cause corruption because of the pipelining
845 */
846void clean_flist(struct file_list *flist)
847{
3ec4dd97 848 int i;
c627d613 849
3ec4dd97
AT
850 if (!flist || flist->count == 0)
851 return;
c627d613 852
3ec4dd97
AT
853 qsort(flist->files,flist->count,
854 sizeof(flist->files[0]),
855 (int (*)())file_compare);
856
857 for (i=1;i<flist->count;i++) {
858 if (flist->files[i]->basename &&
649d65ed 859 flist->files[i-1]->basename &&
3ec4dd97
AT
860 strcmp(f_name(flist->files[i]),
861 f_name(flist->files[i-1])) == 0) {
862 if (verbose > 1 && !am_server)
9486289c 863 rprintf(FINFO,"removing duplicate name %s from file list %d\n",
3ec4dd97
AT
864 f_name(flist->files[i-1]),i-1);
865 free_file(flist->files[i]);
3ec4dd97
AT
866 }
867 }
868}
869
870
871/*
872 * return the full filename of a flist entry
873 */
874char *f_name(struct file_struct *f)
875{
876 static char names[10][MAXPATHLEN];
877 static int n;
878 char *p = names[n];
879
649d65ed 880 if (!f || !f->basename) return NULL;
3ec4dd97
AT
881
882 n = (n+1)%10;
883
884 if (f->dirname) {
e42c9458 885 slprintf(p, MAXPATHLEN-1, "%s/%s", f->dirname, f->basename);
3ec4dd97 886 } else {
e45e0093 887 strlcpy(p, f->basename, MAXPATHLEN-1);
3ec4dd97
AT
888 }
889
890 return p;
c627d613
AT
891}
892