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