blob: 3b172ffe48290ec9a9126a49f0c234212eaee19e [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
Matt Kraai91b28552001-04-23 18:53:07 +000011#include <sys/types.h>
12#include <sys/stat.h>
Eric Andersenaad1a882001-03-16 22:47:14 +000013#include <unistd.h>
14#include <fcntl.h>
Matt Kraai91b28552001-04-23 18:53:07 +000015#include <utime.h>
16#include <errno.h>
17#include <dirent.h>
18#include <stdlib.h>
Manuel Novoa III a2949aa2001-06-29 18:59:32 +000019#include <string.h>
Matt Kraai91b28552001-04-23 18:53:07 +000020
Bernhard Reutner-Fischer421d9e52006-04-03 16:39:31 +000021#include "libbb.h"
Eric Andersenaad1a882001-03-16 22:47:14 +000022
Rob Landley2f309322005-11-01 21:55:14 +000023/* Compiler version-specific crap that should be in a header file somewhere. */
24
25#if !((__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1))
26#define lchown chown
27#endif
28
29
Matt Kraai91b28552001-04-23 18:53:07 +000030int copy_file(const char *source, const char *dest, int flags)
Eric Andersenaad1a882001-03-16 22:47:14 +000031{
Matt Kraai91b28552001-04-23 18:53:07 +000032 struct stat source_stat;
33 struct stat dest_stat;
Eric Andersena9a220b2002-09-17 08:42:21 +000034 int dest_exists = 0;
Matt Kraai91b28552001-04-23 18:53:07 +000035 int status = 0;
Eric Andersenaad1a882001-03-16 22:47:14 +000036
Matt Kraai4c557bf2001-10-05 01:35:10 +000037 if ((!(flags & FILEUTILS_DEREFERENCE) &&
Matt Kraai01441032001-04-24 01:30:02 +000038 lstat(source, &source_stat) < 0) ||
Matt Kraai4c557bf2001-10-05 01:35:10 +000039 ((flags & FILEUTILS_DEREFERENCE) &&
Matt Kraai91b28552001-04-23 18:53:07 +000040 stat(source, &source_stat) < 0)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000041 bb_perror_msg("%s", source);
Matt Kraai91b28552001-04-23 18:53:07 +000042 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +000043 }
Eric Andersenaad1a882001-03-16 22:47:14 +000044
Eric Andersen02b8dfc2002-09-16 10:23:38 +000045 if (lstat(dest, &dest_stat) < 0) {
Matt Kraai91b28552001-04-23 18:53:07 +000046 if (errno != ENOENT) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000047 bb_perror_msg("unable to stat `%s'", dest);
Matt Kraai91b28552001-04-23 18:53:07 +000048 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +000049 }
Eric Andersena9a220b2002-09-17 08:42:21 +000050 } else {
51 if (source_stat.st_dev == dest_stat.st_dev &&
Mike Frysinger2ed05ab2005-04-14 02:52:50 +000052 source_stat.st_ino == dest_stat.st_ino)
53 {
54 bb_error_msg("`%s' and `%s' are the same file", source, dest);
55 return -1;
56 }
Eric Andersena9a220b2002-09-17 08:42:21 +000057 dest_exists = 1;
58 }
Eric Andersenaad1a882001-03-16 22:47:14 +000059
Matt Kraai91b28552001-04-23 18:53:07 +000060 if (S_ISDIR(source_stat.st_mode)) {
61 DIR *dp;
62 struct dirent *d;
Matt Kraai218aa372001-04-30 17:32:43 +000063 mode_t saved_umask = 0;
Matt Kraai91b28552001-04-23 18:53:07 +000064
Matt Kraai01441032001-04-24 01:30:02 +000065 if (!(flags & FILEUTILS_RECUR)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000066 bb_error_msg("%s: omitting directory", source);
Matt Kraai91b28552001-04-23 18:53:07 +000067 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +000068 }
Eric Andersenaad1a882001-03-16 22:47:14 +000069
Matt Kraai91b28552001-04-23 18:53:07 +000070 /* Create DEST. */
71 if (dest_exists) {
72 if (!S_ISDIR(dest_stat.st_mode)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000073 bb_error_msg("`%s' is not a directory", dest);
Matt Kraai91b28552001-04-23 18:53:07 +000074 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +000075 }
Matt Kraai91b28552001-04-23 18:53:07 +000076 } else {
Matt Kraai218aa372001-04-30 17:32:43 +000077 mode_t mode;
Matt Kraai91b28552001-04-23 18:53:07 +000078 saved_umask = umask(0);
Eric Andersenaad1a882001-03-16 22:47:14 +000079
Matt Kraai91b28552001-04-23 18:53:07 +000080 mode = source_stat.st_mode;
Matt Kraai01441032001-04-24 01:30:02 +000081 if (!(flags & FILEUTILS_PRESERVE_STATUS))
Matt Kraai91b28552001-04-23 18:53:07 +000082 mode = source_stat.st_mode & ~saved_umask;
83 mode |= S_IRWXU;
84
85 if (mkdir(dest, mode) < 0) {
86 umask(saved_umask);
Manuel Novoa III cad53642003-03-19 09:13:01 +000087 bb_perror_msg("cannot create directory `%s'", dest);
Matt Kraai91b28552001-04-23 18:53:07 +000088 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +000089 }
Matt Kraai91b28552001-04-23 18:53:07 +000090
91 umask(saved_umask);
Eric Andersenaad1a882001-03-16 22:47:14 +000092 }
Matt Kraaibf0a0102001-06-11 13:58:02 +000093
Matt Kraai91b28552001-04-23 18:53:07 +000094 /* Recursively copy files in SOURCE. */
95 if ((dp = opendir(source)) == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000096 bb_perror_msg("unable to open directory `%s'", source);
Matt Kraai91b28552001-04-23 18:53:07 +000097 status = -1;
Rob Landley2f309322005-11-01 21:55:14 +000098 goto preserve_status;
Matt Kraai91b28552001-04-23 18:53:07 +000099 }
100
101 while ((d = readdir(dp)) != NULL) {
102 char *new_source, *new_dest;
103
Glenn L McGrath393183d2003-05-26 14:07:50 +0000104 new_source = concat_subpath_file(source, d->d_name);
105 if(new_source == NULL)
Matt Kraai91b28552001-04-23 18:53:07 +0000106 continue;
Matt Kraai91b28552001-04-23 18:53:07 +0000107 new_dest = concat_path_file(dest, d->d_name);
108 if (copy_file(new_source, new_dest, flags) < 0)
109 status = -1;
110 free(new_source);
111 free(new_dest);
112 }
Eric Andersena9a220b2002-09-17 08:42:21 +0000113 /* closedir have only EBADF error, but "dp" not changes */
114 closedir(dp);
Matt Kraai218aa372001-04-30 17:32:43 +0000115
116 if (!dest_exists &&
117 chmod(dest, source_stat.st_mode & ~saved_umask) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000118 bb_perror_msg("unable to change permissions of `%s'", dest);
Matt Kraai218aa372001-04-30 17:32:43 +0000119 status = -1;
120 }
Rob Landley2f309322005-11-01 21:55:14 +0000121 } else if (S_ISREG(source_stat.st_mode) || (flags & FILEUTILS_DEREFERENCE))
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000122 {
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000123 int src_fd;
124 int dst_fd;
Matt Kraaiace02dc2001-12-17 15:26:36 +0000125#ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS
126 char *link_name;
127
128 if (!(flags & FILEUTILS_DEREFERENCE) &&
129 is_in_ino_dev_hashtable(&source_stat, &link_name)) {
130 if (link(link_name, dest) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000131 bb_perror_msg("unable to link `%s'", dest);
Matt Kraaiace02dc2001-12-17 15:26:36 +0000132 return -1;
133 }
134
135 return 0;
136 }
Rob Landley2f309322005-11-01 21:55:14 +0000137 add_to_ino_dev_hashtable(&source_stat, dest);
Matt Kraaiace02dc2001-12-17 15:26:36 +0000138#endif
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000139 src_fd = open(source, O_RDONLY);
140 if (src_fd == -1) {
141 bb_perror_msg("unable to open `%s'", source);
142 return(-1);
Matt Kraai14b7c5d2001-12-11 16:43:48 +0000143 }
144
Matt Kraai91b28552001-04-23 18:53:07 +0000145 if (dest_exists) {
Matt Kraai01441032001-04-24 01:30:02 +0000146 if (flags & FILEUTILS_INTERACTIVE) {
Paul Foxc337d292005-07-19 21:31:05 +0000147 fprintf(stderr, "%s: overwrite `%s'? ", bb_applet_name, dest);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000148 if (!bb_ask_confirmation()) {
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000149 close (src_fd);
Matt Kraai91b28552001-04-23 18:53:07 +0000150 return 0;
Matt Kraai14b7c5d2001-12-11 16:43:48 +0000151 }
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000152 }
Matt Kraai91b28552001-04-23 18:53:07 +0000153
Glenn L McGrath447bc2d2004-01-11 05:20:59 +0000154 dst_fd = open(dest, O_WRONLY|O_TRUNC);
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000155 if (dst_fd == -1) {
Matt Kraai01441032001-04-24 01:30:02 +0000156 if (!(flags & FILEUTILS_FORCE)) {
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);
Matt Kraai91b28552001-04-23 18:53:07 +0000159 return -1;
160 }
161
162 if (unlink(dest) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000163 bb_perror_msg("unable to remove `%s'", dest);
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000164 close(src_fd);
Matt Kraai91b28552001-04-23 18:53:07 +0000165 return -1;
166 }
167
Rob Landley386f85e2006-03-14 21:13:48 +0000168 goto dest_removed;
Matt Kraai91b28552001-04-23 18:53:07 +0000169 }
Rob Landley386f85e2006-03-14 21:13:48 +0000170 } else {
171dest_removed:
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000172 dst_fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode);
173 if (dst_fd == -1) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000174 bb_perror_msg("unable to open `%s'", dest);
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000175 close(src_fd);
176 return(-1);
Matt Kraai91b28552001-04-23 18:53:07 +0000177 }
178 }
179
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000180 if (bb_copyfd_eof(src_fd, dst_fd) == -1)
Matt Kraaibf0a0102001-06-11 13:58:02 +0000181 status = -1;
Matt Kraai91b28552001-04-23 18:53:07 +0000182
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000183 if (close(dst_fd) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000184 bb_perror_msg("unable to close `%s'", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000185 status = -1;
186 }
187
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000188 if (close(src_fd) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000189 bb_perror_msg("unable to close `%s'", source);
Matt Kraai91b28552001-04-23 18:53:07 +0000190 status = -1;
191 }
Rob Landley2f309322005-11-01 21:55:14 +0000192 } else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) ||
Eric Andersena9a220b2002-09-17 08:42:21 +0000193 S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode) ||
194 S_ISLNK(source_stat.st_mode)) {
Eric Andersen403a73a2002-09-16 09:23:22 +0000195
Paul Fox0a92bbf2005-07-19 20:47:33 +0000196 if (dest_exists) {
197 if((flags & FILEUTILS_FORCE) == 0) {
198 fprintf(stderr, "`%s' exists\n", dest);
199 return -1;
200 }
201 if(unlink(dest) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000202 bb_perror_msg("unable to remove `%s'", dest);
Eric Andersen403a73a2002-09-16 09:23:22 +0000203 return -1;
Eric Andersen403a73a2002-09-16 09:23:22 +0000204 }
Paul Fox0a92bbf2005-07-19 20:47:33 +0000205 }
Rob Landley2f309322005-11-01 21:55:14 +0000206 if (S_ISFIFO(source_stat.st_mode)) {
207 if (mkfifo(dest, source_stat.st_mode) < 0) {
208 bb_perror_msg("cannot create fifo `%s'", dest);
209 return -1;
210 }
211 } else if (S_ISLNK(source_stat.st_mode)) {
212 char *lpath;
213
214 lpath = xreadlink(source);
215 if (symlink(lpath, dest) < 0) {
216 bb_perror_msg("cannot create symlink `%s'", dest);
217 return -1;
218 }
219 free(lpath);
220
221 if (flags & FILEUTILS_PRESERVE_STATUS)
222 if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
223 bb_perror_msg("unable to preserve ownership of `%s'", dest);
224
225 return 0;
226
227 } else {
228 if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
229 bb_perror_msg("unable to create `%s'", dest);
230 return -1;
231 }
232 }
Eric Andersena9a220b2002-09-17 08:42:21 +0000233 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000234 bb_error_msg("internal error: unrecognized file type");
Eric Andersena9a220b2002-09-17 08:42:21 +0000235 return -1;
Eric Andersenaad1a882001-03-16 22:47:14 +0000236 }
237
Rob Landley2f309322005-11-01 21:55:14 +0000238preserve_status:
Matt Kraai91b28552001-04-23 18:53:07 +0000239
Matt Kraai01441032001-04-24 01:30:02 +0000240 if (flags & FILEUTILS_PRESERVE_STATUS) {
Matt Kraai91b28552001-04-23 18:53:07 +0000241 struct utimbuf times;
Rob Landley2f309322005-11-01 21:55:14 +0000242 char *msg="unable to preserve %s of `%s'";
Matt Kraai91b28552001-04-23 18:53:07 +0000243
244 times.actime = source_stat.st_atime;
245 times.modtime = source_stat.st_mtime;
246 if (utime(dest, &times) < 0)
Rob Landley2f309322005-11-01 21:55:14 +0000247 bb_perror_msg(msg, "times", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000248 if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
249 source_stat.st_mode &= ~(S_ISUID | S_ISGID);
Rob Landley2f309322005-11-01 21:55:14 +0000250 bb_perror_msg(msg, "ownership", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000251 }
252 if (chmod(dest, source_stat.st_mode) < 0)
Rob Landley2f309322005-11-01 21:55:14 +0000253 bb_perror_msg(msg, "permissions", dest);
Eric Andersenaad1a882001-03-16 22:47:14 +0000254 }
255
Matt Kraai24abecc2001-04-30 16:37:04 +0000256 return status;
Eric Andersenaad1a882001-03-16 22:47:14 +0000257}