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 | |
| 23 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 24 | int cp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 25 | int cp_main(int argc, char **argv) |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 26 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 27 | struct stat source_stat; |
| 28 | struct stat dest_stat; |
| 29 | const char *last; |
| 30 | const char *dest; |
| 31 | int s_flags; |
| 32 | int d_flags; |
| 33 | int flags; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 34 | int status = 0; |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 35 | enum { |
| 36 | OPT_a = 1 << (sizeof(FILEUTILS_CP_OPTSTR)-1), |
| 37 | OPT_r = 1 << (sizeof(FILEUTILS_CP_OPTSTR)), |
| 38 | OPT_P = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+1), |
| 39 | OPT_H = 1 << (sizeof(FILEUTILS_CP_OPTSTR)+2), |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 40 | }; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 41 | |
Denis Vlasenko | 6666ac4 | 2007-08-24 21:46:24 +0000 | [diff] [blame] | 42 | // Need at least two arguments |
Denis Vlasenko | c0431ed | 2008-04-27 22:06:24 +0000 | [diff] [blame] | 43 | // Soft- and hardlinking doesn't mix |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 44 | // -P and -d are the same (-P is POSIX, -d is GNU) |
| 45 | // -r and -R are the same |
Denis Vlasenko | 80f647c | 2008-04-11 10:54:37 +0000 | [diff] [blame] | 46 | // -R (and therefore -r) turns on -d (coreutils does this) |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 47 | // -a = -pdR |
Denis Vlasenko | c0431ed | 2008-04-27 22:06:24 +0000 | [diff] [blame] | 48 | opt_complementary = "-2:l--s:s--l:Pd:rRd:Rd:apdR:HL"; |
| 49 | flags = getopt32(argv, FILEUTILS_CP_OPTSTR "arPH"); |
Denis Vlasenko | 6666ac4 | 2007-08-24 21:46:24 +0000 | [diff] [blame] | 50 | argc -= optind; |
| 51 | argv += optind; |
Denis Vlasenko | c0431ed | 2008-04-27 22:06:24 +0000 | [diff] [blame] | 52 | flags ^= FILEUTILS_DEREFERENCE; /* the sense of this flag was reversed */ |
| 53 | /* coreutils 6.9 compat: |
| 54 | * by default, "cp" derefs symlinks (creates regular dest files), |
| 55 | * but "cp -R" does not. We switch off deref if -r or -R (see above). |
| 56 | * However, "cp -RL" must still deref symlinks: */ |
| 57 | if (flags & FILEUTILS_DEREF_SOFTLINK) /* -L */ |
| 58 | flags |= FILEUTILS_DEREFERENCE; |
| 59 | /* The behavior of -H is *almost* like -L, but not quite, so let's |
| 60 | * just ignore it too for fun. TODO. |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 61 | if (flags & OPT_H) ... // deref command-line params only |
Mike Frysinger | 2ed05ab | 2005-04-14 02:52:50 +0000 | [diff] [blame] | 62 | */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 63 | |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 64 | #if ENABLE_SELINUX |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 65 | if (flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) { |
| 66 | selinux_or_die(); |
| 67 | } |
| 68 | #endif |
| 69 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 70 | last = argv[argc - 1]; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 71 | /* If there are only two arguments and... */ |
Denis Vlasenko | 6666ac4 | 2007-08-24 21:46:24 +0000 | [diff] [blame] | 72 | if (argc == 2) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 73 | s_flags = cp_mv_stat2(*argv, &source_stat, |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 74 | (flags & FILEUTILS_DEREFERENCE) ? stat : lstat); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 75 | if (s_flags < 0) |
| 76 | return EXIT_FAILURE; |
| 77 | d_flags = cp_mv_stat(last, &dest_stat); |
| 78 | if (d_flags < 0) |
| 79 | return EXIT_FAILURE; |
| 80 | |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 81 | /* ...if neither is a directory or... */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 82 | if ( !((s_flags | d_flags) & 2) || |
| 83 | /* ...recursing, the 1st is a directory, and the 2nd doesn't exist... */ |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 84 | ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 85 | ) { |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 86 | /* ...do a simple copy. */ |
Denis Vlasenko | 6666ac4 | 2007-08-24 21:46:24 +0000 | [diff] [blame] | 87 | dest = last; |
| 88 | goto DO_COPY; /* NB: argc==2 -> *++argv==last */ |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
Denis Vlasenko | 6666ac4 | 2007-08-24 21:46:24 +0000 | [diff] [blame] | 92 | while (1) { |
Denis Vlasenko | 818322b | 2007-09-24 18:27:04 +0000 | [diff] [blame] | 93 | dest = concat_path_file(last, bb_get_last_path_component_strip(*argv)); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 94 | DO_COPY: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 95 | if (copy_file(*argv, dest, flags) < 0) { |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 96 | status = 1; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 97 | } |
Denis Vlasenko | 6666ac4 | 2007-08-24 21:46:24 +0000 | [diff] [blame] | 98 | if (*++argv == last) { |
| 99 | /* possibly leaking dest... */ |
| 100 | break; |
| 101 | } |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 102 | free((void*)dest); |
Denis Vlasenko | 6666ac4 | 2007-08-24 21:46:24 +0000 | [diff] [blame] | 103 | } |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 104 | |
Denis Vlasenko | 6666ac4 | 2007-08-24 21:46:24 +0000 | [diff] [blame] | 105 | /* 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] | 106 | return status; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 107 | } |