00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "rsync.h"
00033
00034 extern struct stats stats;
00035
00036 extern int verbose;
00037 extern int do_progress;
00038 extern int am_server;
00039 extern int always_checksum;
00040
00041 extern int cvs_exclude;
00042
00043 extern int recurse;
00044
00045 extern int one_file_system;
00046 extern int make_backups;
00047 extern int preserve_links;
00048 extern int preserve_hard_links;
00049 extern int preserve_perms;
00050 extern int preserve_devices;
00051 extern int preserve_uid;
00052 extern int preserve_gid;
00053 extern int preserve_times;
00054 extern int relative_paths;
00055 extern int copy_links;
00056 extern int copy_unsafe_links;
00057 extern int remote_version;
00058 extern int io_error;
00059 extern int sanitize_paths;
00060
00061 extern int read_batch;
00062 extern int write_batch;
00063
00064 static char topsrcname[MAXPATHLEN];
00065
00066 static struct exclude_struct **local_exclude_list;
00067
00068 static struct file_struct null_file;
00069
00070 static void clean_flist(struct file_list *flist, int strip_root);
00071
00072
00073 static int show_filelist_p(void)
00074 {
00075 return verbose && recurse && !am_server;
00076 }
00077
00078 static void start_filelist_progress(char *kind)
00079 {
00080 rprintf(FINFO, "%s ... ", kind);
00081 if ((verbose > 1) || do_progress)
00082 rprintf(FINFO, "\n");
00083 rflush(FINFO);
00084 }
00085
00086
00087 static void emit_filelist_progress(const struct file_list *flist)
00088 {
00089 rprintf(FINFO, " %d files...\r", flist->count);
00090 }
00091
00092
00093 static void maybe_emit_filelist_progress(const struct file_list *flist)
00094 {
00095 if (do_progress && show_filelist_p() && ((flist->count % 100) == 0))
00096 emit_filelist_progress(flist);
00097 }
00098
00099
00100 static void finish_filelist_progress(const struct file_list *flist)
00101 {
00102 if (do_progress) {
00103
00104 rprintf(FINFO, "%d file%sto consider\n",
00105 flist->count, flist->count == 1 ? " " : "s ");
00106 } else {
00107 rprintf(FINFO, "done\n");
00108 }
00109 }
00110
00111 void show_flist_stats(void)
00112 {
00113
00114 }
00115
00116
00117 static struct string_area *string_area_new(int size)
00118 {
00119 struct string_area *a;
00120
00121 if (size <= 0)
00122 size = ARENA_SIZE;
00123 a = malloc(sizeof(*a));
00124 if (!a)
00125 out_of_memory("string_area_new");
00126 a->current = a->base = malloc(size);
00127 if (!a->current)
00128 out_of_memory("string_area_new buffer");
00129 a->end = a->base + size;
00130 a->next = NULL;
00131
00132 return a;
00133 }
00134
00135 static void string_area_free(struct string_area *a)
00136 {
00137 struct string_area *next;
00138
00139 for (; a; a = next) {
00140 next = a->next;
00141 free(a->base);
00142 }
00143 }
00144
00145 static char *string_area_malloc(struct string_area **ap, int size)
00146 {
00147 char *p;
00148 struct string_area *a;
00149
00150
00151 a = *ap;
00152 if (a->current + size >= a->end) {
00153
00154 a = string_area_new(size > ARENA_SIZE ? size : ARENA_SIZE);
00155 a->next = *ap;
00156 *ap = a;
00157 }
00158
00159
00160 p = a->current;
00161 a->current += size;
00162 return p;
00163 }
00164
00165 static char *string_area_strdup(struct string_area **ap, const char *src)
00166 {
00167 char *dest = string_area_malloc(ap, strlen(src) + 1);
00168 return strcpy(dest, src);
00169 }
00170
00171 static void list_file_entry(struct file_struct *f)
00172 {
00173 char perms[11];
00174
00175 if (!f->basename)
00176
00177 return;
00178
00179 permstring(perms, f->mode);
00180
00181 if (preserve_links && S_ISLNK(f->mode)) {
00182 rprintf(FINFO, "%s %11.0f %s %s -> %s\n",
00183 perms,
00184 (double) f->length, timestring(f->modtime),
00185 f_name(f), f->link);
00186 } else {
00187 rprintf(FINFO, "%s %11.0f %s %s\n",
00188 perms,
00189 (double) f->length, timestring(f->modtime),
00190 f_name(f));
00191 }
00192 }
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209 int readlink_stat(const char *path, STRUCT_STAT * buffer, char *linkbuf)
00210 {
00211 #if SUPPORT_LINKS
00212 if (copy_links) {
00213 return do_stat(path, buffer);
00214 }
00215 if (do_lstat(path, buffer) == -1) {
00216 return -1;
00217 }
00218 if (S_ISLNK(buffer->st_mode)) {
00219 int l;
00220 l = readlink((char *) path, linkbuf, MAXPATHLEN - 1);
00221 if (l == -1)
00222 return -1;
00223 linkbuf[l] = 0;
00224 if (copy_unsafe_links && (topsrcname[0] != '\0') &&
00225 unsafe_symlink(linkbuf, topsrcname)) {
00226 return do_stat(path, buffer);
00227 }
00228 }
00229 return 0;
00230 #else
00231 return do_stat(path, buffer);
00232 #endif
00233 }
00234
00235 int link_stat(const char *path, STRUCT_STAT * buffer)
00236 {
00237 #if SUPPORT_LINKS
00238 if (copy_links) {
00239 return do_stat(path, buffer);
00240 } else {
00241 return do_lstat(path, buffer);
00242 }
00243 #else
00244 return do_stat(path, buffer);
00245 #endif
00246 }
00247
00248
00249
00250
00251
00252 static int check_exclude_file(int f, char *fname, STRUCT_STAT * st)
00253 {
00254 extern int delete_excluded;
00255
00256
00257 if ((f == -1) && delete_excluded) {
00258 return 0;
00259 }
00260 if (check_exclude(fname, local_exclude_list, st)) {
00261 return 1;
00262 }
00263 return 0;
00264 }
00265
00266
00267 static dev_t filesystem_dev;
00268
00269 static void set_filesystem(char *fname)
00270 {
00271 STRUCT_STAT st;
00272 if (link_stat(fname, &st) != 0)
00273 return;
00274 filesystem_dev = st.st_dev;
00275 }
00276
00277
00278 static int to_wire_mode(mode_t mode)
00279 {
00280 if (S_ISLNK(mode) && (_S_IFLNK != 0120000)) {
00281 return (mode & ~(_S_IFMT)) | 0120000;
00282 }
00283 return (int) mode;
00284 }
00285
00286 static mode_t from_wire_mode(int mode)
00287 {
00288 if ((mode & (_S_IFMT)) == 0120000 && (_S_IFLNK != 0120000)) {
00289 return (mode & ~(_S_IFMT)) | _S_IFLNK;
00290 }
00291 return (mode_t) mode;
00292 }
00293
00294
00295 static void send_directory(int f, struct file_list *flist, char *dir);
00296
00297 static char *flist_dir;
00298
00299
00300
00301
00302
00303
00304 static void flist_expand(struct file_list *flist)
00305 {
00306 if (flist->count >= flist->malloced) {
00307 size_t new_bytes;
00308 void *new_ptr;
00309
00310 if (flist->malloced < 1000)
00311 flist->malloced += 1000;
00312 else
00313 flist->malloced *= 2;
00314
00315 new_bytes = sizeof(flist->files[0]) * flist->malloced;
00316
00317 if (flist->files)
00318 new_ptr = realloc(flist->files, new_bytes);
00319 else
00320 new_ptr = malloc(new_bytes);
00321
00322 if (verbose >= 2) {
00323 rprintf(FINFO, "expand file_list to %.0f bytes, did%s move\n",
00324 (double) new_bytes,
00325 (new_ptr == flist->files) ? " not" : "");
00326 }
00327
00328 flist->files = (struct file_struct **) new_ptr;
00329
00330 if (!flist->files)
00331 out_of_memory("flist_expand");
00332 }
00333 }
00334
00335
00336 static void send_file_entry(struct file_struct *file, int f,
00337 unsigned base_flags)
00338 {
00339 unsigned char flags;
00340 static time_t last_time;
00341 static mode_t last_mode;
00342 static DEV64_T last_rdev;
00343 static uid_t last_uid;
00344 static gid_t last_gid;
00345 static char lastname[MAXPATHLEN];
00346 char *fname;
00347 int l1, l2;
00348
00349 if (f == -1)
00350 return;
00351
00352 if (!file) {
00353 write_byte(f, 0);
00354 return;
00355 }
00356
00357 io_write_phase = "send_file_entry";
00358
00359 fname = f_name(file);
00360
00361 flags = base_flags;
00362
00363 if (file->mode == last_mode)
00364 flags |= SAME_MODE;
00365 if (file->rdev == last_rdev)
00366 flags |= SAME_RDEV;
00367 if (file->uid == last_uid)
00368 flags |= SAME_UID;
00369 if (file->gid == last_gid)
00370 flags |= SAME_GID;
00371 if (file->modtime == last_time)
00372 flags |= SAME_TIME;
00373
00374 for (l1 = 0;
00375 lastname[l1] && (fname[l1] == lastname[l1]) && (l1 < 255);
00376 l1++);
00377 l2 = strlen(fname) - l1;
00378
00379 if (l1 > 0)
00380 flags |= SAME_NAME;
00381 if (l2 > 255)
00382 flags |= LONG_NAME;
00383
00384
00385
00386 if (flags == 0 && !S_ISDIR(file->mode))
00387 flags |= FLAG_DELETE;
00388 if (flags == 0)
00389 flags |= LONG_NAME;
00390
00391 write_byte(f, flags);
00392 if (flags & SAME_NAME)
00393 write_byte(f, l1);
00394 if (flags & LONG_NAME)
00395 write_int(f, l2);
00396 else
00397 write_byte(f, l2);
00398 write_buf(f, fname + l1, l2);
00399
00400 write_longint(f, file->length);
00401 if (!(flags & SAME_TIME))
00402 write_int(f, (int) file->modtime);
00403 if (!(flags & SAME_MODE))
00404 write_int(f, to_wire_mode(file->mode));
00405 if (preserve_uid && !(flags & SAME_UID)) {
00406 add_uid(file->uid);
00407 write_int(f, (int) file->uid);
00408 }
00409 if (preserve_gid && !(flags & SAME_GID)) {
00410 add_gid(file->gid);
00411 write_int(f, (int) file->gid);
00412 }
00413 if (preserve_devices && IS_DEVICE(file->mode)
00414 && !(flags & SAME_RDEV))
00415 write_int(f, (int) file->rdev);
00416
00417 #if SUPPORT_LINKS
00418 if (preserve_links && S_ISLNK(file->mode)) {
00419 write_int(f, strlen(file->link));
00420 write_buf(f, file->link, strlen(file->link));
00421 }
00422 #endif
00423
00424 #if SUPPORT_HARD_LINKS
00425 if (preserve_hard_links && S_ISREG(file->mode)) {
00426 if (remote_version < 26) {
00427
00428 write_int(f, (int) file->dev);
00429 write_int(f, (int) file->inode);
00430 } else {
00431
00432 write_longint(f, file->dev);
00433 write_longint(f, file->inode);
00434 }
00435 }
00436 #endif
00437
00438 if (always_checksum) {
00439 if (remote_version < 21) {
00440 write_buf(f, file->sum, 2);
00441 } else {
00442 write_buf(f, file->sum, MD4_SUM_LENGTH);
00443 }
00444 }
00445
00446 last_mode = file->mode;
00447 last_rdev = file->rdev;
00448 last_uid = file->uid;
00449 last_gid = file->gid;
00450 last_time = file->modtime;
00451
00452 strlcpy(lastname, fname, MAXPATHLEN);
00453 lastname[MAXPATHLEN - 1] = 0;
00454
00455 io_write_phase = "unknown";
00456 }
00457
00458
00459
00460 static void receive_file_entry(struct file_struct **fptr,
00461 unsigned flags, int f)
00462 {
00463 static time_t last_time;
00464 static mode_t last_mode;
00465 static DEV64_T last_rdev;
00466 static uid_t last_uid;
00467 static gid_t last_gid;
00468 static char lastname[MAXPATHLEN];
00469 char thisname[MAXPATHLEN];
00470 unsigned int l1 = 0, l2 = 0;
00471 char *p;
00472 struct file_struct *file;
00473
00474 if (flags & SAME_NAME)
00475 l1 = read_byte(f);
00476
00477 if (flags & LONG_NAME)
00478 l2 = read_int(f);
00479 else
00480 l2 = read_byte(f);
00481
00482 file = (struct file_struct *) malloc(sizeof(*file));
00483 if (!file)
00484 out_of_memory("receive_file_entry");
00485 memset((char *) file, 0, sizeof(*file));
00486 (*fptr) = file;
00487
00488 if (l2 >= MAXPATHLEN - l1) {
00489 rprintf(FERROR,
00490 "overflow: flags=0x%x l1=%d l2=%d lastname=%s\n",
00491 flags, l1, l2, lastname);
00492 overflow("receive_file_entry");
00493 }
00494
00495 strlcpy(thisname, lastname, l1 + 1);
00496 read_sbuf(f, &thisname[l1], l2);
00497 thisname[l1 + l2] = 0;
00498
00499 strlcpy(lastname, thisname, MAXPATHLEN);
00500 lastname[MAXPATHLEN - 1] = 0;
00501
00502 clean_fname(thisname);
00503
00504 if (sanitize_paths) {
00505 sanitize_path(thisname, NULL);
00506 }
00507
00508 if ((p = strrchr(thisname, '/'))) {
00509 static char *lastdir;
00510 *p = 0;
00511 if (lastdir && strcmp(thisname, lastdir) == 0) {
00512 file->dirname = lastdir;
00513 } else {
00514 file->dirname = strdup(thisname);
00515 lastdir = file->dirname;
00516 }
00517 file->basename = strdup(p + 1);
00518 } else {
00519 file->dirname = NULL;
00520 file->basename = strdup(thisname);
00521 }
00522
00523 if (!file->basename)
00524 out_of_memory("receive_file_entry 1");
00525
00526
00527 file->flags = flags;
00528 file->length = read_longint(f);
00529 file->modtime =
00530 (flags & SAME_TIME) ? last_time : (time_t) read_int(f);
00531 file->mode =
00532 (flags & SAME_MODE) ? last_mode : from_wire_mode(read_int(f));
00533 if (preserve_uid)
00534 file->uid =
00535 (flags & SAME_UID) ? last_uid : (uid_t) read_int(f);
00536 if (preserve_gid)
00537 file->gid =
00538 (flags & SAME_GID) ? last_gid : (gid_t) read_int(f);
00539 if (preserve_devices && IS_DEVICE(file->mode))
00540 file->rdev =
00541 (flags & SAME_RDEV) ? last_rdev : (dev_t) read_int(f);
00542
00543 if (preserve_links && S_ISLNK(file->mode)) {
00544 int l = read_int(f);
00545 if (l < 0) {
00546 rprintf(FERROR, "overflow: l=%d\n", l);
00547 overflow("receive_file_entry");
00548 }
00549 file->link = (char *) malloc(l + 1);
00550 if (!file->link)
00551 out_of_memory("receive_file_entry 2");
00552 read_sbuf(f, file->link, l);
00553 if (sanitize_paths) {
00554 sanitize_path(file->link, file->dirname);
00555 }
00556 }
00557 #if SUPPORT_HARD_LINKS
00558 if (preserve_hard_links && S_ISREG(file->mode)) {
00559 if (remote_version < 26) {
00560 file->dev = read_int(f);
00561 file->inode = read_int(f);
00562 } else {
00563 file->dev = read_longint(f);
00564 file->inode = read_longint(f);
00565 }
00566 }
00567 #endif
00568
00569 if (always_checksum) {
00570 file->sum = (char *) malloc(MD4_SUM_LENGTH);
00571 if (!file->sum)
00572 out_of_memory("md4 sum");
00573 if (remote_version < 21) {
00574 read_buf(f, file->sum, 2);
00575 } else {
00576 read_buf(f, file->sum, MD4_SUM_LENGTH);
00577 }
00578 }
00579
00580 last_mode = file->mode;
00581 last_rdev = file->rdev;
00582 last_uid = file->uid;
00583 last_gid = file->gid;
00584 last_time = file->modtime;
00585
00586 if (!preserve_perms) {
00587 extern int orig_umask;
00588
00589
00590 file->mode &= ~orig_umask;
00591 }
00592 }
00593
00594
00595
00596
00597
00598 static int skip_filesystem(char *fname, STRUCT_STAT * st)
00599 {
00600 STRUCT_STAT st2;
00601 char *p = strrchr(fname, '/');
00602
00603
00604 if (!S_ISDIR(st->st_mode))
00605 return 1;
00606
00607
00608 if (!p)
00609 return 0;
00610
00611 *p = 0;
00612 if (link_stat(fname, &st2)) {
00613 *p = '/';
00614 return 0;
00615 }
00616 *p = '/';
00617
00618 return (st2.st_dev != filesystem_dev);
00619 }
00620
00621 #define STRDUP(ap, p) (ap ? string_area_strdup(ap, p) : strdup(p))
00622
00623 #define MALLOC(ap, i) (ap ? (void*) string_area_malloc(ap, i) : malloc(i))
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640 struct file_struct *make_file(int f, char *fname, struct string_area **ap,
00641 int noexcludes)
00642 {
00643 struct file_struct *file;
00644 STRUCT_STAT st;
00645 char sum[SUM_LENGTH];
00646 char *p;
00647 char cleaned_name[MAXPATHLEN];
00648 char linkbuf[MAXPATHLEN];
00649 extern int module_id;
00650
00651 strlcpy(cleaned_name, fname, MAXPATHLEN);
00652 cleaned_name[MAXPATHLEN - 1] = 0;
00653 clean_fname(cleaned_name);
00654 if (sanitize_paths) {
00655 sanitize_path(cleaned_name, NULL);
00656 }
00657 fname = cleaned_name;
00658
00659 memset(sum, 0, SUM_LENGTH);
00660
00661 if (readlink_stat(fname, &st, linkbuf) != 0) {
00662 int save_errno = errno;
00663 if ((errno == ENOENT) && copy_links && !noexcludes) {
00664
00665 memset((char *) &st, 0, sizeof(st));
00666 if (check_exclude_file(f, fname, &st)) {
00667
00668 return NULL;
00669 }
00670 }
00671 io_error = 1;
00672 rprintf(FERROR, "readlink %s: %s\n",
00673 fname, strerror(save_errno));
00674 return NULL;
00675 }
00676
00677
00678 if (noexcludes)
00679 goto skip_excludes;
00680
00681 if (S_ISDIR(st.st_mode) && !recurse) {
00682 rprintf(FINFO, "skipping directory %s\n", fname);
00683 return NULL;
00684 }
00685
00686 if (one_file_system && st.st_dev != filesystem_dev) {
00687 if (skip_filesystem(fname, &st))
00688 return NULL;
00689 }
00690
00691 if (check_exclude_file(f, fname, &st))
00692 return NULL;
00693
00694
00695 if (lp_ignore_nonreadable(module_id) && access(fname, R_OK) != 0)
00696 return NULL;
00697
00698 skip_excludes:
00699
00700 if (verbose > 2)
00701 rprintf(FINFO, "make_file(%d,%s)\n", f, fname);
00702
00703 file = (struct file_struct *) malloc(sizeof(*file));
00704 if (!file)
00705 out_of_memory("make_file");
00706 memset((char *) file, 0, sizeof(*file));
00707
00708 if ((p = strrchr(fname, '/'))) {
00709 static char *lastdir;
00710 *p = 0;
00711 if (lastdir && strcmp(fname, lastdir) == 0) {
00712 file->dirname = lastdir;
00713 } else {
00714 file->dirname = strdup(fname);
00715 lastdir = file->dirname;
00716 }
00717 file->basename = STRDUP(ap, p + 1);
00718 *p = '/';
00719 } else {
00720 file->dirname = NULL;
00721 file->basename = STRDUP(ap, fname);
00722 }
00723
00724 file->modtime = st.st_mtime;
00725 file->length = st.st_size;
00726 file->mode = st.st_mode;
00727 file->uid = st.st_uid;
00728 file->gid = st.st_gid;
00729 file->dev = st.st_dev;
00730 file->inode = st.st_ino;
00731 #ifdef HAVE_STRUCT_STAT_ST_RDEV
00732 file->rdev = st.st_rdev;
00733 #endif
00734
00735 #if SUPPORT_LINKS
00736 if (S_ISLNK(st.st_mode)) {
00737 file->link = STRDUP(ap, linkbuf);
00738 }
00739 #endif
00740
00741 if (always_checksum) {
00742 file->sum = (char *) MALLOC(ap, MD4_SUM_LENGTH);
00743 if (!file->sum)
00744 out_of_memory("md4 sum");
00745
00746
00747
00748 if (S_ISREG(st.st_mode)) {
00749 file_checksum(fname, file->sum, st.st_size);
00750 } else {
00751 memset(file->sum, 0, MD4_SUM_LENGTH);
00752 }
00753 }
00754
00755 if (flist_dir) {
00756 static char *lastdir;
00757 if (lastdir && strcmp(lastdir, flist_dir) == 0) {
00758 file->basedir = lastdir;
00759 } else {
00760 file->basedir = strdup(flist_dir);
00761 lastdir = file->basedir;
00762 }
00763 } else {
00764 file->basedir = NULL;
00765 }
00766
00767 if (!S_ISDIR(st.st_mode))
00768 stats.total_size += st.st_size;
00769
00770 return file;
00771 }
00772
00773
00774
00775 void send_file_name(int f, struct file_list *flist, char *fname,
00776 int recursive, unsigned base_flags)
00777 {
00778 struct file_struct *file;
00779
00780 file = make_file(f, fname, &flist->string_area, 0);
00781
00782 if (!file)
00783 return;
00784
00785 maybe_emit_filelist_progress(flist);
00786
00787 flist_expand(flist);
00788
00789 if (write_batch)
00790 file->flags = FLAG_DELETE;
00791
00792 if (strcmp(file->basename, "")) {
00793 flist->files[flist->count++] = file;
00794 send_file_entry(file, f, base_flags);
00795 }
00796
00797 if (S_ISDIR(file->mode) && recursive) {
00798 struct exclude_struct **last_exclude_list =
00799 local_exclude_list;
00800 send_directory(f, flist, f_name(file));
00801 local_exclude_list = last_exclude_list;
00802 return;
00803 }
00804 }
00805
00806
00807
00808 static void send_directory(int f, struct file_list *flist, char *dir)
00809 {
00810 DIR *d;
00811 struct dirent *di;
00812 char fname[MAXPATHLEN];
00813 int l;
00814 char *p;
00815
00816 d = opendir(dir);
00817 if (!d) {
00818 io_error = 1;
00819 rprintf(FERROR, "opendir(%s): %s\n", dir, strerror(errno));
00820 return;
00821 }
00822
00823 strlcpy(fname, dir, MAXPATHLEN);
00824 l = strlen(fname);
00825 if (fname[l - 1] != '/') {
00826 if (l == MAXPATHLEN - 1) {
00827 io_error = 1;
00828 rprintf(FERROR,
00829 "skipping long-named directory %s\n",
00830 fname);
00831 closedir(d);
00832 return;
00833 }
00834 strlcat(fname, "/", MAXPATHLEN);
00835 l++;
00836 }
00837 p = fname + strlen(fname);
00838
00839 local_exclude_list = NULL;
00840
00841 if (cvs_exclude) {
00842 if (strlen(fname) + strlen(".cvsignore") <= MAXPATHLEN - 1) {
00843 strcpy(p, ".cvsignore");
00844 local_exclude_list =
00845 make_exclude_list(fname, NULL, 0, 0);
00846 } else {
00847 io_error = 1;
00848 rprintf(FINFO,
00849 "cannot cvs-exclude in long-named directory %s\n",
00850 fname);
00851 }
00852 }
00853
00854 for (di = readdir(d); di; di = readdir(d)) {
00855 char *dname = d_name(di);
00856 if (strcmp(dname, ".") == 0 || strcmp(dname, "..") == 0)
00857 continue;
00858 strlcpy(p, dname, MAXPATHLEN - l);
00859 send_file_name(f, flist, fname, recurse, 0);
00860 }
00861
00862 if (local_exclude_list) {
00863 add_exclude_list("!", &local_exclude_list, 0);
00864 }
00865
00866 closedir(d);
00867 }
00868
00869
00870
00871
00872
00873
00874
00875 struct file_list *send_file_list(int f, int argc, char *argv[])
00876 {
00877 int i, l;
00878 STRUCT_STAT st;
00879 char *p, *dir, *olddir;
00880 char lastpath[MAXPATHLEN] = "";
00881 struct file_list *flist;
00882 int64 start_write;
00883
00884 if (show_filelist_p() && f != -1)
00885 start_filelist_progress("building file list");
00886
00887 start_write = stats.total_written;
00888
00889 flist = flist_new();
00890
00891 if (f != -1) {
00892 io_start_buffering(f);
00893 }
00894
00895 for (i = 0; i < argc; i++) {
00896 char *fname = topsrcname;
00897
00898 strlcpy(fname, argv[i], MAXPATHLEN);
00899
00900 l = strlen(fname);
00901 if (l != 1 && fname[l - 1] == '/') {
00902 if ((l == 2) && (fname[0] == '.')) {
00903
00904
00905
00906
00907
00908 fname[1] = 0;
00909 } else {
00910 strlcat(fname, ".", MAXPATHLEN);
00911 }
00912 }
00913
00914 if (link_stat(fname, &st) != 0) {
00915 if (f != -1) {
00916 io_error = 1;
00917 rprintf(FERROR, "link_stat %s : %s\n",
00918 fname, strerror(errno));
00919 }
00920 continue;
00921 }
00922
00923 if (S_ISDIR(st.st_mode) && !recurse) {
00924 rprintf(FINFO, "skipping directory %s\n", fname);
00925 continue;
00926 }
00927
00928 dir = NULL;
00929 olddir = NULL;
00930
00931 if (!relative_paths) {
00932 p = strrchr(fname, '/');
00933 if (p) {
00934 *p = 0;
00935 if (p == fname)
00936 dir = "/";
00937 else
00938 dir = fname;
00939 fname = p + 1;
00940 }
00941 } else if (f != -1 && (p = strrchr(fname, '/'))) {
00942
00943
00944 *p = 0;
00945 if (strcmp(lastpath, fname)) {
00946 strlcpy(lastpath, fname, sizeof(lastpath));
00947 *p = '/';
00948 for (p = fname + 1; (p = strchr(p, '/'));
00949 p++) {
00950 int copy_links_saved = copy_links;
00951 int recurse_saved = recurse;
00952 *p = 0;
00953 copy_links = copy_unsafe_links;
00954
00955
00956
00957
00958 recurse = 1;
00959 send_file_name(f, flist, fname, 0,
00960 0);
00961 copy_links = copy_links_saved;
00962 recurse = recurse_saved;
00963 *p = '/';
00964 }
00965 } else {
00966 *p = '/';
00967 }
00968 }
00969
00970 if (!*fname)
00971 fname = ".";
00972
00973 if (dir && *dir) {
00974 olddir = push_dir(dir, 1);
00975
00976 if (!olddir) {
00977 io_error = 1;
00978 rprintf(FERROR, "push_dir %s : %s\n",
00979 dir, strerror(errno));
00980 continue;
00981 }
00982
00983 flist_dir = dir;
00984 }
00985
00986 if (one_file_system)
00987 set_filesystem(fname);
00988
00989 send_file_name(f, flist, fname, recurse, FLAG_DELETE);
00990
00991 if (olddir != NULL) {
00992 flist_dir = NULL;
00993 if (pop_dir(olddir) != 0) {
00994 rprintf(FERROR, "pop_dir %s : %s\n",
00995 dir, strerror(errno));
00996 exit_cleanup(RERR_FILESELECT);
00997 }
00998 }
00999 }
01000
01001 topsrcname[0] = '\0';
01002
01003 if (f != -1) {
01004 send_file_entry(NULL, f, 0);
01005 }
01006
01007 if (show_filelist_p() && f != -1) {
01008 finish_filelist_progress(flist);
01009 }
01010
01011 clean_flist(flist, 0);
01012
01013
01014
01015 if (f != -1 && remote_version >= 15) {
01016 send_uid_list(f);
01017 }
01018
01019
01020 if (f != -1 && remote_version >= 17) {
01021 extern int module_id;
01022 write_int(f, lp_ignore_errors(module_id) ? 0 : io_error);
01023 }
01024
01025 if (f != -1) {
01026 io_end_buffering();
01027 stats.flist_size = stats.total_written - start_write;
01028 stats.num_files = flist->count;
01029 if (write_batch)
01030 write_batch_flist_info(flist->count, flist->files);
01031 }
01032
01033 if (verbose > 2)
01034 rprintf(FINFO, "send_file_list done\n");
01035
01036 return flist;
01037 }
01038
01039
01040 struct file_list *recv_file_list(int f)
01041 {
01042 struct file_list *flist;
01043 unsigned char flags;
01044 int64 start_read;
01045 extern int list_only;
01046
01047 if (show_filelist_p())
01048 start_filelist_progress("receiving file list");
01049
01050 start_read = stats.total_read;
01051
01052 flist = (struct file_list *) malloc(sizeof(flist[0]));
01053 if (!flist)
01054 goto oom;
01055
01056 flist->count = 0;
01057 flist->malloced = 1000;
01058 flist->files =
01059 (struct file_struct **) malloc(sizeof(flist->files[0]) *
01060 flist->malloced);
01061 if (!flist->files)
01062 goto oom;
01063
01064
01065 for (flags = read_byte(f); flags; flags = read_byte(f)) {
01066 int i = flist->count;
01067
01068 flist_expand(flist);
01069
01070 receive_file_entry(&flist->files[i], flags, f);
01071
01072 if (S_ISREG(flist->files[i]->mode))
01073 stats.total_size += flist->files[i]->length;
01074
01075 flist->count++;
01076
01077 maybe_emit_filelist_progress(flist);
01078
01079 if (verbose > 2)
01080 rprintf(FINFO, "recv_file_name(%s)\n",
01081 f_name(flist->files[i]));
01082 }
01083
01084
01085 if (verbose > 2)
01086 rprintf(FINFO, "received %d names\n", flist->count);
01087
01088 clean_flist(flist, relative_paths);
01089
01090 if (show_filelist_p()) {
01091 finish_filelist_progress(flist);
01092 }
01093
01094
01095 if (f != -1 && remote_version >= 15) {
01096 recv_uid_list(f, flist);
01097 }
01098
01099
01100 if (f != -1 && remote_version >= 17 && !read_batch) {
01101 extern int module_id;
01102 extern int ignore_errors;
01103 if (lp_ignore_errors(module_id) || ignore_errors) {
01104 read_int(f);
01105 } else {
01106 io_error |= read_int(f);
01107 }
01108 }
01109
01110 if (list_only) {
01111 int i;
01112 for (i = 0; i < flist->count; i++) {
01113 list_file_entry(flist->files[i]);
01114 }
01115 }
01116
01117
01118 if (verbose > 2)
01119 rprintf(FINFO, "recv_file_list done\n");
01120
01121 stats.flist_size = stats.total_read - start_read;
01122 stats.num_files = flist->count;
01123
01124 return flist;
01125
01126 oom:
01127 out_of_memory("recv_file_list");
01128 return NULL;
01129 }
01130
01131
01132
01133
01134
01135
01136 int file_compare(struct file_struct **f1, struct file_struct **f2)
01137 {
01138 if (!(*f1)->basename && !(*f2)->basename)
01139 return 0;
01140 if (!(*f1)->basename)
01141 return -1;
01142 if (!(*f2)->basename)
01143 return 1;
01144 if ((*f1)->dirname == (*f2)->dirname)
01145 return u_strcmp((*f1)->basename, (*f2)->basename);
01146 return u_strcmp(f_name(*f1), f_name(*f2));
01147 }
01148
01149
01150 int flist_find(struct file_list *flist, struct file_struct *f)
01151 {
01152 int low = 0, high = flist->count - 1;
01153
01154 if (flist->count <= 0)
01155 return -1;
01156
01157 while (low != high) {
01158 int mid = (low + high) / 2;
01159 int ret =
01160 file_compare(&flist->files[flist_up(flist, mid)], &f);
01161 if (ret == 0)
01162 return flist_up(flist, mid);
01163 if (ret > 0) {
01164 high = mid;
01165 } else {
01166 low = mid + 1;
01167 }
01168 }
01169
01170 if (file_compare(&flist->files[flist_up(flist, low)], &f) == 0)
01171 return flist_up(flist, low);
01172 return -1;
01173 }
01174
01175
01176
01177
01178
01179 void free_file(struct file_struct *file)
01180 {
01181 if (!file)
01182 return;
01183 if (file->basename)
01184 free(file->basename);
01185 if (file->link)
01186 free(file->link);
01187 if (file->sum)
01188 free(file->sum);
01189 *file = null_file;
01190 }
01191
01192
01193
01194
01195
01196 struct file_list *flist_new()
01197 {
01198 struct file_list *flist;
01199
01200 flist = (struct file_list *) malloc(sizeof(flist[0]));
01201 if (!flist)
01202 out_of_memory("send_file_list");
01203
01204 flist->count = 0;
01205 flist->malloced = 0;
01206 flist->files = NULL;
01207
01208 #if ARENA_SIZE > 0
01209 flist->string_area = string_area_new(0);
01210 #else
01211 flist->string_area = NULL;
01212 #endif
01213 return flist;
01214 }
01215
01216
01217
01218
01219 void flist_free(struct file_list *flist)
01220 {
01221 int i;
01222 for (i = 1; i < flist->count; i++) {
01223 if (!flist->string_area)
01224 free_file(flist->files[i]);
01225 free(flist->files[i]);
01226 }
01227
01228
01229
01230
01231 memset((char *) flist->files, 0,
01232 sizeof(flist->files[0]) * flist->count);
01233 free(flist->files);
01234 if (flist->string_area)
01235 string_area_free(flist->string_area);
01236 memset((char *) flist, 0, sizeof(*flist));
01237 free(flist);
01238 }
01239
01240
01241
01242
01243
01244
01245 static void clean_flist(struct file_list *flist, int strip_root)
01246 {
01247 int i;
01248
01249 if (!flist || flist->count == 0)
01250 return;
01251
01252 qsort(flist->files, flist->count,
01253 sizeof(flist->files[0]), (int (*)()) file_compare);
01254
01255 for (i = 1; i < flist->count; i++) {
01256 if (flist->files[i]->basename &&
01257 flist->files[i - 1]->basename &&
01258 strcmp(f_name(flist->files[i]),
01259 f_name(flist->files[i - 1])) == 0) {
01260 if (verbose > 1 && !am_server)
01261 rprintf(FINFO,
01262 "removing duplicate name %s from file list %d\n",
01263 f_name(flist->files[i - 1]),
01264 i - 1);
01265
01266
01267
01268 if (flist->string_area)
01269 flist->files[i][0] = null_file;
01270 else
01271 free_file(flist->files[i]);
01272 }
01273 }
01274
01275
01276
01277
01278
01279 if (strip_root) {
01280
01281
01282
01283 for (i = 0; i < flist->count; i++) {
01284 if (flist->files[i]->dirname &&
01285 flist->files[i]->dirname[0] == '/') {
01286 memmove(&flist->files[i]->dirname[0],
01287 &flist->files[i]->dirname[1],
01288 strlen(flist->files[i]->dirname));
01289 }
01290
01291 if (flist->files[i]->dirname &&
01292 !flist->files[i]->dirname[0]) {
01293 flist->files[i]->dirname = NULL;
01294 }
01295 }
01296 }
01297
01298
01299 if (verbose <= 3)
01300 return;
01301
01302 for (i = 0; i < flist->count; i++) {
01303 rprintf(FINFO, "[%d] i=%d %s %s mode=0%o len=%.0f\n",
01304 (int) getpid(), i,
01305 NS(flist->files[i]->dirname),
01306 NS(flist->files[i]->basename),
01307 (int) flist->files[i]->mode,
01308 (double) flist->files[i]->length);
01309 }
01310 }
01311
01312
01313
01314
01315
01316
01317
01318
01319
01320 char *f_name(struct file_struct *f)
01321 {
01322 static char names[10][MAXPATHLEN];
01323 static int n;
01324 char *p = names[n];
01325
01326 if (!f || !f->basename)
01327 return NULL;
01328
01329 n = (n + 1) % 10;
01330
01331 if (f->dirname) {
01332 int off;
01333
01334 off = strlcpy(p, f->dirname, MAXPATHLEN);
01335 off += strlcpy(p + off, "/", MAXPATHLEN - off);
01336 off += strlcpy(p + off, f->basename, MAXPATHLEN - off);
01337 } else {
01338 strlcpy(p, f->basename, MAXPATHLEN);
01339 }
01340
01341 return p;
01342 }