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