blob: 455bffbba1299f03aca7fc63e1337be2a6310626 [file] [log] [blame]
Matt Kraai91b28552001-04-23 18:53:07 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini cp implementation for busybox
4 *
Matt Kraai91b28552001-04-23 18:53:07 +00005 * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
Denis Vlasenko49622d72007-03-10 16:58:49 +00006 * SELinux support by Yuichi Nakamura <ynakam@hitachisoft.jp>
Matt Kraai91b28552001-04-23 18:53:07 +00007 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Matt Kraai91b28552001-04-23 18:53:07 +00009 */
Manuel Novoa III cad53642003-03-19 09:13:01 +000010/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
11 *
12 * Size reduction.
13 */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010014//config:config CP
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020015//config: bool "cp (9.7 kb)"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010016//config: default y
17//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020018//config: cp is used to copy files and directories.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010019//config:
20//config:config FEATURE_CP_LONG_OPTIONS
Denys Vlasenkof5604222017-01-10 14:58:54 +010021//config: bool "Enable long options"
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010022//config: default y
23//config: depends on CP && LONG_OPTS
24//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020025//config: Enable long options.
26//config: Also add support for --parents option.
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010027
28//applet:IF_CP(APPLET_NOEXEC(cp, cp, BB_DIR_BIN, BB_SUID_DROP, cp))
Denys Vlasenko88663e42018-01-14 14:41:52 +010029/* NOEXEC despite cases when it can be a "runner" (cp -r LARGE_DIR NEW_DIR) */
Denys Vlasenkoaf3f4202016-11-23 14:46:56 +010030
31//kbuild:lib-$(CONFIG_CP) += cp.o
32
33/* http://www.opengroup.org/onlinepubs/007904975/utilities/cp.html */
Manuel Novoa III cad53642003-03-19 09:13:01 +000034
Pere Orga34425382011-03-31 14:43:25 +020035//usage:#define cp_trivial_usage
Denys Vlasenkob3439d42012-03-05 10:09:18 +010036//usage: "[OPTIONS] SOURCE... DEST"
Pere Orga34425382011-03-31 14:43:25 +020037//usage:#define cp_full_usage "\n\n"
Denys Vlasenkob3439d42012-03-05 10:09:18 +010038//usage: "Copy SOURCE(s) to DEST\n"
Pere Orga34425382011-03-31 14:43:25 +020039//usage: "\n -a Same as -dpR"
40//usage: IF_SELINUX(
41//usage: "\n -c Preserve security context"
42//usage: )
43//usage: "\n -R,-r Recurse"
44//usage: "\n -d,-P Preserve symlinks (default if -R)"
45//usage: "\n -L Follow all symlinks"
46//usage: "\n -H Follow symlinks on command line"
47//usage: "\n -p Preserve file attributes if possible"
48//usage: "\n -f Overwrite"
49//usage: "\n -i Prompt before overwrite"
50//usage: "\n -l,-s Create (sym)links"
Aaro Koskinend15d7a02018-02-01 09:29:05 +010051//usage: "\n -T Treat DEST as a normal file"
Denys Vlasenko877dedb2016-05-27 00:46:38 +020052//usage: "\n -u Copy only newer files"
Pere Orga34425382011-03-31 14:43:25 +020053
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000054#include "libbb.h"
Manuel Novoa III cad53642003-03-19 09:13:01 +000055#include "libcoreutils/coreutils.h"
56
Denis Vlasenko99912ca2007-04-10 15:43:37 +000057/* This is a NOEXEC applet. Be very careful! */
58
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000059int cp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +000060int cp_main(int argc, char **argv)
Matt Kraai91b28552001-04-23 18:53:07 +000061{
Manuel Novoa III cad53642003-03-19 09:13:01 +000062 struct stat source_stat;
63 struct stat dest_stat;
64 const char *last;
65 const char *dest;
66 int s_flags;
67 int d_flags;
68 int flags;
Denys Vlasenko48f11612009-09-26 14:31:04 +020069 int status;
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000070 enum {
Denys Vlasenko877dedb2016-05-27 00:46:38 +020071 FILEUTILS_CP_OPTNUM = sizeof(FILEUTILS_CP_OPTSTR)-1,
Denys Vlasenko48f11612009-09-26 14:31:04 +020072#if ENABLE_FEATURE_CP_LONG_OPTIONS
Denys Vlasenko877dedb2016-05-27 00:46:38 +020073 /*OPT_rmdest = FILEUTILS_RMDEST = 1 << FILEUTILS_CP_OPTNUM */
74 OPT_parents = 1 << (FILEUTILS_CP_OPTNUM+1),
Denys Vlasenko48f11612009-09-26 14:31:04 +020075#endif
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000076 };
Matt Kraai91b28552001-04-23 18:53:07 +000077
Denys Vlasenko48f11612009-09-26 14:31:04 +020078#if ENABLE_FEATURE_CP_LONG_OPTIONS
Denys Vlasenko22542ec2017-08-08 21:55:02 +020079 flags = getopt32long(argv, "^"
80 FILEUTILS_CP_OPTSTR
81 "\0"
82 // Need at least two arguments
83 // Soft- and hardlinking doesn't mix
84 // -P and -d are the same (-P is POSIX, -d is GNU)
85 // -r and -R are the same
86 // -R (and therefore -r) turns on -d (coreutils does this)
87 // -a = -pdR
88 "-2:l--s:s--l:Pd:rRd:Rd:apdR",
Denys Vlasenko48f11612009-09-26 14:31:04 +020089 "archive\0" No_argument "a"
90 "force\0" No_argument "f"
91 "interactive\0" No_argument "i"
92 "link\0" No_argument "l"
93 "dereference\0" No_argument "L"
94 "no-dereference\0" No_argument "P"
95 "recursive\0" No_argument "R"
96 "symbolic-link\0" No_argument "s"
Aaro Koskinend15d7a02018-02-01 09:29:05 +010097 "no-target-directory\0" No_argument "T"
Denys Vlasenko48f11612009-09-26 14:31:04 +020098 "verbose\0" No_argument "v"
Denys Vlasenko877dedb2016-05-27 00:46:38 +020099 "update\0" No_argument "u"
100 "remove-destination\0" No_argument "\xff"
101 "parents\0" No_argument "\xfe"
Denys Vlasenko036585a2017-08-08 16:38:18 +0200102 );
103#else
Denys Vlasenko7f441402018-02-11 21:25:23 +0100104 flags = getopt32(argv, "^"
105 FILEUTILS_CP_OPTSTR
106 "\0"
107 "-2:l--s:s--l:Pd:rRd:Rd:apdR"
108 );
Denys Vlasenko036585a2017-08-08 16:38:18 +0200109#endif
Denis Vlasenko0b281032009-03-20 14:04:00 +0000110 /* Options of cp from GNU coreutils 6.10:
111 * -a, --archive
112 * -f, --force
113 * -i, --interactive
114 * -l, --link
115 * -L, --dereference
116 * -P, --no-dereference
117 * -R, -r, --recursive
118 * -s, --symbolic-link
119 * -v, --verbose
120 * -H follow command-line symbolic links in SOURCE
121 * -d same as --no-dereference --preserve=links
122 * -p same as --preserve=mode,ownership,timestamps
123 * -c same as --preserve=context
Denys Vlasenko877dedb2016-05-27 00:46:38 +0200124 * -u, --update
125 * copy only when the SOURCE file is newer than the destination
126 * file or when the destination file is missing
127 * --remove-destination
128 * remove each existing destination file before attempting to open
Denys Vlasenko48f11612009-09-26 14:31:04 +0200129 * --parents
130 * use full source file name under DIRECTORY
Aaro Koskinend15d7a02018-02-01 09:29:05 +0100131 * -T, --no-target-directory
132 * treat DEST as a normal file
Denis Vlasenko0b281032009-03-20 14:04:00 +0000133 * NOT SUPPORTED IN BBOX:
Denis Vlasenko0b281032009-03-20 14:04:00 +0000134 * --backup[=CONTROL]
135 * make a backup of each existing destination file
136 * -b like --backup but does not accept an argument
137 * --copy-contents
138 * copy contents of special files when recursive
139 * --preserve[=ATTR_LIST]
140 * preserve attributes (default: mode,ownership,timestamps),
141 * if possible additional attributes: security context,links,all
142 * --no-preserve=ATTR_LIST
Denis Vlasenko0b281032009-03-20 14:04:00 +0000143 * --sparse=WHEN
144 * control creation of sparse files
145 * --strip-trailing-slashes
146 * remove any trailing slashes from each SOURCE argument
147 * -S, --suffix=SUFFIX
148 * override the usual backup suffix
149 * -t, --target-directory=DIRECTORY
150 * copy all SOURCE arguments into DIRECTORY
Denis Vlasenko0b281032009-03-20 14:04:00 +0000151 * -x, --one-file-system
152 * stay on this file system
153 * -Z, --context=CONTEXT
154 * (SELinux) set SELinux security context of copy to CONTEXT
155 */
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000156 argc -= optind;
157 argv += optind;
Denys Vlasenkoa40f0622010-01-15 22:05:07 +0100158 /* Reverse this bit. If there is -d, bit is not set: */
159 flags ^= FILEUTILS_DEREFERENCE;
Denis Vlasenkoc0431ed2008-04-27 22:06:24 +0000160 /* coreutils 6.9 compat:
161 * by default, "cp" derefs symlinks (creates regular dest files),
162 * but "cp -R" does not. We switch off deref if -r or -R (see above).
163 * However, "cp -RL" must still deref symlinks: */
164 if (flags & FILEUTILS_DEREF_SOFTLINK) /* -L */
165 flags |= FILEUTILS_DEREFERENCE;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000166
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000167#if ENABLE_SELINUX
Denis Vlasenko49622d72007-03-10 16:58:49 +0000168 if (flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) {
169 selinux_or_die();
170 }
171#endif
172
Denys Vlasenko48f11612009-09-26 14:31:04 +0200173 status = EXIT_SUCCESS;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000174 last = argv[argc - 1];
Matt Kraai91b28552001-04-23 18:53:07 +0000175 /* If there are only two arguments and... */
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000176 if (argc == 2) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000177 s_flags = cp_mv_stat2(*argv, &source_stat,
Denys Vlasenko48f11612009-09-26 14:31:04 +0200178 (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000179 if (s_flags < 0)
180 return EXIT_FAILURE;
181 d_flags = cp_mv_stat(last, &dest_stat);
182 if (d_flags < 0)
183 return EXIT_FAILURE;
184
Aaro Koskinend15d7a02018-02-01 09:29:05 +0100185 if (flags & FILEUTILS_NO_TARGET_DIR) { /* -T */
186 if (!(s_flags & 2) && (d_flags & 2))
187 /* cp -T NOTDIR DIR */
188 bb_error_msg_and_die("'%s' is a directory", last);
189 }
190
Denys Vlasenko48f11612009-09-26 14:31:04 +0200191#if ENABLE_FEATURE_CP_LONG_OPTIONS
Denys Vlasenko877dedb2016-05-27 00:46:38 +0200192 //bb_error_msg("flags:%x FILEUTILS_RMDEST:%x OPT_parents:%x",
193 // flags, FILEUTILS_RMDEST, OPT_parents);
Denys Vlasenko48f11612009-09-26 14:31:04 +0200194 if (flags & OPT_parents) {
195 if (!(d_flags & 2)) {
196 bb_error_msg_and_die("with --parents, the destination must be a directory");
197 }
198 }
Denys Vlasenko877dedb2016-05-27 00:46:38 +0200199 if (flags & FILEUTILS_RMDEST) {
200 flags |= FILEUTILS_FORCE;
201 }
Denys Vlasenko48f11612009-09-26 14:31:04 +0200202#endif
203
204 /* ...if neither is a directory... */
205 if (!((s_flags | d_flags) & 2)
206 /* ...or: recursing, the 1st is a directory, and the 2nd doesn't exist... */
207 || ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags)
Aaro Koskinend15d7a02018-02-01 09:29:05 +0100208 || (flags & FILEUTILS_NO_TARGET_DIR)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000209 ) {
Denys Vlasenko48f11612009-09-26 14:31:04 +0200210 /* Do a simple copy */
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000211 dest = last;
212 goto DO_COPY; /* NB: argc==2 -> *++argv==last */
Matt Kraai91b28552001-04-23 18:53:07 +0000213 }
Aaro Koskinend15d7a02018-02-01 09:29:05 +0100214 } else if (flags & FILEUTILS_NO_TARGET_DIR) {
215 bb_error_msg_and_die("too many arguments");
Matt Kraai91b28552001-04-23 18:53:07 +0000216 }
217
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000218 while (1) {
Denys Vlasenko48f11612009-09-26 14:31:04 +0200219#if ENABLE_FEATURE_CP_LONG_OPTIONS
220 if (flags & OPT_parents) {
221 char *dest_dup;
222 char *dest_dir;
223 dest = concat_path_file(last, *argv);
224 dest_dup = xstrdup(dest);
225 dest_dir = dirname(dest_dup);
226 if (bb_make_directory(dest_dir, -1, FILEUTILS_RECUR)) {
227 return EXIT_FAILURE;
228 }
229 free(dest_dup);
230 goto DO_COPY;
231 }
232#endif
Denis Vlasenko818322b2007-09-24 18:27:04 +0000233 dest = concat_path_file(last, bb_get_last_path_component_strip(*argv));
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000234 DO_COPY:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000235 if (copy_file(*argv, dest, flags) < 0) {
Denys Vlasenko48f11612009-09-26 14:31:04 +0200236 status = EXIT_FAILURE;
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000237 }
Denys Vlasenko48f11612009-09-26 14:31:04 +0200238 if (*++argv == last) {
Denys Vlasenkod5fddcd2009-10-08 01:32:44 +0200239 /* possibly leaking dest... */
Denys Vlasenko48f11612009-09-26 14:31:04 +0200240 break;
241 }
Denys Vlasenkod5fddcd2009-10-08 01:32:44 +0200242 /* don't move up: dest may be == last and not malloced! */
243 free((void*)dest);
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000244 }
Matt Kraai91b28552001-04-23 18:53:07 +0000245
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000246 /* Exit. We are NOEXEC, not NOFORK. We do exit at the end of main() */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000247 return status;
Matt Kraai91b28552001-04-23 18:53:07 +0000248}