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 | * |
Rob Landley | 2f30932 | 2005-11-01 21:55:14 +0000 | [diff] [blame] | 8 | * Licensed under GPL v2 or later, see file LICENSE in this tarball for details. |
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 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 18 | #include "libbb.h" |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 19 | #include "libcoreutils/coreutils.h" |
| 20 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 21 | /* This is a NOEXEC applet. Be very careful! */ |
| 22 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 23 | int cp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 24 | int cp_main(int argc, char **argv) |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 25 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 26 | 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 Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 33 | int status; |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 34 | 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 Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 39 | 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 Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 43 | }; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 44 | |
Denis Vlasenko | 6666ac4 | 2007-08-24 21:46:24 +0000 | [diff] [blame] | 45 | // Need at least two arguments |
Denis Vlasenko | c0431ed | 2008-04-27 22:06:24 +0000 | [diff] [blame] | 46 | // Soft- and hardlinking doesn't mix |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 47 | // -P and -d are the same (-P is POSIX, -d is GNU) |
| 48 | // -r and -R are the same |
Denis Vlasenko | 80f647c | 2008-04-11 10:54:37 +0000 | [diff] [blame] | 49 | // -R (and therefore -r) turns on -d (coreutils does this) |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 50 | // -a = -pdR |
Denis Vlasenko | c0431ed | 2008-04-27 22:06:24 +0000 | [diff] [blame] | 51 | opt_complementary = "-2:l--s:s--l:Pd:rRd:Rd:apdR:HL"; |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 52 | #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 Vlasenko | 0b28103 | 2009-03-20 14:04:00 +0000 | [diff] [blame] | 66 | // -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 Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 82 | * --parents |
| 83 | * use full source file name under DIRECTORY |
Denis Vlasenko | 0b28103 | 2009-03-20 14:04:00 +0000 | [diff] [blame] | 84 | * NOT SUPPORTED IN BBOX: |
Denis Vlasenko | 0b28103 | 2009-03-20 14:04:00 +0000 | [diff] [blame] | 85 | * --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 Vlasenko | 0b28103 | 2009-03-20 14:04:00 +0000 | [diff] [blame] | 94 | * --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 Vlasenko | 6666ac4 | 2007-08-24 21:46:24 +0000 | [diff] [blame] | 114 | argc -= optind; |
| 115 | argv += optind; |
Denis Vlasenko | c0431ed | 2008-04-27 22:06:24 +0000 | [diff] [blame] | 116 | 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 Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 125 | if (flags & OPT_H) ... // deref command-line params only |
Mike Frysinger | 2ed05ab | 2005-04-14 02:52:50 +0000 | [diff] [blame] | 126 | */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 127 | |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 128 | #if ENABLE_SELINUX |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 129 | if (flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) { |
| 130 | selinux_or_die(); |
| 131 | } |
| 132 | #endif |
| 133 | |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 134 | status = EXIT_SUCCESS; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 135 | last = argv[argc - 1]; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 136 | /* If there are only two arguments and... */ |
Denis Vlasenko | 6666ac4 | 2007-08-24 21:46:24 +0000 | [diff] [blame] | 137 | if (argc == 2) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 138 | s_flags = cp_mv_stat2(*argv, &source_stat, |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 139 | (flags & FILEUTILS_DEREFERENCE) ? stat : lstat); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 140 | 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 Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 146 | #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 | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 158 | ) { |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 159 | /* Do a simple copy */ |
Denis Vlasenko | 6666ac4 | 2007-08-24 21:46:24 +0000 | [diff] [blame] | 160 | dest = last; |
| 161 | goto DO_COPY; /* NB: argc==2 -> *++argv==last */ |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
Denis Vlasenko | 6666ac4 | 2007-08-24 21:46:24 +0000 | [diff] [blame] | 165 | while (1) { |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 166 | #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 Vlasenko | 818322b | 2007-09-24 18:27:04 +0000 | [diff] [blame] | 180 | dest = concat_path_file(last, bb_get_last_path_component_strip(*argv)); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 181 | DO_COPY: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 182 | if (copy_file(*argv, dest, flags) < 0) { |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 183 | status = EXIT_FAILURE; |
Denis Vlasenko | 6666ac4 | 2007-08-24 21:46:24 +0000 | [diff] [blame] | 184 | } |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 185 | if (*++argv == last) { |
Denys Vlasenko | d5fddcd | 2009-10-08 01:32:44 +0200 | [diff] [blame^] | 186 | /* possibly leaking dest... */ |
Denys Vlasenko | 48f1161 | 2009-09-26 14:31:04 +0200 | [diff] [blame] | 187 | break; |
| 188 | } |
Denys Vlasenko | d5fddcd | 2009-10-08 01:32:44 +0200 | [diff] [blame^] | 189 | /* don't move up: dest may be == last and not malloced! */ |
| 190 | free((void*)dest); |
Denis Vlasenko | 6666ac4 | 2007-08-24 21:46:24 +0000 | [diff] [blame] | 191 | } |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 192 | |
Denis Vlasenko | 6666ac4 | 2007-08-24 21:46:24 +0000 | [diff] [blame] | 193 | /* 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] | 194 | return status; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 195 | } |