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