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