blob: 23c0f8320b86f19f5d1a478368843340bd1bf51b [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>
Denis Vlasenko49622d72007-03-10 16:58:49 +00006 * SELinux support by Yuichi Nakamura <ynakam@hitachisoft.jp>
Eric Andersenaad1a882001-03-16 22:47:14 +00007 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenaad1a882001-03-16 22:47:14 +00009 */
Rob Landleyc9832742006-06-24 21:27:36 +000010#include "libbb.h"
Eric Andersenaad1a882001-03-16 22:47:14 +000011
Denys Vlasenko2d7b5bf2009-07-05 12:49:29 +020012// FEATURE_NON_POSIX_CP:
13//
Denis Vlasenko54d14ca2007-03-15 13:33:37 +000014// POSIX: if exists and -i, ask (w/o -i assume yes).
15// Then open w/o EXCL (yes, not unlink!).
16// If open still fails and -f, try unlink, then try open again.
17// Result: a mess:
Denys Vlasenko6f58be02009-07-05 13:24:17 +020018// If dest is a (sym)link, we overwrite link destination!
Denis Vlasenko54d14ca2007-03-15 13:33:37 +000019// (or fail, if it points to dir/nonexistent location/etc).
20// This is strange, but POSIX-correct.
21// coreutils cp has --remove-destination to override this...
22
Denys Vlasenko6f58be02009-07-05 13:24:17 +020023/* Called if open of destination, link creation etc fails.
24 * errno must be set to relevant value ("why we cannot create dest?")
25 * to give reasonable error message */
Denis Vlasenko54d14ca2007-03-15 13:33:37 +000026static int ask_and_unlink(const char *dest, int flags)
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000027{
Denis Vlasenkod5fe8802008-02-13 16:52:00 +000028 int e = errno;
Denys Vlasenko6f58be02009-07-05 13:24:17 +020029
Denys Vlasenko2d7b5bf2009-07-05 12:49:29 +020030#if !ENABLE_FEATURE_NON_POSIX_CP
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000031 if (!(flags & (FILEUTILS_FORCE|FILEUTILS_INTERACTIVE))) {
Denys Vlasenko6f58be02009-07-05 13:24:17 +020032 /* Either it exists, or the *path* doesnt exist */
Denys Vlasenko6331cf02009-11-13 09:08:27 +010033 bb_perror_msg("can't create '%s'", dest);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000034 return -1;
35 }
Denis Vlasenko54d14ca2007-03-15 13:33:37 +000036#endif
Denys Vlasenko6f58be02009-07-05 13:24:17 +020037 // else: act as if -f is always in effect.
Denys Vlasenko6331cf02009-11-13 09:08:27 +010038 // We don't want "can't create" msg, we want unlink to be done
Denys Vlasenko6f58be02009-07-05 13:24:17 +020039 // (silently unless -i). Why? POSIX cp usually succeeds with
40 // O_TRUNC open of existing file, and user is left ignorantly happy.
41 // With above block unconditionally enabled, non-POSIX cp
42 // will complain a lot more than POSIX one.
Denis Vlasenko54d14ca2007-03-15 13:33:37 +000043
Denys Vlasenko6f58be02009-07-05 13:24:17 +020044 /* TODO: maybe we should do it only if ctty is present? */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000045 if (flags & FILEUTILS_INTERACTIVE) {
Denis Vlasenko54d14ca2007-03-15 13:33:37 +000046 // We would not do POSIX insanity. -i asks,
47 // then _unlinks_ the offender. Presto.
Denis Vlasenko3d829622007-08-24 14:23:57 +000048 // (No "opening without O_EXCL", no "unlink only if -f")
Denis Vlasenko54d14ca2007-03-15 13:33:37 +000049 // Or else we will end up having 3 open()s!
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000050 fprintf(stderr, "%s: overwrite '%s'? ", applet_name, dest);
51 if (!bb_ask_confirmation())
Denys Vlasenko6f58be02009-07-05 13:24:17 +020052 return 0; /* not allowed to overwrite */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000053 }
54 if (unlink(dest) < 0) {
Denis Vlasenkod5fe8802008-02-13 16:52:00 +000055#if ENABLE_FEATURE_VERBOSE_CP_MESSAGE
56 if (e == errno && e == ENOENT) {
57 /* e == ENOTDIR is similar: path has non-dir component,
58 * but in this case we don't even reach copy_file() */
Denys Vlasenko6331cf02009-11-13 09:08:27 +010059 bb_error_msg("can't create '%s': Path does not exist", dest);
Denys Vlasenko6f58be02009-07-05 13:24:17 +020060 return -1; /* error */
Denis Vlasenkod5fe8802008-02-13 16:52:00 +000061 }
62#endif
Denys Vlasenko6f58be02009-07-05 13:24:17 +020063 errno = e; /* do not use errno from unlink */
Denys Vlasenko6331cf02009-11-13 09:08:27 +010064 bb_perror_msg("can't create '%s'", dest);
Denys Vlasenko6f58be02009-07-05 13:24:17 +020065 return -1; /* error */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000066 }
Denys Vlasenko877dedb2016-05-27 00:46:38 +020067#if ENABLE_FEATURE_CP_LONG_OPTIONS
68 if (flags & FILEUTILS_RMDEST)
69 if (flags & FILEUTILS_VERBOSE)
70 printf("removed '%s'\n", dest);
71#endif
Denys Vlasenko6f58be02009-07-05 13:24:17 +020072 return 1; /* ok (to try again) */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000073}
74
Denis Vlasenko54d14ca2007-03-15 13:33:37 +000075/* Return:
76 * -1 error, copy not made
77 * 0 copy is made or user answered "no" in interactive mode
78 * (failures to preserve mode/owner/times are not reported in exit code)
79 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000080int FAST_FUNC copy_file(const char *source, const char *dest, int flags)
Eric Andersenaad1a882001-03-16 22:47:14 +000081{
Denis Vlasenko30bab712007-08-25 21:14:55 +000082 /* This is a recursive function, try to minimize stack usage */
83 /* NB: each struct stat is ~100 bytes */
Matt Kraai91b28552001-04-23 18:53:07 +000084 struct stat source_stat;
85 struct stat dest_stat;
Denys Vlasenkoaa4f9a22011-01-28 19:14:17 +010086 smallint retval = 0;
87 smallint dest_exists = 0;
88 smallint ovr;
Eric Andersenaad1a882001-03-16 22:47:14 +000089
Denis Vlasenkob9ad75f2008-03-28 17:49:31 +000090/* Inverse of cp -d ("cp without -d") */
Denys Vlasenkoa40f0622010-01-15 22:05:07 +010091#define FLAGS_DEREF (flags & (FILEUTILS_DEREFERENCE + FILEUTILS_DEREFERENCE_L0))
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000092
93 if ((FLAGS_DEREF ? stat : lstat)(source, &source_stat) < 0) {
Denys Vlasenko6f58be02009-07-05 13:24:17 +020094 /* This may be a dangling symlink.
95 * Making [sym]links to dangling symlinks works, so... */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000096 if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK))
97 goto make_links;
Denys Vlasenko6331cf02009-11-13 09:08:27 +010098 bb_perror_msg("can't stat '%s'", source);
Matt Kraai91b28552001-04-23 18:53:07 +000099 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000100 }
Eric Andersenaad1a882001-03-16 22:47:14 +0000101
Eric Andersen02b8dfc2002-09-16 10:23:38 +0000102 if (lstat(dest, &dest_stat) < 0) {
Matt Kraai91b28552001-04-23 18:53:07 +0000103 if (errno != ENOENT) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100104 bb_perror_msg("can't stat '%s'", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000105 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000106 }
Eric Andersena9a220b2002-09-17 08:42:21 +0000107 } else {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000108 if (source_stat.st_dev == dest_stat.st_dev
109 && source_stat.st_ino == dest_stat.st_ino
110 ) {
111 bb_error_msg("'%s' and '%s' are the same file", source, dest);
Mike Frysinger2ed05ab2005-04-14 02:52:50 +0000112 return -1;
113 }
Eric Andersena9a220b2002-09-17 08:42:21 +0000114 dest_exists = 1;
115 }
Eric Andersenaad1a882001-03-16 22:47:14 +0000116
Denis Vlasenko49622d72007-03-10 16:58:49 +0000117#if ENABLE_SELINUX
118 if ((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) && is_selinux_enabled() > 0) {
119 security_context_t con;
120 if (lgetfilecon(source, &con) >= 0) {
121 if (setfscreatecon(con) < 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100122 bb_perror_msg("can't set setfscreatecon %s", con);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000123 freecon(con);
124 return -1;
125 }
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000126 } else if (errno == ENOTSUP || errno == ENODATA) {
127 setfscreatecon_or_die(NULL);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000128 } else {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100129 bb_perror_msg("can't lgetfilecon %s", source);
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000130 return -1;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000131 }
132 }
133#endif
134
Matt Kraai91b28552001-04-23 18:53:07 +0000135 if (S_ISDIR(source_stat.st_mode)) {
136 DIR *dp;
Denis Vlasenko0f214ec2007-08-27 17:02:19 +0000137 const char *tp;
Matt Kraai91b28552001-04-23 18:53:07 +0000138 struct dirent *d;
Matt Kraai218aa372001-04-30 17:32:43 +0000139 mode_t saved_umask = 0;
Matt Kraai91b28552001-04-23 18:53:07 +0000140
Matt Kraai01441032001-04-24 01:30:02 +0000141 if (!(flags & FILEUTILS_RECUR)) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000142 bb_error_msg("omitting directory '%s'", source);
Matt Kraai91b28552001-04-23 18:53:07 +0000143 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000144 }
Eric Andersenaad1a882001-03-16 22:47:14 +0000145
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000146 /* Did we ever create source ourself before? */
Denis Vlasenko0f214ec2007-08-27 17:02:19 +0000147 tp = is_in_ino_dev_hashtable(&source_stat);
148 if (tp) {
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000149 /* We did! it's a recursion! man the lifeboats... */
150 bb_error_msg("recursion detected, omitting directory '%s'",
Denis Vlasenko0f214ec2007-08-27 17:02:19 +0000151 source);
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000152 return -1;
153 }
154
Matt Kraai91b28552001-04-23 18:53:07 +0000155 if (dest_exists) {
156 if (!S_ISDIR(dest_stat.st_mode)) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000157 bb_error_msg("target '%s' is not a directory", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000158 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000159 }
Denis Vlasenkoa9335ea2007-09-11 16:28:14 +0000160 /* race here: user can substitute a symlink between
161 * this check and actual creation of files inside dest */
Matt Kraai91b28552001-04-23 18:53:07 +0000162 } else {
Denys Vlasenkoaa4f9a22011-01-28 19:14:17 +0100163 /* Create DEST */
Matt Kraai218aa372001-04-30 17:32:43 +0000164 mode_t mode;
Matt Kraai91b28552001-04-23 18:53:07 +0000165 saved_umask = umask(0);
Eric Andersenaad1a882001-03-16 22:47:14 +0000166
Matt Kraai91b28552001-04-23 18:53:07 +0000167 mode = source_stat.st_mode;
Matt Kraai01441032001-04-24 01:30:02 +0000168 if (!(flags & FILEUTILS_PRESERVE_STATUS))
Matt Kraai91b28552001-04-23 18:53:07 +0000169 mode = source_stat.st_mode & ~saved_umask;
Denis Vlasenko30bab712007-08-25 21:14:55 +0000170 /* Allow owner to access new dir (at least for now) */
Matt Kraai91b28552001-04-23 18:53:07 +0000171 mode |= S_IRWXU;
Matt Kraai91b28552001-04-23 18:53:07 +0000172 if (mkdir(dest, mode) < 0) {
173 umask(saved_umask);
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100174 bb_perror_msg("can't create directory '%s'", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000175 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000176 }
Matt Kraai91b28552001-04-23 18:53:07 +0000177 umask(saved_umask);
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000178 /* need stat info for add_to_ino_dev_hashtable */
179 if (lstat(dest, &dest_stat) < 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100180 bb_perror_msg("can't stat '%s'", dest);
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000181 return -1;
182 }
Eric Andersenaad1a882001-03-16 22:47:14 +0000183 }
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000184 /* remember (dev,inode) of each created dir.
185 * NULL: name is not remembered */
186 add_to_ino_dev_hashtable(&dest_stat, NULL);
Matt Kraaibf0a0102001-06-11 13:58:02 +0000187
Denis Vlasenko30bab712007-08-25 21:14:55 +0000188 /* Recursively copy files in SOURCE */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000189 dp = opendir(source);
190 if (dp == NULL) {
Denis Vlasenko30bab712007-08-25 21:14:55 +0000191 retval = -1;
192 goto preserve_mode_ugid_time;
Matt Kraai91b28552001-04-23 18:53:07 +0000193 }
194
195 while ((d = readdir(dp)) != NULL) {
196 char *new_source, *new_dest;
197
Glenn L McGrath393183d2003-05-26 14:07:50 +0000198 new_source = concat_subpath_file(source, d->d_name);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000199 if (new_source == NULL)
Matt Kraai91b28552001-04-23 18:53:07 +0000200 continue;
Matt Kraai91b28552001-04-23 18:53:07 +0000201 new_dest = concat_path_file(dest, d->d_name);
Denys Vlasenkoa40f0622010-01-15 22:05:07 +0100202 if (copy_file(new_source, new_dest, flags & ~FILEUTILS_DEREFERENCE_L0) < 0)
Denis Vlasenko30bab712007-08-25 21:14:55 +0000203 retval = -1;
Matt Kraai91b28552001-04-23 18:53:07 +0000204 free(new_source);
205 free(new_dest);
206 }
Eric Andersena9a220b2002-09-17 08:42:21 +0000207 closedir(dp);
Matt Kraai218aa372001-04-30 17:32:43 +0000208
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000209 if (!dest_exists
210 && chmod(dest, source_stat.st_mode & ~saved_umask) < 0
211 ) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100212 bb_perror_msg("can't preserve %s of '%s'", "permissions", dest);
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000213 /* retval = -1; - WRONG! copy *WAS* made */
Matt Kraai218aa372001-04-30 17:32:43 +0000214 }
Denis Vlasenko30bab712007-08-25 21:14:55 +0000215 goto preserve_mode_ugid_time;
216 }
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000217
Denys Vlasenko877dedb2016-05-27 00:46:38 +0200218 if (dest_exists) {
219 if (flags & FILEUTILS_UPDATE) {
220 if (source_stat.st_mtime <= dest_stat.st_mtime) {
221 return 0; /* source file must be newer */
222 }
223 }
224#if ENABLE_FEATURE_CP_LONG_OPTIONS
225 if (flags & FILEUTILS_RMDEST) {
226 ovr = ask_and_unlink(dest, flags);
227 if (ovr <= 0)
228 return ovr;
229 dest_exists = 0;
230 }
231#endif
232 }
233
Denis Vlasenko30bab712007-08-25 21:14:55 +0000234 if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000235 int (*lf)(const char *oldpath, const char *newpath);
236 make_links:
Denys Vlasenko6f58be02009-07-05 13:24:17 +0200237 /* Hmm... maybe
238 * if (DEREF && MAKE_SOFTLINK) source = realpath(source) ?
239 * (but realpath returns NULL on dangling symlinks...) */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000240 lf = (flags & FILEUTILS_MAKE_SOFTLINK) ? symlink : link;
241 if (lf(source, dest) < 0) {
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000242 ovr = ask_and_unlink(dest, flags);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000243 if (ovr <= 0)
244 return ovr;
245 if (lf(source, dest) < 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100246 bb_perror_msg("can't create link '%s'", dest);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000247 return -1;
248 }
249 }
Denis Vlasenko30bab712007-08-25 21:14:55 +0000250 /* _Not_ jumping to preserve_mode_ugid_time:
Denys Vlasenko6f58be02009-07-05 13:24:17 +0200251 * (sym)links don't have those */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000252 return 0;
Denis Vlasenko30bab712007-08-25 21:14:55 +0000253 }
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000254
Denis Vlasenkob9ad75f2008-03-28 17:49:31 +0000255 if (/* "cp thing1 thing2" without -R: just open and read() from thing1 */
256 !(flags & FILEUTILS_RECUR)
257 /* "cp [-opts] regular_file thing2" */
258 || S_ISREG(source_stat.st_mode)
259 /* DEREF uses stat, which never returns S_ISLNK() == true.
260 * So the below is never true: */
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000261 /* || (FLAGS_DEREF && S_ISLNK(source_stat.st_mode)) */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000262 ) {
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000263 int src_fd;
264 int dst_fd;
Denis Vlasenkob9ad75f2008-03-28 17:49:31 +0000265 mode_t new_mode;
266
267 if (!FLAGS_DEREF && S_ISLNK(source_stat.st_mode)) {
268 /* "cp -d symlink dst": create a link */
269 goto dont_cat;
270 }
Denis Vlasenko30bab712007-08-25 21:14:55 +0000271
272 if (ENABLE_FEATURE_PRESERVE_HARDLINKS && !FLAGS_DEREF) {
Denis Vlasenko0f214ec2007-08-27 17:02:19 +0000273 const char *link_target;
Denis Vlasenko30bab712007-08-25 21:14:55 +0000274 link_target = is_in_ino_dev_hashtable(&source_stat);
275 if (link_target) {
276 if (link(link_target, dest) < 0) {
277 ovr = ask_and_unlink(dest, flags);
278 if (ovr <= 0)
279 return ovr;
Denis Vlasenko75103842007-06-20 14:49:47 +0000280 if (link(link_target, dest) < 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100281 bb_perror_msg("can't create link '%s'", dest);
Denis Vlasenko30bab712007-08-25 21:14:55 +0000282 return -1;
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000283 }
Rob Landleyc9832742006-06-24 21:27:36 +0000284 }
Denis Vlasenko30bab712007-08-25 21:14:55 +0000285 return 0;
Matt Kraaiace02dc2001-12-17 15:26:36 +0000286 }
Rob Landleyc9832742006-06-24 21:27:36 +0000287 add_to_ino_dev_hashtable(&source_stat, dest);
Matt Kraaiace02dc2001-12-17 15:26:36 +0000288 }
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000289
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000290 src_fd = open_or_warn(source, O_RDONLY);
Denis Vlasenko30bab712007-08-25 21:14:55 +0000291 if (src_fd < 0)
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000292 return -1;
Matt Kraai14b7c5d2001-12-11 16:43:48 +0000293
Denis Vlasenkob9ad75f2008-03-28 17:49:31 +0000294 /* Do not try to open with weird mode fields */
295 new_mode = source_stat.st_mode;
296 if (!S_ISREG(source_stat.st_mode))
297 new_mode = 0666;
298
Denys Vlasenko98c50f92016-08-13 23:23:48 +0200299 if (ENABLE_FEATURE_NON_POSIX_CP || (flags & FILEUTILS_INTERACTIVE)) {
300 /*
301 * O_CREAT|O_EXCL: require that file did not exist before creation
302 */
Denis Vlasenkob9ad75f2008-03-28 17:49:31 +0000303 dst_fd = open(dest, O_WRONLY|O_CREAT|O_EXCL, new_mode);
Denys Vlasenko98c50f92016-08-13 23:23:48 +0200304 } else { /* POSIX, and not "cp -i" */
305 /*
306 * O_CREAT|O_TRUNC: create, or truncate (security problem versus (sym)link attacks)
307 */
308 dst_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, new_mode);
Denys Vlasenko6f58be02009-07-05 13:24:17 +0200309 }
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000310 if (dst_fd == -1) {
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000311 ovr = ask_and_unlink(dest, flags);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000312 if (ovr <= 0) {
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000313 close(src_fd);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000314 return ovr;
315 }
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000316 /* It shouldn't exist. If it exists, do not open (symlink attack?) */
Denis Vlasenkob9ad75f2008-03-28 17:49:31 +0000317 dst_fd = open3_or_warn(dest, O_WRONLY|O_CREAT|O_EXCL, new_mode);
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000318 if (dst_fd < 0) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000319 close(src_fd);
320 return -1;
Matt Kraai91b28552001-04-23 18:53:07 +0000321 }
322 }
323
Denis Vlasenko49622d72007-03-10 16:58:49 +0000324#if ENABLE_SELINUX
Denis Vlasenkob9ad75f2008-03-28 17:49:31 +0000325 if ((flags & (FILEUTILS_PRESERVE_SECURITY_CONTEXT|FILEUTILS_SET_SECURITY_CONTEXT))
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000326 && is_selinux_enabled() > 0
327 ) {
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000328 security_context_t con;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000329 if (getfscreatecon(&con) == -1) {
330 bb_perror_msg("getfscreatecon");
331 return -1;
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000332 }
Denis Vlasenko49622d72007-03-10 16:58:49 +0000333 if (con) {
Denis Vlasenko51742f42007-04-12 00:32:05 +0000334 if (setfilecon(dest, con) == -1) {
Denis Vlasenko49622d72007-03-10 16:58:49 +0000335 bb_perror_msg("setfilecon:%s,%s", dest, con);
336 freecon(con);
337 return -1;
338 }
339 freecon(con);
340 }
341 }
342#endif
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000343 if (bb_copyfd_eof(src_fd, dst_fd) == -1)
Denis Vlasenko30bab712007-08-25 21:14:55 +0000344 retval = -1;
Denys Vlasenko0cd445f2010-02-06 21:11:49 +0100345 /* Careful with writing... */
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000346 if (close(dst_fd) < 0) {
Denys Vlasenko0cd445f2010-02-06 21:11:49 +0100347 bb_perror_msg("error writing to '%s'", dest);
Denis Vlasenko30bab712007-08-25 21:14:55 +0000348 retval = -1;
Matt Kraai91b28552001-04-23 18:53:07 +0000349 }
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000350 /* ...but read size is already checked by bb_copyfd_eof */
351 close(src_fd);
Denis Vlasenkob9ad75f2008-03-28 17:49:31 +0000352 /* "cp /dev/something new_file" should not
353 * copy mode of /dev/something */
354 if (!S_ISREG(source_stat.st_mode))
355 return retval;
Denis Vlasenko30bab712007-08-25 21:14:55 +0000356 goto preserve_mode_ugid_time;
357 }
Denis Vlasenkob9ad75f2008-03-28 17:49:31 +0000358 dont_cat:
Eric Andersen403a73a2002-09-16 09:23:22 +0000359
Denis Vlasenko30bab712007-08-25 21:14:55 +0000360 /* Source is a symlink or a special file */
361 /* We are lazy here, a bit lax with races... */
362 if (dest_exists) {
363 errno = EEXIST;
364 ovr = ask_and_unlink(dest, flags);
365 if (ovr <= 0)
366 return ovr;
367 }
368 if (S_ISLNK(source_stat.st_mode)) {
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000369 char *lpath = xmalloc_readlink_or_warn(source);
370 if (lpath) {
371 int r = symlink(lpath, dest);
Rob Landley2f309322005-11-01 21:55:14 +0000372 free(lpath);
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000373 if (r < 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100374 bb_perror_msg("can't create symlink '%s'", dest);
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000375 return -1;
376 }
377 if (flags & FILEUTILS_PRESERVE_STATUS)
378 if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100379 bb_perror_msg("can't preserve %s of '%s'", "ownership", dest);
Denis Vlasenko30bab712007-08-25 21:14:55 +0000380 }
Denis Vlasenko30bab712007-08-25 21:14:55 +0000381 /* _Not_ jumping to preserve_mode_ugid_time:
382 * symlinks don't have those */
Denys Vlasenkoca003382016-07-14 20:58:39 +0200383 goto verb_and_exit;
Denis Vlasenko30bab712007-08-25 21:14:55 +0000384 }
385 if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode)
386 || S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode)
387 ) {
388 if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100389 bb_perror_msg("can't create '%s'", dest);
Denis Vlasenko30bab712007-08-25 21:14:55 +0000390 return -1;
Rob Landley2f309322005-11-01 21:55:14 +0000391 }
Eric Andersena9a220b2002-09-17 08:42:21 +0000392 } else {
Denis Vlasenko30bab712007-08-25 21:14:55 +0000393 bb_error_msg("unrecognized file '%s' with mode %x", source, source_stat.st_mode);
Eric Andersena9a220b2002-09-17 08:42:21 +0000394 return -1;
Eric Andersenaad1a882001-03-16 22:47:14 +0000395 }
396
Denis Vlasenko30bab712007-08-25 21:14:55 +0000397 preserve_mode_ugid_time:
Matt Kraai91b28552001-04-23 18:53:07 +0000398
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000399 if (flags & FILEUTILS_PRESERVE_STATUS
400 /* Cannot happen: */
401 /* && !(flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) */
402 ) {
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100403 struct timeval times[2];
Denys Vlasenko389cca42009-11-15 02:28:56 +0100404
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100405 times[1].tv_sec = times[0].tv_sec = source_stat.st_mtime;
406 times[1].tv_usec = times[0].tv_usec = 0;
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000407 /* BTW, utimes sets usec-precision time - just FYI */
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100408 if (utimes(dest, times) < 0)
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100409 bb_perror_msg("can't preserve %s of '%s'", "times", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000410 if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
411 source_stat.st_mode &= ~(S_ISUID | S_ISGID);
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100412 bb_perror_msg("can't preserve %s of '%s'", "ownership", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000413 }
414 if (chmod(dest, source_stat.st_mode) < 0)
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100415 bb_perror_msg("can't preserve %s of '%s'", "permissions", dest);
Eric Andersenaad1a882001-03-16 22:47:14 +0000416 }
417
Denys Vlasenkoca003382016-07-14 20:58:39 +0200418 verb_and_exit:
Denys Vlasenko17f84182014-05-19 16:23:50 +0200419 if (flags & FILEUTILS_VERBOSE) {
420 printf("'%s' -> '%s'\n", source, dest);
421 }
422
Denis Vlasenko30bab712007-08-25 21:14:55 +0000423 return retval;
Eric Andersenaad1a882001-03-16 22:47:14 +0000424}