blob: 71a29396f874630696d57ac24a8aab18f11e51b1 [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
23
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000024int cp_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +000025int cp_main(int argc, char **argv)
Matt Kraai91b28552001-04-23 18:53:07 +000026{
Manuel Novoa III cad53642003-03-19 09:13:01 +000027 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 Kraai91b28552001-04-23 18:53:07 +000034 int status = 0;
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000035 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 Vlasenkof24e1f42006-10-21 23:40:20 +000040 };
Matt Kraai91b28552001-04-23 18:53:07 +000041
Denis Vlasenko6666ac42007-08-24 21:46:24 +000042 // Need at least two arguments
Denis Vlasenkoc0431ed2008-04-27 22:06:24 +000043 // Soft- and hardlinking doesn't mix
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000044 // -P and -d are the same (-P is POSIX, -d is GNU)
45 // -r and -R are the same
Denis Vlasenko80f647c2008-04-11 10:54:37 +000046 // -R (and therefore -r) turns on -d (coreutils does this)
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000047 // -a = -pdR
Denis Vlasenkoc0431ed2008-04-27 22:06:24 +000048 opt_complementary = "-2:l--s:s--l:Pd:rRd:Rd:apdR:HL";
Denis Vlasenko0b281032009-03-20 14:04:00 +000049 // -v (--verbose) is ignored
50 flags = getopt32(argv, FILEUTILS_CP_OPTSTR "arPHv");
51 /* Options of cp from GNU coreutils 6.10:
52 * -a, --archive
53 * -f, --force
54 * -i, --interactive
55 * -l, --link
56 * -L, --dereference
57 * -P, --no-dereference
58 * -R, -r, --recursive
59 * -s, --symbolic-link
60 * -v, --verbose
61 * -H follow command-line symbolic links in SOURCE
62 * -d same as --no-dereference --preserve=links
63 * -p same as --preserve=mode,ownership,timestamps
64 * -c same as --preserve=context
65 * NOT SUPPORTED IN BBOX:
66 * long options are not supported (even those above).
67 * --backup[=CONTROL]
68 * make a backup of each existing destination file
69 * -b like --backup but does not accept an argument
70 * --copy-contents
71 * copy contents of special files when recursive
72 * --preserve[=ATTR_LIST]
73 * preserve attributes (default: mode,ownership,timestamps),
74 * if possible additional attributes: security context,links,all
75 * --no-preserve=ATTR_LIST
76 * --parents
77 * use full source file name under DIRECTORY
78 * --remove-destination
79 * remove each existing destination file before attempting to open
80 * --sparse=WHEN
81 * control creation of sparse files
82 * --strip-trailing-slashes
83 * remove any trailing slashes from each SOURCE argument
84 * -S, --suffix=SUFFIX
85 * override the usual backup suffix
86 * -t, --target-directory=DIRECTORY
87 * copy all SOURCE arguments into DIRECTORY
88 * -T, --no-target-directory
89 * treat DEST as a normal file
90 * -u, --update
91 * copy only when the SOURCE file is newer than the destination
92 * file or when the destination file is missing
93 * -x, --one-file-system
94 * stay on this file system
95 * -Z, --context=CONTEXT
96 * (SELinux) set SELinux security context of copy to CONTEXT
97 */
Denis Vlasenko6666ac42007-08-24 21:46:24 +000098 argc -= optind;
99 argv += optind;
Denis Vlasenkoc0431ed2008-04-27 22:06:24 +0000100 flags ^= FILEUTILS_DEREFERENCE; /* the sense of this flag was reversed */
101 /* coreutils 6.9 compat:
102 * by default, "cp" derefs symlinks (creates regular dest files),
103 * but "cp -R" does not. We switch off deref if -r or -R (see above).
104 * However, "cp -RL" must still deref symlinks: */
105 if (flags & FILEUTILS_DEREF_SOFTLINK) /* -L */
106 flags |= FILEUTILS_DEREFERENCE;
107 /* The behavior of -H is *almost* like -L, but not quite, so let's
108 * just ignore it too for fun. TODO.
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000109 if (flags & OPT_H) ... // deref command-line params only
Mike Frysinger2ed05ab2005-04-14 02:52:50 +0000110 */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000111
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000112#if ENABLE_SELINUX
Denis Vlasenko49622d72007-03-10 16:58:49 +0000113 if (flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) {
114 selinux_or_die();
115 }
116#endif
117
Manuel Novoa III cad53642003-03-19 09:13:01 +0000118 last = argv[argc - 1];
Matt Kraai91b28552001-04-23 18:53:07 +0000119 /* If there are only two arguments and... */
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000120 if (argc == 2) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000121 s_flags = cp_mv_stat2(*argv, &source_stat,
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000122 (flags & FILEUTILS_DEREFERENCE) ? stat : lstat);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000123 if (s_flags < 0)
124 return EXIT_FAILURE;
125 d_flags = cp_mv_stat(last, &dest_stat);
126 if (d_flags < 0)
127 return EXIT_FAILURE;
128
Matt Kraai91b28552001-04-23 18:53:07 +0000129 /* ...if neither is a directory or... */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000130 if ( !((s_flags | d_flags) & 2) ||
131 /* ...recursing, the 1st is a directory, and the 2nd doesn't exist... */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000132 ((flags & FILEUTILS_RECUR) && (s_flags & 2) && !d_flags)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000133 ) {
Matt Kraai91b28552001-04-23 18:53:07 +0000134 /* ...do a simple copy. */
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000135 dest = last;
136 goto DO_COPY; /* NB: argc==2 -> *++argv==last */
Matt Kraai91b28552001-04-23 18:53:07 +0000137 }
138 }
139
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000140 while (1) {
Denis Vlasenko818322b2007-09-24 18:27:04 +0000141 dest = concat_path_file(last, bb_get_last_path_component_strip(*argv));
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000142 DO_COPY:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000143 if (copy_file(*argv, dest, flags) < 0) {
Matt Kraai91b28552001-04-23 18:53:07 +0000144 status = 1;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000145 }
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000146 if (*++argv == last) {
147 /* possibly leaking dest... */
148 break;
149 }
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000150 free((void*)dest);
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000151 }
Matt Kraai91b28552001-04-23 18:53:07 +0000152
Denis Vlasenko6666ac42007-08-24 21:46:24 +0000153 /* Exit. We are NOEXEC, not NOFORK. We do exit at the end of main() */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000154 return status;
Matt Kraai91b28552001-04-23 18:53:07 +0000155}