blob: ed765d8f0c4b6bccdfd40bbb4ddded90e2052737 [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 *
Rob Landley2f309322005-11-01 21:55:14 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
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 Vlasenko6f58be02009-07-05 13:24:17 +020067 return 1; /* ok (to try again) */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000068}
69
Denis Vlasenko54d14ca2007-03-15 13:33:37 +000070/* Return:
71 * -1 error, copy not made
72 * 0 copy is made or user answered "no" in interactive mode
73 * (failures to preserve mode/owner/times are not reported in exit code)
74 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000075int FAST_FUNC copy_file(const char *source, const char *dest, int flags)
Eric Andersenaad1a882001-03-16 22:47:14 +000076{
Denis Vlasenko30bab712007-08-25 21:14:55 +000077 /* This is a recursive function, try to minimize stack usage */
78 /* NB: each struct stat is ~100 bytes */
Matt Kraai91b28552001-04-23 18:53:07 +000079 struct stat source_stat;
80 struct stat dest_stat;
Denis Vlasenko30bab712007-08-25 21:14:55 +000081 signed char retval = 0;
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000082 signed char dest_exists = 0;
83 signed char ovr;
Eric Andersenaad1a882001-03-16 22:47:14 +000084
Denis Vlasenkob9ad75f2008-03-28 17:49:31 +000085/* Inverse of cp -d ("cp without -d") */
Denys Vlasenkoa40f0622010-01-15 22:05:07 +010086#define FLAGS_DEREF (flags & (FILEUTILS_DEREFERENCE + FILEUTILS_DEREFERENCE_L0))
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000087
88 if ((FLAGS_DEREF ? stat : lstat)(source, &source_stat) < 0) {
Denys Vlasenko6f58be02009-07-05 13:24:17 +020089 /* This may be a dangling symlink.
90 * Making [sym]links to dangling symlinks works, so... */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000091 if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK))
92 goto make_links;
Denys Vlasenko6331cf02009-11-13 09:08:27 +010093 bb_perror_msg("can't stat '%s'", source);
Matt Kraai91b28552001-04-23 18:53:07 +000094 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +000095 }
Eric Andersenaad1a882001-03-16 22:47:14 +000096
Eric Andersen02b8dfc2002-09-16 10:23:38 +000097 if (lstat(dest, &dest_stat) < 0) {
Matt Kraai91b28552001-04-23 18:53:07 +000098 if (errno != ENOENT) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +010099 bb_perror_msg("can't stat '%s'", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000100 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000101 }
Eric Andersena9a220b2002-09-17 08:42:21 +0000102 } else {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000103 if (source_stat.st_dev == dest_stat.st_dev
104 && source_stat.st_ino == dest_stat.st_ino
105 ) {
106 bb_error_msg("'%s' and '%s' are the same file", source, dest);
Mike Frysinger2ed05ab2005-04-14 02:52:50 +0000107 return -1;
108 }
Eric Andersena9a220b2002-09-17 08:42:21 +0000109 dest_exists = 1;
110 }
Eric Andersenaad1a882001-03-16 22:47:14 +0000111
Denis Vlasenko49622d72007-03-10 16:58:49 +0000112#if ENABLE_SELINUX
113 if ((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) && is_selinux_enabled() > 0) {
114 security_context_t con;
115 if (lgetfilecon(source, &con) >= 0) {
116 if (setfscreatecon(con) < 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100117 bb_perror_msg("can't set setfscreatecon %s", con);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000118 freecon(con);
119 return -1;
120 }
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000121 } else if (errno == ENOTSUP || errno == ENODATA) {
122 setfscreatecon_or_die(NULL);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000123 } else {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100124 bb_perror_msg("can't lgetfilecon %s", source);
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000125 return -1;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000126 }
127 }
128#endif
129
Matt Kraai91b28552001-04-23 18:53:07 +0000130 if (S_ISDIR(source_stat.st_mode)) {
131 DIR *dp;
Denis Vlasenko0f214ec2007-08-27 17:02:19 +0000132 const char *tp;
Matt Kraai91b28552001-04-23 18:53:07 +0000133 struct dirent *d;
Matt Kraai218aa372001-04-30 17:32:43 +0000134 mode_t saved_umask = 0;
Matt Kraai91b28552001-04-23 18:53:07 +0000135
Matt Kraai01441032001-04-24 01:30:02 +0000136 if (!(flags & FILEUTILS_RECUR)) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000137 bb_error_msg("omitting directory '%s'", source);
Matt Kraai91b28552001-04-23 18:53:07 +0000138 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000139 }
Eric Andersenaad1a882001-03-16 22:47:14 +0000140
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000141 /* Did we ever create source ourself before? */
Denis Vlasenko0f214ec2007-08-27 17:02:19 +0000142 tp = is_in_ino_dev_hashtable(&source_stat);
143 if (tp) {
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000144 /* We did! it's a recursion! man the lifeboats... */
145 bb_error_msg("recursion detected, omitting directory '%s'",
Denis Vlasenko0f214ec2007-08-27 17:02:19 +0000146 source);
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000147 return -1;
148 }
149
Denis Vlasenko30bab712007-08-25 21:14:55 +0000150 /* Create DEST */
Matt Kraai91b28552001-04-23 18:53:07 +0000151 if (dest_exists) {
152 if (!S_ISDIR(dest_stat.st_mode)) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000153 bb_error_msg("target '%s' is not a directory", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000154 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000155 }
Denis Vlasenkoa9335ea2007-09-11 16:28:14 +0000156 /* race here: user can substitute a symlink between
157 * this check and actual creation of files inside dest */
Matt Kraai91b28552001-04-23 18:53:07 +0000158 } else {
Matt Kraai218aa372001-04-30 17:32:43 +0000159 mode_t mode;
Matt Kraai91b28552001-04-23 18:53:07 +0000160 saved_umask = umask(0);
Eric Andersenaad1a882001-03-16 22:47:14 +0000161
Matt Kraai91b28552001-04-23 18:53:07 +0000162 mode = source_stat.st_mode;
Matt Kraai01441032001-04-24 01:30:02 +0000163 if (!(flags & FILEUTILS_PRESERVE_STATUS))
Matt Kraai91b28552001-04-23 18:53:07 +0000164 mode = source_stat.st_mode & ~saved_umask;
Denis Vlasenko30bab712007-08-25 21:14:55 +0000165 /* Allow owner to access new dir (at least for now) */
Matt Kraai91b28552001-04-23 18:53:07 +0000166 mode |= S_IRWXU;
Matt Kraai91b28552001-04-23 18:53:07 +0000167 if (mkdir(dest, mode) < 0) {
168 umask(saved_umask);
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100169 bb_perror_msg("can't create directory '%s'", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000170 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000171 }
Matt Kraai91b28552001-04-23 18:53:07 +0000172 umask(saved_umask);
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000173 /* need stat info for add_to_ino_dev_hashtable */
174 if (lstat(dest, &dest_stat) < 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100175 bb_perror_msg("can't stat '%s'", dest);
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000176 return -1;
177 }
Eric Andersenaad1a882001-03-16 22:47:14 +0000178 }
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000179 /* remember (dev,inode) of each created dir.
180 * NULL: name is not remembered */
181 add_to_ino_dev_hashtable(&dest_stat, NULL);
Matt Kraaibf0a0102001-06-11 13:58:02 +0000182
Denis Vlasenko30bab712007-08-25 21:14:55 +0000183 /* Recursively copy files in SOURCE */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000184 dp = opendir(source);
185 if (dp == NULL) {
Denis Vlasenko30bab712007-08-25 21:14:55 +0000186 retval = -1;
187 goto preserve_mode_ugid_time;
Matt Kraai91b28552001-04-23 18:53:07 +0000188 }
189
190 while ((d = readdir(dp)) != NULL) {
191 char *new_source, *new_dest;
192
Glenn L McGrath393183d2003-05-26 14:07:50 +0000193 new_source = concat_subpath_file(source, d->d_name);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000194 if (new_source == NULL)
Matt Kraai91b28552001-04-23 18:53:07 +0000195 continue;
Matt Kraai91b28552001-04-23 18:53:07 +0000196 new_dest = concat_path_file(dest, d->d_name);
Denys Vlasenkoa40f0622010-01-15 22:05:07 +0100197 if (copy_file(new_source, new_dest, flags & ~FILEUTILS_DEREFERENCE_L0) < 0)
Denis Vlasenko30bab712007-08-25 21:14:55 +0000198 retval = -1;
Matt Kraai91b28552001-04-23 18:53:07 +0000199 free(new_source);
200 free(new_dest);
201 }
Eric Andersena9a220b2002-09-17 08:42:21 +0000202 closedir(dp);
Matt Kraai218aa372001-04-30 17:32:43 +0000203
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000204 if (!dest_exists
205 && chmod(dest, source_stat.st_mode & ~saved_umask) < 0
206 ) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100207 bb_perror_msg("can't preserve %s of '%s'", "permissions", dest);
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000208 /* retval = -1; - WRONG! copy *WAS* made */
Matt Kraai218aa372001-04-30 17:32:43 +0000209 }
Denis Vlasenko30bab712007-08-25 21:14:55 +0000210 goto preserve_mode_ugid_time;
211 }
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000212
Denis Vlasenko30bab712007-08-25 21:14:55 +0000213 if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000214 int (*lf)(const char *oldpath, const char *newpath);
215 make_links:
Denys Vlasenko6f58be02009-07-05 13:24:17 +0200216 /* Hmm... maybe
217 * if (DEREF && MAKE_SOFTLINK) source = realpath(source) ?
218 * (but realpath returns NULL on dangling symlinks...) */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000219 lf = (flags & FILEUTILS_MAKE_SOFTLINK) ? symlink : link;
220 if (lf(source, dest) < 0) {
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000221 ovr = ask_and_unlink(dest, flags);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000222 if (ovr <= 0)
223 return ovr;
224 if (lf(source, dest) < 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100225 bb_perror_msg("can't create link '%s'", dest);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000226 return -1;
227 }
228 }
Denis Vlasenko30bab712007-08-25 21:14:55 +0000229 /* _Not_ jumping to preserve_mode_ugid_time:
Denys Vlasenko6f58be02009-07-05 13:24:17 +0200230 * (sym)links don't have those */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000231 return 0;
Denis Vlasenko30bab712007-08-25 21:14:55 +0000232 }
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000233
Denis Vlasenkob9ad75f2008-03-28 17:49:31 +0000234 if (/* "cp thing1 thing2" without -R: just open and read() from thing1 */
235 !(flags & FILEUTILS_RECUR)
236 /* "cp [-opts] regular_file thing2" */
237 || S_ISREG(source_stat.st_mode)
238 /* DEREF uses stat, which never returns S_ISLNK() == true.
239 * So the below is never true: */
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000240 /* || (FLAGS_DEREF && S_ISLNK(source_stat.st_mode)) */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000241 ) {
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000242 int src_fd;
243 int dst_fd;
Denis Vlasenkob9ad75f2008-03-28 17:49:31 +0000244 mode_t new_mode;
245
246 if (!FLAGS_DEREF && S_ISLNK(source_stat.st_mode)) {
247 /* "cp -d symlink dst": create a link */
248 goto dont_cat;
249 }
Denis Vlasenko30bab712007-08-25 21:14:55 +0000250
251 if (ENABLE_FEATURE_PRESERVE_HARDLINKS && !FLAGS_DEREF) {
Denis Vlasenko0f214ec2007-08-27 17:02:19 +0000252 const char *link_target;
Denis Vlasenko30bab712007-08-25 21:14:55 +0000253 link_target = is_in_ino_dev_hashtable(&source_stat);
254 if (link_target) {
255 if (link(link_target, dest) < 0) {
256 ovr = ask_and_unlink(dest, flags);
257 if (ovr <= 0)
258 return ovr;
Denis Vlasenko75103842007-06-20 14:49:47 +0000259 if (link(link_target, dest) < 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100260 bb_perror_msg("can't create link '%s'", dest);
Denis Vlasenko30bab712007-08-25 21:14:55 +0000261 return -1;
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000262 }
Rob Landleyc9832742006-06-24 21:27:36 +0000263 }
Denis Vlasenko30bab712007-08-25 21:14:55 +0000264 return 0;
Matt Kraaiace02dc2001-12-17 15:26:36 +0000265 }
Rob Landleyc9832742006-06-24 21:27:36 +0000266 add_to_ino_dev_hashtable(&source_stat, dest);
Matt Kraaiace02dc2001-12-17 15:26:36 +0000267 }
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000268
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000269 src_fd = open_or_warn(source, O_RDONLY);
Denis Vlasenko30bab712007-08-25 21:14:55 +0000270 if (src_fd < 0)
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000271 return -1;
Matt Kraai14b7c5d2001-12-11 16:43:48 +0000272
Denis Vlasenkob9ad75f2008-03-28 17:49:31 +0000273 /* Do not try to open with weird mode fields */
274 new_mode = source_stat.st_mode;
275 if (!S_ISREG(source_stat.st_mode))
276 new_mode = 0666;
277
Denys Vlasenko6f58be02009-07-05 13:24:17 +0200278 // POSIX way is a security problem versus (sym)link attacks
279 if (!ENABLE_FEATURE_NON_POSIX_CP) {
Denis Vlasenkob9ad75f2008-03-28 17:49:31 +0000280 dst_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, new_mode);
Denys Vlasenko6f58be02009-07-05 13:24:17 +0200281 } else { /* safe way: */
Denis Vlasenkob9ad75f2008-03-28 17:49:31 +0000282 dst_fd = open(dest, O_WRONLY|O_CREAT|O_EXCL, new_mode);
Denys Vlasenko6f58be02009-07-05 13:24:17 +0200283 }
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000284 if (dst_fd == -1) {
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000285 ovr = ask_and_unlink(dest, flags);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000286 if (ovr <= 0) {
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000287 close(src_fd);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000288 return ovr;
289 }
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000290 /* It shouldn't exist. If it exists, do not open (symlink attack?) */
Denis Vlasenkob9ad75f2008-03-28 17:49:31 +0000291 dst_fd = open3_or_warn(dest, O_WRONLY|O_CREAT|O_EXCL, new_mode);
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000292 if (dst_fd < 0) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000293 close(src_fd);
294 return -1;
Matt Kraai91b28552001-04-23 18:53:07 +0000295 }
296 }
297
Denis Vlasenko49622d72007-03-10 16:58:49 +0000298#if ENABLE_SELINUX
Denis Vlasenkob9ad75f2008-03-28 17:49:31 +0000299 if ((flags & (FILEUTILS_PRESERVE_SECURITY_CONTEXT|FILEUTILS_SET_SECURITY_CONTEXT))
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000300 && is_selinux_enabled() > 0
301 ) {
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000302 security_context_t con;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000303 if (getfscreatecon(&con) == -1) {
304 bb_perror_msg("getfscreatecon");
305 return -1;
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000306 }
Denis Vlasenko49622d72007-03-10 16:58:49 +0000307 if (con) {
Denis Vlasenko51742f42007-04-12 00:32:05 +0000308 if (setfilecon(dest, con) == -1) {
Denis Vlasenko49622d72007-03-10 16:58:49 +0000309 bb_perror_msg("setfilecon:%s,%s", dest, con);
310 freecon(con);
311 return -1;
312 }
313 freecon(con);
314 }
315 }
316#endif
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000317 if (bb_copyfd_eof(src_fd, dst_fd) == -1)
Denis Vlasenko30bab712007-08-25 21:14:55 +0000318 retval = -1;
Denys Vlasenko0cd445f2010-02-06 21:11:49 +0100319 /* Careful with writing... */
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000320 if (close(dst_fd) < 0) {
Denys Vlasenko0cd445f2010-02-06 21:11:49 +0100321 bb_perror_msg("error writing to '%s'", dest);
Denis Vlasenko30bab712007-08-25 21:14:55 +0000322 retval = -1;
Matt Kraai91b28552001-04-23 18:53:07 +0000323 }
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000324 /* ...but read size is already checked by bb_copyfd_eof */
325 close(src_fd);
Denis Vlasenkob9ad75f2008-03-28 17:49:31 +0000326 /* "cp /dev/something new_file" should not
327 * copy mode of /dev/something */
328 if (!S_ISREG(source_stat.st_mode))
329 return retval;
Denis Vlasenko30bab712007-08-25 21:14:55 +0000330 goto preserve_mode_ugid_time;
331 }
Denis Vlasenkob9ad75f2008-03-28 17:49:31 +0000332 dont_cat:
Eric Andersen403a73a2002-09-16 09:23:22 +0000333
Denis Vlasenko30bab712007-08-25 21:14:55 +0000334 /* Source is a symlink or a special file */
335 /* We are lazy here, a bit lax with races... */
336 if (dest_exists) {
337 errno = EEXIST;
338 ovr = ask_and_unlink(dest, flags);
339 if (ovr <= 0)
340 return ovr;
341 }
342 if (S_ISLNK(source_stat.st_mode)) {
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000343 char *lpath = xmalloc_readlink_or_warn(source);
344 if (lpath) {
345 int r = symlink(lpath, dest);
Rob Landley2f309322005-11-01 21:55:14 +0000346 free(lpath);
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000347 if (r < 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100348 bb_perror_msg("can't create symlink '%s'", dest);
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000349 return -1;
350 }
351 if (flags & FILEUTILS_PRESERVE_STATUS)
352 if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100353 bb_perror_msg("can't preserve %s of '%s'", "ownership", dest);
Denis Vlasenko30bab712007-08-25 21:14:55 +0000354 }
Denis Vlasenko30bab712007-08-25 21:14:55 +0000355 /* _Not_ jumping to preserve_mode_ugid_time:
356 * symlinks don't have those */
357 return 0;
358 }
359 if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode)
360 || S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode)
361 ) {
362 if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100363 bb_perror_msg("can't create '%s'", dest);
Denis Vlasenko30bab712007-08-25 21:14:55 +0000364 return -1;
Rob Landley2f309322005-11-01 21:55:14 +0000365 }
Eric Andersena9a220b2002-09-17 08:42:21 +0000366 } else {
Denis Vlasenko30bab712007-08-25 21:14:55 +0000367 bb_error_msg("unrecognized file '%s' with mode %x", source, source_stat.st_mode);
Eric Andersena9a220b2002-09-17 08:42:21 +0000368 return -1;
Eric Andersenaad1a882001-03-16 22:47:14 +0000369 }
370
Denis Vlasenko30bab712007-08-25 21:14:55 +0000371 preserve_mode_ugid_time:
Matt Kraai91b28552001-04-23 18:53:07 +0000372
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000373 if (flags & FILEUTILS_PRESERVE_STATUS
374 /* Cannot happen: */
375 /* && !(flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) */
376 ) {
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100377 struct timeval times[2];
Denys Vlasenko389cca42009-11-15 02:28:56 +0100378
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100379 times[1].tv_sec = times[0].tv_sec = source_stat.st_mtime;
380 times[1].tv_usec = times[0].tv_usec = 0;
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000381 /* BTW, utimes sets usec-precision time - just FYI */
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100382 if (utimes(dest, times) < 0)
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100383 bb_perror_msg("can't preserve %s of '%s'", "times", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000384 if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
385 source_stat.st_mode &= ~(S_ISUID | S_ISGID);
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100386 bb_perror_msg("can't preserve %s of '%s'", "ownership", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000387 }
388 if (chmod(dest, source_stat.st_mode) < 0)
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100389 bb_perror_msg("can't preserve %s of '%s'", "permissions", dest);
Eric Andersenaad1a882001-03-16 22:47:14 +0000390 }
391
Denis Vlasenko30bab712007-08-25 21:14:55 +0000392 return retval;
Eric Andersenaad1a882001-03-16 22:47:14 +0000393}