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