blob: 0120d0b16b988068c40c1abccc7c1909a5369a4d [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
Matt Kraai91b28552001-04-23 18:53:07 +00003 * Mini copy_file implementation for busybox
Eric Andersenaad1a882001-03-16 22:47:14 +00004 *
Matt Kraai91b28552001-04-23 18:53:07 +00005 * Copyright (C) 2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
Eric Andersenaad1a882001-03-16 22:47:14 +00006 *
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 Andersenaad1a882001-03-16 22:47:14 +000021 */
22
Matt Kraai91b28552001-04-23 18:53:07 +000023#include <sys/types.h>
24#include <sys/stat.h>
Eric Andersenaad1a882001-03-16 22:47:14 +000025#include <unistd.h>
26#include <fcntl.h>
Matt Kraai91b28552001-04-23 18:53:07 +000027#include <utime.h>
28#include <errno.h>
29#include <dirent.h>
30#include <stdlib.h>
Manuel Novoa III a2949aa2001-06-29 18:59:32 +000031#include <string.h>
Matt Kraai91b28552001-04-23 18:53:07 +000032
Matt Kraaiace02dc2001-12-17 15:26:36 +000033#include "busybox.h"
Eric Andersenaad1a882001-03-16 22:47:14 +000034
Matt Kraai91b28552001-04-23 18:53:07 +000035int copy_file(const char *source, const char *dest, int flags)
Eric Andersenaad1a882001-03-16 22:47:14 +000036{
Matt Kraai91b28552001-04-23 18:53:07 +000037 struct stat source_stat;
38 struct stat dest_stat;
Eric Andersena9a220b2002-09-17 08:42:21 +000039 int dest_exists = 0;
Matt Kraai91b28552001-04-23 18:53:07 +000040 int status = 0;
Eric Andersenaad1a882001-03-16 22:47:14 +000041
Matt Kraai4c557bf2001-10-05 01:35:10 +000042 if ((!(flags & FILEUTILS_DEREFERENCE) &&
Matt Kraai01441032001-04-24 01:30:02 +000043 lstat(source, &source_stat) < 0) ||
Matt Kraai4c557bf2001-10-05 01:35:10 +000044 ((flags & FILEUTILS_DEREFERENCE) &&
Matt Kraai91b28552001-04-23 18:53:07 +000045 stat(source, &source_stat) < 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000046 bb_perror_msg("%s", source);
Matt Kraai91b28552001-04-23 18:53:07 +000047 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +000048 }
Eric Andersenaad1a882001-03-16 22:47:14 +000049
Eric Andersen02b8dfc2002-09-16 10:23:38 +000050 if (lstat(dest, &dest_stat) < 0) {
Matt Kraai91b28552001-04-23 18:53:07 +000051 if (errno != ENOENT) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000052 bb_perror_msg("unable to stat `%s'", dest);
Matt Kraai91b28552001-04-23 18:53:07 +000053 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +000054 }
Eric Andersena9a220b2002-09-17 08:42:21 +000055 } else {
56 if (source_stat.st_dev == dest_stat.st_dev &&
Mike Frysinger2ed05ab2005-04-14 02:52:50 +000057 source_stat.st_ino == dest_stat.st_ino)
58 {
59 bb_error_msg("`%s' and `%s' are the same file", source, dest);
60 return -1;
61 }
Eric Andersena9a220b2002-09-17 08:42:21 +000062 dest_exists = 1;
63 }
Eric Andersenaad1a882001-03-16 22:47:14 +000064
Matt Kraai91b28552001-04-23 18:53:07 +000065 if (S_ISDIR(source_stat.st_mode)) {
66 DIR *dp;
67 struct dirent *d;
Matt Kraai218aa372001-04-30 17:32:43 +000068 mode_t saved_umask = 0;
Matt Kraai91b28552001-04-23 18:53:07 +000069
Matt Kraai01441032001-04-24 01:30:02 +000070 if (!(flags & FILEUTILS_RECUR)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000071 bb_error_msg("%s: omitting directory", source);
Matt Kraai91b28552001-04-23 18:53:07 +000072 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +000073 }
Eric Andersenaad1a882001-03-16 22:47:14 +000074
Matt Kraai91b28552001-04-23 18:53:07 +000075 /* Create DEST. */
76 if (dest_exists) {
77 if (!S_ISDIR(dest_stat.st_mode)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000078 bb_error_msg("`%s' is not a directory", dest);
Matt Kraai91b28552001-04-23 18:53:07 +000079 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +000080 }
Matt Kraai91b28552001-04-23 18:53:07 +000081 } else {
Matt Kraai218aa372001-04-30 17:32:43 +000082 mode_t mode;
Matt Kraai91b28552001-04-23 18:53:07 +000083 saved_umask = umask(0);
Eric Andersenaad1a882001-03-16 22:47:14 +000084
Matt Kraai91b28552001-04-23 18:53:07 +000085 mode = source_stat.st_mode;
Matt Kraai01441032001-04-24 01:30:02 +000086 if (!(flags & FILEUTILS_PRESERVE_STATUS))
Matt Kraai91b28552001-04-23 18:53:07 +000087 mode = source_stat.st_mode & ~saved_umask;
88 mode |= S_IRWXU;
89
90 if (mkdir(dest, mode) < 0) {
91 umask(saved_umask);
Manuel Novoa III cad53642003-03-19 09:13:01 +000092 bb_perror_msg("cannot create directory `%s'", dest);
Matt Kraai91b28552001-04-23 18:53:07 +000093 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +000094 }
Matt Kraai91b28552001-04-23 18:53:07 +000095
96 umask(saved_umask);
Eric Andersenaad1a882001-03-16 22:47:14 +000097 }
Matt Kraaibf0a0102001-06-11 13:58:02 +000098
Matt Kraai91b28552001-04-23 18:53:07 +000099 /* Recursively copy files in SOURCE. */
100 if ((dp = opendir(source)) == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000101 bb_perror_msg("unable to open directory `%s'", source);
Matt Kraai91b28552001-04-23 18:53:07 +0000102 status = -1;
103 goto end;
104 }
105
106 while ((d = readdir(dp)) != NULL) {
107 char *new_source, *new_dest;
108
Glenn L McGrath393183d2003-05-26 14:07:50 +0000109 new_source = concat_subpath_file(source, d->d_name);
110 if(new_source == NULL)
Matt Kraai91b28552001-04-23 18:53:07 +0000111 continue;
Matt Kraai91b28552001-04-23 18:53:07 +0000112 new_dest = concat_path_file(dest, d->d_name);
113 if (copy_file(new_source, new_dest, flags) < 0)
114 status = -1;
115 free(new_source);
116 free(new_dest);
117 }
Eric Andersena9a220b2002-09-17 08:42:21 +0000118 /* closedir have only EBADF error, but "dp" not changes */
119 closedir(dp);
Matt Kraai218aa372001-04-30 17:32:43 +0000120
121 if (!dest_exists &&
122 chmod(dest, source_stat.st_mode & ~saved_umask) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000123 bb_perror_msg("unable to change permissions of `%s'", dest);
Matt Kraai218aa372001-04-30 17:32:43 +0000124 status = -1;
125 }
Matt Kraai91b28552001-04-23 18:53:07 +0000126 } else if (S_ISREG(source_stat.st_mode)) {
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000127 int src_fd;
128 int dst_fd;
Matt Kraaiace02dc2001-12-17 15:26:36 +0000129#ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS
130 char *link_name;
131
132 if (!(flags & FILEUTILS_DEREFERENCE) &&
133 is_in_ino_dev_hashtable(&source_stat, &link_name)) {
134 if (link(link_name, dest) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000135 bb_perror_msg("unable to link `%s'", dest);
Matt Kraaiace02dc2001-12-17 15:26:36 +0000136 return -1;
137 }
138
139 return 0;
140 }
141#endif
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000142 src_fd = open(source, O_RDONLY);
143 if (src_fd == -1) {
144 bb_perror_msg("unable to open `%s'", source);
145 return(-1);
Matt Kraai14b7c5d2001-12-11 16:43:48 +0000146 }
147
Matt Kraai91b28552001-04-23 18:53:07 +0000148 if (dest_exists) {
Matt Kraai01441032001-04-24 01:30:02 +0000149 if (flags & FILEUTILS_INTERACTIVE) {
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000150 bb_error_msg("overwrite `%s'? ", dest);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000151 if (!bb_ask_confirmation()) {
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000152 close (src_fd);
Matt Kraai91b28552001-04-23 18:53:07 +0000153 return 0;
Matt Kraai14b7c5d2001-12-11 16:43:48 +0000154 }
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000155 }
Matt Kraai91b28552001-04-23 18:53:07 +0000156
Glenn L McGrath447bc2d2004-01-11 05:20:59 +0000157 dst_fd = open(dest, O_WRONLY|O_TRUNC);
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000158 if (dst_fd == -1) {
Matt Kraai01441032001-04-24 01:30:02 +0000159 if (!(flags & FILEUTILS_FORCE)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000160 bb_perror_msg("unable to open `%s'", dest);
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000161 close(src_fd);
Matt Kraai91b28552001-04-23 18:53:07 +0000162 return -1;
163 }
164
165 if (unlink(dest) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000166 bb_perror_msg("unable to remove `%s'", dest);
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000167 close(src_fd);
Matt Kraai91b28552001-04-23 18:53:07 +0000168 return -1;
169 }
170
171 dest_exists = 0;
172 }
Eric Andersenaad1a882001-03-16 22:47:14 +0000173 }
Matt Kraai91b28552001-04-23 18:53:07 +0000174
175 if (!dest_exists) {
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000176 dst_fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode);
177 if (dst_fd == -1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000178 bb_perror_msg("unable to open `%s'", dest);
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000179 close(src_fd);
180 return(-1);
Matt Kraai91b28552001-04-23 18:53:07 +0000181 }
182 }
183
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000184 if (bb_copyfd_eof(src_fd, dst_fd) == -1)
Matt Kraaibf0a0102001-06-11 13:58:02 +0000185 status = -1;
Matt Kraai91b28552001-04-23 18:53:07 +0000186
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000187 if (close(dst_fd) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000188 bb_perror_msg("unable to close `%s'", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000189 status = -1;
190 }
191
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000192 if (close(src_fd) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000193 bb_perror_msg("unable to close `%s'", source);
Matt Kraai91b28552001-04-23 18:53:07 +0000194 status = -1;
195 }
Eric Andersen403a73a2002-09-16 09:23:22 +0000196 }
Eric Andersena9a220b2002-09-17 08:42:21 +0000197 else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) ||
198 S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode) ||
199 S_ISLNK(source_stat.st_mode)) {
Eric Andersen403a73a2002-09-16 09:23:22 +0000200
Eric Andersena9a220b2002-09-17 08:42:21 +0000201 if (dest_exists &&
202 ((flags & FILEUTILS_FORCE) == 0 || unlink(dest) < 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000203 bb_perror_msg("unable to remove `%s'", dest);
Eric Andersen403a73a2002-09-16 09:23:22 +0000204 return -1;
Eric Andersen403a73a2002-09-16 09:23:22 +0000205
Eric Andersen403a73a2002-09-16 09:23:22 +0000206 }
Eric Andersena9a220b2002-09-17 08:42:21 +0000207 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000208 bb_error_msg("internal error: unrecognized file type");
Eric Andersena9a220b2002-09-17 08:42:21 +0000209 return -1;
Eric Andersen403a73a2002-09-16 09:23:22 +0000210 }
Eric Andersena9a220b2002-09-17 08:42:21 +0000211 if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) ||
212 S_ISSOCK(source_stat.st_mode)) {
Matt Kraai91b28552001-04-23 18:53:07 +0000213 if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000214 bb_perror_msg("unable to create `%s'", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000215 return -1;
216 }
217 } else if (S_ISFIFO(source_stat.st_mode)) {
Matt Kraai43ca1372001-04-30 16:43:21 +0000218 if (mkfifo(dest, source_stat.st_mode) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000219 bb_perror_msg("cannot create fifo `%s'", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000220 return -1;
221 }
Matt Kraai91b28552001-04-23 18:53:07 +0000222 } else if (S_ISLNK(source_stat.st_mode)) {
Eric Andersen403a73a2002-09-16 09:23:22 +0000223 char *lpath;
224
Eric Andersen403a73a2002-09-16 09:23:22 +0000225 lpath = xreadlink(source);
Mark Whitley8a633262001-04-30 18:17:00 +0000226 if (symlink(lpath, dest) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000227 bb_perror_msg("cannot create symlink `%s'", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000228 return -1;
229 }
Mark Whitley8a633262001-04-30 18:17:00 +0000230 free(lpath);
Matt Kraai91b28552001-04-23 18:53:07 +0000231
Eric Andersenaad1a882001-03-16 22:47:14 +0000232#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Matt Kraai01441032001-04-24 01:30:02 +0000233 if (flags & FILEUTILS_PRESERVE_STATUS)
Matt Kraai91b28552001-04-23 18:53:07 +0000234 if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000235 bb_perror_msg("unable to preserve ownership of `%s'", dest);
Eric Andersenaad1a882001-03-16 22:47:14 +0000236#endif
Matt Kraaiace02dc2001-12-17 15:26:36 +0000237
238#ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS
239 add_to_ino_dev_hashtable(&source_stat, dest);
240#endif
241
Matt Kraai91b28552001-04-23 18:53:07 +0000242 return 0;
Eric Andersenaad1a882001-03-16 22:47:14 +0000243 }
244
Matt Kraaiace02dc2001-12-17 15:26:36 +0000245#ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS
Glenn L McGrath12198792004-04-19 12:28:02 +0000246 if (! S_ISDIR(source_stat.st_mode)) {
247 add_to_ino_dev_hashtable(&source_stat, dest);
248 }
Matt Kraaiace02dc2001-12-17 15:26:36 +0000249#endif
250
Matt Kraai91b28552001-04-23 18:53:07 +0000251end:
252
Matt Kraai01441032001-04-24 01:30:02 +0000253 if (flags & FILEUTILS_PRESERVE_STATUS) {
Matt Kraai91b28552001-04-23 18:53:07 +0000254 struct utimbuf times;
255
256 times.actime = source_stat.st_atime;
257 times.modtime = source_stat.st_mtime;
258 if (utime(dest, &times) < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000259 bb_perror_msg("unable to preserve times of `%s'", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000260 if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
261 source_stat.st_mode &= ~(S_ISUID | S_ISGID);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000262 bb_perror_msg("unable to preserve ownership of `%s'", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000263 }
264 if (chmod(dest, source_stat.st_mode) < 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000265 bb_perror_msg("unable to preserve permissions of `%s'", dest);
Eric Andersenaad1a882001-03-16 22:47:14 +0000266 }
267
Matt Kraai24abecc2001-04-30 16:37:04 +0000268 return status;
Eric Andersenaad1a882001-03-16 22:47:14 +0000269}