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