Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 3 | * Mini copy_file implementation for busybox |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 4 | * |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 5 | * Copyright (C) 2001 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> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +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. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 9 | */ |
Rob Landley | c983274 | 2006-06-24 21:27:36 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 11 | |
Denys Vlasenko | 2d7b5bf | 2009-07-05 12:49:29 +0200 | [diff] [blame] | 12 | // FEATURE_NON_POSIX_CP: |
| 13 | // |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 14 | // POSIX: if exists and -i, ask (w/o -i assume yes). |
| 15 | // Then open w/o EXCL (yes, not unlink!). |
| 16 | // If open still fails and -f, try unlink, then try open again. |
| 17 | // Result: a mess: |
Denys Vlasenko | 6f58be0 | 2009-07-05 13:24:17 +0200 | [diff] [blame] | 18 | // If dest is a (sym)link, we overwrite link destination! |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 19 | // (or fail, if it points to dir/nonexistent location/etc). |
| 20 | // This is strange, but POSIX-correct. |
| 21 | // coreutils cp has --remove-destination to override this... |
| 22 | |
Denys Vlasenko | 6f58be0 | 2009-07-05 13:24:17 +0200 | [diff] [blame] | 23 | /* Called if open of destination, link creation etc fails. |
| 24 | * errno must be set to relevant value ("why we cannot create dest?") |
| 25 | * to give reasonable error message */ |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 26 | static int ask_and_unlink(const char *dest, int flags) |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 27 | { |
Denis Vlasenko | d5fe880 | 2008-02-13 16:52:00 +0000 | [diff] [blame] | 28 | int e = errno; |
Denys Vlasenko | 6f58be0 | 2009-07-05 13:24:17 +0200 | [diff] [blame] | 29 | |
Denys Vlasenko | 2d7b5bf | 2009-07-05 12:49:29 +0200 | [diff] [blame] | 30 | #if !ENABLE_FEATURE_NON_POSIX_CP |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 31 | if (!(flags & (FILEUTILS_FORCE|FILEUTILS_INTERACTIVE))) { |
Denys Vlasenko | 6f58be0 | 2009-07-05 13:24:17 +0200 | [diff] [blame] | 32 | /* Either it exists, or the *path* doesnt exist */ |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 33 | bb_perror_msg("can't create '%s'", dest); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 34 | return -1; |
| 35 | } |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 36 | #endif |
Denys Vlasenko | 6f58be0 | 2009-07-05 13:24:17 +0200 | [diff] [blame] | 37 | // else: act as if -f is always in effect. |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 38 | // We don't want "can't create" msg, we want unlink to be done |
Denys Vlasenko | 6f58be0 | 2009-07-05 13:24:17 +0200 | [diff] [blame] | 39 | // (silently unless -i). Why? POSIX cp usually succeeds with |
| 40 | // O_TRUNC open of existing file, and user is left ignorantly happy. |
| 41 | // With above block unconditionally enabled, non-POSIX cp |
| 42 | // will complain a lot more than POSIX one. |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 43 | |
Denys Vlasenko | 6f58be0 | 2009-07-05 13:24:17 +0200 | [diff] [blame] | 44 | /* TODO: maybe we should do it only if ctty is present? */ |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 45 | if (flags & FILEUTILS_INTERACTIVE) { |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 46 | // We would not do POSIX insanity. -i asks, |
| 47 | // then _unlinks_ the offender. Presto. |
Denis Vlasenko | 3d82962 | 2007-08-24 14:23:57 +0000 | [diff] [blame] | 48 | // (No "opening without O_EXCL", no "unlink only if -f") |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 49 | // Or else we will end up having 3 open()s! |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 50 | fprintf(stderr, "%s: overwrite '%s'? ", applet_name, dest); |
| 51 | if (!bb_ask_confirmation()) |
Denys Vlasenko | 6f58be0 | 2009-07-05 13:24:17 +0200 | [diff] [blame] | 52 | return 0; /* not allowed to overwrite */ |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 53 | } |
| 54 | if (unlink(dest) < 0) { |
Denis Vlasenko | d5fe880 | 2008-02-13 16:52:00 +0000 | [diff] [blame] | 55 | #if ENABLE_FEATURE_VERBOSE_CP_MESSAGE |
| 56 | if (e == errno && e == ENOENT) { |
| 57 | /* e == ENOTDIR is similar: path has non-dir component, |
| 58 | * but in this case we don't even reach copy_file() */ |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 59 | bb_error_msg("can't create '%s': Path does not exist", dest); |
Denys Vlasenko | 6f58be0 | 2009-07-05 13:24:17 +0200 | [diff] [blame] | 60 | return -1; /* error */ |
Denis Vlasenko | d5fe880 | 2008-02-13 16:52:00 +0000 | [diff] [blame] | 61 | } |
| 62 | #endif |
Denys Vlasenko | 6f58be0 | 2009-07-05 13:24:17 +0200 | [diff] [blame] | 63 | errno = e; /* do not use errno from unlink */ |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 64 | bb_perror_msg("can't create '%s'", dest); |
Denys Vlasenko | 6f58be0 | 2009-07-05 13:24:17 +0200 | [diff] [blame] | 65 | return -1; /* error */ |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 66 | } |
Denys Vlasenko | 877dedb | 2016-05-27 00:46:38 +0200 | [diff] [blame] | 67 | #if ENABLE_FEATURE_CP_LONG_OPTIONS |
| 68 | if (flags & FILEUTILS_RMDEST) |
| 69 | if (flags & FILEUTILS_VERBOSE) |
| 70 | printf("removed '%s'\n", dest); |
| 71 | #endif |
Denys Vlasenko | 6f58be0 | 2009-07-05 13:24:17 +0200 | [diff] [blame] | 72 | return 1; /* ok (to try again) */ |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 73 | } |
| 74 | |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 75 | /* Return: |
| 76 | * -1 error, copy not made |
| 77 | * 0 copy is made or user answered "no" in interactive mode |
| 78 | * (failures to preserve mode/owner/times are not reported in exit code) |
| 79 | */ |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 80 | int FAST_FUNC copy_file(const char *source, const char *dest, int flags) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 81 | { |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 82 | /* This is a recursive function, try to minimize stack usage */ |
| 83 | /* NB: each struct stat is ~100 bytes */ |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 84 | struct stat source_stat; |
| 85 | struct stat dest_stat; |
Denys Vlasenko | aa4f9a2 | 2011-01-28 19:14:17 +0100 | [diff] [blame] | 86 | smallint retval = 0; |
| 87 | smallint dest_exists = 0; |
| 88 | smallint ovr; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 89 | |
Denis Vlasenko | b9ad75f | 2008-03-28 17:49:31 +0000 | [diff] [blame] | 90 | /* Inverse of cp -d ("cp without -d") */ |
Denys Vlasenko | a40f062 | 2010-01-15 22:05:07 +0100 | [diff] [blame] | 91 | #define FLAGS_DEREF (flags & (FILEUTILS_DEREFERENCE + FILEUTILS_DEREFERENCE_L0)) |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 92 | |
| 93 | if ((FLAGS_DEREF ? stat : lstat)(source, &source_stat) < 0) { |
Denys Vlasenko | 6f58be0 | 2009-07-05 13:24:17 +0200 | [diff] [blame] | 94 | /* This may be a dangling symlink. |
| 95 | * Making [sym]links to dangling symlinks works, so... */ |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 96 | if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) |
| 97 | goto make_links; |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 98 | bb_perror_msg("can't stat '%s'", source); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 99 | return -1; |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 100 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 101 | |
Eric Andersen | 02b8dfc | 2002-09-16 10:23:38 +0000 | [diff] [blame] | 102 | if (lstat(dest, &dest_stat) < 0) { |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 103 | if (errno != ENOENT) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 104 | bb_perror_msg("can't stat '%s'", dest); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 105 | return -1; |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 106 | } |
Eric Andersen | a9a220b | 2002-09-17 08:42:21 +0000 | [diff] [blame] | 107 | } else { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 108 | if (source_stat.st_dev == dest_stat.st_dev |
| 109 | && source_stat.st_ino == dest_stat.st_ino |
| 110 | ) { |
| 111 | bb_error_msg("'%s' and '%s' are the same file", source, dest); |
Mike Frysinger | 2ed05ab | 2005-04-14 02:52:50 +0000 | [diff] [blame] | 112 | return -1; |
| 113 | } |
Eric Andersen | a9a220b | 2002-09-17 08:42:21 +0000 | [diff] [blame] | 114 | dest_exists = 1; |
| 115 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 116 | |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 117 | #if ENABLE_SELINUX |
| 118 | if ((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) && is_selinux_enabled() > 0) { |
| 119 | security_context_t con; |
| 120 | if (lgetfilecon(source, &con) >= 0) { |
| 121 | if (setfscreatecon(con) < 0) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 122 | bb_perror_msg("can't set setfscreatecon %s", con); |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 123 | freecon(con); |
| 124 | return -1; |
| 125 | } |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 126 | } else if (errno == ENOTSUP || errno == ENODATA) { |
| 127 | setfscreatecon_or_die(NULL); |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 128 | } else { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 129 | bb_perror_msg("can't lgetfilecon %s", source); |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 130 | return -1; |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | #endif |
| 134 | |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 135 | if (S_ISDIR(source_stat.st_mode)) { |
| 136 | DIR *dp; |
Denis Vlasenko | 0f214ec | 2007-08-27 17:02:19 +0000 | [diff] [blame] | 137 | const char *tp; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 138 | struct dirent *d; |
Matt Kraai | 218aa37 | 2001-04-30 17:32:43 +0000 | [diff] [blame] | 139 | mode_t saved_umask = 0; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 140 | |
Matt Kraai | 0144103 | 2001-04-24 01:30:02 +0000 | [diff] [blame] | 141 | if (!(flags & FILEUTILS_RECUR)) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 142 | bb_error_msg("omitting directory '%s'", source); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 143 | return -1; |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 144 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 145 | |
Denis Vlasenko | 8a5fab6 | 2007-08-27 16:51:30 +0000 | [diff] [blame] | 146 | /* Did we ever create source ourself before? */ |
Denis Vlasenko | 0f214ec | 2007-08-27 17:02:19 +0000 | [diff] [blame] | 147 | tp = is_in_ino_dev_hashtable(&source_stat); |
| 148 | if (tp) { |
Denis Vlasenko | 8a5fab6 | 2007-08-27 16:51:30 +0000 | [diff] [blame] | 149 | /* We did! it's a recursion! man the lifeboats... */ |
| 150 | bb_error_msg("recursion detected, omitting directory '%s'", |
Denis Vlasenko | 0f214ec | 2007-08-27 17:02:19 +0000 | [diff] [blame] | 151 | source); |
Denis Vlasenko | 8a5fab6 | 2007-08-27 16:51:30 +0000 | [diff] [blame] | 152 | return -1; |
| 153 | } |
| 154 | |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 155 | if (dest_exists) { |
| 156 | if (!S_ISDIR(dest_stat.st_mode)) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 157 | bb_error_msg("target '%s' is not a directory", dest); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 158 | return -1; |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 159 | } |
Denis Vlasenko | a9335ea | 2007-09-11 16:28:14 +0000 | [diff] [blame] | 160 | /* race here: user can substitute a symlink between |
| 161 | * this check and actual creation of files inside dest */ |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 162 | } else { |
Denys Vlasenko | aa4f9a2 | 2011-01-28 19:14:17 +0100 | [diff] [blame] | 163 | /* Create DEST */ |
Matt Kraai | 218aa37 | 2001-04-30 17:32:43 +0000 | [diff] [blame] | 164 | mode_t mode; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 165 | saved_umask = umask(0); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 166 | |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 167 | mode = source_stat.st_mode; |
Matt Kraai | 0144103 | 2001-04-24 01:30:02 +0000 | [diff] [blame] | 168 | if (!(flags & FILEUTILS_PRESERVE_STATUS)) |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 169 | mode = source_stat.st_mode & ~saved_umask; |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 170 | /* Allow owner to access new dir (at least for now) */ |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 171 | mode |= S_IRWXU; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 172 | if (mkdir(dest, mode) < 0) { |
| 173 | umask(saved_umask); |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 174 | bb_perror_msg("can't create directory '%s'", dest); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 175 | return -1; |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 176 | } |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 177 | umask(saved_umask); |
Denis Vlasenko | 8a5fab6 | 2007-08-27 16:51:30 +0000 | [diff] [blame] | 178 | /* need stat info for add_to_ino_dev_hashtable */ |
| 179 | if (lstat(dest, &dest_stat) < 0) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 180 | bb_perror_msg("can't stat '%s'", dest); |
Denis Vlasenko | 8a5fab6 | 2007-08-27 16:51:30 +0000 | [diff] [blame] | 181 | return -1; |
| 182 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 183 | } |
Denis Vlasenko | 8a5fab6 | 2007-08-27 16:51:30 +0000 | [diff] [blame] | 184 | /* remember (dev,inode) of each created dir. |
| 185 | * NULL: name is not remembered */ |
| 186 | add_to_ino_dev_hashtable(&dest_stat, NULL); |
Matt Kraai | bf0a010 | 2001-06-11 13:58:02 +0000 | [diff] [blame] | 187 | |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 188 | /* Recursively copy files in SOURCE */ |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 189 | dp = opendir(source); |
| 190 | if (dp == NULL) { |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 191 | retval = -1; |
| 192 | goto preserve_mode_ugid_time; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | while ((d = readdir(dp)) != NULL) { |
| 196 | char *new_source, *new_dest; |
| 197 | |
Glenn L McGrath | 393183d | 2003-05-26 14:07:50 +0000 | [diff] [blame] | 198 | new_source = concat_subpath_file(source, d->d_name); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 199 | if (new_source == NULL) |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 200 | continue; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 201 | new_dest = concat_path_file(dest, d->d_name); |
Denys Vlasenko | a40f062 | 2010-01-15 22:05:07 +0100 | [diff] [blame] | 202 | if (copy_file(new_source, new_dest, flags & ~FILEUTILS_DEREFERENCE_L0) < 0) |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 203 | retval = -1; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 204 | free(new_source); |
| 205 | free(new_dest); |
| 206 | } |
Eric Andersen | a9a220b | 2002-09-17 08:42:21 +0000 | [diff] [blame] | 207 | closedir(dp); |
Matt Kraai | 218aa37 | 2001-04-30 17:32:43 +0000 | [diff] [blame] | 208 | |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 209 | if (!dest_exists |
| 210 | && chmod(dest, source_stat.st_mode & ~saved_umask) < 0 |
| 211 | ) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 212 | bb_perror_msg("can't preserve %s of '%s'", "permissions", dest); |
Denis Vlasenko | 8a5fab6 | 2007-08-27 16:51:30 +0000 | [diff] [blame] | 213 | /* retval = -1; - WRONG! copy *WAS* made */ |
Matt Kraai | 218aa37 | 2001-04-30 17:32:43 +0000 | [diff] [blame] | 214 | } |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 215 | goto preserve_mode_ugid_time; |
| 216 | } |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 217 | |
Denys Vlasenko | 877dedb | 2016-05-27 00:46:38 +0200 | [diff] [blame] | 218 | if (dest_exists) { |
| 219 | if (flags & FILEUTILS_UPDATE) { |
| 220 | if (source_stat.st_mtime <= dest_stat.st_mtime) { |
| 221 | return 0; /* source file must be newer */ |
| 222 | } |
| 223 | } |
| 224 | #if ENABLE_FEATURE_CP_LONG_OPTIONS |
| 225 | if (flags & FILEUTILS_RMDEST) { |
| 226 | ovr = ask_and_unlink(dest, flags); |
| 227 | if (ovr <= 0) |
| 228 | return ovr; |
| 229 | dest_exists = 0; |
| 230 | } |
| 231 | #endif |
| 232 | } |
| 233 | |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 234 | if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 235 | int (*lf)(const char *oldpath, const char *newpath); |
| 236 | make_links: |
Denys Vlasenko | 6f58be0 | 2009-07-05 13:24:17 +0200 | [diff] [blame] | 237 | /* Hmm... maybe |
| 238 | * if (DEREF && MAKE_SOFTLINK) source = realpath(source) ? |
| 239 | * (but realpath returns NULL on dangling symlinks...) */ |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 240 | lf = (flags & FILEUTILS_MAKE_SOFTLINK) ? symlink : link; |
| 241 | if (lf(source, dest) < 0) { |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 242 | ovr = ask_and_unlink(dest, flags); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 243 | if (ovr <= 0) |
| 244 | return ovr; |
| 245 | if (lf(source, dest) < 0) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 246 | bb_perror_msg("can't create link '%s'", dest); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 247 | return -1; |
| 248 | } |
| 249 | } |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 250 | /* _Not_ jumping to preserve_mode_ugid_time: |
Denys Vlasenko | 6f58be0 | 2009-07-05 13:24:17 +0200 | [diff] [blame] | 251 | * (sym)links don't have those */ |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 252 | return 0; |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 253 | } |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 254 | |
Denis Vlasenko | b9ad75f | 2008-03-28 17:49:31 +0000 | [diff] [blame] | 255 | if (/* "cp thing1 thing2" without -R: just open and read() from thing1 */ |
| 256 | !(flags & FILEUTILS_RECUR) |
| 257 | /* "cp [-opts] regular_file thing2" */ |
| 258 | || S_ISREG(source_stat.st_mode) |
| 259 | /* DEREF uses stat, which never returns S_ISLNK() == true. |
| 260 | * So the below is never true: */ |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 261 | /* || (FLAGS_DEREF && S_ISLNK(source_stat.st_mode)) */ |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 262 | ) { |
Glenn L McGrath | f62ea20 | 2003-12-20 04:38:01 +0000 | [diff] [blame] | 263 | int src_fd; |
| 264 | int dst_fd; |
Denis Vlasenko | b9ad75f | 2008-03-28 17:49:31 +0000 | [diff] [blame] | 265 | mode_t new_mode; |
| 266 | |
| 267 | if (!FLAGS_DEREF && S_ISLNK(source_stat.st_mode)) { |
| 268 | /* "cp -d symlink dst": create a link */ |
| 269 | goto dont_cat; |
| 270 | } |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 271 | |
| 272 | if (ENABLE_FEATURE_PRESERVE_HARDLINKS && !FLAGS_DEREF) { |
Denis Vlasenko | 0f214ec | 2007-08-27 17:02:19 +0000 | [diff] [blame] | 273 | const char *link_target; |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 274 | link_target = is_in_ino_dev_hashtable(&source_stat); |
| 275 | if (link_target) { |
| 276 | if (link(link_target, dest) < 0) { |
| 277 | ovr = ask_and_unlink(dest, flags); |
| 278 | if (ovr <= 0) |
| 279 | return ovr; |
Denis Vlasenko | 7510384 | 2007-06-20 14:49:47 +0000 | [diff] [blame] | 280 | if (link(link_target, dest) < 0) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 281 | bb_perror_msg("can't create link '%s'", dest); |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 282 | return -1; |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 283 | } |
Rob Landley | c983274 | 2006-06-24 21:27:36 +0000 | [diff] [blame] | 284 | } |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 285 | return 0; |
Matt Kraai | ace02dc | 2001-12-17 15:26:36 +0000 | [diff] [blame] | 286 | } |
Rob Landley | c983274 | 2006-06-24 21:27:36 +0000 | [diff] [blame] | 287 | add_to_ino_dev_hashtable(&source_stat, dest); |
Matt Kraai | ace02dc | 2001-12-17 15:26:36 +0000 | [diff] [blame] | 288 | } |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 289 | |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 290 | src_fd = open_or_warn(source, O_RDONLY); |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 291 | if (src_fd < 0) |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 292 | return -1; |
Matt Kraai | 14b7c5d | 2001-12-11 16:43:48 +0000 | [diff] [blame] | 293 | |
Denis Vlasenko | b9ad75f | 2008-03-28 17:49:31 +0000 | [diff] [blame] | 294 | /* Do not try to open with weird mode fields */ |
| 295 | new_mode = source_stat.st_mode; |
| 296 | if (!S_ISREG(source_stat.st_mode)) |
| 297 | new_mode = 0666; |
| 298 | |
Denys Vlasenko | 6f58be0 | 2009-07-05 13:24:17 +0200 | [diff] [blame] | 299 | // POSIX way is a security problem versus (sym)link attacks |
| 300 | if (!ENABLE_FEATURE_NON_POSIX_CP) { |
Denis Vlasenko | b9ad75f | 2008-03-28 17:49:31 +0000 | [diff] [blame] | 301 | dst_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, new_mode); |
Denys Vlasenko | 6f58be0 | 2009-07-05 13:24:17 +0200 | [diff] [blame] | 302 | } else { /* safe way: */ |
Denis Vlasenko | b9ad75f | 2008-03-28 17:49:31 +0000 | [diff] [blame] | 303 | dst_fd = open(dest, O_WRONLY|O_CREAT|O_EXCL, new_mode); |
Denys Vlasenko | 6f58be0 | 2009-07-05 13:24:17 +0200 | [diff] [blame] | 304 | } |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 305 | if (dst_fd == -1) { |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 306 | ovr = ask_and_unlink(dest, flags); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 307 | if (ovr <= 0) { |
Glenn L McGrath | f62ea20 | 2003-12-20 04:38:01 +0000 | [diff] [blame] | 308 | close(src_fd); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 309 | return ovr; |
| 310 | } |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 311 | /* It shouldn't exist. If it exists, do not open (symlink attack?) */ |
Denis Vlasenko | b9ad75f | 2008-03-28 17:49:31 +0000 | [diff] [blame] | 312 | dst_fd = open3_or_warn(dest, O_WRONLY|O_CREAT|O_EXCL, new_mode); |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 313 | if (dst_fd < 0) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 314 | close(src_fd); |
| 315 | return -1; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 319 | #if ENABLE_SELINUX |
Denis Vlasenko | b9ad75f | 2008-03-28 17:49:31 +0000 | [diff] [blame] | 320 | if ((flags & (FILEUTILS_PRESERVE_SECURITY_CONTEXT|FILEUTILS_SET_SECURITY_CONTEXT)) |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 321 | && is_selinux_enabled() > 0 |
| 322 | ) { |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 323 | security_context_t con; |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 324 | if (getfscreatecon(&con) == -1) { |
| 325 | bb_perror_msg("getfscreatecon"); |
| 326 | return -1; |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 327 | } |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 328 | if (con) { |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 329 | if (setfilecon(dest, con) == -1) { |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 330 | bb_perror_msg("setfilecon:%s,%s", dest, con); |
| 331 | freecon(con); |
| 332 | return -1; |
| 333 | } |
| 334 | freecon(con); |
| 335 | } |
| 336 | } |
| 337 | #endif |
Glenn L McGrath | f62ea20 | 2003-12-20 04:38:01 +0000 | [diff] [blame] | 338 | if (bb_copyfd_eof(src_fd, dst_fd) == -1) |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 339 | retval = -1; |
Denys Vlasenko | 0cd445f | 2010-02-06 21:11:49 +0100 | [diff] [blame] | 340 | /* Careful with writing... */ |
Glenn L McGrath | f62ea20 | 2003-12-20 04:38:01 +0000 | [diff] [blame] | 341 | if (close(dst_fd) < 0) { |
Denys Vlasenko | 0cd445f | 2010-02-06 21:11:49 +0100 | [diff] [blame] | 342 | bb_perror_msg("error writing to '%s'", dest); |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 343 | retval = -1; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 344 | } |
Denis Vlasenko | 8a5fab6 | 2007-08-27 16:51:30 +0000 | [diff] [blame] | 345 | /* ...but read size is already checked by bb_copyfd_eof */ |
| 346 | close(src_fd); |
Denis Vlasenko | b9ad75f | 2008-03-28 17:49:31 +0000 | [diff] [blame] | 347 | /* "cp /dev/something new_file" should not |
| 348 | * copy mode of /dev/something */ |
| 349 | if (!S_ISREG(source_stat.st_mode)) |
| 350 | return retval; |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 351 | goto preserve_mode_ugid_time; |
| 352 | } |
Denis Vlasenko | b9ad75f | 2008-03-28 17:49:31 +0000 | [diff] [blame] | 353 | dont_cat: |
Eric Andersen | 403a73a | 2002-09-16 09:23:22 +0000 | [diff] [blame] | 354 | |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 355 | /* Source is a symlink or a special file */ |
| 356 | /* We are lazy here, a bit lax with races... */ |
| 357 | if (dest_exists) { |
| 358 | errno = EEXIST; |
| 359 | ovr = ask_and_unlink(dest, flags); |
| 360 | if (ovr <= 0) |
| 361 | return ovr; |
| 362 | } |
| 363 | if (S_ISLNK(source_stat.st_mode)) { |
Denis Vlasenko | 8a5fab6 | 2007-08-27 16:51:30 +0000 | [diff] [blame] | 364 | char *lpath = xmalloc_readlink_or_warn(source); |
| 365 | if (lpath) { |
| 366 | int r = symlink(lpath, dest); |
Rob Landley | 2f30932 | 2005-11-01 21:55:14 +0000 | [diff] [blame] | 367 | free(lpath); |
Denis Vlasenko | 8a5fab6 | 2007-08-27 16:51:30 +0000 | [diff] [blame] | 368 | if (r < 0) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 369 | bb_perror_msg("can't create symlink '%s'", dest); |
Denis Vlasenko | 8a5fab6 | 2007-08-27 16:51:30 +0000 | [diff] [blame] | 370 | return -1; |
| 371 | } |
| 372 | if (flags & FILEUTILS_PRESERVE_STATUS) |
| 373 | if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0) |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 374 | bb_perror_msg("can't preserve %s of '%s'", "ownership", dest); |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 375 | } |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 376 | /* _Not_ jumping to preserve_mode_ugid_time: |
| 377 | * symlinks don't have those */ |
Denys Vlasenko | ca00338 | 2016-07-14 20:58:39 +0200 | [diff] [blame^] | 378 | goto verb_and_exit; |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 379 | } |
| 380 | if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) |
| 381 | || S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode) |
| 382 | ) { |
| 383 | if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 384 | bb_perror_msg("can't create '%s'", dest); |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 385 | return -1; |
Rob Landley | 2f30932 | 2005-11-01 21:55:14 +0000 | [diff] [blame] | 386 | } |
Eric Andersen | a9a220b | 2002-09-17 08:42:21 +0000 | [diff] [blame] | 387 | } else { |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 388 | bb_error_msg("unrecognized file '%s' with mode %x", source, source_stat.st_mode); |
Eric Andersen | a9a220b | 2002-09-17 08:42:21 +0000 | [diff] [blame] | 389 | return -1; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 392 | preserve_mode_ugid_time: |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 393 | |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 394 | if (flags & FILEUTILS_PRESERVE_STATUS |
| 395 | /* Cannot happen: */ |
| 396 | /* && !(flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) */ |
| 397 | ) { |
Denys Vlasenko | dcbfaba | 2009-11-29 19:40:36 +0100 | [diff] [blame] | 398 | struct timeval times[2]; |
Denys Vlasenko | 389cca4 | 2009-11-15 02:28:56 +0100 | [diff] [blame] | 399 | |
Denys Vlasenko | dcbfaba | 2009-11-29 19:40:36 +0100 | [diff] [blame] | 400 | times[1].tv_sec = times[0].tv_sec = source_stat.st_mtime; |
| 401 | times[1].tv_usec = times[0].tv_usec = 0; |
Denis Vlasenko | 8a5fab6 | 2007-08-27 16:51:30 +0000 | [diff] [blame] | 402 | /* BTW, utimes sets usec-precision time - just FYI */ |
Denys Vlasenko | dcbfaba | 2009-11-29 19:40:36 +0100 | [diff] [blame] | 403 | if (utimes(dest, times) < 0) |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 404 | bb_perror_msg("can't preserve %s of '%s'", "times", dest); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 405 | if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) { |
| 406 | source_stat.st_mode &= ~(S_ISUID | S_ISGID); |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 407 | bb_perror_msg("can't preserve %s of '%s'", "ownership", dest); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 408 | } |
| 409 | if (chmod(dest, source_stat.st_mode) < 0) |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 410 | bb_perror_msg("can't preserve %s of '%s'", "permissions", dest); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Denys Vlasenko | ca00338 | 2016-07-14 20:58:39 +0200 | [diff] [blame^] | 413 | verb_and_exit: |
Denys Vlasenko | 17f8418 | 2014-05-19 16:23:50 +0200 | [diff] [blame] | 414 | if (flags & FILEUTILS_VERBOSE) { |
| 415 | printf("'%s' -> '%s'\n", source, dest); |
| 416 | } |
| 417 | |
Denis Vlasenko | 30bab71 | 2007-08-25 21:14:55 +0000 | [diff] [blame] | 418 | return retval; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 419 | } |