blob: d804eccfaee8009cf55f6957233f6685b9f1e1f9 [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 *
Eric Andersenaad1a882001-03-16 22:47:14 +000010 */
11
Rob Landleyc9832742006-06-24 21:27:36 +000012#include "libbb.h"
Eric Andersenaad1a882001-03-16 22:47:14 +000013
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:
18// If dest is a softlink, we overwrite softlink's destination!
19// (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...
Denis Vlasenkofa050742007-09-11 10:39:13 +000022//
23// NB: we have special code which still allows for "cp file /dev/node"
24// to work POSIX-ly (the only realistic case where it makes sense)
Denis Vlasenko54d14ca2007-03-15 13:33:37 +000025
26#define DO_POSIX_CP 0 /* 1 - POSIX behavior, 0 - safe behavior */
27
Denis Vlasenko3d829622007-08-24 14:23:57 +000028// errno must be set to relevant value ("why we cannot create dest?")
29// for POSIX mode to give reasonable error message
Denis Vlasenko54d14ca2007-03-15 13:33:37 +000030static int ask_and_unlink(const char *dest, int flags)
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000031{
Denis Vlasenkod5fe8802008-02-13 16:52:00 +000032 int e = errno;
Denis Vlasenko54d14ca2007-03-15 13:33:37 +000033#if DO_POSIX_CP
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000034 if (!(flags & (FILEUTILS_FORCE|FILEUTILS_INTERACTIVE))) {
Denis Vlasenko3d829622007-08-24 14:23:57 +000035 // Either it exists, or the *path* doesnt exist
36 bb_perror_msg("cannot create '%s'", dest);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000037 return -1;
38 }
Denis Vlasenko54d14ca2007-03-15 13:33:37 +000039#endif
Denis Vlasenko3d829622007-08-24 14:23:57 +000040 // If !DO_POSIX_CP, act as if -f is always in effect - we don't want
41 // "cannot create" msg, we want unlink to be done (silently unless -i).
Denis Vlasenko54d14ca2007-03-15 13:33:37 +000042
43 // TODO: maybe we should do it only if ctty is present?
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000044 if (flags & FILEUTILS_INTERACTIVE) {
Denis Vlasenko54d14ca2007-03-15 13:33:37 +000045 // We would not do POSIX insanity. -i asks,
46 // then _unlinks_ the offender. Presto.
Denis Vlasenko3d829622007-08-24 14:23:57 +000047 // (No "opening without O_EXCL", no "unlink only if -f")
Denis Vlasenko54d14ca2007-03-15 13:33:37 +000048 // Or else we will end up having 3 open()s!
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000049 fprintf(stderr, "%s: overwrite '%s'? ", applet_name, dest);
50 if (!bb_ask_confirmation())
51 return 0; // not allowed to overwrite
52 }
53 if (unlink(dest) < 0) {
Denis Vlasenkod5fe8802008-02-13 16:52:00 +000054#if ENABLE_FEATURE_VERBOSE_CP_MESSAGE
55 if (e == errno && e == ENOENT) {
56 /* e == ENOTDIR is similar: path has non-dir component,
57 * but in this case we don't even reach copy_file() */
58 bb_error_msg("cannot create '%s': Path does not exist", dest);
59 return -1; // error
60 }
61#endif
62 errno = e;
63 bb_perror_msg("cannot create '%s'", dest);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000064 return -1; // error
65 }
66 return 1; // ok (to try again)
67}
68
Denis Vlasenko54d14ca2007-03-15 13:33:37 +000069/* Return:
70 * -1 error, copy not made
71 * 0 copy is made or user answered "no" in interactive mode
72 * (failures to preserve mode/owner/times are not reported in exit code)
73 */
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000074int FAST_FUNC copy_file(const char *source, const char *dest, int flags)
Eric Andersenaad1a882001-03-16 22:47:14 +000075{
Denis Vlasenko30bab712007-08-25 21:14:55 +000076 /* This is a recursive function, try to minimize stack usage */
77 /* NB: each struct stat is ~100 bytes */
Matt Kraai91b28552001-04-23 18:53:07 +000078 struct stat source_stat;
79 struct stat dest_stat;
Denis Vlasenko30bab712007-08-25 21:14:55 +000080 signed char retval = 0;
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000081 signed char dest_exists = 0;
82 signed char ovr;
Eric Andersenaad1a882001-03-16 22:47:14 +000083
Denis Vlasenkob9ad75f2008-03-28 17:49:31 +000084/* Inverse of cp -d ("cp without -d") */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000085#define FLAGS_DEREF (flags & FILEUTILS_DEREFERENCE)
86
87 if ((FLAGS_DEREF ? stat : lstat)(source, &source_stat) < 0) {
88 // This may be a dangling symlink.
89 // Making [sym]links to dangling symlinks works, so...
90 if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK))
91 goto make_links;
92 bb_perror_msg("cannot stat '%s'", source);
Matt Kraai91b28552001-04-23 18:53:07 +000093 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +000094 }
Eric Andersenaad1a882001-03-16 22:47:14 +000095
Eric Andersen02b8dfc2002-09-16 10:23:38 +000096 if (lstat(dest, &dest_stat) < 0) {
Matt Kraai91b28552001-04-23 18:53:07 +000097 if (errno != ENOENT) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000098 bb_perror_msg("cannot stat '%s'", dest);
Matt Kraai91b28552001-04-23 18:53:07 +000099 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000100 }
Eric Andersena9a220b2002-09-17 08:42:21 +0000101 } else {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000102 if (source_stat.st_dev == dest_stat.st_dev
103 && source_stat.st_ino == dest_stat.st_ino
104 ) {
105 bb_error_msg("'%s' and '%s' are the same file", source, dest);
Mike Frysinger2ed05ab2005-04-14 02:52:50 +0000106 return -1;
107 }
Eric Andersena9a220b2002-09-17 08:42:21 +0000108 dest_exists = 1;
109 }
Eric Andersenaad1a882001-03-16 22:47:14 +0000110
Denis Vlasenko49622d72007-03-10 16:58:49 +0000111#if ENABLE_SELINUX
112 if ((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) && is_selinux_enabled() > 0) {
113 security_context_t con;
114 if (lgetfilecon(source, &con) >= 0) {
115 if (setfscreatecon(con) < 0) {
116 bb_perror_msg("cannot set setfscreatecon %s", con);
117 freecon(con);
118 return -1;
119 }
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000120 } else if (errno == ENOTSUP || errno == ENODATA) {
121 setfscreatecon_or_die(NULL);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000122 } else {
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000123 bb_perror_msg("cannot lgetfilecon %s", source);
124 return -1;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000125 }
126 }
127#endif
128
Matt Kraai91b28552001-04-23 18:53:07 +0000129 if (S_ISDIR(source_stat.st_mode)) {
130 DIR *dp;
Denis Vlasenko0f214ec2007-08-27 17:02:19 +0000131 const char *tp;
Matt Kraai91b28552001-04-23 18:53:07 +0000132 struct dirent *d;
Matt Kraai218aa372001-04-30 17:32:43 +0000133 mode_t saved_umask = 0;
Matt Kraai91b28552001-04-23 18:53:07 +0000134
Matt Kraai01441032001-04-24 01:30:02 +0000135 if (!(flags & FILEUTILS_RECUR)) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000136 bb_error_msg("omitting directory '%s'", source);
Matt Kraai91b28552001-04-23 18:53:07 +0000137 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000138 }
Eric Andersenaad1a882001-03-16 22:47:14 +0000139
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000140 /* Did we ever create source ourself before? */
Denis Vlasenko0f214ec2007-08-27 17:02:19 +0000141 tp = is_in_ino_dev_hashtable(&source_stat);
142 if (tp) {
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000143 /* We did! it's a recursion! man the lifeboats... */
144 bb_error_msg("recursion detected, omitting directory '%s'",
Denis Vlasenko0f214ec2007-08-27 17:02:19 +0000145 source);
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000146 return -1;
147 }
148
Denis Vlasenko30bab712007-08-25 21:14:55 +0000149 /* Create DEST */
Matt Kraai91b28552001-04-23 18:53:07 +0000150 if (dest_exists) {
151 if (!S_ISDIR(dest_stat.st_mode)) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000152 bb_error_msg("target '%s' is not a directory", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000153 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000154 }
Denis Vlasenkoa9335ea2007-09-11 16:28:14 +0000155 /* race here: user can substitute a symlink between
156 * this check and actual creation of files inside dest */
Matt Kraai91b28552001-04-23 18:53:07 +0000157 } else {
Matt Kraai218aa372001-04-30 17:32:43 +0000158 mode_t mode;
Matt Kraai91b28552001-04-23 18:53:07 +0000159 saved_umask = umask(0);
Eric Andersenaad1a882001-03-16 22:47:14 +0000160
Matt Kraai91b28552001-04-23 18:53:07 +0000161 mode = source_stat.st_mode;
Matt Kraai01441032001-04-24 01:30:02 +0000162 if (!(flags & FILEUTILS_PRESERVE_STATUS))
Matt Kraai91b28552001-04-23 18:53:07 +0000163 mode = source_stat.st_mode & ~saved_umask;
Denis Vlasenko30bab712007-08-25 21:14:55 +0000164 /* Allow owner to access new dir (at least for now) */
Matt Kraai91b28552001-04-23 18:53:07 +0000165 mode |= S_IRWXU;
Matt Kraai91b28552001-04-23 18:53:07 +0000166 if (mkdir(dest, mode) < 0) {
167 umask(saved_umask);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000168 bb_perror_msg("cannot create directory '%s'", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000169 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000170 }
Matt Kraai91b28552001-04-23 18:53:07 +0000171 umask(saved_umask);
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000172 /* need stat info for add_to_ino_dev_hashtable */
173 if (lstat(dest, &dest_stat) < 0) {
174 bb_perror_msg("cannot stat '%s'", dest);
175 return -1;
176 }
Eric Andersenaad1a882001-03-16 22:47:14 +0000177 }
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000178 /* remember (dev,inode) of each created dir.
179 * NULL: name is not remembered */
180 add_to_ino_dev_hashtable(&dest_stat, NULL);
Matt Kraaibf0a0102001-06-11 13:58:02 +0000181
Denis Vlasenko30bab712007-08-25 21:14:55 +0000182 /* Recursively copy files in SOURCE */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000183 dp = opendir(source);
184 if (dp == NULL) {
Denis Vlasenko30bab712007-08-25 21:14:55 +0000185 retval = -1;
186 goto preserve_mode_ugid_time;
Matt Kraai91b28552001-04-23 18:53:07 +0000187 }
188
189 while ((d = readdir(dp)) != NULL) {
190 char *new_source, *new_dest;
191
Glenn L McGrath393183d2003-05-26 14:07:50 +0000192 new_source = concat_subpath_file(source, d->d_name);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000193 if (new_source == NULL)
Matt Kraai91b28552001-04-23 18:53:07 +0000194 continue;
Matt Kraai91b28552001-04-23 18:53:07 +0000195 new_dest = concat_path_file(dest, d->d_name);
196 if (copy_file(new_source, new_dest, flags) < 0)
Denis Vlasenko30bab712007-08-25 21:14:55 +0000197 retval = -1;
Matt Kraai91b28552001-04-23 18:53:07 +0000198 free(new_source);
199 free(new_dest);
200 }
Eric Andersena9a220b2002-09-17 08:42:21 +0000201 closedir(dp);
Matt Kraai218aa372001-04-30 17:32:43 +0000202
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000203 if (!dest_exists
204 && chmod(dest, source_stat.st_mode & ~saved_umask) < 0
205 ) {
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000206 bb_perror_msg("cannot preserve %s of '%s'", "permissions", dest);
207 /* retval = -1; - WRONG! copy *WAS* made */
Matt Kraai218aa372001-04-30 17:32:43 +0000208 }
Denis Vlasenko30bab712007-08-25 21:14:55 +0000209 goto preserve_mode_ugid_time;
210 }
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000211
Denis Vlasenko30bab712007-08-25 21:14:55 +0000212 if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000213 int (*lf)(const char *oldpath, const char *newpath);
214 make_links:
215 // Hmm... maybe
216 // if (DEREF && MAKE_SOFTLINK) source = realpath(source) ?
217 // (but realpath returns NULL on dangling symlinks...)
218 lf = (flags & FILEUTILS_MAKE_SOFTLINK) ? symlink : link;
219 if (lf(source, dest) < 0) {
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000220 ovr = ask_and_unlink(dest, flags);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000221 if (ovr <= 0)
222 return ovr;
223 if (lf(source, dest) < 0) {
224 bb_perror_msg("cannot create link '%s'", dest);
225 return -1;
226 }
227 }
Denis Vlasenko30bab712007-08-25 21:14:55 +0000228 /* _Not_ jumping to preserve_mode_ugid_time:
229 * hard/softlinks don't have those */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000230 return 0;
Denis Vlasenko30bab712007-08-25 21:14:55 +0000231 }
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000232
Denis Vlasenkob9ad75f2008-03-28 17:49:31 +0000233 if (/* "cp thing1 thing2" without -R: just open and read() from thing1 */
234 !(flags & FILEUTILS_RECUR)
235 /* "cp [-opts] regular_file thing2" */
236 || S_ISREG(source_stat.st_mode)
237 /* DEREF uses stat, which never returns S_ISLNK() == true.
238 * So the below is never true: */
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000239 /* || (FLAGS_DEREF && S_ISLNK(source_stat.st_mode)) */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000240 ) {
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000241 int src_fd;
242 int dst_fd;
Denis Vlasenkob9ad75f2008-03-28 17:49:31 +0000243 mode_t new_mode;
244
245 if (!FLAGS_DEREF && S_ISLNK(source_stat.st_mode)) {
246 /* "cp -d symlink dst": create a link */
247 goto dont_cat;
248 }
Denis Vlasenko30bab712007-08-25 21:14:55 +0000249
250 if (ENABLE_FEATURE_PRESERVE_HARDLINKS && !FLAGS_DEREF) {
Denis Vlasenko0f214ec2007-08-27 17:02:19 +0000251 const char *link_target;
Denis Vlasenko30bab712007-08-25 21:14:55 +0000252 link_target = is_in_ino_dev_hashtable(&source_stat);
253 if (link_target) {
254 if (link(link_target, dest) < 0) {
255 ovr = ask_and_unlink(dest, flags);
256 if (ovr <= 0)
257 return ovr;
Denis Vlasenko75103842007-06-20 14:49:47 +0000258 if (link(link_target, dest) < 0) {
Denis Vlasenko30bab712007-08-25 21:14:55 +0000259 bb_perror_msg("cannot create link '%s'", dest);
260 return -1;
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000261 }
Rob Landleyc9832742006-06-24 21:27:36 +0000262 }
Denis Vlasenko30bab712007-08-25 21:14:55 +0000263 return 0;
Matt Kraaiace02dc2001-12-17 15:26:36 +0000264 }
Rob Landleyc9832742006-06-24 21:27:36 +0000265 add_to_ino_dev_hashtable(&source_stat, dest);
Matt Kraaiace02dc2001-12-17 15:26:36 +0000266 }
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000267
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000268 src_fd = open_or_warn(source, O_RDONLY);
Denis Vlasenko30bab712007-08-25 21:14:55 +0000269 if (src_fd < 0)
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000270 return -1;
Matt Kraai14b7c5d2001-12-11 16:43:48 +0000271
Denis Vlasenkob9ad75f2008-03-28 17:49:31 +0000272 /* Do not try to open with weird mode fields */
273 new_mode = source_stat.st_mode;
274 if (!S_ISREG(source_stat.st_mode))
275 new_mode = 0666;
276
Denis Vlasenkofa050742007-09-11 10:39:13 +0000277 /* POSIX way is a security problem versus symlink attacks,
Denis Vlasenkoa9335ea2007-09-11 16:28:14 +0000278 * we do it only for non-symlinks, and only for non-recursive,
279 * non-interactive cp. NB: it is still racy
280 * for "cp file /home/bad_user/file" case
281 * (user can rm file and create a link to /etc/passwd) */
Denis Vlasenkofa050742007-09-11 10:39:13 +0000282 if (DO_POSIX_CP
Denis Vlasenkob9ad75f2008-03-28 17:49:31 +0000283 || (dest_exists
284 && !(flags & (FILEUTILS_RECUR|FILEUTILS_INTERACTIVE))
Denis Vlasenkoa9335ea2007-09-11 16:28:14 +0000285 && !S_ISLNK(dest_stat.st_mode))
Denis Vlasenkofa050742007-09-11 10:39:13 +0000286 ) {
Denis Vlasenkob9ad75f2008-03-28 17:49:31 +0000287 dst_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, new_mode);
Denis Vlasenkofa050742007-09-11 10:39:13 +0000288 } else /* safe way: */
Denis Vlasenkob9ad75f2008-03-28 17:49:31 +0000289 dst_fd = open(dest, O_WRONLY|O_CREAT|O_EXCL, new_mode);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000290 if (dst_fd == -1) {
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000291 ovr = ask_and_unlink(dest, flags);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000292 if (ovr <= 0) {
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000293 close(src_fd);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000294 return ovr;
295 }
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000296 /* It shouldn't exist. If it exists, do not open (symlink attack?) */
Denis Vlasenkob9ad75f2008-03-28 17:49:31 +0000297 dst_fd = open3_or_warn(dest, O_WRONLY|O_CREAT|O_EXCL, new_mode);
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000298 if (dst_fd < 0) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000299 close(src_fd);
300 return -1;
Matt Kraai91b28552001-04-23 18:53:07 +0000301 }
302 }
303
Denis Vlasenko49622d72007-03-10 16:58:49 +0000304#if ENABLE_SELINUX
Denis Vlasenkob9ad75f2008-03-28 17:49:31 +0000305 if ((flags & (FILEUTILS_PRESERVE_SECURITY_CONTEXT|FILEUTILS_SET_SECURITY_CONTEXT))
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000306 && is_selinux_enabled() > 0
307 ) {
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000308 security_context_t con;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000309 if (getfscreatecon(&con) == -1) {
310 bb_perror_msg("getfscreatecon");
311 return -1;
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000312 }
Denis Vlasenko49622d72007-03-10 16:58:49 +0000313 if (con) {
Denis Vlasenko51742f42007-04-12 00:32:05 +0000314 if (setfilecon(dest, con) == -1) {
Denis Vlasenko49622d72007-03-10 16:58:49 +0000315 bb_perror_msg("setfilecon:%s,%s", dest, con);
316 freecon(con);
317 return -1;
318 }
319 freecon(con);
320 }
321 }
322#endif
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000323 if (bb_copyfd_eof(src_fd, dst_fd) == -1)
Denis Vlasenko30bab712007-08-25 21:14:55 +0000324 retval = -1;
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000325 /* Ok, writing side I can understand... */
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000326 if (close(dst_fd) < 0) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000327 bb_perror_msg("cannot close '%s'", dest);
Denis Vlasenko30bab712007-08-25 21:14:55 +0000328 retval = -1;
Matt Kraai91b28552001-04-23 18:53:07 +0000329 }
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000330 /* ...but read size is already checked by bb_copyfd_eof */
331 close(src_fd);
Denis Vlasenkob9ad75f2008-03-28 17:49:31 +0000332 /* "cp /dev/something new_file" should not
333 * copy mode of /dev/something */
334 if (!S_ISREG(source_stat.st_mode))
335 return retval;
Denis Vlasenko30bab712007-08-25 21:14:55 +0000336 goto preserve_mode_ugid_time;
337 }
Denis Vlasenkob9ad75f2008-03-28 17:49:31 +0000338 dont_cat:
Eric Andersen403a73a2002-09-16 09:23:22 +0000339
Denis Vlasenko30bab712007-08-25 21:14:55 +0000340 /* Source is a symlink or a special file */
341 /* We are lazy here, a bit lax with races... */
342 if (dest_exists) {
343 errno = EEXIST;
344 ovr = ask_and_unlink(dest, flags);
345 if (ovr <= 0)
346 return ovr;
347 }
348 if (S_ISLNK(source_stat.st_mode)) {
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000349 char *lpath = xmalloc_readlink_or_warn(source);
350 if (lpath) {
351 int r = symlink(lpath, dest);
Rob Landley2f309322005-11-01 21:55:14 +0000352 free(lpath);
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000353 if (r < 0) {
354 bb_perror_msg("cannot create symlink '%s'", dest);
355 return -1;
356 }
357 if (flags & FILEUTILS_PRESERVE_STATUS)
358 if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
359 bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest);
Denis Vlasenko30bab712007-08-25 21:14:55 +0000360 }
Denis Vlasenko30bab712007-08-25 21:14:55 +0000361 /* _Not_ jumping to preserve_mode_ugid_time:
362 * symlinks don't have those */
363 return 0;
364 }
365 if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode)
366 || S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode)
367 ) {
368 if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
369 bb_perror_msg("cannot create '%s'", dest);
370 return -1;
Rob Landley2f309322005-11-01 21:55:14 +0000371 }
Eric Andersena9a220b2002-09-17 08:42:21 +0000372 } else {
Denis Vlasenko30bab712007-08-25 21:14:55 +0000373 bb_error_msg("unrecognized file '%s' with mode %x", source, source_stat.st_mode);
Eric Andersena9a220b2002-09-17 08:42:21 +0000374 return -1;
Eric Andersenaad1a882001-03-16 22:47:14 +0000375 }
376
Denis Vlasenko30bab712007-08-25 21:14:55 +0000377 preserve_mode_ugid_time:
Matt Kraai91b28552001-04-23 18:53:07 +0000378
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000379 if (flags & FILEUTILS_PRESERVE_STATUS
380 /* Cannot happen: */
381 /* && !(flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) */
382 ) {
Matt Kraai91b28552001-04-23 18:53:07 +0000383 struct utimbuf times;
384
385 times.actime = source_stat.st_atime;
386 times.modtime = source_stat.st_mtime;
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000387 /* BTW, utimes sets usec-precision time - just FYI */
Matt Kraai91b28552001-04-23 18:53:07 +0000388 if (utime(dest, &times) < 0)
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000389 bb_perror_msg("cannot preserve %s of '%s'", "times", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000390 if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
391 source_stat.st_mode &= ~(S_ISUID | S_ISGID);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000392 bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000393 }
394 if (chmod(dest, source_stat.st_mode) < 0)
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000395 bb_perror_msg("cannot preserve %s of '%s'", "permissions", dest);
Eric Andersenaad1a882001-03-16 22:47:14 +0000396 }
397
Denis Vlasenko30bab712007-08-25 21:14:55 +0000398 return retval;
Eric Andersenaad1a882001-03-16 22:47:14 +0000399}