preparing for release of 1.7.2
[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
82306bf6
AT
51int link_stat(const char *Path, struct stat *Buffer)
52{
53#if SUPPORT_LINKS
54 if (copy_links) {
55 return stat(Path, Buffer);
56 } else {
57 return lstat(Path, Buffer);
58 }
59#else
60 return stat(Path, Buffer);
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 */
68static int match_file_name(char *fname,struct stat *st)
69{
70 if (check_exclude(fname,local_exclude_list)) {
71 if (verbose > 2)
6ba9279f 72 fprintf(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{
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
137void send_file_entry(struct file_struct *file,int f)
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
182dca5c
AT
158 flags = FILE_VALID;
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) {
43a481dc 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
3ec4dd97 221 strncpy(lastname,fname,MAXPATHLEN-1);
13a1f792 222 lastname[MAXPATHLEN-1] = 0;
182dca5c
AT
223}
224
225
226
3ec4dd97
AT
227void receive_file_entry(struct file_struct **fptr,
228 unsigned char 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
3ec4dd97 256 strncpy(thisname,lastname,l1);
575f2fca 257 read_sbuf(f,&thisname[l1],l2);
3ec4dd97
AT
258 thisname[l1+l2] = 0;
259
260 strncpy(lastname,thisname,MAXPATHLEN-1);
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
3a6a366f 283 file->length = read_longint(f);
4fe159a8 284 file->modtime = (flags & SAME_TIME) ? last_time : (time_t)read_int(f);
182dca5c
AT
285 file->mode = (flags & SAME_MODE) ? last_mode : (mode_t)read_int(f);
286 if (preserve_uid)
287 file->uid = (flags & SAME_UID) ? last_uid : (uid_t)read_int(f);
288 if (preserve_gid)
289 file->gid = (flags & SAME_GID) ? last_gid : (gid_t)read_int(f);
290 if (preserve_devices && IS_DEVICE(file->mode))
dc5ddbcc 291 file->rdev = (flags & SAME_RDEV) ? last_rdev : (dev_t)read_int(f);
182dca5c 292
182dca5c
AT
293 if (preserve_links && S_ISLNK(file->mode)) {
294 int l = read_int(f);
295 file->link = (char *)malloc(l+1);
6dd1782c 296 if (!file->link) out_of_memory("receive_file_entry 2");
575f2fca 297 read_sbuf(f,file->link,l);
182dca5c 298 }
dc5ddbcc
AT
299
300#if SUPPORT_HARD_LINKS
301 if (preserve_hard_links && S_ISREG(file->mode)) {
302 file->dev = read_int(f);
303 file->inode = read_int(f);
304 }
305#endif
182dca5c 306
2d0bb8eb
AT
307 if (always_checksum) {
308 file->sum = (char *)malloc(MD4_SUM_LENGTH);
309 if (!file->sum) out_of_memory("md4 sum");
310 read_buf(f,file->sum,csum_length);
311 }
182dca5c
AT
312
313 last_mode = file->mode;
dc5ddbcc 314 last_rdev = file->rdev;
182dca5c
AT
315 last_uid = file->uid;
316 last_gid = file->gid;
4fe159a8 317 last_time = file->modtime;
c627d613
AT
318}
319
320
0c5f37d9
AT
321/* determine if a file in a different filesstem should be skipped
322 when one_file_system is set. We bascally only want to include
323 the mount points - but they can be hard to find! */
324static int skip_filesystem(char *fname, struct stat *st)
325{
326 struct stat st2;
327 char *p = strrchr(fname, '/');
328
329 /* skip all but directories */
330 if (!S_ISDIR(st->st_mode)) return 1;
331
332 /* if its not a subdirectory then allow */
333 if (!p) return 0;
334
335 *p = 0;
336 if (link_stat(fname, &st2)) {
337 *p = '/';
338 return 0;
339 }
340 *p = '/';
341
342 return (st2.st_dev != filesystem_dev);
343}
4fe159a8 344
a06d19e3 345static struct file_struct *make_file(char *fname)
c627d613 346{
3ec4dd97
AT
347 struct file_struct *file;
348 struct stat st;
349 char sum[SUM_LENGTH];
350 char *p;
351 char cleaned_name[MAXPATHLEN];
352
353 strncpy(cleaned_name, fname, MAXPATHLEN-1);
354 cleaned_name[MAXPATHLEN-1] = 0;
355 clean_fname(cleaned_name);
356 fname = cleaned_name;
357
358 bzero(sum,SUM_LENGTH);
359
360 if (link_stat(fname,&st) != 0) {
6ba9279f 361 io_error = 1;
3ec4dd97
AT
362 fprintf(FERROR,"%s: %s\n",
363 fname,strerror(errno));
364 return NULL;
365 }
c627d613 366
3ec4dd97 367 if (S_ISDIR(st.st_mode) && !recurse) {
6ba9279f 368 fprintf(FINFO,"skipping directory %s\n",fname);
3ec4dd97
AT
369 return NULL;
370 }
371
0c5f37d9
AT
372 if (one_file_system && st.st_dev != filesystem_dev) {
373 if (skip_filesystem(fname, &st))
374 return NULL;
375 }
3ec4dd97
AT
376
377 if (!match_file_name(fname,&st))
378 return NULL;
379
380 if (verbose > 2)
6ba9279f 381 fprintf(FINFO,"make_file(%s)\n",fname);
3ec4dd97
AT
382
383 file = (struct file_struct *)malloc(sizeof(*file));
384 if (!file) out_of_memory("make_file");
385 bzero((char *)file,sizeof(*file));
386
387 if ((p = strrchr(fname,'/'))) {
388 static char *lastdir;
389 *p = 0;
390 if (lastdir && strcmp(fname, lastdir)==0) {
391 file->dirname = lastdir;
392 } else {
393 file->dirname = strdup(fname);
394 lastdir = file->dirname;
395 }
396 file->basename = strdup(p+1);
397 *p = '/';
398 } else {
399 file->dirname = NULL;
400 file->basename = strdup(fname);
401 }
c627d613 402
3ec4dd97
AT
403 file->modtime = st.st_mtime;
404 file->length = st.st_size;
405 file->mode = st.st_mode;
406 file->uid = st.st_uid;
407 file->gid = st.st_gid;
408 file->dev = st.st_dev;
409 file->inode = st.st_ino;
c627d613 410#ifdef HAVE_ST_RDEV
3ec4dd97 411 file->rdev = st.st_rdev;
c627d613
AT
412#endif
413
414#if SUPPORT_LINKS
3ec4dd97
AT
415 if (S_ISLNK(st.st_mode)) {
416 int l;
417 char lnk[MAXPATHLEN];
418 if ((l=readlink(fname,lnk,MAXPATHLEN-1)) == -1) {
6ba9279f 419 io_error=1;
3ec4dd97
AT
420 fprintf(FERROR,"readlink %s : %s\n",
421 fname,strerror(errno));
422 return NULL;
423 }
424 lnk[l] = 0;
425 file->link = strdup(lnk);
426 }
c627d613
AT
427#endif
428
3ec4dd97 429 if (always_checksum && S_ISREG(st.st_mode)) {
2d0bb8eb
AT
430 file->sum = (char *)malloc(MD4_SUM_LENGTH);
431 if (!file->sum) out_of_memory("md4 sum");
3ec4dd97
AT
432 file_checksum(fname,file->sum,st.st_size);
433 }
c627d613 434
3ec4dd97
AT
435 if (flist_dir) {
436 static char *lastdir;
437 if (lastdir && strcmp(lastdir, flist_dir)==0) {
438 file->basedir = lastdir;
439 } else {
440 file->basedir = strdup(flist_dir);
441 lastdir = file->basedir;
442 }
443 } else {
444 file->basedir = NULL;
445 }
c627d613 446
3ec4dd97
AT
447 if (!S_ISDIR(st.st_mode))
448 total_size += st.st_size;
c627d613 449
3ec4dd97 450 return file;
c627d613
AT
451}
452
453
454
649d65ed
AT
455static void send_file_name(int f,struct file_list *flist,char *fname,
456 int recursive)
c627d613
AT
457{
458 struct file_struct *file;
459
a06d19e3 460 file = make_file(fname);
c627d613
AT
461
462 if (!file) return;
463
464 if (flist->count >= flist->malloced) {
3ec4dd97
AT
465 if (flist->malloced < 1000)
466 flist->malloced += 1000;
f9c51620 467 else
3ec4dd97
AT
468 flist->malloced *= 2;
469 flist->files = (struct file_struct **)realloc(flist->files,
470 sizeof(flist->files[0])*
471 flist->malloced);
f9c51620
AT
472 if (!flist->files)
473 out_of_memory("send_file_name");
c627d613
AT
474 }
475
3ec4dd97
AT
476 if (strcmp(file->basename,"")) {
477 flist->files[flist->count++] = file;
c627d613
AT
478 send_file_entry(file,f);
479 }
480
649d65ed 481 if (S_ISDIR(file->mode) && recursive) {
c627d613 482 char **last_exclude_list = local_exclude_list;
3ec4dd97 483 send_directory(f,flist,f_name(file));
c627d613
AT
484 local_exclude_list = last_exclude_list;
485 return;
486 }
487}
488
489
490
491static void send_directory(int f,struct file_list *flist,char *dir)
492{
3ec4dd97
AT
493 DIR *d;
494 struct dirent *di;
495 char fname[MAXPATHLEN];
496 int l;
497 char *p;
498
499 d = opendir(dir);
500 if (!d) {
6ba9279f 501 io_error = 1;
3ec4dd97
AT
502 fprintf(FERROR,"%s: %s\n",
503 dir,strerror(errno));
504 return;
505 }
c627d613 506
3ec4dd97
AT
507 strncpy(fname,dir,MAXPATHLEN-1);
508 fname[MAXPATHLEN-1]=0;
509 l = strlen(fname);
510 if (fname[l-1] != '/') {
511 if (l == MAXPATHLEN-1) {
6ba9279f 512 io_error = 1;
3ec4dd97
AT
513 fprintf(FERROR,"skipping long-named directory %s\n",fname);
514 closedir(d);
515 return;
516 }
517 strcat(fname,"/");
518 l++;
519 }
520 p = fname + strlen(fname);
c627d613 521
3ec4dd97
AT
522 if (cvs_exclude) {
523 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN-1) {
524 strcpy(p,".cvsignore");
525 local_exclude_list = make_exclude_list(fname,NULL,0);
526 } else {
6ba9279f
AT
527 io_error = 1;
528 fprintf(FINFO,"cannot cvs-exclude in long-named directory %s\n",fname);
3ec4dd97
AT
529 }
530 }
531
532 for (di=readdir(d); di; di=readdir(d)) {
533 if (strcmp(di->d_name,".")==0 ||
534 strcmp(di->d_name,"..")==0)
535 continue;
536 strncpy(p,di->d_name,MAXPATHLEN-(l+1));
649d65ed 537 send_file_name(f,flist,fname,recurse);
3ec4dd97 538 }
c627d613 539
3ec4dd97 540 closedir(d);
c627d613
AT
541}
542
543
544
a06d19e3 545struct file_list *send_file_list(int f,int argc,char *argv[])
c627d613 546{
649d65ed
AT
547 int i,l;
548 struct stat st;
549 char *p,*dir;
550 char dbuf[MAXPATHLEN];
551 char lastpath[MAXPATHLEN]="";
552 struct file_list *flist;
553
554 if (verbose && recurse && !am_server && f != -1) {
555 fprintf(FINFO,"building file list ... ");
556 fflush(FINFO);
557 }
c627d613 558
649d65ed
AT
559 flist = (struct file_list *)malloc(sizeof(flist[0]));
560 if (!flist) out_of_memory("send_file_list");
c627d613 561
649d65ed
AT
562 flist->count=0;
563 flist->malloced = 1000;
564 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
565 flist->malloced);
566 if (!flist->files) out_of_memory("send_file_list");
c627d613 567
649d65ed
AT
568 for (i=0;i<argc;i++) {
569 char fname2[MAXPATHLEN];
570 char *fname = fname2;
c627d613 571
649d65ed
AT
572 strncpy(fname,argv[i],MAXPATHLEN-1);
573 fname[MAXPATHLEN-1] = 0;
c627d613 574
649d65ed
AT
575 l = strlen(fname);
576 if (l != 1 && fname[l-1] == '/') {
577 strcat(fname,".");
578 }
c627d613 579
649d65ed 580 if (link_stat(fname,&st) != 0) {
6ba9279f 581 io_error=1;
649d65ed
AT
582 fprintf(FERROR,"%s : %s\n",fname,strerror(errno));
583 continue;
584 }
c627d613 585
649d65ed 586 if (S_ISDIR(st.st_mode) && !recurse) {
6ba9279f 587 fprintf(FINFO,"skipping directory %s\n",fname);
649d65ed
AT
588 continue;
589 }
c627d613 590
649d65ed
AT
591 dir = NULL;
592
593 if (!relative_paths) {
594 p = strrchr(fname,'/');
595 if (p) {
596 *p = 0;
597 if (p == fname)
598 dir = "/";
599 else
600 dir = fname;
601 fname = p+1;
602 }
603 } else if (f != -1 && (p=strrchr(fname,'/'))) {
604 /* this ensures we send the intermediate directories,
605 thus getting their permissions right */
606 *p = 0;
607 if (strcmp(lastpath,fname)) {
608 strcpy(lastpath, fname);
609 *p = '/';
610 for (p=fname+1; (p=strchr(p,'/')); p++) {
611 *p = 0;
612 send_file_name(f, flist, fname, 0);
613 *p = '/';
614 }
615 } else {
616 *p = '/';
617 }
618 }
619
620 if (!*fname)
621 fname = ".";
622
623 if (dir && *dir) {
624 if (getcwd(dbuf,MAXPATHLEN-1) == NULL) {
625 fprintf(FERROR,"getwd : %s\n",strerror(errno));
626 exit_cleanup(1);
627 }
628 if (chdir(dir) != 0) {
6ba9279f 629 io_error=1;
649d65ed
AT
630 fprintf(FERROR,"chdir %s : %s\n",
631 dir,strerror(errno));
632 continue;
633 }
634 flist_dir = dir;
635 if (one_file_system)
636 set_filesystem(fname);
637 send_file_name(f,flist,fname,recurse);
638 flist_dir = NULL;
639 if (chdir(dbuf) != 0) {
640 fprintf(FERROR,"chdir %s : %s\n",
641 dbuf,strerror(errno));
642 exit_cleanup(1);
643 }
644 continue;
645 }
646
647 if (one_file_system)
648 set_filesystem(fname);
649 send_file_name(f,flist,fname,recurse);
650 }
dc5ddbcc 651
649d65ed
AT
652 if (f != -1) {
653 send_file_entry(NULL,f);
654 write_flush(f);
655 }
c627d613 656
649d65ed
AT
657 if (verbose && recurse && !am_server && f != -1)
658 fprintf(FINFO,"done\n");
659
660 clean_flist(flist);
661
662 /* now send the uid/gid list. This was introduced in protocol
663 version 15 */
664 if (f != -1 && remote_version >= 15) {
665 send_uid_list(f);
666 }
f6c34742 667
6ba9279f
AT
668 /* if protocol version is >= 17 then send the io_error flag */
669 if (f != -1 && remote_version >= 17) {
670 write_int(f, io_error);
671 }
672
17faa41c
AT
673 if (verbose > 2)
674 fprintf(FINFO,"send_file_list done\n");
675
649d65ed 676 return flist;
c627d613
AT
677}
678
679
680struct file_list *recv_file_list(int f)
681{
c627d613 682 struct file_list *flist;
182dca5c 683 unsigned char flags;
c627d613 684
a06d19e3
AT
685 if (verbose && recurse && !am_server) {
686 fprintf(FINFO,"receiving file list ... ");
687 fflush(FINFO);
688 }
c627d613
AT
689
690 flist = (struct file_list *)malloc(sizeof(flist[0]));
691 if (!flist)
692 goto oom;
693
694 flist->count=0;
3ec4dd97
AT
695 flist->malloced=1000;
696 flist->files = (struct file_struct **)malloc(sizeof(flist->files[0])*
697 flist->malloced);
c627d613
AT
698 if (!flist->files)
699 goto oom;
700
701
182dca5c 702 for (flags=read_byte(f); flags; flags=read_byte(f)) {
c627d613
AT
703 int i = flist->count;
704
705 if (i >= flist->malloced) {
3ec4dd97
AT
706 if (flist->malloced < 1000)
707 flist->malloced += 1000;
f9c51620 708 else
3ec4dd97
AT
709 flist->malloced *= 2;
710 flist->files =(struct file_struct **)realloc(flist->files,
711 sizeof(flist->files[0])*
712 flist->malloced);
f9c51620
AT
713 if (!flist->files)
714 goto oom;
c627d613
AT
715 }
716
182dca5c 717 receive_file_entry(&flist->files[i],flags,f);
c627d613 718
3ec4dd97
AT
719 if (S_ISREG(flist->files[i]->mode))
720 total_size += flist->files[i]->length;
c627d613
AT
721
722 flist->count++;
723
724 if (verbose > 2)
6ba9279f 725 fprintf(FINFO,"recv_file_name(%s)\n",f_name(flist->files[i]));
c627d613
AT
726 }
727
728
729 if (verbose > 2)
6ba9279f 730 fprintf(FINFO,"received %d names\n",flist->count);
c627d613
AT
731
732 clean_flist(flist);
733
a06d19e3
AT
734 if (verbose && recurse && !am_server) {
735 fprintf(FINFO,"done\n");
736 }
737
f6c34742
AT
738 /* now recv the uid/gid list. This was introduced in protocol version 15 */
739 if (f != -1 && remote_version >= 15) {
740 recv_uid_list(f, flist);
741 }
742
6ba9279f
AT
743 /* if protocol version is >= 17 then recv the io_error flag */
744 if (f != -1 && remote_version >= 17) {
745 io_error |= read_int(f);
746 }
747
17faa41c
AT
748 if (verbose > 2)
749 fprintf(FINFO,"recv_file_list done\n");
750
c627d613
AT
751 return flist;
752
753oom:
754 out_of_memory("recv_file_list");
755 return NULL; /* not reached */
756}
757
758
3ec4dd97 759int file_compare(struct file_struct **f1,struct file_struct **f2)
c627d613 760{
3ec4dd97
AT
761 if (!(*f1)->basename && !(*f2)->basename) return 0;
762 if (!(*f1)->basename) return -1;
763 if (!(*f2)->basename) return 1;
764 if ((*f1)->dirname == (*f2)->dirname)
765 return strcmp((*f1)->basename, (*f2)->basename);
766 return strcmp(f_name(*f1),f_name(*f2));
c627d613
AT
767}
768
769
770int flist_find(struct file_list *flist,struct file_struct *f)
771{
d966ee25
AT
772 int low=0,high=flist->count-1;
773
774 if (flist->count <= 0) return -1;
775
776 while (low != high) {
777 int mid = (low+high)/2;
3ec4dd97 778 int ret = file_compare(&flist->files[flist_up(flist, mid)],&f);
d966ee25
AT
779 if (ret == 0) return flist_up(flist, mid);
780 if (ret > 0) {
781 high=mid;
782 } else {
783 low=mid+1;
784 }
785 }
786
3ec4dd97 787 if (file_compare(&flist->files[flist_up(flist,low)],&f) == 0)
d966ee25
AT
788 return flist_up(flist,low);
789 return -1;
c627d613
AT
790}
791
792
3ec4dd97
AT
793/*
794 * free up one file
795 */
796static void free_file(struct file_struct *file)
c627d613 797{
3ec4dd97
AT
798 if (!file) return;
799 if (file->basename) free(file->basename);
800 if (file->link) free(file->link);
0b910560 801 if (file->sum) free(file->sum);
3ec4dd97 802 bzero((char *)file, sizeof(*file));
3ec4dd97 803}
c627d613 804
c627d613 805
3ec4dd97
AT
806/*
807 * free up all elements in a flist
808 */
809void flist_free(struct file_list *flist)
810{
811 int i;
812 for (i=1;i<flist->count;i++) {
813 free_file(flist->files[i]);
649d65ed 814 free(flist->files[i]);
3ec4dd97
AT
815 }
816 bzero((char *)flist->files, sizeof(flist->files[0])*flist->count);
817 free(flist->files);
818 bzero((char *)flist, sizeof(*flist));
819 free(flist);
c627d613
AT
820}
821
822
823/*
824 * This routine ensures we don't have any duplicate names in our file list.
825 * duplicate names can cause corruption because of the pipelining
826 */
827void clean_flist(struct file_list *flist)
828{
3ec4dd97 829 int i;
c627d613 830
3ec4dd97
AT
831 if (!flist || flist->count == 0)
832 return;
c627d613 833
3ec4dd97
AT
834 qsort(flist->files,flist->count,
835 sizeof(flist->files[0]),
836 (int (*)())file_compare);
837
838 for (i=1;i<flist->count;i++) {
839 if (flist->files[i]->basename &&
649d65ed 840 flist->files[i-1]->basename &&
3ec4dd97
AT
841 strcmp(f_name(flist->files[i]),
842 f_name(flist->files[i-1])) == 0) {
843 if (verbose > 1 && !am_server)
6ba9279f 844 fprintf(FINFO,"removing duplicate name %s from file list %d\n",
3ec4dd97
AT
845 f_name(flist->files[i-1]),i-1);
846 free_file(flist->files[i]);
3ec4dd97
AT
847 }
848 }
849}
850
851
852/*
853 * return the full filename of a flist entry
854 */
855char *f_name(struct file_struct *f)
856{
857 static char names[10][MAXPATHLEN];
858 static int n;
859 char *p = names[n];
860
649d65ed 861 if (!f || !f->basename) return NULL;
3ec4dd97
AT
862
863 n = (n+1)%10;
864
865 if (f->dirname) {
866 sprintf(p, "%s/%s", f->dirname, f->basename);
867 } else {
868 strcpy(p, f->basename);
869 }
870
871 return p;
c627d613
AT
872}
873