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 | * |
Rob Landley | 2f30932 | 2005-11-01 21:55:14 +0000 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 9 | * |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 10 | */ |
| 11 | |
Rob Landley | c983274 | 2006-06-24 21:27:36 +0000 | [diff] [blame] | 12 | #include "libbb.h" |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 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: |
| 18 | // If dest is a softlink, we overwrite softlink's destination! |
| 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 | |
| 23 | #define DO_POSIX_CP 0 /* 1 - POSIX behavior, 0 - safe behavior */ |
| 24 | |
| 25 | |
| 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 | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 28 | // If !DO_POSIX_CP, act as if -f is always in effect - we don't want |
| 29 | // "'file' exists" msg, we want unlink to be done (silently unless -i |
| 30 | // is also in effect). |
| 31 | // This prevents safe way from asking more questions than POSIX does. |
| 32 | #if DO_POSIX_CP |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 33 | if (!(flags & (FILEUTILS_FORCE|FILEUTILS_INTERACTIVE))) { |
| 34 | fprintf(stderr, "'%s' exists\n", dest); |
| 35 | return -1; |
| 36 | } |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 37 | #endif |
| 38 | |
| 39 | // TODO: maybe we should do it only if ctty is present? |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 40 | if (flags & FILEUTILS_INTERACTIVE) { |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 41 | // We would not do POSIX insanity. -i asks, |
| 42 | // then _unlinks_ the offender. Presto. |
| 43 | // (No opening without O_EXCL, no unlinks only if -f) |
| 44 | // Or else we will end up having 3 open()s! |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 45 | fprintf(stderr, "%s: overwrite '%s'? ", applet_name, dest); |
| 46 | if (!bb_ask_confirmation()) |
| 47 | return 0; // not allowed to overwrite |
| 48 | } |
| 49 | if (unlink(dest) < 0) { |
| 50 | bb_perror_msg("cannot remove '%s'", dest); |
| 51 | return -1; // error |
| 52 | } |
| 53 | return 1; // ok (to try again) |
| 54 | } |
| 55 | |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 56 | /* Return: |
| 57 | * -1 error, copy not made |
| 58 | * 0 copy is made or user answered "no" in interactive mode |
| 59 | * (failures to preserve mode/owner/times are not reported in exit code) |
| 60 | */ |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 61 | int copy_file(const char *source, const char *dest, int flags) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 62 | { |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 63 | struct stat source_stat; |
| 64 | struct stat dest_stat; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 65 | int status = 0; |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 66 | signed char dest_exists = 0; |
| 67 | signed char ovr; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 68 | |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 69 | #define FLAGS_DEREF (flags & FILEUTILS_DEREFERENCE) |
| 70 | |
| 71 | if ((FLAGS_DEREF ? stat : lstat)(source, &source_stat) < 0) { |
| 72 | // This may be a dangling symlink. |
| 73 | // Making [sym]links to dangling symlinks works, so... |
| 74 | if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) |
| 75 | goto make_links; |
| 76 | bb_perror_msg("cannot stat '%s'", source); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 77 | return -1; |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 78 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 79 | |
Eric Andersen | 02b8dfc | 2002-09-16 10:23:38 +0000 | [diff] [blame] | 80 | if (lstat(dest, &dest_stat) < 0) { |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 81 | if (errno != ENOENT) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 82 | bb_perror_msg("cannot stat '%s'", dest); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 83 | return -1; |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 84 | } |
Eric Andersen | a9a220b | 2002-09-17 08:42:21 +0000 | [diff] [blame] | 85 | } else { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 86 | if (source_stat.st_dev == dest_stat.st_dev |
| 87 | && source_stat.st_ino == dest_stat.st_ino |
| 88 | ) { |
| 89 | bb_error_msg("'%s' and '%s' are the same file", source, dest); |
Mike Frysinger | 2ed05ab | 2005-04-14 02:52:50 +0000 | [diff] [blame] | 90 | return -1; |
| 91 | } |
Eric Andersen | a9a220b | 2002-09-17 08:42:21 +0000 | [diff] [blame] | 92 | dest_exists = 1; |
| 93 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 94 | |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 95 | #if ENABLE_SELINUX |
| 96 | if ((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) && is_selinux_enabled() > 0) { |
| 97 | security_context_t con; |
| 98 | if (lgetfilecon(source, &con) >= 0) { |
| 99 | if (setfscreatecon(con) < 0) { |
| 100 | bb_perror_msg("cannot set setfscreatecon %s", con); |
| 101 | freecon(con); |
| 102 | return -1; |
| 103 | } |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 104 | } else if (errno == ENOTSUP || errno == ENODATA) { |
| 105 | setfscreatecon_or_die(NULL); |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 106 | } else { |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 107 | bb_perror_msg("cannot lgetfilecon %s", source); |
| 108 | return -1; |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | #endif |
| 112 | |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 113 | if (S_ISDIR(source_stat.st_mode)) { |
| 114 | DIR *dp; |
| 115 | struct dirent *d; |
Matt Kraai | 218aa37 | 2001-04-30 17:32:43 +0000 | [diff] [blame] | 116 | mode_t saved_umask = 0; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 117 | |
Matt Kraai | 0144103 | 2001-04-24 01:30:02 +0000 | [diff] [blame] | 118 | if (!(flags & FILEUTILS_RECUR)) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 119 | bb_error_msg("omitting directory '%s'", source); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 120 | return -1; |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 121 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 122 | |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 123 | /* Create DEST. */ |
| 124 | if (dest_exists) { |
| 125 | if (!S_ISDIR(dest_stat.st_mode)) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 126 | bb_error_msg("target '%s' is not a directory", dest); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 127 | return -1; |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 128 | } |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 129 | } else { |
Matt Kraai | 218aa37 | 2001-04-30 17:32:43 +0000 | [diff] [blame] | 130 | mode_t mode; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 131 | saved_umask = umask(0); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 132 | |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 133 | mode = source_stat.st_mode; |
Matt Kraai | 0144103 | 2001-04-24 01:30:02 +0000 | [diff] [blame] | 134 | if (!(flags & FILEUTILS_PRESERVE_STATUS)) |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 135 | mode = source_stat.st_mode & ~saved_umask; |
| 136 | mode |= S_IRWXU; |
| 137 | |
| 138 | if (mkdir(dest, mode) < 0) { |
| 139 | umask(saved_umask); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 140 | bb_perror_msg("cannot create directory '%s'", dest); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 141 | return -1; |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 142 | } |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 143 | |
| 144 | umask(saved_umask); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 145 | } |
Matt Kraai | bf0a010 | 2001-06-11 13:58:02 +0000 | [diff] [blame] | 146 | |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 147 | /* Recursively copy files in SOURCE. */ |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 148 | dp = opendir(source); |
| 149 | if (dp == NULL) { |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 150 | status = -1; |
Rob Landley | 2f30932 | 2005-11-01 21:55:14 +0000 | [diff] [blame] | 151 | goto preserve_status; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | while ((d = readdir(dp)) != NULL) { |
| 155 | char *new_source, *new_dest; |
| 156 | |
Glenn L McGrath | 393183d | 2003-05-26 14:07:50 +0000 | [diff] [blame] | 157 | new_source = concat_subpath_file(source, d->d_name); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 158 | if (new_source == NULL) |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 159 | continue; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 160 | new_dest = concat_path_file(dest, d->d_name); |
| 161 | if (copy_file(new_source, new_dest, flags) < 0) |
| 162 | status = -1; |
| 163 | free(new_source); |
| 164 | free(new_dest); |
| 165 | } |
Eric Andersen | a9a220b | 2002-09-17 08:42:21 +0000 | [diff] [blame] | 166 | closedir(dp); |
Matt Kraai | 218aa37 | 2001-04-30 17:32:43 +0000 | [diff] [blame] | 167 | |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 168 | if (!dest_exists |
| 169 | && chmod(dest, source_stat.st_mode & ~saved_umask) < 0 |
| 170 | ) { |
| 171 | bb_perror_msg("cannot change permissions of '%s'", dest); |
Matt Kraai | 218aa37 | 2001-04-30 17:32:43 +0000 | [diff] [blame] | 172 | status = -1; |
| 173 | } |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 174 | |
| 175 | } else if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) { |
| 176 | int (*lf)(const char *oldpath, const char *newpath); |
| 177 | make_links: |
| 178 | // Hmm... maybe |
| 179 | // if (DEREF && MAKE_SOFTLINK) source = realpath(source) ? |
| 180 | // (but realpath returns NULL on dangling symlinks...) |
| 181 | lf = (flags & FILEUTILS_MAKE_SOFTLINK) ? symlink : link; |
| 182 | if (lf(source, dest) < 0) { |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 183 | ovr = ask_and_unlink(dest, flags); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 184 | if (ovr <= 0) |
| 185 | return ovr; |
| 186 | if (lf(source, dest) < 0) { |
| 187 | bb_perror_msg("cannot create link '%s'", dest); |
| 188 | return -1; |
| 189 | } |
| 190 | } |
| 191 | return 0; |
| 192 | |
| 193 | } else if (S_ISREG(source_stat.st_mode) |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 194 | /* Huh? DEREF uses stat, which never returns links! */ |
| 195 | /* || (FLAGS_DEREF && S_ISLNK(source_stat.st_mode)) */ |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 196 | ) { |
Glenn L McGrath | f62ea20 | 2003-12-20 04:38:01 +0000 | [diff] [blame] | 197 | int src_fd; |
| 198 | int dst_fd; |
Rob Landley | c983274 | 2006-06-24 21:27:36 +0000 | [diff] [blame] | 199 | if (ENABLE_FEATURE_PRESERVE_HARDLINKS) { |
Denis Vlasenko | 7510384 | 2007-06-20 14:49:47 +0000 | [diff] [blame] | 200 | char *link_target; |
Matt Kraai | ace02dc | 2001-12-17 15:26:36 +0000 | [diff] [blame] | 201 | |
Denis Vlasenko | 6ef06ee | 2007-03-14 22:06:01 +0000 | [diff] [blame] | 202 | if (!FLAGS_DEREF) { |
Denis Vlasenko | 7510384 | 2007-06-20 14:49:47 +0000 | [diff] [blame] | 203 | link_target = is_in_ino_dev_hashtable(&source_stat); |
| 204 | if (link_target) { |
| 205 | if (link(link_target, dest) < 0) { |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 206 | ovr = ask_and_unlink(dest, flags); |
Denis Vlasenko | 6ef06ee | 2007-03-14 22:06:01 +0000 | [diff] [blame] | 207 | if (ovr <= 0) |
| 208 | return ovr; |
Denis Vlasenko | 7510384 | 2007-06-20 14:49:47 +0000 | [diff] [blame] | 209 | if (link(link_target, dest) < 0) { |
Denis Vlasenko | 6ef06ee | 2007-03-14 22:06:01 +0000 | [diff] [blame] | 210 | bb_perror_msg("cannot create link '%s'", dest); |
| 211 | return -1; |
| 212 | } |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 213 | } |
Denis Vlasenko | 6ef06ee | 2007-03-14 22:06:01 +0000 | [diff] [blame] | 214 | return 0; |
Rob Landley | c983274 | 2006-06-24 21:27:36 +0000 | [diff] [blame] | 215 | } |
Matt Kraai | ace02dc | 2001-12-17 15:26:36 +0000 | [diff] [blame] | 216 | } |
Rob Landley | c983274 | 2006-06-24 21:27:36 +0000 | [diff] [blame] | 217 | add_to_ino_dev_hashtable(&source_stat, dest); |
Matt Kraai | ace02dc | 2001-12-17 15:26:36 +0000 | [diff] [blame] | 218 | } |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 219 | |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 220 | src_fd = open_or_warn(source, O_RDONLY); |
| 221 | if (src_fd < 0) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 222 | return -1; |
Matt Kraai | 14b7c5d | 2001-12-11 16:43:48 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 225 | #if DO_POSIX_CP /* POSIX way (a security problem versus symlink attacks!): */ |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 226 | dst_fd = open(dest, (flags & FILEUTILS_INTERACTIVE) |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 227 | ? O_WRONLY|O_CREAT|O_EXCL |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 228 | : O_WRONLY|O_CREAT|O_TRUNC, source_stat.st_mode); |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 229 | #else /* safe way: */ |
| 230 | dst_fd = open(dest, O_WRONLY|O_CREAT|O_EXCL, source_stat.st_mode); |
| 231 | #endif |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 232 | if (dst_fd == -1) { |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 233 | ovr = ask_and_unlink(dest, flags); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 234 | if (ovr <= 0) { |
Glenn L McGrath | f62ea20 | 2003-12-20 04:38:01 +0000 | [diff] [blame] | 235 | close(src_fd); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 236 | return ovr; |
| 237 | } |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 238 | /* It shouldn't exist. If it exists, do not open (symlink attack?) */ |
Denis Vlasenko | 50f7f44 | 2007-04-11 23:20:53 +0000 | [diff] [blame] | 239 | dst_fd = open3_or_warn(dest, O_WRONLY|O_CREAT|O_EXCL, source_stat.st_mode); |
| 240 | if (dst_fd < 0) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 241 | close(src_fd); |
| 242 | return -1; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 246 | #if ENABLE_SELINUX |
| 247 | if (((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) |
| 248 | || (flags & FILEUTILS_SET_SECURITY_CONTEXT)) |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 249 | && is_selinux_enabled() > 0 |
| 250 | ) { |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 251 | security_context_t con; |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 252 | if (getfscreatecon(&con) == -1) { |
| 253 | bb_perror_msg("getfscreatecon"); |
| 254 | return -1; |
Denis Vlasenko | c86e052 | 2007-03-20 11:30:28 +0000 | [diff] [blame] | 255 | } |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 256 | if (con) { |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 257 | if (setfilecon(dest, con) == -1) { |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 258 | bb_perror_msg("setfilecon:%s,%s", dest, con); |
| 259 | freecon(con); |
| 260 | return -1; |
| 261 | } |
| 262 | freecon(con); |
| 263 | } |
| 264 | } |
| 265 | #endif |
Glenn L McGrath | f62ea20 | 2003-12-20 04:38:01 +0000 | [diff] [blame] | 266 | if (bb_copyfd_eof(src_fd, dst_fd) == -1) |
Matt Kraai | bf0a010 | 2001-06-11 13:58:02 +0000 | [diff] [blame] | 267 | status = -1; |
Glenn L McGrath | f62ea20 | 2003-12-20 04:38:01 +0000 | [diff] [blame] | 268 | if (close(dst_fd) < 0) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 269 | bb_perror_msg("cannot close '%s'", dest); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 270 | status = -1; |
| 271 | } |
Glenn L McGrath | f62ea20 | 2003-12-20 04:38:01 +0000 | [diff] [blame] | 272 | if (close(src_fd) < 0) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 273 | bb_perror_msg("cannot close '%s'", source); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 274 | status = -1; |
| 275 | } |
Eric Andersen | 403a73a | 2002-09-16 09:23:22 +0000 | [diff] [blame] | 276 | |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 277 | } else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) |
| 278 | || S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode) |
| 279 | || S_ISLNK(source_stat.st_mode) |
| 280 | ) { |
| 281 | // We are lazy here, a bit lax with races... |
Paul Fox | 0a92bbf | 2005-07-19 20:47:33 +0000 | [diff] [blame] | 282 | if (dest_exists) { |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 283 | ovr = ask_and_unlink(dest, flags); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 284 | if (ovr <= 0) |
| 285 | return ovr; |
Paul Fox | 0a92bbf | 2005-07-19 20:47:33 +0000 | [diff] [blame] | 286 | } |
Rob Landley | 2f30932 | 2005-11-01 21:55:14 +0000 | [diff] [blame] | 287 | if (S_ISFIFO(source_stat.st_mode)) { |
| 288 | if (mkfifo(dest, source_stat.st_mode) < 0) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 289 | bb_perror_msg("cannot create fifo '%s'", dest); |
Rob Landley | 2f30932 | 2005-11-01 21:55:14 +0000 | [diff] [blame] | 290 | return -1; |
| 291 | } |
| 292 | } else if (S_ISLNK(source_stat.st_mode)) { |
| 293 | char *lpath; |
| 294 | |
Denis Vlasenko | 6ca0444 | 2007-02-11 16:19:28 +0000 | [diff] [blame] | 295 | lpath = xmalloc_readlink_or_warn(source); |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 296 | if (lpath && symlink(lpath, dest) < 0) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 297 | bb_perror_msg("cannot create symlink '%s'", dest); |
| 298 | free(lpath); |
Rob Landley | 2f30932 | 2005-11-01 21:55:14 +0000 | [diff] [blame] | 299 | return -1; |
| 300 | } |
| 301 | free(lpath); |
| 302 | |
| 303 | if (flags & FILEUTILS_PRESERVE_STATUS) |
| 304 | if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0) |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 305 | bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest); |
Rob Landley | 2f30932 | 2005-11-01 21:55:14 +0000 | [diff] [blame] | 306 | |
| 307 | return 0; |
| 308 | |
| 309 | } else { |
| 310 | if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 311 | bb_perror_msg("cannot create '%s'", dest); |
Rob Landley | 2f30932 | 2005-11-01 21:55:14 +0000 | [diff] [blame] | 312 | return -1; |
| 313 | } |
| 314 | } |
Eric Andersen | a9a220b | 2002-09-17 08:42:21 +0000 | [diff] [blame] | 315 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 316 | bb_error_msg("internal error: unrecognized file type"); |
Eric Andersen | a9a220b | 2002-09-17 08:42:21 +0000 | [diff] [blame] | 317 | return -1; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 320 | preserve_status: |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 321 | |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 322 | if (flags & FILEUTILS_PRESERVE_STATUS |
| 323 | /* Cannot happen: */ |
| 324 | /* && !(flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) */ |
| 325 | ) { |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 326 | struct utimbuf times; |
| 327 | |
| 328 | times.actime = source_stat.st_atime; |
| 329 | times.modtime = source_stat.st_mtime; |
| 330 | if (utime(dest, ×) < 0) |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 331 | bb_perror_msg("cannot preserve %s of '%s'", "times", dest); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 332 | if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) { |
| 333 | source_stat.st_mode &= ~(S_ISUID | S_ISGID); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 334 | bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 335 | } |
| 336 | if (chmod(dest, source_stat.st_mode) < 0) |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 337 | bb_perror_msg("cannot preserve %s of '%s'", "permissions", dest); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Matt Kraai | 24abecc | 2001-04-30 16:37:04 +0000 | [diff] [blame] | 340 | return status; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 341 | } |