Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Mini cp implementation for busybox |
| 4 | * |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 5 | * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu> |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 6 | * SELinux support by Yuichi Nakamura <ynakam@hitachisoft.jp> |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 7 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 9 | */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 10 | /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org) |
| 11 | * |
| 12 | * Size reduction. |
| 13 | */ |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 14 | //config:config CP |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 15 | //config: bool "cp (9.7 kb)" |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 16 | //config: default y |
| 17 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 18 | //config: cp is used to copy files and directories. |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 19 | //config: |
| 20 | //config:config FEATURE_CP_LONG_OPTIONS |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 21 | //config: bool "Enable long options" |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 22 | //config: default y |
| 23 | //config: depends on CP && LONG_OPTS |
| 24 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 25 | //config: Enable long options. |
| 26 | //config: Also add support for --parents option. |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 27 | |
| 28 | //applet:IF_CP(APPLET_NOEXEC(cp, cp, BB_DIR_BIN, BB_SUID_DROP, cp)) |
| 29 | |
| 30 | //kbuild:lib-$(CONFIG_CP) += cp.o |
| 31 | |
| 32 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/cp.html */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 33 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 34 | //usage:#define cp_trivial_usage |
Denys Vlasenko | b3439d4 | 2012-03-05 10:09:18 +0100 | [diff] [blame] | 35 | //usage: "[OPTIONS] SOURCE... DEST" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 36 | //usage:#define cp_full_usage "\n\n" |
Denys Vlasenko | b3439d4 | 2012-03-05 10:09:18 +0100 | [diff] [blame] | 37 | //usage: "Copy SOURCE(s) to DEST\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 38 | //usage: "\n -a Same as -dpR" |
| 39 | //usage: IF_SELINUX( |
| 40 | //usage: "\n -c Preserve security context" |
| 41 | //usage: ) |
| 42 | //usage: "\n -R,-r Recurse" |
| 43 | //usage: "\n -d,-P Preserve symlinks (default if -R)" |
| 44 | //usage: "\n -L Follow all symlinks" |
| 45 | //usage: "\n -H Follow symlinks on command line" |
| 46 | //usage: "\n -p Preserve file attributes if possible" |
| 47 | //usage: "\n -f Overwrite" |
| 48 | //usage: "\n -i Prompt before overwrite" |
| 49 | //usage: "\n -l,-s Create (sym)links" |
Denys Vlasenko | 877dedb | 2016-05-27 00:46:38 +0200 | [diff] [blame] | 50 | //usage: "\n -u Copy only newer files" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 51 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 52 | #include "libbb.h" |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 53 | #include "libcoreutils/coreutils.h" |
| 54 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 55 | /* This is a NOEXEC applet. Be very careful! */ |
| 56 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 57 | int cp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 58 | int cp_main(int argc, char **argv) |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 59 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 60 | struct stat source_stat; |
| 61 | struct stat dest_stat; |
| 62 | const char *last; |
| 63 | const char *dest; |
| 64 | int s_flags; |
| 65 | int d_flags; |
| 66 | int flags; |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 67 | int status; |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 68 | enum { |
Denys Vlasenko | 877dedb | 2016-05-27 00:46:38 +0200 | [diff] [blame] | 69 | FILEUTILS_CP_OPTNUM = sizeof(FILEUTILS_CP_OPTSTR)-1, |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 70 | #if ENABLE_FEATURE_CP_LONG_OPTIONS |
Denys Vlasenko | 877dedb | 2016-05-27 00:46:38 +0200 | [diff] [blame] | 71 | /*OPT_rmdest = FILEUTILS_RMDEST = 1 << FILEUTILS_CP_OPTNUM */ |
| 72 | OPT_parents = 1 << (FILEUTILS_CP_OPTNUM+1), |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 73 | #endif |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 74 | }; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 75 | |
Denis Vlasenko | 6666ac4 | 2007-08-24 21:46:24 +0000 | [diff] [blame] | 76 | // Need at least two arguments |
Denis Vlasenko | c0431ed | 2008-04-27 22:06:24 +0000 | [diff] [blame] | 77 | // Soft- and hardlinking doesn't mix |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 78 | // -P and -d are the same (-P is POSIX, -d is GNU) |
| 79 | // -r and -R are the same |
Denis Vlasenko | 80f647c | 2008-04-11 10:54:37 +0000 | [diff] [blame] | 80 | // -R (and therefore -r) turns on -d (coreutils does this) |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 81 | // -a = -pdR |
Denys Vlasenko | a40f062 | 2010-01-15 22:05:07 +0100 | [diff] [blame] | 82 | opt_complementary = "-2:l--s:s--l:Pd:rRd:Rd:apdR"; |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 83 | #if ENABLE_FEATURE_CP_LONG_OPTIONS |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame^] | 84 | flags = getopt32long(argv, FILEUTILS_CP_OPTSTR, |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 85 | "archive\0" No_argument "a" |
| 86 | "force\0" No_argument "f" |
| 87 | "interactive\0" No_argument "i" |
| 88 | "link\0" No_argument "l" |
| 89 | "dereference\0" No_argument "L" |
| 90 | "no-dereference\0" No_argument "P" |
| 91 | "recursive\0" No_argument "R" |
| 92 | "symbolic-link\0" No_argument "s" |
| 93 | "verbose\0" No_argument "v" |
Denys Vlasenko | 877dedb | 2016-05-27 00:46:38 +0200 | [diff] [blame] | 94 | "update\0" No_argument "u" |
| 95 | "remove-destination\0" No_argument "\xff" |
| 96 | "parents\0" No_argument "\xfe" |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame^] | 97 | ); |
| 98 | #else |
Denys Vlasenko | 877dedb | 2016-05-27 00:46:38 +0200 | [diff] [blame] | 99 | flags = getopt32(argv, FILEUTILS_CP_OPTSTR); |
Denys Vlasenko | 036585a | 2017-08-08 16:38:18 +0200 | [diff] [blame^] | 100 | #endif |
Denis Vlasenko | 0b28103 | 2009-03-20 14:04:00 +0000 | [diff] [blame] | 101 | /* Options of cp from GNU coreutils 6.10: |
| 102 | * -a, --archive |
| 103 | * -f, --force |
| 104 | * -i, --interactive |
| 105 | * -l, --link |
| 106 | * -L, --dereference |
| 107 | * -P, --no-dereference |
| 108 | * -R, -r, --recursive |
| 109 | * -s, --symbolic-link |
| 110 | * -v, --verbose |
| 111 | * -H follow command-line symbolic links in SOURCE |
| 112 | * -d same as --no-dereference --preserve=links |
| 113 | * -p same as --preserve=mode,ownership,timestamps |
| 114 | * -c same as --preserve=context |
Denys Vlasenko | 877dedb | 2016-05-27 00:46:38 +0200 | [diff] [blame] | 115 | * -u, --update |
| 116 | * copy only when the SOURCE file is newer than the destination |
| 117 | * file or when the destination file is missing |
| 118 | * --remove-destination |
| 119 | * remove each existing destination file before attempting to open |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 120 | * --parents |
| 121 | * use full source file name under DIRECTORY |
Denis Vlasenko | 0b28103 | 2009-03-20 14:04:00 +0000 | [diff] [blame] | 122 | * NOT SUPPORTED IN BBOX: |
Denis Vlasenko | 0b28103 | 2009-03-20 14:04:00 +0000 | [diff] [blame] | 123 | * --backup[=CONTROL] |
| 124 | * make a backup of each existing destination file |
| 125 | * -b like --backup but does not accept an argument |
| 126 | * --copy-contents |
| 127 | * copy contents of special files when recursive |
| 128 | * --preserve[=ATTR_LIST] |
| 129 | * preserve attributes (default: mode,ownership,timestamps), |
| 130 | * if possible additional attributes: security context,links,all |
| 131 | * --no-preserve=ATTR_LIST |
Denis Vlasenko | 0b28103 | 2009-03-20 14:04:00 +0000 | [diff] [blame] | 132 | * --sparse=WHEN |
| 133 | * control creation of sparse files |
| 134 | * --strip-trailing-slashes |
| 135 | * remove any trailing slashes from each SOURCE argument |
| 136 | * -S, --suffix=SUFFIX |
| 137 | * override the usual backup suffix |
| 138 | * -t, --target-directory=DIRECTORY |
| 139 | * copy all SOURCE arguments into DIRECTORY |
| 140 | * -T, --no-target-directory |
| 141 | * treat DEST as a normal file |
Denis Vlasenko | 0b28103 | 2009-03-20 14:04:00 +0000 | [diff] [blame] | 142 | * -x, --one-file-system |
| 143 | * stay on this file system |
| 144 | * -Z, --context=CONTEXT |
| 145 | * (SELinux) set SELinux security context of copy to CONTEXT |
| 146 | */ |
Denis Vlasenko | 6666ac4 | 2007-08-24 21:46:24 +0000 | [diff] [blame] | 147 | argc -= optind; |
| 148 | argv += optind; |
Denys Vlasenko | a40f062 | 2010-01-15 22:05:07 +0100 | [diff] [blame] | 149 | /* Reverse this bit. If there is -d, bit is not set: */ |
| 150 | flags ^= FILEUTILS_DEREFERENCE; |
Denis Vlasenko | c0431ed | 2008-04-27 22:06:24 +0000 | [diff] [blame] | 151 | /* coreutils 6.9 compat: |
| 152 | * by default, "cp" derefs symlinks (creates regular dest files), |
| 153 | * but "cp -R" does not. We switch off deref if -r or -R (see above). |
| 154 | * However, "cp -RL" must still deref symlinks: */ |
| 155 | if (flags & FILEUTILS_DEREF_SOFTLINK) /* -L */ |
| 156 | flags |= FILEUTILS_DEREFERENCE; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 157 | |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 158 | #if ENABLE_SELINUX |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 159 | if (flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) { |
| 160 | selinux_or_die(); |
| 161 | } |
| 162 | #endif |
| 163 | |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 164 | status = EXIT_SUCCESS; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 165 | last = argv[argc - 1]; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 166 | /* If there are only two arguments and... */ |
Denis Vlasenko | 6666ac4 | 2007-08-24 21:46:24 +0000 | [diff] [blame] | 167 | if (argc == 2) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 168 | s_flags = cp_mv_stat2(*argv, &source_stat, |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 169 | (flags & FILEUTILS_DEREFERENCE) ? stat : lstat); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 170 | if (s_flags < 0) |
| 171 | return EXIT_FAILURE; |
| 172 | d_flags = cp_mv_stat(last, &dest_stat); |
| 173 | if (d_flags < 0) |
| 174 | return EXIT_FAILURE; |
| 175 | |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 176 | #if ENABLE_FEATURE_CP_LONG_OPTIONS |
Denys Vlasenko | 877dedb | 2016-05-27 00:46:38 +0200 | [diff] [blame] | 177 | //bb_error_msg("flags:%x FILEUTILS_RMDEST:%x OPT_parents:%x", |
| 178 | // flags, FILEUTILS_RMDEST, OPT_parents); |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 179 | if (flags & OPT_parents) { |
| 180 | if (!(d_flags & 2)) { |
| 181 | bb_error_msg_and_die("with --parents, the destination must be a directory"); |
| 182 | } |
| 183 | } |
Denys Vlasenko | 877dedb | 2016-05-27 00:46:38 +0200 | [diff] [blame] | 184 | if (flags & FILEUTILS_RMDEST) { |
| 185 | flags |= FILEUTILS_FORCE; |
| 186 | } |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 187 | #endif |
| 188 | |
| 189 | /* ...if neither is a directory... */ |
| 190 | if (!((s_flags | d_flags) & 2) |
| 191 | /* ...or: recursing, the 1st is a directory, and the 2nd doesn't exist... */ |
| 192 | || ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 193 | ) { |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 194 | /* Do a simple copy */ |
Denis Vlasenko | 6666ac4 | 2007-08-24 21:46:24 +0000 | [diff] [blame] | 195 | dest = last; |
| 196 | goto DO_COPY; /* NB: argc==2 -> *++argv==last */ |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
Denis Vlasenko | 6666ac4 | 2007-08-24 21:46:24 +0000 | [diff] [blame] | 200 | while (1) { |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 201 | #if ENABLE_FEATURE_CP_LONG_OPTIONS |
| 202 | if (flags & OPT_parents) { |
| 203 | char *dest_dup; |
| 204 | char *dest_dir; |
| 205 | dest = concat_path_file(last, *argv); |
| 206 | dest_dup = xstrdup(dest); |
| 207 | dest_dir = dirname(dest_dup); |
| 208 | if (bb_make_directory(dest_dir, -1, FILEUTILS_RECUR)) { |
| 209 | return EXIT_FAILURE; |
| 210 | } |
| 211 | free(dest_dup); |
| 212 | goto DO_COPY; |
| 213 | } |
| 214 | #endif |
Denis Vlasenko | 818322b | 2007-09-24 18:27:04 +0000 | [diff] [blame] | 215 | dest = concat_path_file(last, bb_get_last_path_component_strip(*argv)); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 216 | DO_COPY: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 217 | if (copy_file(*argv, dest, flags) < 0) { |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 218 | status = EXIT_FAILURE; |
Denis Vlasenko | 6666ac4 | 2007-08-24 21:46:24 +0000 | [diff] [blame] | 219 | } |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 220 | if (*++argv == last) { |
Denys Vlasenko | d5fddcd | 2009-10-08 01:32:44 +0200 | [diff] [blame] | 221 | /* possibly leaking dest... */ |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 222 | break; |
| 223 | } |
Denys Vlasenko | d5fddcd | 2009-10-08 01:32:44 +0200 | [diff] [blame] | 224 | /* don't move up: dest may be == last and not malloced! */ |
| 225 | free((void*)dest); |
Denis Vlasenko | 6666ac4 | 2007-08-24 21:46:24 +0000 | [diff] [blame] | 226 | } |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 227 | |
Denis Vlasenko | 6666ac4 | 2007-08-24 21:46:24 +0000 | [diff] [blame] | 228 | /* Exit. We are NOEXEC, not NOFORK. We do exit at the end of main() */ |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 229 | return status; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 230 | } |