blob: 9f6c12367af0ff7d5160ded666af356e56d49ba1 [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 *
Rob Landley2f309322005-11-01 21:55:14 +00008 * Licensed under GPL v2 or later, see file LICENSE in this tarball for details.
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),
38 OPT_H = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+2),
Denys Vlasenko48f11612009-09-26 14:31:04 +020039 OPT_v = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+3),
40#if ENABLE_FEATURE_CP_LONG_OPTIONS
41 OPT_parents = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+4),
42#endif
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000043 };
Matt Kraai91b28552001-04-23 18:53:07 +000044
Denis Vlasenko6666ac42007-08-24 21:46:24 +000045 // Need at least two arguments
Denis Vlasenkoc0431ed2008-04-27 22:06:24 +000046 // Soft- and hardlinking doesn't mix
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000047 // -P and -d are the same (-P is POSIX, -d is GNU)
48 // -r and -R are the same
Denis Vlasenko80f647c2008-04-11 10:54:37 +000049 // -R (and therefore -r) turns on -d (coreutils does this)
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000050 // -a = -pdR
Denis Vlasenkoc0431ed2008-04-27 22:06:24 +000051 opt_complementary = "-2:l--s:s--l:Pd:rRd:Rd:apdR:HL";
Denys Vlasenko48f11612009-09-26 14:31:04 +020052#if ENABLE_FEATURE_CP_LONG_OPTIONS
53 applet_long_options =
54 "archive\0" No_argument "a"
55 "force\0" No_argument "f"
56 "interactive\0" No_argument "i"
57 "link\0" No_argument "l"
58 "dereference\0" No_argument "L"
59 "no-dereference\0" No_argument "P"
60 "recursive\0" No_argument "R"
61 "symbolic-link\0" No_argument "s"
62 "verbose\0" No_argument "v"
63 "parents\0" No_argument "\xff"
64 ;
65#endif
Denis Vlasenko0b281032009-03-20 14:04:00 +000066 // -v (--verbose) is ignored
67 flags = getopt32(argv, FILEUTILS_CP_OPTSTR "arPHv");
68 /* Options of cp from GNU coreutils 6.10:
69 * -a, --archive
70 * -f, --force
71 * -i, --interactive
72 * -l, --link
73 * -L, --dereference
74 * -P, --no-dereference
75 * -R, -r, --recursive
76 * -s, --symbolic-link
77 * -v, --verbose
78 * -H follow command-line symbolic links in SOURCE
79 * -d same as --no-dereference --preserve=links
80 * -p same as --preserve=mode,ownership,timestamps
81 * -c same as --preserve=context
Denys Vlasenko48f11612009-09-26 14:31:04 +020082 * --parents
83 * use full source file name under DIRECTORY
Denis Vlasenko0b281032009-03-20 14:04:00 +000084 * NOT SUPPORTED IN BBOX:
Denis Vlasenko0b281032009-03-20 14:04:00 +000085 * --backup[=CONTROL]
86 * make a backup of each existing destination file
87 * -b like --backup but does not accept an argument
88 * --copy-contents
89 * copy contents of special files when recursive
90 * --preserve[=ATTR_LIST]
91 * preserve attributes (default: mode,ownership,timestamps),
92 * if possible additional attributes: security context,links,all
93 * --no-preserve=ATTR_LIST
Denis Vlasenko0b281032009-03-20 14:04:00 +000094 * --remove-destination
95 * remove each existing destination file before attempting to open
96 * --sparse=WHEN
97 * control creation of sparse files
98 * --strip-trailing-slashes
99 * remove any trailing slashes from each SOURCE argument
100 * -S, --suffix=SUFFIX
101 * override the usual backup suffix
102 * -t, --target-directory=DIRECTORY
103 * copy all SOURCE arguments into DIRECTORY
104 * -T, --no-target-directory
105 * treat DEST as a normal file
106 * -u, --update
107 * copy only when the SOURCE file is newer than the destination
108 * file or when the destination file is missing
109 * -x, --one-file-system
110 * stay on this file system
111 * -Z, --context=CONTEXT
112 * (SELinux) set SELinux security context of copy to CONTEXT
113 */
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000114 argc -= optind;
115 argv += optind;
Denis Vlasenkoc0431ed2008-04-27 22:06:24 +0000116 flags ^= FILEUTILS_DEREFERENCE; /* the sense of this flag was reversed */
117 /* 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;
123 /* The behavior of -H is *almost* like -L, but not quite, so let's
124 * just ignore it too for fun. TODO.
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000125 if (flags & OPT_H) ... // deref command-line params only
Mike Frysinger2ed05ab2005-04-14 02:52:50 +0000126 */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000127
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000128#if ENABLE_SELINUX
Denis Vlasenko49622d72007-03-10 16:58:49 +0000129 if (flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) {
130 selinux_or_die();
131 }
132#endif
133
Denys Vlasenko48f11612009-09-26 14:31:04 +0200134 status = EXIT_SUCCESS;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000135 last = argv[argc - 1];
Matt Kraai91b28552001-04-23 18:53:07 +0000136 /* If there are only two arguments and... */
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000137 if (argc == 2) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000138 s_flags = cp_mv_stat2(*argv, &source_stat,
Denys Vlasenko48f11612009-09-26 14:31:04 +0200139 (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000140 if (s_flags < 0)
141 return EXIT_FAILURE;
142 d_flags = cp_mv_stat(last, &dest_stat);
143 if (d_flags < 0)
144 return EXIT_FAILURE;
145
Denys Vlasenko48f11612009-09-26 14:31:04 +0200146#if ENABLE_FEATURE_CP_LONG_OPTIONS
147 if (flags & OPT_parents) {
148 if (!(d_flags & 2)) {
149 bb_error_msg_and_die("with --parents, the destination must be a directory");
150 }
151 }
152#endif
153
154 /* ...if neither is a directory... */
155 if (!((s_flags | d_flags) & 2)
156 /* ...or: recursing, the 1st is a directory, and the 2nd doesn't exist... */
157 || ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000158 ) {
Denys Vlasenko48f11612009-09-26 14:31:04 +0200159 /* Do a simple copy */
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000160 dest = last;
161 goto DO_COPY; /* NB: argc==2 -> *++argv==last */
Matt Kraai91b28552001-04-23 18:53:07 +0000162 }
163 }
164
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000165 while (1) {
Denys Vlasenko48f11612009-09-26 14:31:04 +0200166#if ENABLE_FEATURE_CP_LONG_OPTIONS
167 if (flags & OPT_parents) {
168 char *dest_dup;
169 char *dest_dir;
170 dest = concat_path_file(last, *argv);
171 dest_dup = xstrdup(dest);
172 dest_dir = dirname(dest_dup);
173 if (bb_make_directory(dest_dir, -1, FILEUTILS_RECUR)) {
174 return EXIT_FAILURE;
175 }
176 free(dest_dup);
177 goto DO_COPY;
178 }
179#endif
Denis Vlasenko818322b2007-09-24 18:27:04 +0000180 dest = concat_path_file(last, bb_get_last_path_component_strip(*argv));
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000181 DO_COPY:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000182 if (copy_file(*argv, dest, flags) < 0) {
Denys Vlasenko48f11612009-09-26 14:31:04 +0200183 status = EXIT_FAILURE;
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000184 }
Denys Vlasenko48f11612009-09-26 14:31:04 +0200185 if (*++argv == last) {
Denys Vlasenkod5fddcd2009-10-08 01:32:44 +0200186 /* possibly leaking dest... */
Denys Vlasenko48f11612009-09-26 14:31:04 +0200187 break;
188 }
Denys Vlasenkod5fddcd2009-10-08 01:32:44 +0200189 /* don't move up: dest may be == last and not malloced! */
190 free((void*)dest);
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000191 }
Matt Kraai91b28552001-04-23 18:53:07 +0000192
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000193 /* Exit. We are NOEXEC, not NOFORK. We do exit at the end of main() */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000194 return status;
Matt Kraai91b28552001-04-23 18:53:07 +0000195}