preparing for release of 2.0.14
[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
2b6b4d53 49static struct exclude_struct **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 69{
2b6b4d53 70 if (check_exclude(fname,local_exclude_list,st)) {
c627d613 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
49d11b78 166 for (l1=0;lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);l1++) ;
3ec4dd97 167 l2 = strlen(fname) - l1;
4fe159a8
AT
168
169 if (l1 > 0) flags |= SAME_NAME;
170 if (l2 > 255) flags |= LONG_NAME;
d89322c4 171
cad2bba7
AT
172 /* we must make sure we don't send a zero flags byte or the other
173 end will terminate the flist transfer */
174 if (flags == 0 && !S_ISDIR(file->mode)) flags |= FLAG_DELETE;
175 if (flags == 0) flags |= LONG_NAME;
176
4fe159a8
AT
177 write_byte(f,flags);
178 if (flags & SAME_NAME)
179 write_byte(f,l1);
180 if (flags & LONG_NAME)
181 write_int(f,l2);
9f3541e6 182 else
4fe159a8 183 write_byte(f,l2);
3ec4dd97 184 write_buf(f,fname+l1,l2);
4fe159a8 185
3a6a366f 186 write_longint(f,file->length);
4fe159a8
AT
187 if (!(flags & SAME_TIME))
188 write_int(f,(int)file->modtime);
182dca5c
AT
189 if (!(flags & SAME_MODE))
190 write_int(f,(int)file->mode);
f6c34742
AT
191 if (preserve_uid && !(flags & SAME_UID)) {
192 add_uid(file->uid);
193 write_int(f,(int)file->uid);
194 }
195 if (preserve_gid && !(flags & SAME_GID)) {
196 add_gid(file->gid);
197 write_int(f,(int)file->gid);
198 }
dc5ddbcc
AT
199 if (preserve_devices && IS_DEVICE(file->mode) && !(flags & SAME_RDEV))
200 write_int(f,(int)file->rdev);
c627d613
AT
201
202#if SUPPORT_LINKS
203 if (preserve_links && S_ISLNK(file->mode)) {
204 write_int(f,strlen(file->link));
205 write_buf(f,file->link,strlen(file->link));
206 }
207#endif
208
dc5ddbcc
AT
209#if SUPPORT_HARD_LINKS
210 if (preserve_hard_links && S_ISREG(file->mode)) {
3a6a366f
AT
211 write_int(f,(int)file->dev);
212 write_int(f,(int)file->inode);
dc5ddbcc
AT
213 }
214#endif
215
c627d613 216 if (always_checksum) {
1250f24e 217 write_buf(f,file->sum,csum_length);
c627d613 218 }
182dca5c
AT
219
220 last_mode = file->mode;
dc5ddbcc 221 last_rdev = file->rdev;
182dca5c
AT
222 last_uid = file->uid;
223 last_gid = file->gid;
4fe159a8
AT
224 last_time = file->modtime;
225
7a6421fa 226 strlcpy(lastname,fname,MAXPATHLEN-1);
13a1f792 227 lastname[MAXPATHLEN-1] = 0;
182dca5c
AT
228}
229
230
231
3333ffbd
AT
232static void receive_file_entry(struct file_struct **fptr,
233 unsigned flags,int f)
182dca5c 234{
3a6a366f
AT
235 static time_t last_time;
236 static mode_t last_mode;
237 static dev_t last_rdev;
238 static uid_t last_uid;
239 static gid_t last_gid;
3ec4dd97
AT
240 static char lastname[MAXPATHLEN];
241 char thisname[MAXPATHLEN];
4fe159a8 242 int l1=0,l2=0;
3ec4dd97
AT
243 char *p;
244 struct file_struct *file;
182dca5c 245
4fe159a8 246 if (flags & SAME_NAME)
9f3541e6 247 l1 = read_byte(f);
4fe159a8
AT
248
249 if (flags & LONG_NAME)
250 l2 = read_int(f);
251 else
252 l2 = read_byte(f);
182dca5c 253
3ec4dd97
AT
254 file = (struct file_struct *)malloc(sizeof(*file));
255 if (!file) out_of_memory("receive_file_entry");
dc5ddbcc 256 bzero((char *)file,sizeof(*file));
3ec4dd97
AT
257 (*fptr) = file;
258
575f2fca
AT
259 if (l2 >= MAXPATHLEN-l1) overflow("receive_file_entry");
260
7a6421fa 261 strlcpy(thisname,lastname,l1);
575f2fca 262 read_sbuf(f,&thisname[l1],l2);
3ec4dd97
AT
263 thisname[l1+l2] = 0;
264
7a6421fa 265 strlcpy(lastname,thisname,MAXPATHLEN-1);
3ec4dd97
AT
266 lastname[MAXPATHLEN-1] = 0;
267
268 clean_fname(thisname);
269
130b4ae4
AT
270 if (relative_paths && thisname[0] == '/') {
271 /* strip / off absolute paths in destination */
272 memmove(thisname, thisname+1, strlen(thisname));
273 if (!thisname[0]) strcpy(thisname,".");
274 }
275
3ec4dd97
AT
276 if ((p = strrchr(thisname,'/'))) {
277 static char *lastdir;
278 *p = 0;
279 if (lastdir && strcmp(thisname, lastdir)==0) {
280 file->dirname = lastdir;
281 } else {
282 file->dirname = strdup(thisname);
283 lastdir = file->dirname;
284 }
285 file->basename = strdup(p+1);
286 } else {
287 file->dirname = NULL;
288 file->basename = strdup(thisname);
289 }
dc5ddbcc 290
3ec4dd97 291 if (!file->basename) out_of_memory("receive_file_entry 1");
182dca5c 292
182dca5c 293
3333ffbd 294 file->flags = flags;
3a6a366f 295 file->length = read_longint(f);
4fe159a8 296 file->modtime = (flags & SAME_TIME) ? last_time : (time_t)read_int(f);
182dca5c
AT
297 file->mode = (flags & SAME_MODE) ? last_mode : (mode_t)read_int(f);
298 if (preserve_uid)
299 file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f);
300 if (preserve_gid)
301 file->gid = (flags & SAME_GID) ? last_gid : (gid_t)read_int(f);
302 if (preserve_devices && IS_DEVICE(file->mode))
dc5ddbcc 303 file->rdev = (flags & SAME_RDEV) ? last_rdev : (dev_t)read_int(f);
182dca5c 304
182dca5c
AT
305 if (preserve_links && S_ISLNK(file->mode)) {
306 int l = read_int(f);
307 file->link = (char *)malloc(l+1);
6dd1782c 308 if (!file->link) out_of_memory("receive_file_entry 2");
575f2fca 309 read_sbuf(f,file->link,l);
182dca5c 310 }
dc5ddbcc
AT
311
312#if SUPPORT_HARD_LINKS
313 if (preserve_hard_links && S_ISREG(file->mode)) {
314 file->dev = read_int(f);
315 file->inode = read_int(f);
316 }
317#endif
182dca5c 318
2d0bb8eb
AT
319 if (always_checksum) {
320 file->sum = (char *)malloc(MD4_SUM_LENGTH);
321 if (!file->sum) out_of_memory("md4 sum");
322 read_buf(f,file->sum,csum_length);
323 }
182dca5c
AT
324
325 last_mode = file->mode;
dc5ddbcc 326 last_rdev = file->rdev;
182dca5c
AT
327 last_uid = file->uid;
328 last_gid = file->gid;
4fe159a8 329 last_time = file->modtime;
c627d613
AT
330}
331
332
0c5f37d9
AT
333/* determine if a file in a different filesstem should be skipped
334 when one_file_system is set. We bascally only want to include
335 the mount points - but they can be hard to find! */
bcacc18b 336static int skip_filesystem(char *fname, STRUCT_STAT *st)
0c5f37d9 337{
bcacc18b 338 STRUCT_STAT st2;
0c5f37d9
AT
339 char *p = strrchr(fname, '/');
340
341 /* skip all but directories */
342 if (!S_ISDIR(st->st_mode)) return 1;
343
344 /* if its not a subdirectory then allow */
345 if (!p) return 0;
346
347 *p = 0;
348 if (link_stat(fname, &st2)) {
349 *p = '/';
350 return 0;
351 }
352 *p = '/';
353
354 return (st2.st_dev != filesystem_dev);
355}
4fe159a8 356
a06d19e3 357static struct file_struct *make_file(char *fname)
c627d613 358{
3ec4dd97 359 struct file_struct *file;
bcacc18b 360 STRUCT_STAT st;
3ec4dd97
AT
361 char sum[SUM_LENGTH];
362 char *p;
363 char cleaned_name[MAXPATHLEN];
364
7a6421fa 365 strlcpy(cleaned_name, fname, MAXPATHLEN-1);
3ec4dd97
AT
366 cleaned_name[MAXPATHLEN-1] = 0;
367 clean_fname(cleaned_name);
368 fname = cleaned_name;
369
370 bzero(sum,SUM_LENGTH);
371
372 if (link_stat(fname,&st) != 0) {
6ba9279f 373 io_error = 1;
9486289c 374 rprintf(FERROR,"%s: %s\n",
3ec4dd97
AT
375 fname,strerror(errno));
376 return NULL;
377 }
c627d613 378
3ec4dd97 379 if (S_ISDIR(st.st_mode) && !recurse) {
9486289c 380 rprintf(FINFO,"skipping directory %s\n",fname);
3ec4dd97
AT
381 return NULL;
382 }
383
0c5f37d9
AT
384 if (one_file_system && st.st_dev != filesystem_dev) {
385 if (skip_filesystem(fname, &st))
386 return NULL;
387 }
3ec4dd97
AT
388
389 if (!match_file_name(fname,&st))
390 return NULL;
391
392 if (verbose > 2)
9486289c 393 rprintf(FINFO,"make_file(%s)\n",fname);
3ec4dd97
AT
394
395 file = (struct file_struct *)malloc(sizeof(*file));
396 if (!file) out_of_memory("make_file");
397 bzero((char *)file,sizeof(*file));
398
399 if ((p = strrchr(fname,'/'))) {
400 static char *lastdir;
401 *p = 0;
402 if (lastdir && strcmp(fname, lastdir)==0) {
403 file->dirname = lastdir;
404 } else {
405 file->dirname = strdup(fname);
406 lastdir = file->dirname;
407 }
408 file->basename = strdup(p+1);
409 *p = '/';
410 } else {
411 file->dirname = NULL;
412 file->basename = strdup(fname);
413 }
c627d613 414
3ec4dd97
AT
415 file->modtime = st.st_mtime;
416 file->length = st.st_size;
417 file->mode = st.st_mode;
418 file->uid = st.st_uid;
419 file->gid = st.st_gid;
420 file->dev = st.st_dev;
421 file->inode = st.st_ino;
c627d613 422#ifdef HAVE_ST_RDEV
3ec4dd97 423 file->rdev = st.st_rdev;
c627d613
AT
424#endif
425
426#if SUPPORT_LINKS
3ec4dd97
AT
427 if (S_ISLNK(st.st_mode)) {
428 int l;
429 char lnk[MAXPATHLEN];
430 if ((l=readlink(fname,lnk,MAXPATHLEN-1)) == -1) {
6ba9279f 431 io_error=1;
9486289c 432 rprintf(FERROR,"readlink %s : %s\n",
3ec4dd97
AT
433 fname,strerror(errno));
434 return NULL;
435 }
436 lnk[l] = 0;
437 file->link = strdup(lnk);
438 }
c627d613
AT
439#endif
440
1250f24e 441 if (always_checksum) {
2d0bb8eb
AT
442 file->sum = (char *)malloc(MD4_SUM_LENGTH);
443 if (!file->sum) out_of_memory("md4 sum");
1250f24e
AT
444 /* drat. we have to provide a null checksum for non-regular
445 files in order to be compatible with earlier versions
446 of rsync */
447 if (S_ISREG(st.st_mode)) {
448 file_checksum(fname,file->sum,st.st_size);
449 } else {
450 memset(file->sum, 0, MD4_SUM_LENGTH);
451 }
3ec4dd97 452 }
c627d613 453
3ec4dd97
AT
454 if (flist_dir) {
455 static char *lastdir;
456 if (lastdir && strcmp(lastdir, flist_dir)==0) {
457 file->basedir = lastdir;
458 } else {
459 file->basedir = strdup(flist_dir);
460 lastdir = file->basedir;
461 }
462 } else {
463 file->basedir = NULL;
464 }
c627d613 465
3ec4dd97
AT
466 if (!S_ISDIR(st.st_mode))
467 total_size += st.st_size;
c627d613 468
3ec4dd97 469 return file;
c627d613
AT
470}
471
472
473
649d65ed 474static void send_file_name(int f,struct file_list *flist,char *fname,
3333ffbd 475 int recursive, unsigned base_flags)
c627d613
AT
476{
477 struct file_struct *file;
478
a06d19e3 479 file = make_file(fname);
c627d613
AT
480
481 if (!file) return;
482
483 if (flist->count >= flist->malloced) {
3ec4dd97
AT
484 if (flist->malloced < 1000)
485 flist->malloced += 1000;
f9c51620 486 else
3ec4dd97
AT
487 flist->malloced *= 2;
488 flist->files = (struct file_struct **)realloc(flist->files,
489 sizeof(flist->files[0])*
490 flist->malloced);
f9c51620
AT
491 if (!flist->files)
492 out_of_memory("send_file_name");
c627d613
AT
493 }
494
3ec4dd97
AT
495 if (strcmp(file->basename,"")) {
496 flist->files[flist->count++] = file;
3333ffbd 497 send_file_entry(file,f,base_flags);
c627d613
AT
498 }
499
649d65ed 500 if (S_ISDIR(file->mode) && recursive) {
2b6b4d53
AT
501 struct exclude_struct **last_exclude_list = local_exclude_list;
502 send_directory(f,flist,f_name(file));
503 local_exclude_list = last_exclude_list;
504 return;
c627d613
AT
505 }
506}
507
508
509
510static void send_directory(int f,struct file_list *flist,char *dir)
511{
3ec4dd97
AT
512 DIR *d;
513 struct dirent *di;
514 char fname[MAXPATHLEN];
515 int l;
516 char *p;
517
518 d = opendir(dir);
519 if (!d) {
6ba9279f 520 io_error = 1;
2f7512b0 521 rprintf(FERROR,"opendir(%s): %s\n",
3ec4dd97
AT
522 dir,strerror(errno));
523 return;
524 }
c627d613 525
7a6421fa 526 strlcpy(fname,dir,MAXPATHLEN-1);
3ec4dd97
AT
527 l = strlen(fname);
528 if (fname[l-1] != '/') {
529 if (l == MAXPATHLEN-1) {
6ba9279f 530 io_error = 1;
9486289c 531 rprintf(FERROR,"skipping long-named directory %s\n",fname);
3ec4dd97
AT
532 closedir(d);
533 return;
534 }
e42c9458 535 strlcat(fname,"/", MAXPATHLEN-1);
3ec4dd97
AT
536 l++;
537 }
538 p = fname + strlen(fname);
c627d613 539
3ec4dd97
AT
540 if (cvs_exclude) {
541 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN-1) {
542 strcpy(p,".cvsignore");
2b6b4d53 543 local_exclude_list = make_exclude_list(fname,NULL,0,0);
3ec4dd97 544 } else {
6ba9279f 545 io_error = 1;
9486289c 546 rprintf(FINFO,"cannot cvs-exclude in long-named directory %s\n",fname);
3ec4dd97
AT
547 }
548 }
549
550 for (di=readdir(d); di; di=readdir(d)) {
d6e6ecbd
AT
551 char *dname = d_name(di);
552 if (strcmp(dname,".")==0 ||
553 strcmp(dname,"..")==0)
3ec4dd97 554 continue;
7a6421fa 555 strlcpy(p,dname,MAXPATHLEN-(l+1));
7b1ce0d7 556 send_file_name(f,flist,fname,recurse,0);
3ec4dd97 557 }
c627d613 558
3ec4dd97 559 closedir(d);
c627d613
AT
560}
561
562
563
a06d19e3 564struct file_list *send_file_list(int f,int argc,char *argv[])
c627d613 565{
649d65ed 566 int i,l;
bcacc18b 567 STRUCT_STAT st;
649d65ed
AT
568 char *p,*dir;
569 char dbuf[MAXPATHLEN];
570 char lastpath[MAXPATHLEN]="";
571 struct file_list *flist;
572
573 if (verbose && recurse && !am_server && f != -1) {
9486289c
AT
574 rprintf(FINFO,"building file list ... ");
575 rflush(FINFO);
649d65ed 576 }
c627d613 577
649d65ed
AT
578 flist = (struct file_list *)malloc(sizeof(flist[0]));
579 if (!flist) out_of_memory("send_file_list");
c627d613 580
649d65ed
AT
581 flist->count=0;
582 flist->malloced = 1000;
583 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
584 flist->malloced);
585 if (!flist->files) out_of_memory("send_file_list");
c627d613 586
d6dead6b
AT
587 if (f != -1) {
588 io_start_buffering(f);
589 }
590
649d65ed
AT
591 for (i=0;i<argc;i++) {
592 char fname2[MAXPATHLEN];
593 char *fname = fname2;
c627d613 594
7a6421fa 595 strlcpy(fname,argv[i],MAXPATHLEN-1);
c627d613 596
649d65ed
AT
597 l = strlen(fname);
598 if (l != 1 && fname[l-1] == '/') {
e42c9458 599 strlcat(fname,".",MAXPATHLEN-1);
649d65ed 600 }
c627d613 601
649d65ed 602 if (link_stat(fname,&st) != 0) {
6ba9279f 603 io_error=1;
9486289c 604 rprintf(FERROR,"%s : %s\n",fname,strerror(errno));
649d65ed
AT
605 continue;
606 }
c627d613 607
649d65ed 608 if (S_ISDIR(st.st_mode) && !recurse) {
9486289c 609 rprintf(FINFO,"skipping directory %s\n",fname);
649d65ed
AT
610 continue;
611 }
c627d613 612
649d65ed
AT
613 dir = NULL;
614
615 if (!relative_paths) {
616 p = strrchr(fname,'/');
617 if (p) {
618 *p = 0;
619 if (p == fname)
620 dir = "/";
621 else
622 dir = fname;
623 fname = p+1;
624 }
625 } else if (f != -1 && (p=strrchr(fname,'/'))) {
626 /* this ensures we send the intermediate directories,
627 thus getting their permissions right */
628 *p = 0;
629 if (strcmp(lastpath,fname)) {
1a016bfd 630 strlcpy(lastpath, fname, sizeof(lastpath)-1);
649d65ed
AT
631 *p = '/';
632 for (p=fname+1; (p=strchr(p,'/')); p++) {
6c82f74b 633 int copy_links_saved = copy_links;
649d65ed 634 *p = 0;
6c82f74b 635 copy_links = 0;
3333ffbd 636 send_file_name(f, flist, fname, 0, 0);
6c82f74b 637 copy_links = copy_links_saved;
649d65ed
AT
638 *p = '/';
639 }
640 } else {
641 *p = '/';
642 }
643 }
644
645 if (!*fname)
646 fname = ".";
647
648 if (dir && *dir) {
649 if (getcwd(dbuf,MAXPATHLEN-1) == NULL) {
9486289c 650 rprintf(FERROR,"getwd : %s\n",strerror(errno));
649d65ed
AT
651 exit_cleanup(1);
652 }
653 if (chdir(dir) != 0) {
6ba9279f 654 io_error=1;
9486289c 655 rprintf(FERROR,"chdir %s : %s\n",
649d65ed
AT
656 dir,strerror(errno));
657 continue;
658 }
659 flist_dir = dir;
660 if (one_file_system)
661 set_filesystem(fname);
3333ffbd 662 send_file_name(f,flist,fname,recurse,FLAG_DELETE);
649d65ed
AT
663 flist_dir = NULL;
664 if (chdir(dbuf) != 0) {
9486289c 665 rprintf(FERROR,"chdir %s : %s\n",
649d65ed
AT
666 dbuf,strerror(errno));
667 exit_cleanup(1);
668 }
669 continue;
670 }
671
672 if (one_file_system)
673 set_filesystem(fname);
3333ffbd 674 send_file_name(f,flist,fname,recurse,FLAG_DELETE);
649d65ed 675 }
dc5ddbcc 676
649d65ed 677 if (f != -1) {
3333ffbd 678 send_file_entry(NULL,f,0);
649d65ed 679 }
c627d613 680
649d65ed 681 if (verbose && recurse && !am_server && f != -1)
9486289c 682 rprintf(FINFO,"done\n");
649d65ed
AT
683
684 clean_flist(flist);
685
686 /* now send the uid/gid list. This was introduced in protocol
687 version 15 */
688 if (f != -1 && remote_version >= 15) {
689 send_uid_list(f);
690 }
f6c34742 691
6ba9279f
AT
692 /* if protocol version is >= 17 then send the io_error flag */
693 if (f != -1 && remote_version >= 17) {
694 write_int(f, io_error);
695 }
696
d6dead6b
AT
697 if (f != -1) {
698 io_end_buffering(f);
d6dead6b
AT
699 }
700
17faa41c 701 if (verbose > 2)
9486289c 702 rprintf(FINFO,"send_file_list done\n");
17faa41c 703
649d65ed 704 return flist;
c627d613
AT
705}
706
707
708struct file_list *recv_file_list(int f)
709{
c627d613 710 struct file_list *flist;
182dca5c 711 unsigned char flags;
c627d613 712
a06d19e3 713 if (verbose && recurse && !am_server) {
9486289c
AT
714 rprintf(FINFO,"receiving file list ... ");
715 rflush(FINFO);
a06d19e3 716 }
c627d613
AT
717
718 flist = (struct file_list *)malloc(sizeof(flist[0]));
719 if (!flist)
720 goto oom;
721
722 flist->count=0;
3ec4dd97
AT
723 flist->malloced=1000;
724 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
725 flist->malloced);
c627d613
AT
726 if (!flist->files)
727 goto oom;
728
729
182dca5c 730 for (flags=read_byte(f); flags; flags=read_byte(f)) {
c627d613
AT
731 int i = flist->count;
732
733 if (i >= flist->malloced) {
3ec4dd97
AT
734 if (flist->malloced < 1000)
735 flist->malloced += 1000;
f9c51620 736 else
3ec4dd97
AT
737 flist->malloced *= 2;
738 flist->files =(struct file_struct **)realloc(flist->files,
739 sizeof(flist->files[0])*
740 flist->malloced);
f9c51620
AT
741 if (!flist->files)
742 goto oom;
c627d613
AT
743 }
744
182dca5c 745 receive_file_entry(&flist->files[i],flags,f);
c627d613 746
3ec4dd97
AT
747 if (S_ISREG(flist->files[i]->mode))
748 total_size += flist->files[i]->length;
c627d613
AT
749
750 flist->count++;
751
752 if (verbose > 2)
9486289c 753 rprintf(FINFO,"recv_file_name(%s)\n",f_name(flist->files[i]));
c627d613
AT
754 }
755
756
757 if (verbose > 2)
9486289c 758 rprintf(FINFO,"received %d names\n",flist->count);
c627d613
AT
759
760 clean_flist(flist);
761
a06d19e3 762 if (verbose && recurse && !am_server) {
9486289c 763 rprintf(FINFO,"done\n");
a06d19e3
AT
764 }
765
f6c34742
AT
766 /* now recv the uid/gid list. This was introduced in protocol version 15 */
767 if (f != -1 && remote_version >= 15) {
768 recv_uid_list(f, flist);
769 }
770
6ba9279f
AT
771 /* if protocol version is >= 17 then recv the io_error flag */
772 if (f != -1 && remote_version >= 17) {
773 io_error |= read_int(f);
774 }
775
17faa41c 776 if (verbose > 2)
9486289c 777 rprintf(FINFO,"recv_file_list done\n");
17faa41c 778
c627d613
AT
779 return flist;
780
781oom:
782 out_of_memory("recv_file_list");
783 return NULL; /* not reached */
784}
785
786
3ec4dd97 787int file_compare(struct file_struct **f1,struct file_struct **f2)
c627d613 788{
3ec4dd97
AT
789 if (!(*f1)->basename && !(*f2)->basename) return 0;
790 if (!(*f1)->basename) return -1;
791 if (!(*f2)->basename) return 1;
792 if ((*f1)->dirname == (*f2)->dirname)
793 return strcmp((*f1)->basename, (*f2)->basename);
794 return strcmp(f_name(*f1),f_name(*f2));
c627d613
AT
795}
796
797
798int flist_find(struct file_list *flist,struct file_struct *f)
799{
d966ee25
AT
800 int low=0,high=flist->count-1;
801
802 if (flist->count <= 0) return -1;
803
804 while (low != high) {
805 int mid = (low+high)/2;
3ec4dd97 806 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
d966ee25
AT
807 if (ret == 0) return flist_up(flist, mid);
808 if (ret > 0) {
809 high=mid;
810 } else {
811 low=mid+1;
812 }
813 }
814
3ec4dd97 815 if (file_compare(&flist->files[flist_up(flist,low)],&f) == 0)
d966ee25
AT
816 return flist_up(flist,low);
817 return -1;
c627d613
AT
818}
819
820
3ec4dd97
AT
821/*
822 * free up one file
823 */
824static void free_file(struct file_struct *file)
c627d613 825{
3ec4dd97
AT
826 if (!file) return;
827 if (file->basename) free(file->basename);
828 if (file->link) free(file->link);
0b910560 829 if (file->sum) free(file->sum);
3ec4dd97 830 bzero((char *)file, sizeof(*file));
3ec4dd97 831}
c627d613 832
c627d613 833
3ec4dd97
AT
834/*
835 * free up all elements in a flist
836 */
837void flist_free(struct file_list *flist)
838{
839 int i;
840 for (i=1;i<flist->count;i++) {
841 free_file(flist->files[i]);
649d65ed 842 free(flist->files[i]);
3ec4dd97
AT
843 }
844 bzero((char *)flist->files, sizeof(flist->files[0])*flist->count);
845 free(flist->files);
846 bzero((char *)flist, sizeof(*flist));
847 free(flist);
c627d613
AT
848}
849
850
851/*
852 * This routine ensures we don't have any duplicate names in our file list.
853 * duplicate names can cause corruption because of the pipelining
854 */
855void clean_flist(struct file_list *flist)
856{
3ec4dd97 857 int i;
c627d613 858
3ec4dd97
AT
859 if (!flist || flist->count == 0)
860 return;
c627d613 861
3ec4dd97
AT
862 qsort(flist->files,flist->count,
863 sizeof(flist->files[0]),
864 (int (*)())file_compare);
865
866 for (i=1;i<flist->count;i++) {
867 if (flist->files[i]->basename &&
649d65ed 868 flist->files[i-1]->basename &&
3ec4dd97
AT
869 strcmp(f_name(flist->files[i]),
870 f_name(flist->files[i-1])) == 0) {
871 if (verbose > 1 && !am_server)
9486289c 872 rprintf(FINFO,"removing duplicate name %s from file list %d\n",
3ec4dd97
AT
873 f_name(flist->files[i-1]),i-1);
874 free_file(flist->files[i]);
3ec4dd97
AT
875 }
876 }
877}
878
879
880/*
881 * return the full filename of a flist entry
882 */
883char *f_name(struct file_struct *f)
884{
885 static char names[10][MAXPATHLEN];
886 static int n;
887 char *p = names[n];
888
649d65ed 889 if (!f || !f->basename) return NULL;
3ec4dd97
AT
890
891 n = (n+1)%10;
892
893 if (f->dirname) {
e42c9458 894 slprintf(p, MAXPATHLEN-1, "%s/%s", f->dirname, f->basename);
3ec4dd97 895 } else {
e45e0093 896 strlcpy(p, f->basename, MAXPATHLEN-1);
3ec4dd97
AT
897 }
898
899 return p;
c627d613
AT
900}
901