blob: 337054d7bda6016d51a514c346c126bf49014f27 [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 */
10
Manuel Novoa III cad53642003-03-19 09:13:01 +000011/* http://www.opengroup.org/onlinepubs/007904975/utilities/cp.html */
12
13/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
14 *
15 * Size reduction.
16 */
17
Pere Orga34425382011-03-31 14:43:25 +020018//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"
22//usage: "\nOptions:"
23//usage: "\n -a Same as -dpR"
24//usage: IF_SELINUX(
25//usage: "\n -c Preserve security context"
26//usage: )
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"
35
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000036#include "libbb.h"
Manuel Novoa III cad53642003-03-19 09:13:01 +000037#include "libcoreutils/coreutils.h"
38
Denis Vlasenko99912ca2007-04-10 15:43:37 +000039/* This is a NOEXEC applet. Be very careful! */
40
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000041int cp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +000042int cp_main(int argc, char **argv)
Matt Kraai91b28552001-04-23 18:53:07 +000043{
Manuel Novoa III cad53642003-03-19 09:13:01 +000044 struct stat source_stat;
45 struct stat dest_stat;
46 const char *last;
47 const char *dest;
48 int s_flags;
49 int d_flags;
50 int flags;
Denys Vlasenko48f11612009-09-26 14:31:04 +020051 int status;
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000052 enum {
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),
Denys Vlasenkoa40f0622010-01-15 22:05:07 +010056 OPT_v = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+2),
Denys Vlasenko48f11612009-09-26 14:31:04 +020057#if ENABLE_FEATURE_CP_LONG_OPTIONS
Denys Vlasenkoa40f0622010-01-15 22:05:07 +010058 OPT_parents = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+3),
Denys Vlasenko48f11612009-09-26 14:31:04 +020059#endif
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000060 };
Matt Kraai91b28552001-04-23 18:53:07 +000061
Denis Vlasenko6666ac42007-08-24 21:46:24 +000062 // Need at least two arguments
Denis Vlasenkoc0431ed2008-04-27 22:06:24 +000063 // Soft- and hardlinking doesn't mix
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000064 // -P and -d are the same (-P is POSIX, -d is GNU)
65 // -r and -R are the same
Denis Vlasenko80f647c2008-04-11 10:54:37 +000066 // -R (and therefore -r) turns on -d (coreutils does this)
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000067 // -a = -pdR
Denys Vlasenkoa40f0622010-01-15 22:05:07 +010068 opt_complementary = "-2:l--s:s--l:Pd:rRd:Rd:apdR";
Denys Vlasenko48f11612009-09-26 14:31:04 +020069#if ENABLE_FEATURE_CP_LONG_OPTIONS
70 applet_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"
81 ;
82#endif
Denis Vlasenko0b281032009-03-20 14:04:00 +000083 // -v (--verbose) is ignored
Denys Vlasenkoa40f0622010-01-15 22:05:07 +010084 flags = getopt32(argv, FILEUTILS_CP_OPTSTR "arPv");
Denis Vlasenko0b281032009-03-20 14:04:00 +000085 /* Options of cp from GNU coreutils 6.10:
86 * -a, --archive
87 * -f, --force
88 * -i, --interactive
89 * -l, --link
90 * -L, --dereference
91 * -P, --no-dereference
92 * -R, -r, --recursive
93 * -s, --symbolic-link
94 * -v, --verbose
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
Denys Vlasenko48f11612009-09-26 14:31:04 +020099 * --parents
100 * use full source file name under DIRECTORY
Denis Vlasenko0b281032009-03-20 14:04:00 +0000101 * NOT SUPPORTED IN BBOX:
Denis Vlasenko0b281032009-03-20 14:04:00 +0000102 * --backup[=CONTROL]
103 * make a backup of each existing destination file
104 * -b like --backup but does not accept an argument
105 * --copy-contents
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
Denis Vlasenko0b281032009-03-20 14:04:00 +0000111 * --remove-destination
112 * remove each existing destination file before attempting to open
113 * --sparse=WHEN
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
123 * -u, --update
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
130 */
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000131 argc -= optind;
132 argv += optind;
Denys Vlasenkoa40f0622010-01-15 22:05:07 +0100133 /* Reverse this bit. If there is -d, bit is not set: */
134 flags ^= FILEUTILS_DEREFERENCE;
Denis Vlasenkoc0431ed2008-04-27 22:06:24 +0000135 /* 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;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000141
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000142#if ENABLE_SELINUX
Denis Vlasenko49622d72007-03-10 16:58:49 +0000143 if (flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) {
144 selinux_or_die();
145 }
146#endif
147
Denys Vlasenko48f11612009-09-26 14:31:04 +0200148 status = EXIT_SUCCESS;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000149 last = argv[argc - 1];
Matt Kraai91b28552001-04-23 18:53:07 +0000150 /* If there are only two arguments and... */
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000151 if (argc == 2) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000152 s_flags = cp_mv_stat2(*argv, &source_stat,
Denys Vlasenko48f11612009-09-26 14:31:04 +0200153 (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000154 if (s_flags < 0)
155 return EXIT_FAILURE;
156 d_flags = cp_mv_stat(last, &dest_stat);
157 if (d_flags < 0)
158 return EXIT_FAILURE;
159
Denys Vlasenko48f11612009-09-26 14:31:04 +0200160#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");
164 }
165 }
166#endif
167
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)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000172 ) {
Denys Vlasenko48f11612009-09-26 14:31:04 +0200173 /* Do a simple copy */
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000174 dest = last;
175 goto DO_COPY; /* NB: argc==2 -> *++argv==last */
Matt Kraai91b28552001-04-23 18:53:07 +0000176 }
177 }
178
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000179 while (1) {
Denys Vlasenko48f11612009-09-26 14:31:04 +0200180#if ENABLE_FEATURE_CP_LONG_OPTIONS
181 if (flags & OPT_parents) {
182 char *dest_dup;
183 char *dest_dir;
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)) {
188 return EXIT_FAILURE;
189 }
190 free(dest_dup);
191 goto DO_COPY;
192 }
193#endif
Denis Vlasenko818322b2007-09-24 18:27:04 +0000194 dest = concat_path_file(last, bb_get_last_path_component_strip(*argv));
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000195 DO_COPY:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000196 if (copy_file(*argv, dest, flags) < 0) {
Denys Vlasenko48f11612009-09-26 14:31:04 +0200197 status = EXIT_FAILURE;
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000198 }
Denys Vlasenko48f11612009-09-26 14:31:04 +0200199 if (*++argv == last) {
Denys Vlasenkod5fddcd2009-10-08 01:32:44 +0200200 /* possibly leaking dest... */
Denys Vlasenko48f11612009-09-26 14:31:04 +0200201 break;
202 }
Denys Vlasenkod5fddcd2009-10-08 01:32:44 +0200203 /* don't move up: dest may be == last and not malloced! */
204 free((void*)dest);
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000205 }
Matt Kraai91b28552001-04-23 18:53:07 +0000206
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000207 /* Exit. We are NOEXEC, not NOFORK. We do exit at the end of main() */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000208 return status;
Matt Kraai91b28552001-04-23 18:53:07 +0000209}