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; |
| 39 | int dest_exists = 1; |
| 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)) { |
| 46 | perror_msg("%s", source); |
| 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 | |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 50 | if (stat(dest, &dest_stat) < 0) { |
| 51 | if (errno != ENOENT) { |
| 52 | perror_msg("unable to stat `%s'", dest); |
| 53 | return -1; |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 54 | } |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 55 | dest_exists = 0; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Matt Kraai | 9ef2ea2 | 2002-06-11 13:25:26 +0000 | [diff] [blame^] | 58 | if (dest_exists && source_stat.st_dev == dest_stat.st_dev && |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 59 | source_stat.st_ino == dest_stat.st_ino) { |
| 60 | error_msg("`%s' and `%s' are the same file", source, dest); |
| 61 | return -1; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 62 | } |
| 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)) { |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 70 | error_msg("%s: omitting directory", source); |
| 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)) { |
| 77 | error_msg("`%s' is not a directory", dest); |
| 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); |
| 91 | perror_msg("cannot create directory `%s'", dest); |
| 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) { |
| 100 | perror_msg("unable to open directory `%s'", source); |
| 101 | status = -1; |
| 102 | goto end; |
| 103 | } |
| 104 | |
| 105 | while ((d = readdir(dp)) != NULL) { |
| 106 | char *new_source, *new_dest; |
| 107 | |
| 108 | if (strcmp(d->d_name, ".") == 0 || |
| 109 | strcmp(d->d_name, "..") == 0) |
| 110 | continue; |
| 111 | |
| 112 | new_source = concat_path_file(source, d->d_name); |
| 113 | new_dest = concat_path_file(dest, d->d_name); |
| 114 | if (copy_file(new_source, new_dest, flags) < 0) |
| 115 | status = -1; |
| 116 | free(new_source); |
| 117 | free(new_dest); |
| 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 | /* ??? What if an error occurs in readdir? */ |
| 121 | |
| 122 | if (closedir(dp) < 0) { |
| 123 | perror_msg("unable to close directory `%s'", source); |
| 124 | status = -1; |
| 125 | } |
Matt Kraai | 218aa37 | 2001-04-30 17:32:43 +0000 | [diff] [blame] | 126 | |
| 127 | if (!dest_exists && |
| 128 | chmod(dest, source_stat.st_mode & ~saved_umask) < 0) { |
| 129 | perror_msg("unable to change permissions of `%s'", dest); |
| 130 | status = -1; |
| 131 | } |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 132 | } else if (S_ISREG(source_stat.st_mode)) { |
Eric Andersen | 75220b5 | 2001-08-21 23:36:32 +0000 | [diff] [blame] | 133 | FILE *sfp, *dfp=NULL; |
Matt Kraai | ace02dc | 2001-12-17 15:26:36 +0000 | [diff] [blame] | 134 | #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS |
| 135 | char *link_name; |
| 136 | |
| 137 | if (!(flags & FILEUTILS_DEREFERENCE) && |
| 138 | is_in_ino_dev_hashtable(&source_stat, &link_name)) { |
| 139 | if (link(link_name, dest) < 0) { |
| 140 | perror_msg("unable to link `%s'", dest); |
| 141 | return -1; |
| 142 | } |
| 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | #endif |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 147 | |
Matt Kraai | 14b7c5d | 2001-12-11 16:43:48 +0000 | [diff] [blame] | 148 | if ((sfp = fopen(source, "r")) == NULL) { |
| 149 | perror_msg("unable to open `%s'", source); |
| 150 | return -1; |
| 151 | } |
| 152 | |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 153 | if (dest_exists) { |
Matt Kraai | 0144103 | 2001-04-24 01:30:02 +0000 | [diff] [blame] | 154 | if (flags & FILEUTILS_INTERACTIVE) { |
Matt Kraai | 9ff9325 | 2001-04-24 01:12:33 +0000 | [diff] [blame] | 155 | fprintf(stderr, "%s: overwrite `%s'? ", applet_name, dest); |
Matt Kraai | 14b7c5d | 2001-12-11 16:43:48 +0000 | [diff] [blame] | 156 | if (!ask_confirmation()) { |
| 157 | fclose (sfp); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 158 | return 0; |
Matt Kraai | 14b7c5d | 2001-12-11 16:43:48 +0000 | [diff] [blame] | 159 | } |
Glenn L McGrath | 4949faf | 2001-04-11 16:23:35 +0000 | [diff] [blame] | 160 | } |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 161 | |
| 162 | if ((dfp = fopen(dest, "w")) == NULL) { |
Matt Kraai | 0144103 | 2001-04-24 01:30:02 +0000 | [diff] [blame] | 163 | if (!(flags & FILEUTILS_FORCE)) { |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 164 | perror_msg("unable to open `%s'", dest); |
Matt Kraai | 14b7c5d | 2001-12-11 16:43:48 +0000 | [diff] [blame] | 165 | fclose (sfp); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 166 | return -1; |
| 167 | } |
| 168 | |
| 169 | if (unlink(dest) < 0) { |
| 170 | perror_msg("unable to remove `%s'", dest); |
Matt Kraai | 14b7c5d | 2001-12-11 16:43:48 +0000 | [diff] [blame] | 171 | fclose (sfp); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 172 | return -1; |
| 173 | } |
| 174 | |
| 175 | dest_exists = 0; |
| 176 | } |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 177 | } |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 178 | |
| 179 | if (!dest_exists) { |
| 180 | int fd; |
| 181 | |
| 182 | if ((fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode)) < 0 || |
| 183 | (dfp = fdopen(fd, "w")) == NULL) { |
| 184 | if (fd >= 0) |
| 185 | close(fd); |
| 186 | perror_msg("unable to open `%s'", dest); |
Matt Kraai | 14b7c5d | 2001-12-11 16:43:48 +0000 | [diff] [blame] | 187 | fclose (sfp); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 188 | return -1; |
| 189 | } |
| 190 | } |
| 191 | |
Matt Kraai | bf0a010 | 2001-06-11 13:58:02 +0000 | [diff] [blame] | 192 | if (copy_file_chunk(sfp, dfp, -1) < 0) |
| 193 | status = -1; |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 194 | |
| 195 | if (fclose(dfp) < 0) { |
| 196 | perror_msg("unable to close `%s'", dest); |
| 197 | status = -1; |
| 198 | } |
| 199 | |
| 200 | if (fclose(sfp) < 0) { |
| 201 | perror_msg("unable to close `%s'", source); |
| 202 | status = -1; |
| 203 | } |
| 204 | } else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) || |
| 205 | S_ISSOCK(source_stat.st_mode)) { |
| 206 | if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) { |
| 207 | perror_msg("unable to create `%s'", dest); |
| 208 | return -1; |
| 209 | } |
| 210 | } else if (S_ISFIFO(source_stat.st_mode)) { |
Matt Kraai | 43ca137 | 2001-04-30 16:43:21 +0000 | [diff] [blame] | 211 | if (mkfifo(dest, source_stat.st_mode) < 0) { |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 212 | perror_msg("cannot create fifo `%s'", dest); |
| 213 | return -1; |
| 214 | } |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 215 | } else if (S_ISLNK(source_stat.st_mode)) { |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 216 | char *lpath = xreadlink(source); |
| 217 | if (symlink(lpath, dest) < 0) { |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 218 | perror_msg("cannot create symlink `%s'", dest); |
| 219 | return -1; |
| 220 | } |
Mark Whitley | 8a63326 | 2001-04-30 18:17:00 +0000 | [diff] [blame] | 221 | free(lpath); |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 222 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 223 | #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) |
Matt Kraai | 0144103 | 2001-04-24 01:30:02 +0000 | [diff] [blame] | 224 | if (flags & FILEUTILS_PRESERVE_STATUS) |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 225 | if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0) |
| 226 | perror_msg("unable to preserve ownership of `%s'", dest); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 227 | #endif |
Matt Kraai | ace02dc | 2001-12-17 15:26:36 +0000 | [diff] [blame] | 228 | |
| 229 | #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS |
| 230 | add_to_ino_dev_hashtable(&source_stat, dest); |
| 231 | #endif |
| 232 | |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 233 | return 0; |
| 234 | } else { |
| 235 | error_msg("internal error: unrecognized file type"); |
| 236 | return -1; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Matt Kraai | ace02dc | 2001-12-17 15:26:36 +0000 | [diff] [blame] | 239 | #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS |
| 240 | add_to_ino_dev_hashtable(&source_stat, dest); |
| 241 | #endif |
| 242 | |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 243 | end: |
| 244 | |
Matt Kraai | 0144103 | 2001-04-24 01:30:02 +0000 | [diff] [blame] | 245 | if (flags & FILEUTILS_PRESERVE_STATUS) { |
Matt Kraai | 91b2855 | 2001-04-23 18:53:07 +0000 | [diff] [blame] | 246 | struct utimbuf times; |
| 247 | |
| 248 | times.actime = source_stat.st_atime; |
| 249 | times.modtime = source_stat.st_mtime; |
| 250 | if (utime(dest, ×) < 0) |
| 251 | perror_msg("unable to preserve times of `%s'", dest); |
| 252 | if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) { |
| 253 | source_stat.st_mode &= ~(S_ISUID | S_ISGID); |
| 254 | perror_msg("unable to preserve ownership of `%s'", dest); |
| 255 | } |
| 256 | if (chmod(dest, source_stat.st_mode) < 0) |
| 257 | perror_msg("unable to preserve permissions of `%s'", dest); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Matt Kraai | 24abecc | 2001-04-30 16:37:04 +0000 | [diff] [blame] | 260 | return status; |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 261 | } |