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> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | * |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 21 | */ |
| 22 | |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 23 | #include <sys/types.h> |
| 24 | #include <sys/stat.h> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 25 | #include <unistd.h> |
| 26 | #include <fcntl.h> |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 27 | #include <utime.h> |
| 28 | #include <errno.h> |
| 29 | #include <dirent.h> |
| 30 | #include <stdlib.h> |
Manuel Novoa III | a2949aa | 2001-06-29 18:59:32 +0000 | [diff] [blame] | 31 | #include <string.h> |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 32 | |
Matt Kraai | ace02dc | 2001-12-17 15:26:36 +0000 | [diff] [blame] | 33 | #include "busybox.h" |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 34 | |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 35 | int copy_file(const char *source, const char *dest, int flags) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 36 | { |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 37 | struct stat source_stat; |
| 38 | struct stat dest_stat; |
Eric Andersen | a9a220b | 2002-09-17 08:42:21 +0000 | [diff] [blame] | 39 | int dest_exists = 0; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 40 | int status = 0; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 41 | |
Matt Kraai | 4c557bf | 2001-10-05 01:35:10 +0000 | [diff] [blame] | 42 | if ((!(flags & FILEUTILS_DEREFERENCE) && |
Matt Kraai | 0144103 | 2001-04-24 01:30:02 +0000 | [diff] [blame] | 43 | lstat(source, &source_stat) < 0) || |
Matt Kraai | 4c557bf | 2001-10-05 01:35:10 +0000 | [diff] [blame] | 44 | ((flags & FILEUTILS_DEREFERENCE) && |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 45 | stat(source, &source_stat) < 0)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 46 | bb_perror_msg("%s", source); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 47 | return -1; |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 48 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 49 | |
Eric Andersen | 02b8dfc | 2002-09-16 10:23:38 +0000 | [diff] [blame] | 50 | if (lstat(dest, &dest_stat) < 0) { |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 51 | if (errno != ENOENT) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 52 | bb_perror_msg("unable to stat `%s'", dest); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 53 | return -1; |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 54 | } |
Eric Andersen | a9a220b | 2002-09-17 08:42:21 +0000 | [diff] [blame] | 55 | } else { |
| 56 | if (source_stat.st_dev == dest_stat.st_dev && |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 57 | source_stat.st_ino == dest_stat.st_ino) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 58 | bb_error_msg("`%s' and `%s' are the same file", source, dest); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 59 | return -1; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 60 | } |
Eric Andersen | a9a220b | 2002-09-17 08:42:21 +0000 | [diff] [blame] | 61 | dest_exists = 1; |
| 62 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 63 | |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 64 | if (S_ISDIR(source_stat.st_mode)) { |
| 65 | DIR *dp; |
| 66 | struct dirent *d; |
Matt Kraai | 218aa37 | 2001-04-30 17:32:43 +0000 | [diff] [blame] | 67 | mode_t saved_umask = 0; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 68 | |
Matt Kraai | 0144103 | 2001-04-24 01:30:02 +0000 | [diff] [blame] | 69 | if (!(flags & FILEUTILS_RECUR)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 70 | bb_error_msg("%s: omitting directory", source); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 71 | return -1; |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 72 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 73 | |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 74 | /* Create DEST. */ |
| 75 | if (dest_exists) { |
| 76 | if (!S_ISDIR(dest_stat.st_mode)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 77 | bb_error_msg("`%s' is not a directory", dest); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 78 | return -1; |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 79 | } |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 80 | } else { |
Matt Kraai | 218aa37 | 2001-04-30 17:32:43 +0000 | [diff] [blame] | 81 | mode_t mode; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 82 | saved_umask = umask(0); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 83 | |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 84 | mode = source_stat.st_mode; |
Matt Kraai | 0144103 | 2001-04-24 01:30:02 +0000 | [diff] [blame] | 85 | if (!(flags & FILEUTILS_PRESERVE_STATUS)) |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 86 | mode = source_stat.st_mode & ~saved_umask; |
| 87 | mode |= S_IRWXU; |
| 88 | |
| 89 | if (mkdir(dest, mode) < 0) { |
| 90 | umask(saved_umask); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 91 | bb_perror_msg("cannot create directory `%s'", dest); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 92 | return -1; |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 93 | } |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 94 | |
| 95 | umask(saved_umask); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 96 | } |
Matt Kraai | bf0a010 | 2001-06-11 13:58:02 +0000 | [diff] [blame] | 97 | |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 98 | /* Recursively copy files in SOURCE. */ |
| 99 | if ((dp = opendir(source)) == NULL) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 100 | bb_perror_msg("unable to open directory `%s'", source); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 101 | status = -1; |
| 102 | goto end; |
| 103 | } |
| 104 | |
| 105 | while ((d = readdir(dp)) != NULL) { |
| 106 | char *new_source, *new_dest; |
| 107 | |
Glenn L McGrath | 393183d | 2003-05-26 14:07:50 +0000 | [diff] [blame] | 108 | new_source = concat_subpath_file(source, d->d_name); |
| 109 | if(new_source == NULL) |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 110 | continue; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 111 | new_dest = concat_path_file(dest, d->d_name); |
| 112 | if (copy_file(new_source, new_dest, flags) < 0) |
| 113 | status = -1; |
| 114 | free(new_source); |
| 115 | free(new_dest); |
| 116 | } |
Eric Andersen | a9a220b | 2002-09-17 08:42:21 +0000 | [diff] [blame] | 117 | /* closedir have only EBADF error, but "dp" not changes */ |
| 118 | closedir(dp); |
Matt Kraai | 218aa37 | 2001-04-30 17:32:43 +0000 | [diff] [blame] | 119 | |
| 120 | if (!dest_exists && |
| 121 | chmod(dest, source_stat.st_mode & ~saved_umask) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 122 | bb_perror_msg("unable to change permissions of `%s'", dest); |
Matt Kraai | 218aa37 | 2001-04-30 17:32:43 +0000 | [diff] [blame] | 123 | status = -1; |
| 124 | } |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 125 | } else if (S_ISREG(source_stat.st_mode)) { |
Glenn L McGrath | f62ea20 | 2003-12-20 04:38:01 +0000 | [diff] [blame] | 126 | int src_fd; |
| 127 | int dst_fd; |
Matt Kraai | ace02dc | 2001-12-17 15:26:36 +0000 | [diff] [blame] | 128 | #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS |
| 129 | char *link_name; |
| 130 | |
| 131 | if (!(flags & FILEUTILS_DEREFERENCE) && |
| 132 | is_in_ino_dev_hashtable(&source_stat, &link_name)) { |
| 133 | if (link(link_name, dest) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 134 | bb_perror_msg("unable to link `%s'", dest); |
Matt Kraai | ace02dc | 2001-12-17 15:26:36 +0000 | [diff] [blame] | 135 | return -1; |
| 136 | } |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | #endif |
Glenn L McGrath | f62ea20 | 2003-12-20 04:38:01 +0000 | [diff] [blame] | 141 | src_fd = open(source, O_RDONLY); |
| 142 | if (src_fd == -1) { |
| 143 | bb_perror_msg("unable to open `%s'", source); |
| 144 | return(-1); |
Matt Kraai | 14b7c5d | 2001-12-11 16:43:48 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 147 | if (dest_exists) { |
Matt Kraai | 0144103 | 2001-04-24 01:30:02 +0000 | [diff] [blame] | 148 | if (flags & FILEUTILS_INTERACTIVE) { |
Glenn L McGrath | f62ea20 | 2003-12-20 04:38:01 +0000 | [diff] [blame] | 149 | bb_error_msg("overwrite `%s'? ", dest); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 150 | if (!bb_ask_confirmation()) { |
Glenn L McGrath | f62ea20 | 2003-12-20 04:38:01 +0000 | [diff] [blame] | 151 | close (src_fd); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 152 | return 0; |
Matt Kraai | 14b7c5d | 2001-12-11 16:43:48 +0000 | [diff] [blame] | 153 | } |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 154 | } |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 155 | |
Glenn L McGrath | 447bc2d | 2004-01-11 05:20:59 +0000 | [diff] [blame] | 156 | dst_fd = open(dest, O_WRONLY|O_TRUNC); |
Glenn L McGrath | f62ea20 | 2003-12-20 04:38:01 +0000 | [diff] [blame] | 157 | if (dst_fd == -1) { |
Matt Kraai | 0144103 | 2001-04-24 01:30:02 +0000 | [diff] [blame] | 158 | if (!(flags & FILEUTILS_FORCE)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 159 | bb_perror_msg("unable to open `%s'", dest); |
Glenn L McGrath | f62ea20 | 2003-12-20 04:38:01 +0000 | [diff] [blame] | 160 | close(src_fd); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 161 | return -1; |
| 162 | } |
| 163 | |
| 164 | if (unlink(dest) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 165 | bb_perror_msg("unable to remove `%s'", dest); |
Glenn L McGrath | f62ea20 | 2003-12-20 04:38:01 +0000 | [diff] [blame] | 166 | close(src_fd); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 167 | return -1; |
| 168 | } |
| 169 | |
| 170 | dest_exists = 0; |
| 171 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 172 | } |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 173 | |
| 174 | if (!dest_exists) { |
Glenn L McGrath | f62ea20 | 2003-12-20 04:38:01 +0000 | [diff] [blame] | 175 | dst_fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode); |
| 176 | if (dst_fd == -1) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 177 | bb_perror_msg("unable to open `%s'", dest); |
Glenn L McGrath | f62ea20 | 2003-12-20 04:38:01 +0000 | [diff] [blame] | 178 | close(src_fd); |
| 179 | return(-1); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
Glenn L McGrath | f62ea20 | 2003-12-20 04:38:01 +0000 | [diff] [blame] | 183 | if (bb_copyfd_eof(src_fd, dst_fd) == -1) |
Matt Kraai | bf0a010 | 2001-06-11 13:58:02 +0000 | [diff] [blame] | 184 | status = -1; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 185 | |
Glenn L McGrath | f62ea20 | 2003-12-20 04:38:01 +0000 | [diff] [blame] | 186 | if (close(dst_fd) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 187 | bb_perror_msg("unable to close `%s'", dest); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 188 | status = -1; |
| 189 | } |
| 190 | |
Glenn L McGrath | f62ea20 | 2003-12-20 04:38:01 +0000 | [diff] [blame] | 191 | if (close(src_fd) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 192 | bb_perror_msg("unable to close `%s'", source); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 193 | status = -1; |
| 194 | } |
Eric Andersen | 403a73a | 2002-09-16 09:23:22 +0000 | [diff] [blame] | 195 | } |
Eric Andersen | a9a220b | 2002-09-17 08:42:21 +0000 | [diff] [blame] | 196 | else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) || |
| 197 | S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode) || |
| 198 | S_ISLNK(source_stat.st_mode)) { |
Eric Andersen | 403a73a | 2002-09-16 09:23:22 +0000 | [diff] [blame] | 199 | |
Eric Andersen | a9a220b | 2002-09-17 08:42:21 +0000 | [diff] [blame] | 200 | if (dest_exists && |
| 201 | ((flags & FILEUTILS_FORCE) == 0 || unlink(dest) < 0)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 202 | bb_perror_msg("unable to remove `%s'", dest); |
Eric Andersen | 403a73a | 2002-09-16 09:23:22 +0000 | [diff] [blame] | 203 | return -1; |
Eric Andersen | 403a73a | 2002-09-16 09:23:22 +0000 | [diff] [blame] | 204 | |
Eric Andersen | 403a73a | 2002-09-16 09:23:22 +0000 | [diff] [blame] | 205 | } |
Eric Andersen | a9a220b | 2002-09-17 08:42:21 +0000 | [diff] [blame] | 206 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 207 | bb_error_msg("internal error: unrecognized file type"); |
Eric Andersen | a9a220b | 2002-09-17 08:42:21 +0000 | [diff] [blame] | 208 | return -1; |
Eric Andersen | 403a73a | 2002-09-16 09:23:22 +0000 | [diff] [blame] | 209 | } |
Eric Andersen | a9a220b | 2002-09-17 08:42:21 +0000 | [diff] [blame] | 210 | if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) || |
| 211 | S_ISSOCK(source_stat.st_mode)) { |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 212 | if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 213 | bb_perror_msg("unable to create `%s'", dest); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 214 | return -1; |
| 215 | } |
| 216 | } else if (S_ISFIFO(source_stat.st_mode)) { |
Matt Kraai | 43ca137 | 2001-04-30 16:43:21 +0000 | [diff] [blame] | 217 | if (mkfifo(dest, source_stat.st_mode) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 218 | bb_perror_msg("cannot create fifo `%s'", dest); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 219 | return -1; |
| 220 | } |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 221 | } else if (S_ISLNK(source_stat.st_mode)) { |
Eric Andersen | 403a73a | 2002-09-16 09:23:22 +0000 | [diff] [blame] | 222 | char *lpath; |
| 223 | |
Eric Andersen | 403a73a | 2002-09-16 09:23:22 +0000 | [diff] [blame] | 224 | lpath = xreadlink(source); |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 225 | if (symlink(lpath, dest) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 226 | bb_perror_msg("cannot create symlink `%s'", dest); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 227 | return -1; |
| 228 | } |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 229 | free(lpath); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 230 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 231 | #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) |
Matt Kraai | 0144103 | 2001-04-24 01:30:02 +0000 | [diff] [blame] | 232 | if (flags & FILEUTILS_PRESERVE_STATUS) |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 233 | if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 234 | bb_perror_msg("unable to preserve ownership of `%s'", dest); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 235 | #endif |
Matt Kraai | ace02dc | 2001-12-17 15:26:36 +0000 | [diff] [blame] | 236 | |
| 237 | #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS |
| 238 | add_to_ino_dev_hashtable(&source_stat, dest); |
| 239 | #endif |
| 240 | |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 241 | return 0; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Matt Kraai | ace02dc | 2001-12-17 15:26:36 +0000 | [diff] [blame] | 244 | #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS |
Glenn L McGrath | 1219879 | 2004-04-19 12:28:02 +0000 | [diff] [blame] | 245 | if (! S_ISDIR(source_stat.st_mode)) { |
| 246 | add_to_ino_dev_hashtable(&source_stat, dest); |
| 247 | } |
Matt Kraai | ace02dc | 2001-12-17 15:26:36 +0000 | [diff] [blame] | 248 | #endif |
| 249 | |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 250 | end: |
| 251 | |
Matt Kraai | 0144103 | 2001-04-24 01:30:02 +0000 | [diff] [blame] | 252 | if (flags & FILEUTILS_PRESERVE_STATUS) { |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 253 | struct utimbuf times; |
| 254 | |
| 255 | times.actime = source_stat.st_atime; |
| 256 | times.modtime = source_stat.st_mtime; |
| 257 | if (utime(dest, ×) < 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 258 | bb_perror_msg("unable to preserve times of `%s'", dest); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 259 | if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) { |
| 260 | source_stat.st_mode &= ~(S_ISUID | S_ISGID); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 261 | bb_perror_msg("unable to preserve ownership of `%s'", dest); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 262 | } |
| 263 | if (chmod(dest, source_stat.st_mode) < 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 264 | bb_perror_msg("unable to preserve permissions of `%s'", dest); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Matt Kraai | 24abecc | 2001-04-30 16:37:04 +0000 | [diff] [blame] | 267 | return status; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 268 | } |