blob: ab17b39a69f2fcc272601b748747a97072f73ed5 [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000018#include "libbb.h"
Manuel Novoa III cad53642003-03-19 09:13:01 +000019#include "libcoreutils/coreutils.h"
20
Denis Vlasenko99912ca2007-04-10 15:43:37 +000021/* This is a NOEXEC applet. Be very careful! */
22
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000023int cp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +000024int cp_main(int argc, char **argv)
Matt Kraai91b28552001-04-23 18:53:07 +000025{
Manuel Novoa III cad53642003-03-19 09:13:01 +000026 struct stat source_stat;
27 struct stat dest_stat;
28 const char *last;
29 const char *dest;
30 int s_flags;
31 int d_flags;
32 int flags;
Denys Vlasenko48f11612009-09-26 14:31:04 +020033 int status;
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000034 enum {
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),
Denys Vlasenkoa40f0622010-01-15 22:05:07 +010038 OPT_v = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+2),
Denys Vlasenko48f11612009-09-26 14:31:04 +020039#if ENABLE_FEATURE_CP_LONG_OPTIONS
Denys Vlasenkoa40f0622010-01-15 22:05:07 +010040 OPT_parents = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+3),
Denys Vlasenko48f11612009-09-26 14:31:04 +020041#endif
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000042 };
Matt Kraai91b28552001-04-23 18:53:07 +000043
Denis Vlasenko6666ac42007-08-24 21:46:24 +000044 // Need at least two arguments
Denis Vlasenkoc0431ed2008-04-27 22:06:24 +000045 // Soft- and hardlinking doesn't mix
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000046 // -P and -d are the same (-P is POSIX, -d is GNU)
47 // -r and -R are the same
Denis Vlasenko80f647c2008-04-11 10:54:37 +000048 // -R (and therefore -r) turns on -d (coreutils does this)
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000049 // -a = -pdR
Denys Vlasenkoa40f0622010-01-15 22:05:07 +010050 opt_complementary = "-2:l--s:s--l:Pd:rRd:Rd:apdR";
Denys Vlasenko48f11612009-09-26 14:31:04 +020051#if ENABLE_FEATURE_CP_LONG_OPTIONS
52 applet_long_options =
53 "archive\0" No_argument "a"
54 "force\0" No_argument "f"
55 "interactive\0" No_argument "i"
56 "link\0" No_argument "l"
57 "dereference\0" No_argument "L"
58 "no-dereference\0" No_argument "P"
59 "recursive\0" No_argument "R"
60 "symbolic-link\0" No_argument "s"
61 "verbose\0" No_argument "v"
62 "parents\0" No_argument "\xff"
63 ;
64#endif
Denis Vlasenko0b281032009-03-20 14:04:00 +000065 // -v (--verbose) is ignored
Denys Vlasenkoa40f0622010-01-15 22:05:07 +010066 flags = getopt32(argv, FILEUTILS_CP_OPTSTR "arPv");
Denis Vlasenko0b281032009-03-20 14:04:00 +000067 /* Options of cp from GNU coreutils 6.10:
68 * -a, --archive
69 * -f, --force
70 * -i, --interactive
71 * -l, --link
72 * -L, --dereference
73 * -P, --no-dereference
74 * -R, -r, --recursive
75 * -s, --symbolic-link
76 * -v, --verbose
77 * -H follow command-line symbolic links in SOURCE
78 * -d same as --no-dereference --preserve=links
79 * -p same as --preserve=mode,ownership,timestamps
80 * -c same as --preserve=context
Denys Vlasenko48f11612009-09-26 14:31:04 +020081 * --parents
82 * use full source file name under DIRECTORY
Denis Vlasenko0b281032009-03-20 14:04:00 +000083 * NOT SUPPORTED IN BBOX:
Denis Vlasenko0b281032009-03-20 14:04:00 +000084 * --backup[=CONTROL]
85 * make a backup of each existing destination file
86 * -b like --backup but does not accept an argument
87 * --copy-contents
88 * copy contents of special files when recursive
89 * --preserve[=ATTR_LIST]
90 * preserve attributes (default: mode,ownership,timestamps),
91 * if possible additional attributes: security context,links,all
92 * --no-preserve=ATTR_LIST
Denis Vlasenko0b281032009-03-20 14:04:00 +000093 * --remove-destination
94 * remove each existing destination file before attempting to open
95 * --sparse=WHEN
96 * control creation of sparse files
97 * --strip-trailing-slashes
98 * remove any trailing slashes from each SOURCE argument
99 * -S, --suffix=SUFFIX
100 * override the usual backup suffix
101 * -t, --target-directory=DIRECTORY
102 * copy all SOURCE arguments into DIRECTORY
103 * -T, --no-target-directory
104 * treat DEST as a normal file
105 * -u, --update
106 * copy only when the SOURCE file is newer than the destination
107 * file or when the destination file is missing
108 * -x, --one-file-system
109 * stay on this file system
110 * -Z, --context=CONTEXT
111 * (SELinux) set SELinux security context of copy to CONTEXT
112 */
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000113 argc -= optind;
114 argv += optind;
Denys Vlasenkoa40f0622010-01-15 22:05:07 +0100115 /* Reverse this bit. If there is -d, bit is not set: */
116 flags ^= FILEUTILS_DEREFERENCE;
Denis Vlasenkoc0431ed2008-04-27 22:06:24 +0000117 /* 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;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000123
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000124#if ENABLE_SELINUX
Denis Vlasenko49622d72007-03-10 16:58:49 +0000125 if (flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) {
126 selinux_or_die();
127 }
128#endif
129
Denys Vlasenko48f11612009-09-26 14:31:04 +0200130 status = EXIT_SUCCESS;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000131 last = argv[argc - 1];
Matt Kraai91b28552001-04-23 18:53:07 +0000132 /* If there are only two arguments and... */
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000133 if (argc == 2) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000134 s_flags = cp_mv_stat2(*argv, &source_stat,
Denys Vlasenko48f11612009-09-26 14:31:04 +0200135 (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000136 if (s_flags < 0)
137 return EXIT_FAILURE;
138 d_flags = cp_mv_stat(last, &dest_stat);
139 if (d_flags < 0)
140 return EXIT_FAILURE;
141
Denys Vlasenko48f11612009-09-26 14:31:04 +0200142#if ENABLE_FEATURE_CP_LONG_OPTIONS
143 if (flags & OPT_parents) {
144 if (!(d_flags & 2)) {
145 bb_error_msg_and_die("with --parents, the destination must be a directory");
146 }
147 }
148#endif
149
150 /* ...if neither is a directory... */
151 if (!((s_flags | d_flags) & 2)
152 /* ...or: recursing, the 1st is a directory, and the 2nd doesn't exist... */
153 || ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000154 ) {
Denys Vlasenko48f11612009-09-26 14:31:04 +0200155 /* Do a simple copy */
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000156 dest = last;
157 goto DO_COPY; /* NB: argc==2 -> *++argv==last */
Matt Kraai91b28552001-04-23 18:53:07 +0000158 }
159 }
160
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000161 while (1) {
Denys Vlasenko48f11612009-09-26 14:31:04 +0200162#if ENABLE_FEATURE_CP_LONG_OPTIONS
163 if (flags & OPT_parents) {
164 char *dest_dup;
165 char *dest_dir;
166 dest = concat_path_file(last, *argv);
167 dest_dup = xstrdup(dest);
168 dest_dir = dirname(dest_dup);
169 if (bb_make_directory(dest_dir, -1, FILEUTILS_RECUR)) {
170 return EXIT_FAILURE;
171 }
172 free(dest_dup);
173 goto DO_COPY;
174 }
175#endif
Denis Vlasenko818322b2007-09-24 18:27:04 +0000176 dest = concat_path_file(last, bb_get_last_path_component_strip(*argv));
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000177 DO_COPY:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000178 if (copy_file(*argv, dest, flags) < 0) {
Denys Vlasenko48f11612009-09-26 14:31:04 +0200179 status = EXIT_FAILURE;
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000180 }
Denys Vlasenko48f11612009-09-26 14:31:04 +0200181 if (*++argv == last) {
Denys Vlasenkod5fddcd2009-10-08 01:32:44 +0200182 /* possibly leaking dest... */
Denys Vlasenko48f11612009-09-26 14:31:04 +0200183 break;
184 }
Denys Vlasenkod5fddcd2009-10-08 01:32:44 +0200185 /* don't move up: dest may be == last and not malloced! */
186 free((void*)dest);
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000187 }
Matt Kraai91b28552001-04-23 18:53:07 +0000188
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000189 /* Exit. We are NOEXEC, not NOFORK. We do exit at the end of main() */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000190 return status;
Matt Kraai91b28552001-04-23 18:53:07 +0000191}