blob: d2794e7d646e985e2821143457a374515ec51222 [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
Matt Kraai91b28552001-04-23 18:53:07 +000013int copy_file(const char *source, const char *dest, int flags)
Eric Andersenaad1a882001-03-16 22:47:14 +000014{
Matt Kraai91b28552001-04-23 18:53:07 +000015 struct stat source_stat;
16 struct stat dest_stat;
Eric Andersena9a220b2002-09-17 08:42:21 +000017 int dest_exists = 0;
Matt Kraai91b28552001-04-23 18:53:07 +000018 int status = 0;
Eric Andersenaad1a882001-03-16 22:47:14 +000019
Matt Kraai4c557bf2001-10-05 01:35:10 +000020 if ((!(flags & FILEUTILS_DEREFERENCE) &&
Matt Kraai01441032001-04-24 01:30:02 +000021 lstat(source, &source_stat) < 0) ||
Matt Kraai4c557bf2001-10-05 01:35:10 +000022 ((flags & FILEUTILS_DEREFERENCE) &&
Matt Kraai91b28552001-04-23 18:53:07 +000023 stat(source, &source_stat) < 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000024 bb_perror_msg("%s", source);
Matt Kraai91b28552001-04-23 18:53:07 +000025 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +000026 }
Eric Andersenaad1a882001-03-16 22:47:14 +000027
Eric Andersen02b8dfc2002-09-16 10:23:38 +000028 if (lstat(dest, &dest_stat) < 0) {
Matt Kraai91b28552001-04-23 18:53:07 +000029 if (errno != ENOENT) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000030 bb_perror_msg("unable to stat `%s'", dest);
Matt Kraai91b28552001-04-23 18:53:07 +000031 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +000032 }
Eric Andersena9a220b2002-09-17 08:42:21 +000033 } else {
34 if (source_stat.st_dev == dest_stat.st_dev &&
Mike Frysinger2ed05ab2005-04-14 02:52:50 +000035 source_stat.st_ino == dest_stat.st_ino)
36 {
37 bb_error_msg("`%s' and `%s' are the same file", source, dest);
38 return -1;
39 }
Eric Andersena9a220b2002-09-17 08:42:21 +000040 dest_exists = 1;
41 }
Eric Andersenaad1a882001-03-16 22:47:14 +000042
Matt Kraai91b28552001-04-23 18:53:07 +000043 if (S_ISDIR(source_stat.st_mode)) {
44 DIR *dp;
45 struct dirent *d;
Matt Kraai218aa372001-04-30 17:32:43 +000046 mode_t saved_umask = 0;
Matt Kraai91b28552001-04-23 18:53:07 +000047
Matt Kraai01441032001-04-24 01:30:02 +000048 if (!(flags & FILEUTILS_RECUR)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000049 bb_error_msg("%s: omitting directory", source);
Matt Kraai91b28552001-04-23 18:53:07 +000050 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +000051 }
Eric Andersenaad1a882001-03-16 22:47:14 +000052
Matt Kraai91b28552001-04-23 18:53:07 +000053 /* Create DEST. */
54 if (dest_exists) {
55 if (!S_ISDIR(dest_stat.st_mode)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000056 bb_error_msg("`%s' is not a directory", dest);
Matt Kraai91b28552001-04-23 18:53:07 +000057 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +000058 }
Matt Kraai91b28552001-04-23 18:53:07 +000059 } else {
Matt Kraai218aa372001-04-30 17:32:43 +000060 mode_t mode;
Matt Kraai91b28552001-04-23 18:53:07 +000061 saved_umask = umask(0);
Eric Andersenaad1a882001-03-16 22:47:14 +000062
Matt Kraai91b28552001-04-23 18:53:07 +000063 mode = source_stat.st_mode;
Matt Kraai01441032001-04-24 01:30:02 +000064 if (!(flags & FILEUTILS_PRESERVE_STATUS))
Matt Kraai91b28552001-04-23 18:53:07 +000065 mode = source_stat.st_mode & ~saved_umask;
66 mode |= S_IRWXU;
67
68 if (mkdir(dest, mode) < 0) {
69 umask(saved_umask);
Manuel Novoa III cad53642003-03-19 09:13:01 +000070 bb_perror_msg("cannot create directory `%s'", dest);
Matt Kraai91b28552001-04-23 18:53:07 +000071 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +000072 }
Matt Kraai91b28552001-04-23 18:53:07 +000073
74 umask(saved_umask);
Eric Andersenaad1a882001-03-16 22:47:14 +000075 }
Matt Kraaibf0a0102001-06-11 13:58:02 +000076
Matt Kraai91b28552001-04-23 18:53:07 +000077 /* Recursively copy files in SOURCE. */
Rob Landleyd921b2e2006-08-03 15:41:12 +000078 if ((dp = opendir(source)) == NULL) {
Matt Kraai91b28552001-04-23 18:53:07 +000079 status = -1;
Rob Landley2f309322005-11-01 21:55:14 +000080 goto preserve_status;
Matt Kraai91b28552001-04-23 18:53:07 +000081 }
82
83 while ((d = readdir(dp)) != NULL) {
84 char *new_source, *new_dest;
85
Glenn L McGrath393183d2003-05-26 14:07:50 +000086 new_source = concat_subpath_file(source, d->d_name);
87 if(new_source == NULL)
Matt Kraai91b28552001-04-23 18:53:07 +000088 continue;
Matt Kraai91b28552001-04-23 18:53:07 +000089 new_dest = concat_path_file(dest, d->d_name);
90 if (copy_file(new_source, new_dest, flags) < 0)
91 status = -1;
92 free(new_source);
93 free(new_dest);
94 }
Eric Andersena9a220b2002-09-17 08:42:21 +000095 /* closedir have only EBADF error, but "dp" not changes */
96 closedir(dp);
Matt Kraai218aa372001-04-30 17:32:43 +000097
98 if (!dest_exists &&
99 chmod(dest, source_stat.st_mode & ~saved_umask) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000100 bb_perror_msg("unable to change permissions of `%s'", dest);
Matt Kraai218aa372001-04-30 17:32:43 +0000101 status = -1;
102 }
Rob Landleyc9832742006-06-24 21:27:36 +0000103 } else if (S_ISREG(source_stat.st_mode) ||
104 (S_ISLNK(source_stat.st_mode) && (flags & FILEUTILS_DEREFERENCE)))
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000105 {
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000106 int src_fd;
107 int dst_fd;
Rob Landleyc9832742006-06-24 21:27:36 +0000108 if (ENABLE_FEATURE_PRESERVE_HARDLINKS) {
109 char *link_name;
Matt Kraaiace02dc2001-12-17 15:26:36 +0000110
Rob Landleyc9832742006-06-24 21:27:36 +0000111 if (!(flags & FILEUTILS_DEREFERENCE) &&
112 is_in_ino_dev_hashtable(&source_stat, &link_name)) {
113 if (link(link_name, dest) < 0) {
114 bb_perror_msg("unable to link `%s'", dest);
115 return -1;
116 }
117
118 return 0;
Matt Kraaiace02dc2001-12-17 15:26:36 +0000119 }
Rob Landleyc9832742006-06-24 21:27:36 +0000120 add_to_ino_dev_hashtable(&source_stat, dest);
Matt Kraaiace02dc2001-12-17 15:26:36 +0000121 }
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000122 src_fd = open(source, O_RDONLY);
123 if (src_fd == -1) {
124 bb_perror_msg("unable to open `%s'", source);
125 return(-1);
Matt Kraai14b7c5d2001-12-11 16:43:48 +0000126 }
127
Matt Kraai91b28552001-04-23 18:53:07 +0000128 if (dest_exists) {
Matt Kraai01441032001-04-24 01:30:02 +0000129 if (flags & FILEUTILS_INTERACTIVE) {
Paul Foxc337d292005-07-19 21:31:05 +0000130 fprintf(stderr, "%s: overwrite `%s'? ", bb_applet_name, dest);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000131 if (!bb_ask_confirmation()) {
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000132 close (src_fd);
Matt Kraai91b28552001-04-23 18:53:07 +0000133 return 0;
Matt Kraai14b7c5d2001-12-11 16:43:48 +0000134 }
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000135 }
Matt Kraai91b28552001-04-23 18:53:07 +0000136
Glenn L McGrath447bc2d2004-01-11 05:20:59 +0000137 dst_fd = open(dest, O_WRONLY|O_TRUNC);
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000138 if (dst_fd == -1) {
Matt Kraai01441032001-04-24 01:30:02 +0000139 if (!(flags & FILEUTILS_FORCE)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000140 bb_perror_msg("unable to open `%s'", dest);
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000141 close(src_fd);
Matt Kraai91b28552001-04-23 18:53:07 +0000142 return -1;
143 }
144
145 if (unlink(dest) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000146 bb_perror_msg("unable to remove `%s'", dest);
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000147 close(src_fd);
Matt Kraai91b28552001-04-23 18:53:07 +0000148 return -1;
149 }
150
Rob Landley386f85e2006-03-14 21:13:48 +0000151 goto dest_removed;
Matt Kraai91b28552001-04-23 18:53:07 +0000152 }
Rob Landley386f85e2006-03-14 21:13:48 +0000153 } else {
154dest_removed:
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000155 dst_fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode);
156 if (dst_fd == -1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000157 bb_perror_msg("unable to open `%s'", dest);
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000158 close(src_fd);
159 return(-1);
Matt Kraai91b28552001-04-23 18:53:07 +0000160 }
161 }
162
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000163 if (bb_copyfd_eof(src_fd, dst_fd) == -1)
Matt Kraaibf0a0102001-06-11 13:58:02 +0000164 status = -1;
Matt Kraai91b28552001-04-23 18:53:07 +0000165
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000166 if (close(dst_fd) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000167 bb_perror_msg("unable to close `%s'", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000168 status = -1;
169 }
170
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000171 if (close(src_fd) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000172 bb_perror_msg("unable to close `%s'", source);
Matt Kraai91b28552001-04-23 18:53:07 +0000173 status = -1;
174 }
Rob Landley2f309322005-11-01 21:55:14 +0000175 } else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) ||
Eric Andersena9a220b2002-09-17 08:42:21 +0000176 S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode) ||
177 S_ISLNK(source_stat.st_mode)) {
Eric Andersen403a73a2002-09-16 09:23:22 +0000178
Paul Fox0a92bbf2005-07-19 20:47:33 +0000179 if (dest_exists) {
180 if((flags & FILEUTILS_FORCE) == 0) {
181 fprintf(stderr, "`%s' exists\n", dest);
182 return -1;
183 }
184 if(unlink(dest) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000185 bb_perror_msg("unable to remove `%s'", dest);
Eric Andersen403a73a2002-09-16 09:23:22 +0000186 return -1;
Eric Andersen403a73a2002-09-16 09:23:22 +0000187 }
Paul Fox0a92bbf2005-07-19 20:47:33 +0000188 }
Rob Landley2f309322005-11-01 21:55:14 +0000189 if (S_ISFIFO(source_stat.st_mode)) {
190 if (mkfifo(dest, source_stat.st_mode) < 0) {
191 bb_perror_msg("cannot create fifo `%s'", dest);
192 return -1;
193 }
194 } else if (S_ISLNK(source_stat.st_mode)) {
195 char *lpath;
196
197 lpath = xreadlink(source);
198 if (symlink(lpath, dest) < 0) {
199 bb_perror_msg("cannot create symlink `%s'", dest);
200 return -1;
201 }
202 free(lpath);
203
204 if (flags & FILEUTILS_PRESERVE_STATUS)
205 if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
206 bb_perror_msg("unable to preserve ownership of `%s'", dest);
207
208 return 0;
209
210 } else {
211 if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
212 bb_perror_msg("unable to create `%s'", dest);
213 return -1;
214 }
215 }
Eric Andersena9a220b2002-09-17 08:42:21 +0000216 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000217 bb_error_msg("internal error: unrecognized file type");
Eric Andersena9a220b2002-09-17 08:42:21 +0000218 return -1;
Eric Andersenaad1a882001-03-16 22:47:14 +0000219 }
220
Rob Landley2f309322005-11-01 21:55:14 +0000221preserve_status:
Matt Kraai91b28552001-04-23 18:53:07 +0000222
Matt Kraai01441032001-04-24 01:30:02 +0000223 if (flags & FILEUTILS_PRESERVE_STATUS) {
Matt Kraai91b28552001-04-23 18:53:07 +0000224 struct utimbuf times;
Rob Landley2f309322005-11-01 21:55:14 +0000225 char *msg="unable to preserve %s of `%s'";
Matt Kraai91b28552001-04-23 18:53:07 +0000226
227 times.actime = source_stat.st_atime;
228 times.modtime = source_stat.st_mtime;
229 if (utime(dest, &times) < 0)
Rob Landley2f309322005-11-01 21:55:14 +0000230 bb_perror_msg(msg, "times", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000231 if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
232 source_stat.st_mode &= ~(S_ISUID | S_ISGID);
Rob Landley2f309322005-11-01 21:55:14 +0000233 bb_perror_msg(msg, "ownership", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000234 }
235 if (chmod(dest, source_stat.st_mode) < 0)
Rob Landley2f309322005-11-01 21:55:14 +0000236 bb_perror_msg(msg, "permissions", dest);
Eric Andersenaad1a882001-03-16 22:47:14 +0000237 }
238
Matt Kraai24abecc2001-04-30 16:37:04 +0000239 return status;
Eric Andersenaad1a882001-03-16 22:47:14 +0000240}