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