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 GPLv2 or later, see file LICENSE in this source tree.
11 /* http://www.opengroup.org/onlinepubs/007904975/utilities/cp.html */
13 /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
18 //usage:#define cp_trivial_usage
19 //usage: "[OPTIONS] SOURCE DEST"
20 //usage:#define cp_full_usage "\n\n"
21 //usage: "Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY\n"
23 //usage: "\n -a Same as -dpR"
25 //usage: "\n -c Preserve security context"
27 //usage: "\n -R,-r Recurse"
28 //usage: "\n -d,-P Preserve symlinks (default if -R)"
29 //usage: "\n -L Follow all symlinks"
30 //usage: "\n -H Follow symlinks on command line"
31 //usage: "\n -p Preserve file attributes if possible"
32 //usage: "\n -f Overwrite"
33 //usage: "\n -i Prompt before overwrite"
34 //usage: "\n -l,-s Create (sym)links"
37 #include "libcoreutils/coreutils.h"
39 /* This is a NOEXEC applet. Be very careful! */
41 int cp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
42 int cp_main(int argc, char **argv)
44 struct stat source_stat;
45 struct stat dest_stat;
53 OPT_a = 1 << (sizeof(FILEUTILS_CP_OPTSTR)-1),
54 OPT_r = 1 << (sizeof(FILEUTILS_CP_OPTSTR)),
55 OPT_P = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+1),
56 OPT_v = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+2),
57 #if ENABLE_FEATURE_CP_LONG_OPTIONS
58 OPT_parents = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+3),
62 // Need at least two arguments
63 // Soft- and hardlinking doesn't mix
64 // -P and -d are the same (-P is POSIX, -d is GNU)
65 // -r and -R are the same
66 // -R (and therefore -r) turns on -d (coreutils does this)
68 opt_complementary = "-2:l--s:s--l:Pd:rRd:Rd:apdR";
69 #if ENABLE_FEATURE_CP_LONG_OPTIONS
71 "archive\0" No_argument "a"
72 "force\0" No_argument "f"
73 "interactive\0" No_argument "i"
74 "link\0" No_argument "l"
75 "dereference\0" No_argument "L"
76 "no-dereference\0" No_argument "P"
77 "recursive\0" No_argument "R"
78 "symbolic-link\0" No_argument "s"
79 "verbose\0" No_argument "v"
80 "parents\0" No_argument "\xff"
83 // -v (--verbose) is ignored
84 flags = getopt32(argv, FILEUTILS_CP_OPTSTR "arPv");
85 /* Options of cp from GNU coreutils 6.10:
91 * -P, --no-dereference
95 * -H follow command-line symbolic links in SOURCE
96 * -d same as --no-dereference --preserve=links
97 * -p same as --preserve=mode,ownership,timestamps
98 * -c same as --preserve=context
100 * use full source file name under DIRECTORY
101 * NOT SUPPORTED IN BBOX:
103 * make a backup of each existing destination file
104 * -b like --backup but does not accept an argument
106 * copy contents of special files when recursive
107 * --preserve[=ATTR_LIST]
108 * preserve attributes (default: mode,ownership,timestamps),
109 * if possible additional attributes: security context,links,all
110 * --no-preserve=ATTR_LIST
111 * --remove-destination
112 * remove each existing destination file before attempting to open
114 * control creation of sparse files
115 * --strip-trailing-slashes
116 * remove any trailing slashes from each SOURCE argument
117 * -S, --suffix=SUFFIX
118 * override the usual backup suffix
119 * -t, --target-directory=DIRECTORY
120 * copy all SOURCE arguments into DIRECTORY
121 * -T, --no-target-directory
122 * treat DEST as a normal file
124 * copy only when the SOURCE file is newer than the destination
125 * file or when the destination file is missing
126 * -x, --one-file-system
127 * stay on this file system
128 * -Z, --context=CONTEXT
129 * (SELinux) set SELinux security context of copy to CONTEXT
133 /* Reverse this bit. If there is -d, bit is not set: */
134 flags ^= FILEUTILS_DEREFERENCE;
135 /* coreutils 6.9 compat:
136 * by default, "cp" derefs symlinks (creates regular dest files),
137 * but "cp -R" does not. We switch off deref if -r or -R (see above).
138 * However, "cp -RL" must still deref symlinks: */
139 if (flags & FILEUTILS_DEREF_SOFTLINK) /* -L */
140 flags |= FILEUTILS_DEREFERENCE;
143 if (flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) {
148 status = EXIT_SUCCESS;
149 last = argv[argc - 1];
150 /* If there are only two arguments and... */
152 s_flags = cp_mv_stat2(*argv, &source_stat,
153 (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
156 d_flags = cp_mv_stat(last, &dest_stat);
160 #if ENABLE_FEATURE_CP_LONG_OPTIONS
161 if (flags & OPT_parents) {
162 if (!(d_flags & 2)) {
163 bb_error_msg_and_die("with --parents, the destination must be a directory");
168 /* ...if neither is a directory... */
169 if (!((s_flags | d_flags) & 2)
170 /* ...or: recursing, the 1st is a directory, and the 2nd doesn't exist... */
171 || ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags)
173 /* Do a simple copy */
175 goto DO_COPY; /* NB: argc==2 -> *++argv==last */
180 #if ENABLE_FEATURE_CP_LONG_OPTIONS
181 if (flags & OPT_parents) {
184 dest = concat_path_file(last, *argv);
185 dest_dup = xstrdup(dest);
186 dest_dir = dirname(dest_dup);
187 if (bb_make_directory(dest_dir, -1, FILEUTILS_RECUR)) {
194 dest = concat_path_file(last, bb_get_last_path_component_strip(*argv));
196 if (copy_file(*argv, dest, flags) < 0) {
197 status = EXIT_FAILURE;
199 if (*++argv == last) {
200 /* possibly leaking dest... */
203 /* don't move up: dest may be == last and not malloced! */
207 /* Exit. We are NOEXEC, not NOFORK. We do exit at the end of main() */