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