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) { |
| 200 | char *link_name; |
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) { |
| 203 | link_name = is_in_ino_dev_hashtable(&source_stat); |
| 204 | if (link_name) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 205 | if (link(link_name, 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; |
| 209 | if (link(link_name, dest) < 0) { |
| 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 | |
Glenn L McGrath | f62ea20 | 2003-12-20 04:38:01 +0000 | [diff] [blame] | 220 | src_fd = open(source, O_RDONLY); |
| 221 | if (src_fd == -1) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 222 | bb_perror_msg("cannot open '%s'", source); |
| 223 | return -1; |
Matt Kraai | 14b7c5d | 2001-12-11 16:43:48 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 226 | #if DO_POSIX_CP /* POSIX way (a security problem versus symlink attacks!): */ |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 227 | dst_fd = open(dest, (flags & FILEUTILS_INTERACTIVE) |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 228 | ? O_WRONLY|O_CREAT|O_EXCL |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 229 | : O_WRONLY|O_CREAT|O_TRUNC, source_stat.st_mode); |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 230 | #else /* safe way: */ |
| 231 | dst_fd = open(dest, O_WRONLY|O_CREAT|O_EXCL, source_stat.st_mode); |
| 232 | #endif |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 233 | if (dst_fd == -1) { |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 234 | ovr = ask_and_unlink(dest, flags); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 235 | if (ovr <= 0) { |
Glenn L McGrath | f62ea20 | 2003-12-20 04:38:01 +0000 | [diff] [blame] | 236 | close(src_fd); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 237 | return ovr; |
| 238 | } |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 239 | /* It shouldn't exist. If it exists, do not open (symlink attack?) */ |
| 240 | dst_fd = open(dest, O_WRONLY|O_CREAT|O_EXCL, source_stat.st_mode); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 241 | if (dst_fd == -1) { |
| 242 | bb_perror_msg("cannot open '%s'", dest); |
| 243 | close(src_fd); |
| 244 | return -1; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 248 | #if ENABLE_SELINUX |
| 249 | if (((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) |
| 250 | || (flags & FILEUTILS_SET_SECURITY_CONTEXT)) |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 251 | && is_selinux_enabled() > 0 |
| 252 | ) { |
Denis Vlasenko | 49622d7 | 2007-03-10 16:58:49 +0000 | [diff] [blame] | 253 | security_context_t con; |
| 254 | if (getfscreatecon(&con) == -1) { |
| 255 | bb_perror_msg("getfscreatecon"); |
| 256 | return -1; |
| 257 | } |
| 258 | if (con) { |
| 259 | if(setfilecon(dest, con) == -1) { |
| 260 | bb_perror_msg("setfilecon:%s,%s", dest, con); |
| 261 | freecon(con); |
| 262 | return -1; |
| 263 | } |
| 264 | freecon(con); |
| 265 | } |
| 266 | } |
| 267 | #endif |
Glenn L McGrath | f62ea20 | 2003-12-20 04:38:01 +0000 | [diff] [blame] | 268 | if (bb_copyfd_eof(src_fd, dst_fd) == -1) |
Matt Kraai | bf0a010 | 2001-06-11 13:58:02 +0000 | [diff] [blame] | 269 | status = -1; |
Glenn L McGrath | f62ea20 | 2003-12-20 04:38:01 +0000 | [diff] [blame] | 270 | if (close(dst_fd) < 0) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 271 | bb_perror_msg("cannot close '%s'", dest); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 272 | status = -1; |
| 273 | } |
Glenn L McGrath | f62ea20 | 2003-12-20 04:38:01 +0000 | [diff] [blame] | 274 | if (close(src_fd) < 0) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 275 | bb_perror_msg("cannot close '%s'", source); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 276 | status = -1; |
| 277 | } |
Eric Andersen | 403a73a | 2002-09-16 09:23:22 +0000 | [diff] [blame] | 278 | |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 279 | } else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) |
| 280 | || S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode) |
| 281 | || S_ISLNK(source_stat.st_mode) |
| 282 | ) { |
| 283 | // We are lazy here, a bit lax with races... |
Paul Fox | 0a92bbf | 2005-07-19 20:47:33 +0000 | [diff] [blame] | 284 | if (dest_exists) { |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 285 | ovr = ask_and_unlink(dest, flags); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 286 | if (ovr <= 0) |
| 287 | return ovr; |
Paul Fox | 0a92bbf | 2005-07-19 20:47:33 +0000 | [diff] [blame] | 288 | } |
Rob Landley | 2f30932 | 2005-11-01 21:55:14 +0000 | [diff] [blame] | 289 | if (S_ISFIFO(source_stat.st_mode)) { |
| 290 | if (mkfifo(dest, source_stat.st_mode) < 0) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 291 | bb_perror_msg("cannot create fifo '%s'", dest); |
Rob Landley | 2f30932 | 2005-11-01 21:55:14 +0000 | [diff] [blame] | 292 | return -1; |
| 293 | } |
| 294 | } else if (S_ISLNK(source_stat.st_mode)) { |
| 295 | char *lpath; |
| 296 | |
Denis Vlasenko | 6ca0444 | 2007-02-11 16:19:28 +0000 | [diff] [blame] | 297 | lpath = xmalloc_readlink_or_warn(source); |
Denis Vlasenko | 54d14ca | 2007-03-15 13:33:37 +0000 | [diff] [blame] | 298 | if (lpath && symlink(lpath, dest) < 0) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 299 | bb_perror_msg("cannot create symlink '%s'", dest); |
| 300 | free(lpath); |
Rob Landley | 2f30932 | 2005-11-01 21:55:14 +0000 | [diff] [blame] | 301 | return -1; |
| 302 | } |
| 303 | free(lpath); |
| 304 | |
| 305 | if (flags & FILEUTILS_PRESERVE_STATUS) |
| 306 | if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0) |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 307 | bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest); |
Rob Landley | 2f30932 | 2005-11-01 21:55:14 +0000 | [diff] [blame] | 308 | |
| 309 | return 0; |
| 310 | |
| 311 | } else { |
| 312 | if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) { |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 313 | bb_perror_msg("cannot create '%s'", dest); |
Rob Landley | 2f30932 | 2005-11-01 21:55:14 +0000 | [diff] [blame] | 314 | return -1; |
| 315 | } |
| 316 | } |
Eric Andersen | a9a220b | 2002-09-17 08:42:21 +0000 | [diff] [blame] | 317 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 318 | bb_error_msg("internal error: unrecognized file type"); |
Eric Andersen | a9a220b | 2002-09-17 08:42:21 +0000 | [diff] [blame] | 319 | return -1; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 322 | preserve_status: |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 323 | |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 324 | if (flags & FILEUTILS_PRESERVE_STATUS |
| 325 | /* Cannot happen: */ |
| 326 | /* && !(flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) */ |
| 327 | ) { |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 328 | struct utimbuf times; |
| 329 | |
| 330 | times.actime = source_stat.st_atime; |
| 331 | times.modtime = source_stat.st_mtime; |
| 332 | if (utime(dest, ×) < 0) |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 333 | bb_perror_msg("cannot preserve %s of '%s'", "times", dest); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 334 | if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) { |
| 335 | source_stat.st_mode &= ~(S_ISUID | S_ISGID); |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 336 | bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 337 | } |
| 338 | if (chmod(dest, source_stat.st_mode) < 0) |
Denis Vlasenko | f24e1f4 | 2006-10-21 23:40:20 +0000 | [diff] [blame] | 339 | bb_perror_msg("cannot preserve %s of '%s'", "permissions", dest); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Matt Kraai | 24abecc | 2001-04-30 16:37:04 +0000 | [diff] [blame] | 342 | return status; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 343 | } |