1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1988, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * David Hitz of Auspex Systems Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 /* 36 * Cp copies source files to target files. 37 * 38 * The global PATH_T structure "to" always contains the path to the 39 * current target file. Since fts(3) does not change directories, 40 * this path can be either absolute or dot-relative. 41 * 42 * The basic algorithm is to initialize "to" and use fts(3) to traverse 43 * the file hierarchy rooted in the argument list. A trivial case is the 44 * case of 'cp file1 file2'. The more interesting case is the case of 45 * 'cp file1 file2 ... fileN dir' where the hierarchy is traversed and the 46 * path (relative to the root of the traversal) is appended to dir (stored 47 * in "to") to form the final target path. 48 */ 49 50 #include <sys/types.h> 51 #include <sys/stat.h> 52 53 #include <assert.h> 54 #include <err.h> 55 #include <errno.h> 56 #include <fts.h> 57 #include <limits.h> 58 #include <signal.h> 59 #include <stdio.h> 60 #include <stdlib.h> 61 #include <string.h> 62 #include <unistd.h> 63 64 #include "extern.h" 65 66 #define STRIP_TRAILING_SLASH(p) { \ 67 while ((p).p_end > (p).p_path + 1 && (p).p_end[-1] == '/') \ 68 *--(p).p_end = 0; \ 69 } 70 71 static char emptystring[] = ""; 72 73 PATH_T to = { to.p_path, emptystring, "" }; 74 75 int Nflag, fflag, iflag, lflag, nflag, pflag, sflag, vflag; 76 static int Hflag, Lflag, Pflag, Rflag, rflag; 77 volatile sig_atomic_t info; 78 79 enum op { FILE_TO_FILE, FILE_TO_DIR, DIR_TO_DNE }; 80 81 static int copy(char *[], enum op, int, struct stat *); 82 static void siginfo(int __unused); 83 84 int 85 main(int argc, char *argv[]) 86 { 87 struct stat to_stat, tmp_stat; 88 enum op type; 89 int ch, fts_options, r, have_trailing_slash; 90 char *target; 91 92 fts_options = FTS_NOCHDIR | FTS_PHYSICAL; 93 while ((ch = getopt(argc, argv, "HLPRafilNnprsvx")) != -1) 94 switch (ch) { 95 case 'H': 96 Hflag = 1; 97 Lflag = Pflag = 0; 98 break; 99 case 'L': 100 Lflag = 1; 101 Hflag = Pflag = 0; 102 break; 103 case 'P': 104 Pflag = 1; 105 Hflag = Lflag = 0; 106 break; 107 case 'R': 108 Rflag = 1; 109 break; 110 case 'a': 111 pflag = 1; 112 Rflag = 1; 113 Pflag = 1; 114 Hflag = Lflag = 0; 115 break; 116 case 'f': 117 fflag = 1; 118 iflag = nflag = 0; 119 break; 120 case 'i': 121 iflag = 1; 122 fflag = nflag = 0; 123 break; 124 case 'l': 125 lflag = 1; 126 break; 127 case 'N': 128 Nflag = 1; 129 break; 130 case 'n': 131 nflag = 1; 132 fflag = iflag = 0; 133 break; 134 case 'p': 135 pflag = 1; 136 break; 137 case 'r': 138 rflag = Lflag = 1; 139 Hflag = Pflag = 0; 140 break; 141 case 's': 142 sflag = 1; 143 break; 144 case 'v': 145 vflag = 1; 146 break; 147 case 'x': 148 fts_options |= FTS_XDEV; 149 break; 150 default: 151 usage(); 152 break; 153 } 154 argc -= optind; 155 argv += optind; 156 157 if (argc < 2) 158 usage(); 159 160 if (Rflag && rflag) 161 errx(1, "the -R and -r options may not be specified together"); 162 if (lflag && sflag) 163 errx(1, "the -l and -s options may not be specified together"); 164 if (rflag) 165 Rflag = 1; 166 if (Rflag) { 167 if (Hflag) 168 fts_options |= FTS_COMFOLLOW; 169 if (Lflag) { 170 fts_options &= ~FTS_PHYSICAL; 171 fts_options |= FTS_LOGICAL; 172 } 173 } else if (!Pflag) { 174 fts_options &= ~FTS_PHYSICAL; 175 fts_options |= FTS_LOGICAL | FTS_COMFOLLOW; 176 } 177 (void)signal(SIGINFO, siginfo); 178 179 /* Save the target base in "to". */ 180 target = argv[--argc]; 181 if (strlcpy(to.p_path, target, sizeof(to.p_path)) >= sizeof(to.p_path)) 182 errx(1, "%s: name too long", target); 183 to.p_end = to.p_path + strlen(to.p_path); 184 if (to.p_path == to.p_end) { 185 *to.p_end++ = '.'; 186 *to.p_end = 0; 187 } 188 have_trailing_slash = (to.p_end[-1] == '/'); 189 if (have_trailing_slash) 190 STRIP_TRAILING_SLASH(to); 191 to.target_end = to.p_end; 192 193 /* Set end of argument list for fts(3). */ 194 argv[argc] = NULL; 195 196 /* 197 * Cp has two distinct cases: 198 * 199 * cp [-R] source target 200 * cp [-R] source1 ... sourceN directory 201 * 202 * In both cases, source can be either a file or a directory. 203 * 204 * In (1), the target becomes a copy of the source. That is, if the 205 * source is a file, the target will be a file, and likewise for 206 * directories. 207 * 208 * In (2), the real target is not directory, but "directory/source". 209 */ 210 r = stat(to.p_path, &to_stat); 211 if (r == -1 && errno != ENOENT) 212 err(1, "%s", to.p_path); 213 if (r == -1 || !S_ISDIR(to_stat.st_mode)) { 214 /* 215 * Case (1). Target is not a directory. 216 */ 217 if (argc > 1) 218 errx(1, "%s is not a directory", to.p_path); 219 220 /* 221 * Need to detect the case: 222 * cp -R dir foo 223 * Where dir is a directory and foo does not exist, where 224 * we want pathname concatenations turned on but not for 225 * the initial mkdir(). 226 */ 227 if (r == -1) { 228 if (Rflag && (Lflag || Hflag)) 229 stat(*argv, &tmp_stat); 230 else 231 lstat(*argv, &tmp_stat); 232 233 if (S_ISDIR(tmp_stat.st_mode) && Rflag) 234 type = DIR_TO_DNE; 235 else 236 type = FILE_TO_FILE; 237 } else 238 type = FILE_TO_FILE; 239 240 if (have_trailing_slash && type == FILE_TO_FILE) { 241 if (r == -1) { 242 errx(1, "directory %s does not exist", 243 to.p_path); 244 } else 245 errx(1, "%s is not a directory", to.p_path); 246 } 247 } else 248 /* 249 * Case (2). Target is a directory. 250 */ 251 type = FILE_TO_DIR; 252 253 /* 254 * For DIR_TO_DNE, we could provide copy() with the to_stat we've 255 * already allocated on the stack here that isn't being used for 256 * anything. Not doing so, though, simplifies later logic a little bit 257 * as we need to skip checking root_stat on the first iteration and 258 * ensure that we set it with the first mkdir(). 259 */ 260 exit (copy(argv, type, fts_options, (type == DIR_TO_DNE ? NULL : 261 &to_stat))); 262 } 263 264 /* Does the right thing based on -R + -H/-L/-P */ 265 static int 266 copy_stat(const char *path, struct stat *sb) 267 { 268 269 /* 270 * For -R -H/-P, we need to lstat() instead; copy() cares about the link 271 * itself rather than the target if we're not following links during the 272 * traversal. 273 */ 274 if (!Rflag || Lflag) 275 return (stat(path, sb)); 276 return (lstat(path, sb)); 277 } 278 279 280 static int 281 copy(char *argv[], enum op type, int fts_options, struct stat *root_stat) 282 { 283 char rootname[NAME_MAX]; 284 struct stat created_root_stat, to_stat; 285 FTS *ftsp; 286 FTSENT *curr; 287 int base = 0, dne, badcp, rval; 288 size_t nlen; 289 char *p, *recurse_path, *target_mid; 290 mode_t mask, mode; 291 292 /* 293 * Keep an inverted copy of the umask, for use in correcting 294 * permissions on created directories when not using -p. 295 */ 296 mask = ~umask(0777); 297 umask(~mask); 298 299 recurse_path = NULL; 300 if ((ftsp = fts_open(argv, fts_options, NULL)) == NULL) 301 err(1, "fts_open"); 302 for (badcp = rval = 0; (curr = fts_read(ftsp)) != NULL; badcp = 0) { 303 switch (curr->fts_info) { 304 case FTS_NS: 305 case FTS_DNR: 306 case FTS_ERR: 307 warnx("%s: %s", 308 curr->fts_path, strerror(curr->fts_errno)); 309 badcp = rval = 1; 310 continue; 311 case FTS_DC: /* Warn, continue. */ 312 warnx("%s: directory causes a cycle", curr->fts_path); 313 badcp = rval = 1; 314 continue; 315 default: 316 ; 317 } 318 319 /* 320 * Stash the root basename off for detecting recursion later. 321 * 322 * This will be essential if the root is a symlink and we're 323 * rolling with -L or -H. The later bits will need this bit in 324 * particular. 325 */ 326 if (curr->fts_level == FTS_ROOTLEVEL) { 327 strlcpy(rootname, curr->fts_name, sizeof(rootname)); 328 } 329 330 /* 331 * If we are in case (2) or (3) above, we need to append the 332 * source name to the target name. 333 */ 334 if (type != FILE_TO_FILE) { 335 /* 336 * Need to remember the roots of traversals to create 337 * correct pathnames. If there's a directory being 338 * copied to a non-existent directory, e.g. 339 * cp -R a/dir noexist 340 * the resulting path name should be noexist/foo, not 341 * noexist/dir/foo (where foo is a file in dir), which 342 * is the case where the target exists. 343 * 344 * Also, check for "..". This is for correct path 345 * concatenation for paths ending in "..", e.g. 346 * cp -R .. /tmp 347 * Paths ending in ".." are changed to ".". This is 348 * tricky, but seems the easiest way to fix the problem. 349 * 350 * XXX 351 * Since the first level MUST be FTS_ROOTLEVEL, base 352 * is always initialized. 353 */ 354 if (curr->fts_level == FTS_ROOTLEVEL) { 355 if (type != DIR_TO_DNE) { 356 p = strrchr(curr->fts_path, '/'); 357 base = (p == NULL) ? 0 : 358 (int)(p - curr->fts_path + 1); 359 360 if (!strcmp(&curr->fts_path[base], 361 "..")) 362 base += 1; 363 } else 364 base = curr->fts_pathlen; 365 } 366 367 p = &curr->fts_path[base]; 368 nlen = curr->fts_pathlen - base; 369 target_mid = to.target_end; 370 if (*p != '/' && target_mid[-1] != '/') 371 *target_mid++ = '/'; 372 *target_mid = 0; 373 if (target_mid - to.p_path + nlen >= PATH_MAX) { 374 warnx("%s%s: name too long (not copied)", 375 to.p_path, p); 376 badcp = rval = 1; 377 continue; 378 } 379 (void)strncat(target_mid, p, nlen); 380 to.p_end = target_mid + nlen; 381 *to.p_end = 0; 382 STRIP_TRAILING_SLASH(to); 383 384 /* 385 * We're on the verge of recursing on ourselves. Either 386 * we need to stop right here (we knowingly just created 387 * it), or we will in an immediate descendant. Record 388 * the path of the immediate descendant to make our 389 * lives a little less complicated looking. 390 */ 391 if (curr->fts_info == FTS_D && root_stat != NULL && 392 root_stat->st_dev == curr->fts_statp->st_dev && 393 root_stat->st_ino == curr->fts_statp->st_ino) { 394 assert(recurse_path == NULL); 395 396 if (root_stat == &created_root_stat) { 397 /* 398 * This directory didn't exist when we 399 * started, we created it as part of 400 * traversal. Stop right here before we 401 * do something silly. 402 */ 403 fts_set(ftsp, curr, FTS_SKIP); 404 continue; 405 } 406 407 408 if (asprintf(&recurse_path, "%s/%s", to.p_path, 409 rootname) == -1) 410 err(1, "asprintf"); 411 } 412 413 if (recurse_path != NULL && 414 strcmp(to.p_path, recurse_path) == 0) { 415 fts_set(ftsp, curr, FTS_SKIP); 416 continue; 417 } 418 } 419 420 if (curr->fts_info == FTS_DP) { 421 /* 422 * We are nearly finished with this directory. If we 423 * didn't actually copy it, or otherwise don't need to 424 * change its attributes, then we are done. 425 */ 426 if (!curr->fts_number) 427 continue; 428 /* 429 * If -p is in effect, set all the attributes. 430 * Otherwise, set the correct permissions, limited 431 * by the umask. Optimise by avoiding a chmod() 432 * if possible (which is usually the case if we 433 * made the directory). Note that mkdir() does not 434 * honour setuid, setgid and sticky bits, but we 435 * normally want to preserve them on directories. 436 */ 437 if (pflag) { 438 if (setfile(curr->fts_statp, -1)) 439 rval = 1; 440 if (preserve_dir_acls(curr->fts_statp, 441 curr->fts_accpath, to.p_path) != 0) 442 rval = 1; 443 } else { 444 mode = curr->fts_statp->st_mode; 445 if ((mode & (S_ISUID | S_ISGID | S_ISTXT)) || 446 ((mode | S_IRWXU) & mask) != (mode & mask)) 447 if (chmod(to.p_path, mode & mask) != 448 0) { 449 warn("chmod: %s", to.p_path); 450 rval = 1; 451 } 452 } 453 continue; 454 } 455 456 /* Not an error but need to remember it happened. */ 457 if (copy_stat(to.p_path, &to_stat) == -1) 458 dne = 1; 459 else { 460 if (to_stat.st_dev == curr->fts_statp->st_dev && 461 to_stat.st_ino == curr->fts_statp->st_ino) { 462 warnx("%s and %s are identical (not copied).", 463 to.p_path, curr->fts_path); 464 badcp = rval = 1; 465 if (S_ISDIR(curr->fts_statp->st_mode)) 466 (void)fts_set(ftsp, curr, FTS_SKIP); 467 continue; 468 } 469 if (!S_ISDIR(curr->fts_statp->st_mode) && 470 S_ISDIR(to_stat.st_mode)) { 471 warnx("cannot overwrite directory %s with " 472 "non-directory %s", 473 to.p_path, curr->fts_path); 474 badcp = rval = 1; 475 continue; 476 } 477 dne = 0; 478 } 479 480 switch (curr->fts_statp->st_mode & S_IFMT) { 481 case S_IFLNK: 482 /* Catch special case of a non-dangling symlink. */ 483 if ((fts_options & FTS_LOGICAL) || 484 ((fts_options & FTS_COMFOLLOW) && 485 curr->fts_level == 0)) { 486 if (copy_file(curr, dne)) 487 badcp = rval = 1; 488 } else { 489 if (copy_link(curr, !dne)) 490 badcp = rval = 1; 491 } 492 break; 493 case S_IFDIR: 494 if (!Rflag) { 495 warnx("%s is a directory (not copied).", 496 curr->fts_path); 497 (void)fts_set(ftsp, curr, FTS_SKIP); 498 badcp = rval = 1; 499 break; 500 } 501 /* 502 * If the directory doesn't exist, create the new 503 * one with the from file mode plus owner RWX bits, 504 * modified by the umask. Trade-off between being 505 * able to write the directory (if from directory is 506 * 555) and not causing a permissions race. If the 507 * umask blocks owner writes, we fail. 508 */ 509 if (dne) { 510 if (mkdir(to.p_path, 511 curr->fts_statp->st_mode | S_IRWXU) < 0) 512 err(1, "%s", to.p_path); 513 /* 514 * First DNE with a NULL root_stat is the root 515 * path, so set root_stat. We can't really 516 * tell in all cases if the target path is 517 * within the src path, so we just stat() the 518 * first directory we created and use that. 519 */ 520 if (root_stat == NULL && 521 stat(to.p_path, &created_root_stat) == -1) { 522 err(1, "stat"); 523 } else if (root_stat == NULL) { 524 root_stat = &created_root_stat; 525 } 526 } else if (!S_ISDIR(to_stat.st_mode)) { 527 errno = ENOTDIR; 528 err(1, "%s", to.p_path); 529 } 530 /* 531 * Arrange to correct directory attributes later 532 * (in the post-order phase) if this is a new 533 * directory, or if the -p flag is in effect. 534 */ 535 curr->fts_number = pflag || dne; 536 break; 537 case S_IFBLK: 538 case S_IFCHR: 539 if (Rflag && !sflag) { 540 if (copy_special(curr->fts_statp, !dne)) 541 badcp = rval = 1; 542 } else { 543 if (copy_file(curr, dne)) 544 badcp = rval = 1; 545 } 546 break; 547 case S_IFSOCK: 548 warnx("%s is a socket (not copied).", 549 curr->fts_path); 550 break; 551 case S_IFIFO: 552 if (Rflag && !sflag) { 553 if (copy_fifo(curr->fts_statp, !dne)) 554 badcp = rval = 1; 555 } else { 556 if (copy_file(curr, dne)) 557 badcp = rval = 1; 558 } 559 break; 560 default: 561 if (copy_file(curr, dne)) 562 badcp = rval = 1; 563 break; 564 } 565 if (vflag && !badcp) 566 (void)printf("%s -> %s\n", curr->fts_path, to.p_path); 567 } 568 if (errno) 569 err(1, "fts_read"); 570 fts_close(ftsp); 571 free(recurse_path); 572 return (rval); 573 } 574 575 static void 576 siginfo(int sig __unused) 577 { 578 579 info = 1; 580 } 581