1 /* vi: set sw=4 ts=4: */
3 * Mini cp implementation for busybox
5 * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
6 * SELinux support by Yuichi Nakamura <ynakam@hitachisoft.jp>
8 * Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
11 /* http://www.opengroup.org/onlinepubs/007904975/utilities/cp.html */
13 /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
19 #include "libcoreutils/coreutils.h"
21 /* This is a NOEXEC applet. Be very careful! */
23 int cp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
24 int cp_main(int argc, char **argv)
26 struct stat source_stat;
27 struct stat dest_stat;
35 OPT_a = 1 << (sizeof(FILEUTILS_CP_OPTSTR)-1),
36 OPT_r = 1 << (sizeof(FILEUTILS_CP_OPTSTR)),
37 OPT_P = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+1),
38 OPT_H = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+2),
39 OPT_v = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+3),
40 #if ENABLE_FEATURE_CP_LONG_OPTIONS
41 OPT_parents = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+4),
45 // Need at least two arguments
46 // Soft- and hardlinking doesn't mix
47 // -P and -d are the same (-P is POSIX, -d is GNU)
48 // -r and -R are the same
49 // -R (and therefore -r) turns on -d (coreutils does this)
51 opt_complementary = "-2:l--s:s--l:Pd:rRd:Rd:apdR:HL";
52 #if ENABLE_FEATURE_CP_LONG_OPTIONS
54 "archive\0" No_argument "a"
55 "force\0" No_argument "f"
56 "interactive\0" No_argument "i"
57 "link\0" No_argument "l"
58 "dereference\0" No_argument "L"
59 "no-dereference\0" No_argument "P"
60 "recursive\0" No_argument "R"
61 "symbolic-link\0" No_argument "s"
62 "verbose\0" No_argument "v"
63 "parents\0" No_argument "\xff"
66 // -v (--verbose) is ignored
67 flags = getopt32(argv, FILEUTILS_CP_OPTSTR "arPHv");
68 /* Options of cp from GNU coreutils 6.10:
74 * -P, --no-dereference
78 * -H follow command-line symbolic links in SOURCE
79 * -d same as --no-dereference --preserve=links
80 * -p same as --preserve=mode,ownership,timestamps
81 * -c same as --preserve=context
83 * use full source file name under DIRECTORY
84 * NOT SUPPORTED IN BBOX:
86 * make a backup of each existing destination file
87 * -b like --backup but does not accept an argument
89 * copy contents of special files when recursive
90 * --preserve[=ATTR_LIST]
91 * preserve attributes (default: mode,ownership,timestamps),
92 * if possible additional attributes: security context,links,all
93 * --no-preserve=ATTR_LIST
94 * --remove-destination
95 * remove each existing destination file before attempting to open
97 * control creation of sparse files
98 * --strip-trailing-slashes
99 * remove any trailing slashes from each SOURCE argument
100 * -S, --suffix=SUFFIX
101 * override the usual backup suffix
102 * -t, --target-directory=DIRECTORY
103 * copy all SOURCE arguments into DIRECTORY
104 * -T, --no-target-directory
105 * treat DEST as a normal file
107 * copy only when the SOURCE file is newer than the destination
108 * file or when the destination file is missing
109 * -x, --one-file-system
110 * stay on this file system
111 * -Z, --context=CONTEXT
112 * (SELinux) set SELinux security context of copy to CONTEXT
116 flags ^= FILEUTILS_DEREFERENCE; /* the sense of this flag was reversed */
117 /* coreutils 6.9 compat:
118 * by default, "cp" derefs symlinks (creates regular dest files),
119 * but "cp -R" does not. We switch off deref if -r or -R (see above).
120 * However, "cp -RL" must still deref symlinks: */
121 if (flags & FILEUTILS_DEREF_SOFTLINK) /* -L */
122 flags |= FILEUTILS_DEREFERENCE;
123 /* The behavior of -H is *almost* like -L, but not quite, so let's
124 * just ignore it too for fun. TODO.
125 if (flags & OPT_H) ... // deref command-line params only
129 if (flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) {
134 status = EXIT_SUCCESS;
135 last = argv[argc - 1];
136 /* If there are only two arguments and... */
138 s_flags = cp_mv_stat2(*argv, &source_stat,
139 (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
142 d_flags = cp_mv_stat(last, &dest_stat);
146 #if ENABLE_FEATURE_CP_LONG_OPTIONS
147 if (flags & OPT_parents) {
148 if (!(d_flags & 2)) {
149 bb_error_msg_and_die("with --parents, the destination must be a directory");
154 /* ...if neither is a directory... */
155 if (!((s_flags | d_flags) & 2)
156 /* ...or: recursing, the 1st is a directory, and the 2nd doesn't exist... */
157 || ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags)
159 /* Do a simple copy */
161 goto DO_COPY; /* NB: argc==2 -> *++argv==last */
166 #if ENABLE_FEATURE_CP_LONG_OPTIONS
167 if (flags & OPT_parents) {
170 dest = concat_path_file(last, *argv);
171 dest_dup = xstrdup(dest);
172 dest_dir = dirname(dest_dup);
173 if (bb_make_directory(dest_dir, -1, FILEUTILS_RECUR)) {
180 dest = concat_path_file(last, bb_get_last_path_component_strip(*argv));
182 if (copy_file(*argv, dest, flags) < 0) {
183 status = EXIT_FAILURE;
185 if (*++argv == last) {
186 /* possibly leaking dest... */
189 /* don't move up: dest may be == last and not malloced! */
193 /* Exit. We are NOEXEC, not NOFORK. We do exit at the end of main() */