blob: 0135831fe0a35536ddfc090f74e18f36dd0c9cf1 [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 *
Rob Landley2f309322005-11-01 21:55:14 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenaad1a882001-03-16 22:47:14 +00008 *
Eric Andersenaad1a882001-03-16 22:47:14 +00009 */
10
Rob Landleyc9832742006-06-24 21:27:36 +000011#include "libbb.h"
Eric Andersenaad1a882001-03-16 22:47:14 +000012
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000013static int retry_overwrite(const char *dest, int flags)
14{
15 if (!(flags & (FILEUTILS_FORCE|FILEUTILS_INTERACTIVE))) {
16 fprintf(stderr, "'%s' exists\n", dest);
17 return -1;
18 }
19 if (flags & FILEUTILS_INTERACTIVE) {
20 fprintf(stderr, "%s: overwrite '%s'? ", applet_name, dest);
21 if (!bb_ask_confirmation())
22 return 0; // not allowed to overwrite
23 }
24 if (unlink(dest) < 0) {
25 bb_perror_msg("cannot remove '%s'", dest);
26 return -1; // error
27 }
28 return 1; // ok (to try again)
29}
30
Matt Kraai91b28552001-04-23 18:53:07 +000031int copy_file(const char *source, const char *dest, int flags)
Eric Andersenaad1a882001-03-16 22:47:14 +000032{
Matt Kraai91b28552001-04-23 18:53:07 +000033 struct stat source_stat;
34 struct stat dest_stat;
Matt Kraai91b28552001-04-23 18:53:07 +000035 int status = 0;
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000036 signed char dest_exists = 0;
37 signed char ovr;
Eric Andersenaad1a882001-03-16 22:47:14 +000038
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000039#define FLAGS_DEREF (flags & FILEUTILS_DEREFERENCE)
40
41 if ((FLAGS_DEREF ? stat : lstat)(source, &source_stat) < 0) {
42 // This may be a dangling symlink.
43 // Making [sym]links to dangling symlinks works, so...
44 if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK))
45 goto make_links;
46 bb_perror_msg("cannot stat '%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) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000052 bb_perror_msg("cannot 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 {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000056 if (source_stat.st_dev == dest_stat.st_dev
57 && source_stat.st_ino == dest_stat.st_ino
58 ) {
59 bb_error_msg("'%s' and '%s' are the same file", source, dest);
Mike Frysinger2ed05ab2005-04-14 02:52:50 +000060 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)) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000071 bb_error_msg("omitting directory '%s'", 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)) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000078 bb_error_msg("target '%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);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +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. */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000100 dp = opendir(source);
101 if (dp == NULL) {
Matt Kraai91b28552001-04-23 18:53:07 +0000102 status = -1;
Rob Landley2f309322005-11-01 21:55:14 +0000103 goto preserve_status;
Matt Kraai91b28552001-04-23 18:53:07 +0000104 }
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);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000110 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(dp);
Matt Kraai218aa372001-04-30 17:32:43 +0000119
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000120 if (!dest_exists
121 && chmod(dest, source_stat.st_mode & ~saved_umask) < 0
122 ) {
123 bb_perror_msg("cannot change permissions of '%s'", dest);
Matt Kraai218aa372001-04-30 17:32:43 +0000124 status = -1;
125 }
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000126
127 } else if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) {
128 int (*lf)(const char *oldpath, const char *newpath);
129 make_links:
130 // Hmm... maybe
131 // if (DEREF && MAKE_SOFTLINK) source = realpath(source) ?
132 // (but realpath returns NULL on dangling symlinks...)
133 lf = (flags & FILEUTILS_MAKE_SOFTLINK) ? symlink : link;
134 if (lf(source, dest) < 0) {
135 ovr = retry_overwrite(dest, flags);
136 if (ovr <= 0)
137 return ovr;
138 if (lf(source, dest) < 0) {
139 bb_perror_msg("cannot create link '%s'", dest);
140 return -1;
141 }
142 }
143 return 0;
144
145 } else if (S_ISREG(source_stat.st_mode)
146 // Huh? DEREF uses stat, which never returns links IIRC...
147 || (FLAGS_DEREF && S_ISLNK(source_stat.st_mode))
148 ) {
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000149 int src_fd;
150 int dst_fd;
Rob Landleyc9832742006-06-24 21:27:36 +0000151 if (ENABLE_FEATURE_PRESERVE_HARDLINKS) {
152 char *link_name;
Matt Kraaiace02dc2001-12-17 15:26:36 +0000153
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000154 if (!FLAGS_DEREF
155 && is_in_ino_dev_hashtable(&source_stat, &link_name)
156 ) {
Rob Landleyc9832742006-06-24 21:27:36 +0000157 if (link(link_name, dest) < 0) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000158 ovr = retry_overwrite(dest, flags);
159 if (ovr <= 0)
160 return ovr;
161 if (link(link_name, dest) < 0) {
162 bb_perror_msg("cannot create link '%s'", dest);
163 return -1;
164 }
Rob Landleyc9832742006-06-24 21:27:36 +0000165 }
Rob Landleyc9832742006-06-24 21:27:36 +0000166 return 0;
Matt Kraaiace02dc2001-12-17 15:26:36 +0000167 }
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000168 // TODO: probably is_in_.. and add_to_...
169 // can be combined: find_or_add_...
Rob Landleyc9832742006-06-24 21:27:36 +0000170 add_to_ino_dev_hashtable(&source_stat, dest);
Matt Kraaiace02dc2001-12-17 15:26:36 +0000171 }
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000172
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000173 src_fd = open(source, O_RDONLY);
174 if (src_fd == -1) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000175 bb_perror_msg("cannot open '%s'", source);
176 return -1;
Matt Kraai14b7c5d2001-12-11 16:43:48 +0000177 }
178
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000179 // POSIX: if exists and -i, ask (w/o -i assume yes).
180 // Then open w/o EXCL.
181 // If open still fails and -f, try unlink, then try open again.
182 // Result: a mess:
183 // If dest is a softlink, we overwrite softlink's destination!
184 // (or fail, if it points to dir/nonexistent location/etc).
185 // This is strange, but POSIX-correct.
186 // coreutils cp has --remove-destination to override this...
187 dst_fd = open(dest, (flags & FILEUTILS_INTERACTIVE)
188 ? O_WRONLY|O_CREAT|O_TRUNC|O_EXCL
189 : O_WRONLY|O_CREAT|O_TRUNC, source_stat.st_mode);
190 if (dst_fd == -1) {
191 // We would not do POSIX insanity. -i asks,
192 // then _unlinks_ the offender. Presto.
193 // Or else we will end up having 3 open()s!
194 ovr = retry_overwrite(dest, flags);
195 if (ovr <= 0) {
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000196 close(src_fd);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000197 return ovr;
198 }
199 dst_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, source_stat.st_mode);
200 if (dst_fd == -1) {
201 bb_perror_msg("cannot open '%s'", dest);
202 close(src_fd);
203 return -1;
Matt Kraai91b28552001-04-23 18:53:07 +0000204 }
205 }
206
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000207 if (bb_copyfd_eof(src_fd, dst_fd) == -1)
Matt Kraaibf0a0102001-06-11 13:58:02 +0000208 status = -1;
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000209 if (close(dst_fd) < 0) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000210 bb_perror_msg("cannot close '%s'", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000211 status = -1;
212 }
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000213 if (close(src_fd) < 0) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000214 bb_perror_msg("cannot close '%s'", source);
Matt Kraai91b28552001-04-23 18:53:07 +0000215 status = -1;
216 }
Eric Andersen403a73a2002-09-16 09:23:22 +0000217
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000218 } else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode)
219 || S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode)
220 || S_ISLNK(source_stat.st_mode)
221 ) {
222 // We are lazy here, a bit lax with races...
Paul Fox0a92bbf2005-07-19 20:47:33 +0000223 if (dest_exists) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000224 ovr = retry_overwrite(dest, flags);
225 if (ovr <= 0)
226 return ovr;
Paul Fox0a92bbf2005-07-19 20:47:33 +0000227 }
Rob Landley2f309322005-11-01 21:55:14 +0000228 if (S_ISFIFO(source_stat.st_mode)) {
229 if (mkfifo(dest, source_stat.st_mode) < 0) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000230 bb_perror_msg("cannot create fifo '%s'", dest);
Rob Landley2f309322005-11-01 21:55:14 +0000231 return -1;
232 }
233 } else if (S_ISLNK(source_stat.st_mode)) {
234 char *lpath;
235
236 lpath = xreadlink(source);
237 if (symlink(lpath, dest) < 0) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000238 bb_perror_msg("cannot create symlink '%s'", dest);
239 free(lpath);
Rob Landley2f309322005-11-01 21:55:14 +0000240 return -1;
241 }
242 free(lpath);
243
244 if (flags & FILEUTILS_PRESERVE_STATUS)
245 if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000246 bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest);
Rob Landley2f309322005-11-01 21:55:14 +0000247
248 return 0;
249
250 } else {
251 if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000252 bb_perror_msg("cannot create '%s'", dest);
Rob Landley2f309322005-11-01 21:55:14 +0000253 return -1;
254 }
255 }
Eric Andersena9a220b2002-09-17 08:42:21 +0000256 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000257 bb_error_msg("internal error: unrecognized file type");
Eric Andersena9a220b2002-09-17 08:42:21 +0000258 return -1;
Eric Andersenaad1a882001-03-16 22:47:14 +0000259 }
260
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000261 preserve_status:
Matt Kraai91b28552001-04-23 18:53:07 +0000262
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000263 if (flags & FILEUTILS_PRESERVE_STATUS
264 /* Cannot happen: */
265 /* && !(flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) */
266 ) {
Matt Kraai91b28552001-04-23 18:53:07 +0000267 struct utimbuf times;
268
269 times.actime = source_stat.st_atime;
270 times.modtime = source_stat.st_mtime;
271 if (utime(dest, &times) < 0)
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000272 bb_perror_msg("cannot preserve %s of '%s'", "times", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000273 if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
274 source_stat.st_mode &= ~(S_ISUID | S_ISGID);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000275 bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000276 }
277 if (chmod(dest, source_stat.st_mode) < 0)
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000278 bb_perror_msg("cannot preserve %s of '%s'", "permissions", dest);
Eric Andersenaad1a882001-03-16 22:47:14 +0000279 }
280
Matt Kraai24abecc2001-04-30 16:37:04 +0000281 return status;
Eric Andersenaad1a882001-03-16 22:47:14 +0000282}