blob: de2e512be3b5ee510624b368a1f060615854055a [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
Denys Vlasenkob3439d42012-03-05 10:09:18 +010019//usage: "[OPTIONS] SOURCE... DEST"
Pere Orga34425382011-03-31 14:43:25 +020020//usage:#define cp_full_usage "\n\n"
Denys Vlasenkob3439d42012-03-05 10:09:18 +010021//usage: "Copy SOURCE(s) to DEST\n"
Pere Orga34425382011-03-31 14:43:25 +020022//usage: "\n -a Same as -dpR"
23//usage: IF_SELINUX(
24//usage: "\n -c Preserve security context"
25//usage: )
26//usage: "\n -R,-r Recurse"
27//usage: "\n -d,-P Preserve symlinks (default if -R)"
28//usage: "\n -L Follow all symlinks"
29//usage: "\n -H Follow symlinks on command line"
30//usage: "\n -p Preserve file attributes if possible"
31//usage: "\n -f Overwrite"
32//usage: "\n -i Prompt before overwrite"
33//usage: "\n -l,-s Create (sym)links"
34
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000035#include "libbb.h"
Manuel Novoa III cad53642003-03-19 09:13:01 +000036#include "libcoreutils/coreutils.h"
37
Denis Vlasenko99912ca2007-04-10 15:43:37 +000038/* This is a NOEXEC applet. Be very careful! */
39
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000040int cp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +000041int cp_main(int argc, char **argv)
Matt Kraai91b28552001-04-23 18:53:07 +000042{
Manuel Novoa III cad53642003-03-19 09:13:01 +000043 struct stat source_stat;
44 struct stat dest_stat;
45 const char *last;
46 const char *dest;
47 int s_flags;
48 int d_flags;
49 int flags;
Denys Vlasenko48f11612009-09-26 14:31:04 +020050 int status;
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000051 enum {
52 OPT_a = 1 << (sizeof(FILEUTILS_CP_OPTSTR)-1),
53 OPT_r = 1 << (sizeof(FILEUTILS_CP_OPTSTR)),
54 OPT_P = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+1),
Denys Vlasenkoa40f0622010-01-15 22:05:07 +010055 OPT_v = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+2),
Denys Vlasenko48f11612009-09-26 14:31:04 +020056#if ENABLE_FEATURE_CP_LONG_OPTIONS
Denys Vlasenkoa40f0622010-01-15 22:05:07 +010057 OPT_parents = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+3),
Denys Vlasenko48f11612009-09-26 14:31:04 +020058#endif
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000059 };
Matt Kraai91b28552001-04-23 18:53:07 +000060
Denis Vlasenko6666ac42007-08-24 21:46:24 +000061 // Need at least two arguments
Denis Vlasenkoc0431ed2008-04-27 22:06:24 +000062 // Soft- and hardlinking doesn't mix
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000063 // -P and -d are the same (-P is POSIX, -d is GNU)
64 // -r and -R are the same
Denis Vlasenko80f647c2008-04-11 10:54:37 +000065 // -R (and therefore -r) turns on -d (coreutils does this)
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000066 // -a = -pdR
Denys Vlasenkoa40f0622010-01-15 22:05:07 +010067 opt_complementary = "-2:l--s:s--l:Pd:rRd:Rd:apdR";
Denys Vlasenko48f11612009-09-26 14:31:04 +020068#if ENABLE_FEATURE_CP_LONG_OPTIONS
69 applet_long_options =
70 "archive\0" No_argument "a"
71 "force\0" No_argument "f"
72 "interactive\0" No_argument "i"
73 "link\0" No_argument "l"
74 "dereference\0" No_argument "L"
75 "no-dereference\0" No_argument "P"
76 "recursive\0" No_argument "R"
77 "symbolic-link\0" No_argument "s"
78 "verbose\0" No_argument "v"
79 "parents\0" No_argument "\xff"
80 ;
81#endif
Denis Vlasenko0b281032009-03-20 14:04:00 +000082 // -v (--verbose) is ignored
Denys Vlasenkoa40f0622010-01-15 22:05:07 +010083 flags = getopt32(argv, FILEUTILS_CP_OPTSTR "arPv");
Denis Vlasenko0b281032009-03-20 14:04:00 +000084 /* Options of cp from GNU coreutils 6.10:
85 * -a, --archive
86 * -f, --force
87 * -i, --interactive
88 * -l, --link
89 * -L, --dereference
90 * -P, --no-dereference
91 * -R, -r, --recursive
92 * -s, --symbolic-link
93 * -v, --verbose
94 * -H follow command-line symbolic links in SOURCE
95 * -d same as --no-dereference --preserve=links
96 * -p same as --preserve=mode,ownership,timestamps
97 * -c same as --preserve=context
Denys Vlasenko48f11612009-09-26 14:31:04 +020098 * --parents
99 * use full source file name under DIRECTORY
Denis Vlasenko0b281032009-03-20 14:04:00 +0000100 * NOT SUPPORTED IN BBOX:
Denis Vlasenko0b281032009-03-20 14:04:00 +0000101 * --backup[=CONTROL]
102 * make a backup of each existing destination file
103 * -b like --backup but does not accept an argument
104 * --copy-contents
105 * copy contents of special files when recursive
106 * --preserve[=ATTR_LIST]
107 * preserve attributes (default: mode,ownership,timestamps),
108 * if possible additional attributes: security context,links,all
109 * --no-preserve=ATTR_LIST
Denis Vlasenko0b281032009-03-20 14:04:00 +0000110 * --remove-destination
111 * remove each existing destination file before attempting to open
112 * --sparse=WHEN
113 * control creation of sparse files
114 * --strip-trailing-slashes
115 * remove any trailing slashes from each SOURCE argument
116 * -S, --suffix=SUFFIX
117 * override the usual backup suffix
118 * -t, --target-directory=DIRECTORY
119 * copy all SOURCE arguments into DIRECTORY
120 * -T, --no-target-directory
121 * treat DEST as a normal file
122 * -u, --update
123 * copy only when the SOURCE file is newer than the destination
124 * file or when the destination file is missing
125 * -x, --one-file-system
126 * stay on this file system
127 * -Z, --context=CONTEXT
128 * (SELinux) set SELinux security context of copy to CONTEXT
129 */
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000130 argc -= optind;
131 argv += optind;
Denys Vlasenkoa40f0622010-01-15 22:05:07 +0100132 /* Reverse this bit. If there is -d, bit is not set: */
133 flags ^= FILEUTILS_DEREFERENCE;
Denis Vlasenkoc0431ed2008-04-27 22:06:24 +0000134 /* coreutils 6.9 compat:
135 * by default, "cp" derefs symlinks (creates regular dest files),
136 * but "cp -R" does not. We switch off deref if -r or -R (see above).
137 * However, "cp -RL" must still deref symlinks: */
138 if (flags & FILEUTILS_DEREF_SOFTLINK) /* -L */
139 flags |= FILEUTILS_DEREFERENCE;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000140
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000141#if ENABLE_SELINUX
Denis Vlasenko49622d72007-03-10 16:58:49 +0000142 if (flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) {
143 selinux_or_die();
144 }
145#endif
146
Denys Vlasenko48f11612009-09-26 14:31:04 +0200147 status = EXIT_SUCCESS;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000148 last = argv[argc - 1];
Matt Kraai91b28552001-04-23 18:53:07 +0000149 /* If there are only two arguments and... */
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000150 if (argc == 2) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000151 s_flags = cp_mv_stat2(*argv, &source_stat,
Denys Vlasenko48f11612009-09-26 14:31:04 +0200152 (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000153 if (s_flags < 0)
154 return EXIT_FAILURE;
155 d_flags = cp_mv_stat(last, &dest_stat);
156 if (d_flags < 0)
157 return EXIT_FAILURE;
158
Denys Vlasenko48f11612009-09-26 14:31:04 +0200159#if ENABLE_FEATURE_CP_LONG_OPTIONS
160 if (flags & OPT_parents) {
161 if (!(d_flags & 2)) {
162 bb_error_msg_and_die("with --parents, the destination must be a directory");
163 }
164 }
165#endif
166
167 /* ...if neither is a directory... */
168 if (!((s_flags | d_flags) & 2)
169 /* ...or: recursing, the 1st is a directory, and the 2nd doesn't exist... */
170 || ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000171 ) {
Denys Vlasenko48f11612009-09-26 14:31:04 +0200172 /* Do a simple copy */
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000173 dest = last;
174 goto DO_COPY; /* NB: argc==2 -> *++argv==last */
Matt Kraai91b28552001-04-23 18:53:07 +0000175 }
176 }
177
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000178 while (1) {
Denys Vlasenko48f11612009-09-26 14:31:04 +0200179#if ENABLE_FEATURE_CP_LONG_OPTIONS
180 if (flags & OPT_parents) {
181 char *dest_dup;
182 char *dest_dir;
183 dest = concat_path_file(last, *argv);
184 dest_dup = xstrdup(dest);
185 dest_dir = dirname(dest_dup);
186 if (bb_make_directory(dest_dir, -1, FILEUTILS_RECUR)) {
187 return EXIT_FAILURE;
188 }
189 free(dest_dup);
190 goto DO_COPY;
191 }
192#endif
Denis Vlasenko818322b2007-09-24 18:27:04 +0000193 dest = concat_path_file(last, bb_get_last_path_component_strip(*argv));
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000194 DO_COPY:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000195 if (copy_file(*argv, dest, flags) < 0) {
Denys Vlasenko48f11612009-09-26 14:31:04 +0200196 status = EXIT_FAILURE;
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000197 }
Denys Vlasenko48f11612009-09-26 14:31:04 +0200198 if (*++argv == last) {
Denys Vlasenkod5fddcd2009-10-08 01:32:44 +0200199 /* possibly leaking dest... */
Denys Vlasenko48f11612009-09-26 14:31:04 +0200200 break;
201 }
Denys Vlasenkod5fddcd2009-10-08 01:32:44 +0200202 /* don't move up: dest may be == last and not malloced! */
203 free((void*)dest);
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000204 }
Matt Kraai91b28552001-04-23 18:53:07 +0000205
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000206 /* Exit. We are NOEXEC, not NOFORK. We do exit at the end of main() */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000207 return status;
Matt Kraai91b28552001-04-23 18:53:07 +0000208}