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 | */ |
| 10 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 11 | /* 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 Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 18 | //usage:#define cp_trivial_usage |
Denys Vlasenko | b3439d4 | 2012-03-05 10:09:18 +0100 | [diff] [blame] | 19 | //usage: "[OPTIONS] SOURCE... DEST" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 20 | //usage:#define cp_full_usage "\n\n" |
Denys Vlasenko | b3439d4 | 2012-03-05 10:09:18 +0100 | [diff] [blame] | 21 | //usage: "Copy SOURCE(s) to DEST\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 22 | //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 Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 35 | #include "libbb.h" |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 36 | #include "libcoreutils/coreutils.h" |
| 37 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 38 | /* This is a NOEXEC applet. Be very careful! */ |
| 39 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 40 | int cp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 41 | int cp_main(int argc, char **argv) |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 42 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 43 | 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 Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 50 | int status; |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 51 | 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 Vlasenko | a40f062 | 2010-01-15 22:05:07 +0100 | [diff] [blame] | 55 | OPT_v = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+2), |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 56 | #if ENABLE_FEATURE_CP_LONG_OPTIONS |
Denys Vlasenko | a40f062 | 2010-01-15 22:05:07 +0100 | [diff] [blame] | 57 | OPT_parents = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+3), |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 58 | #endif |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 59 | }; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 60 | |
Denis Vlasenko | 6666ac4 | 2007-08-24 21:46:24 +0000 | [diff] [blame] | 61 | // Need at least two arguments |
Denis Vlasenko | c0431ed | 2008-04-27 22:06:24 +0000 | [diff] [blame] | 62 | // Soft- and hardlinking doesn't mix |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 63 | // -P and -d are the same (-P is POSIX, -d is GNU) |
| 64 | // -r and -R are the same |
Denis Vlasenko | 80f647c | 2008-04-11 10:54:37 +0000 | [diff] [blame] | 65 | // -R (and therefore -r) turns on -d (coreutils does this) |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 66 | // -a = -pdR |
Denys Vlasenko | a40f062 | 2010-01-15 22:05:07 +0100 | [diff] [blame] | 67 | opt_complementary = "-2:l--s:s--l:Pd:rRd:Rd:apdR"; |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 68 | #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 |
Denys Vlasenko | a40f062 | 2010-01-15 22:05:07 +0100 | [diff] [blame] | 82 | flags = getopt32(argv, FILEUTILS_CP_OPTSTR "arPv"); |
Denis Vlasenko | 0b28103 | 2009-03-20 14:04:00 +0000 | [diff] [blame] | 83 | /* Options of cp from GNU coreutils 6.10: |
| 84 | * -a, --archive |
| 85 | * -f, --force |
| 86 | * -i, --interactive |
| 87 | * -l, --link |
| 88 | * -L, --dereference |
| 89 | * -P, --no-dereference |
| 90 | * -R, -r, --recursive |
| 91 | * -s, --symbolic-link |
| 92 | * -v, --verbose |
| 93 | * -H follow command-line symbolic links in SOURCE |
| 94 | * -d same as --no-dereference --preserve=links |
| 95 | * -p same as --preserve=mode,ownership,timestamps |
| 96 | * -c same as --preserve=context |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 97 | * --parents |
| 98 | * use full source file name under DIRECTORY |
Denis Vlasenko | 0b28103 | 2009-03-20 14:04:00 +0000 | [diff] [blame] | 99 | * NOT SUPPORTED IN BBOX: |
Denis Vlasenko | 0b28103 | 2009-03-20 14:04:00 +0000 | [diff] [blame] | 100 | * --backup[=CONTROL] |
| 101 | * make a backup of each existing destination file |
| 102 | * -b like --backup but does not accept an argument |
| 103 | * --copy-contents |
| 104 | * copy contents of special files when recursive |
| 105 | * --preserve[=ATTR_LIST] |
| 106 | * preserve attributes (default: mode,ownership,timestamps), |
| 107 | * if possible additional attributes: security context,links,all |
| 108 | * --no-preserve=ATTR_LIST |
Denis Vlasenko | 0b28103 | 2009-03-20 14:04:00 +0000 | [diff] [blame] | 109 | * --remove-destination |
| 110 | * remove each existing destination file before attempting to open |
| 111 | * --sparse=WHEN |
| 112 | * control creation of sparse files |
| 113 | * --strip-trailing-slashes |
| 114 | * remove any trailing slashes from each SOURCE argument |
| 115 | * -S, --suffix=SUFFIX |
| 116 | * override the usual backup suffix |
| 117 | * -t, --target-directory=DIRECTORY |
| 118 | * copy all SOURCE arguments into DIRECTORY |
| 119 | * -T, --no-target-directory |
| 120 | * treat DEST as a normal file |
| 121 | * -u, --update |
| 122 | * copy only when the SOURCE file is newer than the destination |
| 123 | * file or when the destination file is missing |
| 124 | * -x, --one-file-system |
| 125 | * stay on this file system |
| 126 | * -Z, --context=CONTEXT |
| 127 | * (SELinux) set SELinux security context of copy to CONTEXT |
| 128 | */ |
Denis Vlasenko | 6666ac4 | 2007-08-24 21:46:24 +0000 | [diff] [blame] | 129 | argc -= optind; |
| 130 | argv += optind; |
Denys Vlasenko | a40f062 | 2010-01-15 22:05:07 +0100 | [diff] [blame] | 131 | /* Reverse this bit. If there is -d, bit is not set: */ |
| 132 | flags ^= FILEUTILS_DEREFERENCE; |
Denis Vlasenko | c0431ed | 2008-04-27 22:06:24 +0000 | [diff] [blame] | 133 | /* coreutils 6.9 compat: |
| 134 | * by default, "cp" derefs symlinks (creates regular dest files), |
| 135 | * but "cp -R" does not. We switch off deref if -r or -R (see above). |
| 136 | * However, "cp -RL" must still deref symlinks: */ |
| 137 | if (flags & FILEUTILS_DEREF_SOFTLINK) /* -L */ |
| 138 | flags |= FILEUTILS_DEREFERENCE; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 139 | |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 140 | #if ENABLE_SELINUX |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 141 | if (flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) { |
| 142 | selinux_or_die(); |
| 143 | } |
| 144 | #endif |
| 145 | |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 146 | status = EXIT_SUCCESS; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 147 | last = argv[argc - 1]; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 148 | /* If there are only two arguments and... */ |
Denis Vlasenko | 6666ac4 | 2007-08-24 21:46:24 +0000 | [diff] [blame] | 149 | if (argc == 2) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 150 | s_flags = cp_mv_stat2(*argv, &source_stat, |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 151 | (flags & FILEUTILS_DEREFERENCE) ? stat : lstat); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 152 | if (s_flags < 0) |
| 153 | return EXIT_FAILURE; |
| 154 | d_flags = cp_mv_stat(last, &dest_stat); |
| 155 | if (d_flags < 0) |
| 156 | return EXIT_FAILURE; |
| 157 | |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 158 | #if ENABLE_FEATURE_CP_LONG_OPTIONS |
| 159 | if (flags & OPT_parents) { |
| 160 | if (!(d_flags & 2)) { |
| 161 | bb_error_msg_and_die("with --parents, the destination must be a directory"); |
| 162 | } |
| 163 | } |
| 164 | #endif |
| 165 | |
| 166 | /* ...if neither is a directory... */ |
| 167 | if (!((s_flags | d_flags) & 2) |
| 168 | /* ...or: recursing, the 1st is a directory, and the 2nd doesn't exist... */ |
| 169 | || ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 170 | ) { |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 171 | /* Do a simple copy */ |
Denis Vlasenko | 6666ac4 | 2007-08-24 21:46:24 +0000 | [diff] [blame] | 172 | dest = last; |
| 173 | goto DO_COPY; /* NB: argc==2 -> *++argv==last */ |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | |
Denis Vlasenko | 6666ac4 | 2007-08-24 21:46:24 +0000 | [diff] [blame] | 177 | while (1) { |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 178 | #if ENABLE_FEATURE_CP_LONG_OPTIONS |
| 179 | if (flags & OPT_parents) { |
| 180 | char *dest_dup; |
| 181 | char *dest_dir; |
| 182 | dest = concat_path_file(last, *argv); |
| 183 | dest_dup = xstrdup(dest); |
| 184 | dest_dir = dirname(dest_dup); |
| 185 | if (bb_make_directory(dest_dir, -1, FILEUTILS_RECUR)) { |
| 186 | return EXIT_FAILURE; |
| 187 | } |
| 188 | free(dest_dup); |
| 189 | goto DO_COPY; |
| 190 | } |
| 191 | #endif |
Denis Vlasenko | 818322b | 2007-09-24 18:27:04 +0000 | [diff] [blame] | 192 | dest = concat_path_file(last, bb_get_last_path_component_strip(*argv)); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 193 | DO_COPY: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 194 | if (copy_file(*argv, dest, flags) < 0) { |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 195 | status = EXIT_FAILURE; |
Denis Vlasenko | 6666ac4 | 2007-08-24 21:46:24 +0000 | [diff] [blame] | 196 | } |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 197 | if (*++argv == last) { |
Denys Vlasenko | d5fddcd | 2009-10-08 01:32:44 +0200 | [diff] [blame] | 198 | /* possibly leaking dest... */ |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 199 | break; |
| 200 | } |
Denys Vlasenko | d5fddcd | 2009-10-08 01:32:44 +0200 | [diff] [blame] | 201 | /* don't move up: dest may be == last and not malloced! */ |
| 202 | free((void*)dest); |
Denis Vlasenko | 6666ac4 | 2007-08-24 21:46:24 +0000 | [diff] [blame] | 203 | } |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 204 | |
Denis Vlasenko | 6666ac4 | 2007-08-24 21:46:24 +0000 | [diff] [blame] | 205 | /* 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] | 206 | return status; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 207 | } |