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