blob: 4ee3efdbc59270140663d5ac0b44356d9c48ce03 [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 *
6 * Copyright (C) 2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
Eric Andersenaad1a882001-03-16 22:47:14 +00007 *
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 Andersenaad1a882001-03-16 22:47:14 +000022 */
23
Matt Kraai91b28552001-04-23 18:53:07 +000024#include <sys/types.h>
25#include <sys/stat.h>
Eric Andersenaad1a882001-03-16 22:47:14 +000026#include <unistd.h>
27#include <fcntl.h>
Matt Kraai91b28552001-04-23 18:53:07 +000028#include <utime.h>
29#include <errno.h>
30#include <dirent.h>
31#include <stdlib.h>
32
Eric Andersenaad1a882001-03-16 22:47:14 +000033#include "libbb.h"
34
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;
39 int dest_exists = 1;
40 int status = 0;
Eric Andersenaad1a882001-03-16 22:47:14 +000041
Matt Kraai01441032001-04-24 01:30:02 +000042 if (((flags & FILEUTILS_PRESERVE_SYMLINKS) &&
43 lstat(source, &source_stat) < 0) ||
44 (!(flags & FILEUTILS_PRESERVE_SYMLINKS) &&
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
Matt Kraai91b28552001-04-23 18:53:07 +000050 if (stat(dest, &dest_stat) < 0) {
51 if (errno != ENOENT) {
52 perror_msg("unable to stat `%s'", dest);
53 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +000054 }
Matt Kraai91b28552001-04-23 18:53:07 +000055 dest_exists = 0;
Eric Andersenaad1a882001-03-16 22:47:14 +000056 }
57
Matt Kraai91b28552001-04-23 18:53:07 +000058 if (dest_exists && source_stat.st_rdev == dest_stat.st_rdev &&
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 Andersenaad1a882001-03-16 22:47:14 +000062 }
63
Matt Kraai91b28552001-04-23 18:53:07 +000064 if (S_ISDIR(source_stat.st_mode)) {
65 DIR *dp;
66 struct dirent *d;
67
Matt Kraai01441032001-04-24 01:30:02 +000068 if (!(flags & FILEUTILS_RECUR)) {
Matt Kraai91b28552001-04-23 18:53:07 +000069 error_msg("%s: omitting directory", source);
70 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +000071 }
Eric Andersenaad1a882001-03-16 22:47:14 +000072
Matt Kraai91b28552001-04-23 18:53:07 +000073 /* Create DEST. */
74 if (dest_exists) {
75 if (!S_ISDIR(dest_stat.st_mode)) {
76 error_msg("`%s' is not a directory", dest);
77 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +000078 }
Matt Kraai91b28552001-04-23 18:53:07 +000079 } else {
80 mode_t mode, saved_umask;
81 saved_umask = umask(0);
Eric Andersenaad1a882001-03-16 22:47:14 +000082
Matt Kraai91b28552001-04-23 18:53:07 +000083 mode = source_stat.st_mode;
Matt Kraai01441032001-04-24 01:30:02 +000084 if (!(flags & FILEUTILS_PRESERVE_STATUS))
Matt Kraai91b28552001-04-23 18:53:07 +000085 mode = source_stat.st_mode & ~saved_umask;
86 mode |= S_IRWXU;
87
88 if (mkdir(dest, mode) < 0) {
89 umask(saved_umask);
90 perror_msg("cannot create directory `%s'", dest);
91 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +000092 }
Matt Kraai91b28552001-04-23 18:53:07 +000093
94 umask(saved_umask);
Eric Andersenaad1a882001-03-16 22:47:14 +000095 }
Matt Kraai91b28552001-04-23 18:53:07 +000096
97 /* Recursively copy files in SOURCE. */
98 if ((dp = opendir(source)) == NULL) {
99 perror_msg("unable to open directory `%s'", source);
100 status = -1;
101 goto end;
102 }
103
104 while ((d = readdir(dp)) != NULL) {
105 char *new_source, *new_dest;
106
107 if (strcmp(d->d_name, ".") == 0 ||
108 strcmp(d->d_name, "..") == 0)
109 continue;
110
111 new_source = concat_path_file(source, d->d_name);
112 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 }
118
119 /* ??? What if an error occurs in readdir? */
120
121 if (closedir(dp) < 0) {
122 perror_msg("unable to close directory `%s'", source);
123 status = -1;
124 }
125 } else if (S_ISREG(source_stat.st_mode)) {
126 FILE *sfp, *dfp;
127
128 if (dest_exists) {
Matt Kraai01441032001-04-24 01:30:02 +0000129 if (flags & FILEUTILS_INTERACTIVE) {
Matt Kraai9ff93252001-04-24 01:12:33 +0000130 fprintf(stderr, "%s: overwrite `%s'? ", applet_name, dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000131 if (!ask_confirmation())
132 return 0;
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000133 }
Matt Kraai91b28552001-04-23 18:53:07 +0000134
135 if ((dfp = fopen(dest, "w")) == NULL) {
Matt Kraai01441032001-04-24 01:30:02 +0000136 if (!(flags & FILEUTILS_FORCE)) {
Matt Kraai91b28552001-04-23 18:53:07 +0000137 perror_msg("unable to open `%s'", dest);
138 return -1;
139 }
140
141 if (unlink(dest) < 0) {
142 perror_msg("unable to remove `%s'", dest);
143 return -1;
144 }
145
146 dest_exists = 0;
147 }
Eric Andersenaad1a882001-03-16 22:47:14 +0000148 }
Matt Kraai91b28552001-04-23 18:53:07 +0000149
150 if (!dest_exists) {
151 int fd;
152
153 if ((fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode)) < 0 ||
154 (dfp = fdopen(fd, "w")) == NULL) {
155 if (fd >= 0)
156 close(fd);
157 perror_msg("unable to open `%s'", dest);
158 return -1;
159 }
160 }
161
162 if ((sfp = fopen(source, "r")) == NULL) {
163 fclose(dfp);
164 perror_msg("unable to open `%s'", source);
165 status = -1;
166 goto end;
167 }
168
169 copy_file_chunk(sfp, dfp, source_stat.st_size);
170
171 if (fclose(dfp) < 0) {
172 perror_msg("unable to close `%s'", dest);
173 status = -1;
174 }
175
176 if (fclose(sfp) < 0) {
177 perror_msg("unable to close `%s'", source);
178 status = -1;
179 }
180 } else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) ||
181 S_ISSOCK(source_stat.st_mode)) {
182 if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
183 perror_msg("unable to create `%s'", dest);
184 return -1;
185 }
186 } else if (S_ISFIFO(source_stat.st_mode)) {
187 mode_t mode, saved_umask;
188 saved_umask = umask(0);
189
190 mode = source_stat.st_mode;
Matt Kraai01441032001-04-24 01:30:02 +0000191 if (!(flags & FILEUTILS_PRESERVE_STATUS))
Matt Kraai91b28552001-04-23 18:53:07 +0000192 mode = source_stat.st_mode & ~saved_umask;
193 mode |= S_IRWXU;
194
195 if (mkfifo(dest, mode) < 0) {
196 umask(saved_umask);
197 perror_msg("cannot create fifo `%s'", dest);
198 return -1;
199 }
200
201 umask(saved_umask);
202 } else if (S_ISLNK(source_stat.st_mode)) {
203 char buf[BUFSIZ + 1];
204
205 if (readlink(source, buf, BUFSIZ) < 0) {
206 perror_msg("cannot read `%s'", source);
207 return -1;
208 }
209 buf[BUFSIZ] = '\0';
210
211 if (symlink(buf, dest) < 0) {
212 perror_msg("cannot create symlink `%s'", dest);
213 return -1;
214 }
215
Eric Andersenaad1a882001-03-16 22:47:14 +0000216#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
Matt Kraai01441032001-04-24 01:30:02 +0000217 if (flags & FILEUTILS_PRESERVE_STATUS)
Matt Kraai91b28552001-04-23 18:53:07 +0000218 if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
219 perror_msg("unable to preserve ownership of `%s'", dest);
Eric Andersenaad1a882001-03-16 22:47:14 +0000220#endif
Matt Kraai91b28552001-04-23 18:53:07 +0000221 return 0;
222 } else {
223 error_msg("internal error: unrecognized file type");
224 return -1;
Eric Andersenaad1a882001-03-16 22:47:14 +0000225 }
226
Matt Kraai91b28552001-04-23 18:53:07 +0000227end:
228
Matt Kraai01441032001-04-24 01:30:02 +0000229 if (flags & FILEUTILS_PRESERVE_STATUS) {
Matt Kraai91b28552001-04-23 18:53:07 +0000230 struct utimbuf times;
231
232 times.actime = source_stat.st_atime;
233 times.modtime = source_stat.st_mtime;
234 if (utime(dest, &times) < 0)
235 perror_msg("unable to preserve times of `%s'", dest);
236 if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
237 source_stat.st_mode &= ~(S_ISUID | S_ISGID);
238 perror_msg("unable to preserve ownership of `%s'", dest);
239 }
240 if (chmod(dest, source_stat.st_mode) < 0)
241 perror_msg("unable to preserve permissions of `%s'", dest);
Eric Andersenaad1a882001-03-16 22:47:14 +0000242 }
243
Matt Kraai91b28552001-04-23 18:53:07 +0000244 return 0;
Eric Andersenaad1a882001-03-16 22:47:14 +0000245}