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