blob: 5f667cf4f79d9683c33c2509faf3d99152b9e05a [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)) {
46 perror_msg("%s", source);
47 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) {
52 perror_msg("unable to stat `%s'", dest);
53 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 &&
Matt Kraai91b28552001-04-23 18:53:07 +000057 source_stat.st_ino == dest_stat.st_ino) {
58 error_msg("`%s' and `%s' are the same file", source, dest);
59 return -1;
Eric Andersenaad1a882001-03-16 22:47:14 +000060 }
Eric Andersena9a220b2002-09-17 08:42:21 +000061 dest_exists = 1;
62 }
Eric Andersenaad1a882001-03-16 22:47:14 +000063
Matt Kraai91b28552001-04-23 18:53:07 +000064 if (S_ISDIR(source_stat.st_mode)) {
65 DIR *dp;
66 struct dirent *d;
Matt Kraai218aa372001-04-30 17:32:43 +000067 mode_t saved_umask = 0;
Matt Kraai91b28552001-04-23 18:53:07 +000068
Matt Kraai01441032001-04-24 01:30:02 +000069 if (!(flags & FILEUTILS_RECUR)) {
Matt Kraai91b28552001-04-23 18:53:07 +000070 error_msg("%s: omitting directory", source);
71 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +000072 }
Eric Andersenaad1a882001-03-16 22:47:14 +000073
Matt Kraai91b28552001-04-23 18:53:07 +000074 /* 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 McGrath4949faf2001-04-11 16:23:35 +000079 }
Matt Kraai91b28552001-04-23 18:53:07 +000080 } else {
Matt Kraai218aa372001-04-30 17:32:43 +000081 mode_t mode;
Matt Kraai91b28552001-04-23 18:53:07 +000082 saved_umask = umask(0);
Eric Andersenaad1a882001-03-16 22:47:14 +000083
Matt Kraai91b28552001-04-23 18:53:07 +000084 mode = source_stat.st_mode;
Matt Kraai01441032001-04-24 01:30:02 +000085 if (!(flags & FILEUTILS_PRESERVE_STATUS))
Matt Kraai91b28552001-04-23 18:53:07 +000086 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 McGrath4949faf2001-04-11 16:23:35 +000093 }
Matt Kraai91b28552001-04-23 18:53:07 +000094
95 umask(saved_umask);
Eric Andersenaad1a882001-03-16 22:47:14 +000096 }
Matt Kraaibf0a0102001-06-11 13:58:02 +000097
Matt Kraai91b28552001-04-23 18:53:07 +000098 /* 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 }
Eric Andersena9a220b2002-09-17 08:42:21 +0000119 /* closedir have only EBADF error, but "dp" not changes */
120 closedir(dp);
Matt Kraai218aa372001-04-30 17:32:43 +0000121
122 if (!dest_exists &&
123 chmod(dest, source_stat.st_mode & ~saved_umask) < 0) {
124 perror_msg("unable to change permissions of `%s'", dest);
125 status = -1;
126 }
Matt Kraai91b28552001-04-23 18:53:07 +0000127 } else if (S_ISREG(source_stat.st_mode)) {
Eric Andersen75220b52001-08-21 23:36:32 +0000128 FILE *sfp, *dfp=NULL;
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) {
135 perror_msg("unable to link `%s'", dest);
136 return -1;
137 }
138
139 return 0;
140 }
141#endif
Matt Kraai91b28552001-04-23 18:53:07 +0000142
Eric Andersena9a220b2002-09-17 08:42:21 +0000143 if ((sfp = wfopen(source, "r")) == NULL) {
Matt Kraai14b7c5d2001-12-11 16:43:48 +0000144 return -1;
145 }
146
Matt Kraai91b28552001-04-23 18:53:07 +0000147 if (dest_exists) {
Matt Kraai01441032001-04-24 01:30:02 +0000148 if (flags & FILEUTILS_INTERACTIVE) {
Matt Kraai9ff93252001-04-24 01:12:33 +0000149 fprintf(stderr, "%s: overwrite `%s'? ", applet_name, dest);
Matt Kraai14b7c5d2001-12-11 16:43:48 +0000150 if (!ask_confirmation()) {
151 fclose (sfp);
Matt Kraai91b28552001-04-23 18:53:07 +0000152 return 0;
Matt Kraai14b7c5d2001-12-11 16:43:48 +0000153 }
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000154 }
Matt Kraai91b28552001-04-23 18:53:07 +0000155
156 if ((dfp = fopen(dest, "w")) == NULL) {
Matt Kraai01441032001-04-24 01:30:02 +0000157 if (!(flags & FILEUTILS_FORCE)) {
Matt Kraai91b28552001-04-23 18:53:07 +0000158 perror_msg("unable to open `%s'", dest);
Matt Kraai14b7c5d2001-12-11 16:43:48 +0000159 fclose (sfp);
Matt Kraai91b28552001-04-23 18:53:07 +0000160 return -1;
161 }
162
163 if (unlink(dest) < 0) {
164 perror_msg("unable to remove `%s'", dest);
Matt Kraai14b7c5d2001-12-11 16:43:48 +0000165 fclose (sfp);
Matt Kraai91b28552001-04-23 18:53:07 +0000166 return -1;
167 }
168
169 dest_exists = 0;
170 }
Eric Andersenaad1a882001-03-16 22:47:14 +0000171 }
Matt Kraai91b28552001-04-23 18:53:07 +0000172
173 if (!dest_exists) {
174 int fd;
175
176 if ((fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode)) < 0 ||
177 (dfp = fdopen(fd, "w")) == NULL) {
178 if (fd >= 0)
179 close(fd);
180 perror_msg("unable to open `%s'", dest);
Matt Kraai14b7c5d2001-12-11 16:43:48 +0000181 fclose (sfp);
Matt Kraai91b28552001-04-23 18:53:07 +0000182 return -1;
183 }
184 }
185
Matt Kraaibf0a0102001-06-11 13:58:02 +0000186 if (copy_file_chunk(sfp, dfp, -1) < 0)
187 status = -1;
Matt Kraai91b28552001-04-23 18:53:07 +0000188
189 if (fclose(dfp) < 0) {
190 perror_msg("unable to close `%s'", dest);
191 status = -1;
192 }
193
194 if (fclose(sfp) < 0) {
195 perror_msg("unable to close `%s'", source);
196 status = -1;
197 }
Eric Andersen403a73a2002-09-16 09:23:22 +0000198 }
Eric Andersena9a220b2002-09-17 08:42:21 +0000199 else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) ||
200 S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode) ||
201 S_ISLNK(source_stat.st_mode)) {
Eric Andersen403a73a2002-09-16 09:23:22 +0000202
Eric Andersena9a220b2002-09-17 08:42:21 +0000203 if (dest_exists &&
204 ((flags & FILEUTILS_FORCE) == 0 || unlink(dest) < 0)) {
Eric Andersen403a73a2002-09-16 09:23:22 +0000205 perror_msg("unable to remove `%s'", dest);
206 return -1;
Eric Andersen403a73a2002-09-16 09:23:22 +0000207
Eric Andersen403a73a2002-09-16 09:23:22 +0000208 }
Eric Andersena9a220b2002-09-17 08:42:21 +0000209 } else {
210 error_msg("internal error: unrecognized file type");
211 return -1;
Eric Andersen403a73a2002-09-16 09:23:22 +0000212 }
Eric Andersena9a220b2002-09-17 08:42:21 +0000213 if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) ||
214 S_ISSOCK(source_stat.st_mode)) {
Matt Kraai91b28552001-04-23 18:53:07 +0000215 if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
216 perror_msg("unable to create `%s'", dest);
217 return -1;
218 }
219 } else if (S_ISFIFO(source_stat.st_mode)) {
Matt Kraai43ca1372001-04-30 16:43:21 +0000220 if (mkfifo(dest, source_stat.st_mode) < 0) {
Matt Kraai91b28552001-04-23 18:53:07 +0000221 perror_msg("cannot create fifo `%s'", dest);
222 return -1;
223 }
Matt Kraai91b28552001-04-23 18:53:07 +0000224 } else if (S_ISLNK(source_stat.st_mode)) {
Eric Andersen403a73a2002-09-16 09:23:22 +0000225 char *lpath;
226
Eric Andersen403a73a2002-09-16 09:23:22 +0000227 lpath = xreadlink(source);
Mark Whitley8a633262001-04-30 18:17:00 +0000228 if (symlink(lpath, dest) < 0) {
Matt Kraai91b28552001-04-23 18:53:07 +0000229 perror_msg("cannot create symlink `%s'", dest);
230 return -1;
231 }
Mark Whitley8a633262001-04-30 18:17:00 +0000232 free(lpath);
Matt Kraai91b28552001-04-23 18:53:07 +0000233
Eric Andersenaad1a882001-03-16 22:47:14 +0000234#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Matt Kraai01441032001-04-24 01:30:02 +0000235 if (flags & FILEUTILS_PRESERVE_STATUS)
Matt Kraai91b28552001-04-23 18:53:07 +0000236 if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
237 perror_msg("unable to preserve ownership of `%s'", dest);
Eric Andersenaad1a882001-03-16 22:47:14 +0000238#endif
Matt Kraaiace02dc2001-12-17 15:26:36 +0000239
240#ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS
241 add_to_ino_dev_hashtable(&source_stat, dest);
242#endif
243
Matt Kraai91b28552001-04-23 18:53:07 +0000244 return 0;
Eric Andersenaad1a882001-03-16 22:47:14 +0000245 }
246
Matt Kraaiace02dc2001-12-17 15:26:36 +0000247#ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS
248 add_to_ino_dev_hashtable(&source_stat, dest);
249#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)
259 perror_msg("unable to preserve times of `%s'", dest);
260 if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
261 source_stat.st_mode &= ~(S_ISUID | S_ISGID);
262 perror_msg("unable to preserve ownership of `%s'", dest);
263 }
264 if (chmod(dest, source_stat.st_mode) < 0)
265 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}