blob: d37d515627ed541bc5205ba747eb8d0b2410ef56 [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 */
Matt Kraai91b28552001-04-23 18:53:07 +000074int 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 Vlasenkof24e1f42006-10-21 23:40:20 +000084#define FLAGS_DEREF (flags & FILEUTILS_DEREFERENCE)
85
86 if ((FLAGS_DEREF ? stat : lstat)(source, &source_stat) < 0) {
87 // This may be a dangling symlink.
88 // Making [sym]links to dangling symlinks works, so...
89 if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK))
90 goto make_links;
91 bb_perror_msg("cannot stat '%s'", source);
Matt Kraai91b28552001-04-23 18:53:07 +000092 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +000093 }
Eric Andersenaad1a882001-03-16 22:47:14 +000094
Eric Andersen02b8dfc2002-09-16 10:23:38 +000095 if (lstat(dest, &dest_stat) < 0) {
Matt Kraai91b28552001-04-23 18:53:07 +000096 if (errno != ENOENT) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000097 bb_perror_msg("cannot stat '%s'", dest);
Matt Kraai91b28552001-04-23 18:53:07 +000098 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +000099 }
Eric Andersena9a220b2002-09-17 08:42:21 +0000100 } else {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000101 if (source_stat.st_dev == dest_stat.st_dev
102 && source_stat.st_ino == dest_stat.st_ino
103 ) {
104 bb_error_msg("'%s' and '%s' are the same file", source, dest);
Mike Frysinger2ed05ab2005-04-14 02:52:50 +0000105 return -1;
106 }
Eric Andersena9a220b2002-09-17 08:42:21 +0000107 dest_exists = 1;
108 }
Eric Andersenaad1a882001-03-16 22:47:14 +0000109
Denis Vlasenko49622d72007-03-10 16:58:49 +0000110#if ENABLE_SELINUX
111 if ((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) && is_selinux_enabled() > 0) {
112 security_context_t con;
113 if (lgetfilecon(source, &con) >= 0) {
114 if (setfscreatecon(con) < 0) {
115 bb_perror_msg("cannot set setfscreatecon %s", con);
116 freecon(con);
117 return -1;
118 }
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000119 } else if (errno == ENOTSUP || errno == ENODATA) {
120 setfscreatecon_or_die(NULL);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000121 } else {
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000122 bb_perror_msg("cannot lgetfilecon %s", source);
123 return -1;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000124 }
125 }
126#endif
127
Matt Kraai91b28552001-04-23 18:53:07 +0000128 if (S_ISDIR(source_stat.st_mode)) {
129 DIR *dp;
Denis Vlasenko0f214ec2007-08-27 17:02:19 +0000130 const char *tp;
Matt Kraai91b28552001-04-23 18:53:07 +0000131 struct dirent *d;
Matt Kraai218aa372001-04-30 17:32:43 +0000132 mode_t saved_umask = 0;
Matt Kraai91b28552001-04-23 18:53:07 +0000133
Matt Kraai01441032001-04-24 01:30:02 +0000134 if (!(flags & FILEUTILS_RECUR)) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000135 bb_error_msg("omitting directory '%s'", source);
Matt Kraai91b28552001-04-23 18:53:07 +0000136 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000137 }
Eric Andersenaad1a882001-03-16 22:47:14 +0000138
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000139 /* Did we ever create source ourself before? */
Denis Vlasenko0f214ec2007-08-27 17:02:19 +0000140 tp = is_in_ino_dev_hashtable(&source_stat);
141 if (tp) {
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000142 /* We did! it's a recursion! man the lifeboats... */
143 bb_error_msg("recursion detected, omitting directory '%s'",
Denis Vlasenko0f214ec2007-08-27 17:02:19 +0000144 source);
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000145 return -1;
146 }
147
Denis Vlasenko30bab712007-08-25 21:14:55 +0000148 /* Create DEST */
Matt Kraai91b28552001-04-23 18:53:07 +0000149 if (dest_exists) {
150 if (!S_ISDIR(dest_stat.st_mode)) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000151 bb_error_msg("target '%s' is not a directory", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000152 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000153 }
Denis Vlasenkoa9335ea2007-09-11 16:28:14 +0000154 /* race here: user can substitute a symlink between
155 * this check and actual creation of files inside dest */
Matt Kraai91b28552001-04-23 18:53:07 +0000156 } else {
Matt Kraai218aa372001-04-30 17:32:43 +0000157 mode_t mode;
Matt Kraai91b28552001-04-23 18:53:07 +0000158 saved_umask = umask(0);
Eric Andersenaad1a882001-03-16 22:47:14 +0000159
Matt Kraai91b28552001-04-23 18:53:07 +0000160 mode = source_stat.st_mode;
Matt Kraai01441032001-04-24 01:30:02 +0000161 if (!(flags & FILEUTILS_PRESERVE_STATUS))
Matt Kraai91b28552001-04-23 18:53:07 +0000162 mode = source_stat.st_mode & ~saved_umask;
Denis Vlasenko30bab712007-08-25 21:14:55 +0000163 /* Allow owner to access new dir (at least for now) */
Matt Kraai91b28552001-04-23 18:53:07 +0000164 mode |= S_IRWXU;
Matt Kraai91b28552001-04-23 18:53:07 +0000165 if (mkdir(dest, mode) < 0) {
166 umask(saved_umask);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000167 bb_perror_msg("cannot create directory '%s'", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000168 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000169 }
Matt Kraai91b28552001-04-23 18:53:07 +0000170 umask(saved_umask);
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000171 /* need stat info for add_to_ino_dev_hashtable */
172 if (lstat(dest, &dest_stat) < 0) {
173 bb_perror_msg("cannot stat '%s'", dest);
174 return -1;
175 }
Eric Andersenaad1a882001-03-16 22:47:14 +0000176 }
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000177 /* remember (dev,inode) of each created dir.
178 * NULL: name is not remembered */
179 add_to_ino_dev_hashtable(&dest_stat, NULL);
Matt Kraaibf0a0102001-06-11 13:58:02 +0000180
Denis Vlasenko30bab712007-08-25 21:14:55 +0000181 /* Recursively copy files in SOURCE */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000182 dp = opendir(source);
183 if (dp == NULL) {
Denis Vlasenko30bab712007-08-25 21:14:55 +0000184 retval = -1;
185 goto preserve_mode_ugid_time;
Matt Kraai91b28552001-04-23 18:53:07 +0000186 }
187
188 while ((d = readdir(dp)) != NULL) {
189 char *new_source, *new_dest;
190
Glenn L McGrath393183d2003-05-26 14:07:50 +0000191 new_source = concat_subpath_file(source, d->d_name);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000192 if (new_source == NULL)
Matt Kraai91b28552001-04-23 18:53:07 +0000193 continue;
Matt Kraai91b28552001-04-23 18:53:07 +0000194 new_dest = concat_path_file(dest, d->d_name);
195 if (copy_file(new_source, new_dest, flags) < 0)
Denis Vlasenko30bab712007-08-25 21:14:55 +0000196 retval = -1;
Matt Kraai91b28552001-04-23 18:53:07 +0000197 free(new_source);
198 free(new_dest);
199 }
Eric Andersena9a220b2002-09-17 08:42:21 +0000200 closedir(dp);
Matt Kraai218aa372001-04-30 17:32:43 +0000201
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000202 if (!dest_exists
203 && chmod(dest, source_stat.st_mode & ~saved_umask) < 0
204 ) {
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000205 bb_perror_msg("cannot preserve %s of '%s'", "permissions", dest);
206 /* retval = -1; - WRONG! copy *WAS* made */
Matt Kraai218aa372001-04-30 17:32:43 +0000207 }
Denis Vlasenko30bab712007-08-25 21:14:55 +0000208 goto preserve_mode_ugid_time;
209 }
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000210
Denis Vlasenko30bab712007-08-25 21:14:55 +0000211 if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000212 int (*lf)(const char *oldpath, const char *newpath);
213 make_links:
214 // Hmm... maybe
215 // if (DEREF && MAKE_SOFTLINK) source = realpath(source) ?
216 // (but realpath returns NULL on dangling symlinks...)
217 lf = (flags & FILEUTILS_MAKE_SOFTLINK) ? symlink : link;
218 if (lf(source, dest) < 0) {
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000219 ovr = ask_and_unlink(dest, flags);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000220 if (ovr <= 0)
221 return ovr;
222 if (lf(source, dest) < 0) {
223 bb_perror_msg("cannot create link '%s'", dest);
224 return -1;
225 }
226 }
Denis Vlasenko30bab712007-08-25 21:14:55 +0000227 /* _Not_ jumping to preserve_mode_ugid_time:
228 * hard/softlinks don't have those */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000229 return 0;
Denis Vlasenko30bab712007-08-25 21:14:55 +0000230 }
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000231
Denis Vlasenko30bab712007-08-25 21:14:55 +0000232 if (S_ISREG(source_stat.st_mode)
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000233 /* DEREF uses stat, which never returns S_ISLNK() == true. */
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000234 /* || (FLAGS_DEREF && S_ISLNK(source_stat.st_mode)) */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000235 ) {
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000236 int src_fd;
237 int dst_fd;
Denis Vlasenko30bab712007-08-25 21:14:55 +0000238
239 if (ENABLE_FEATURE_PRESERVE_HARDLINKS && !FLAGS_DEREF) {
Denis Vlasenko0f214ec2007-08-27 17:02:19 +0000240 const char *link_target;
Denis Vlasenko30bab712007-08-25 21:14:55 +0000241 link_target = is_in_ino_dev_hashtable(&source_stat);
242 if (link_target) {
243 if (link(link_target, dest) < 0) {
244 ovr = ask_and_unlink(dest, flags);
245 if (ovr <= 0)
246 return ovr;
Denis Vlasenko75103842007-06-20 14:49:47 +0000247 if (link(link_target, dest) < 0) {
Denis Vlasenko30bab712007-08-25 21:14:55 +0000248 bb_perror_msg("cannot create link '%s'", dest);
249 return -1;
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000250 }
Rob Landleyc9832742006-06-24 21:27:36 +0000251 }
Denis Vlasenko30bab712007-08-25 21:14:55 +0000252 return 0;
Matt Kraaiace02dc2001-12-17 15:26:36 +0000253 }
Rob Landleyc9832742006-06-24 21:27:36 +0000254 add_to_ino_dev_hashtable(&source_stat, dest);
Matt Kraaiace02dc2001-12-17 15:26:36 +0000255 }
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000256
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000257 src_fd = open_or_warn(source, O_RDONLY);
Denis Vlasenko30bab712007-08-25 21:14:55 +0000258 if (src_fd < 0)
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000259 return -1;
Matt Kraai14b7c5d2001-12-11 16:43:48 +0000260
Denis Vlasenkofa050742007-09-11 10:39:13 +0000261 /* POSIX way is a security problem versus symlink attacks,
Denis Vlasenkoa9335ea2007-09-11 16:28:14 +0000262 * we do it only for non-symlinks, and only for non-recursive,
263 * non-interactive cp. NB: it is still racy
264 * for "cp file /home/bad_user/file" case
265 * (user can rm file and create a link to /etc/passwd) */
Denis Vlasenkofa050742007-09-11 10:39:13 +0000266 if (DO_POSIX_CP
267 || (dest_exists && !(flags & (FILEUTILS_RECUR|FILEUTILS_INTERACTIVE))
Denis Vlasenkoa9335ea2007-09-11 16:28:14 +0000268 && !S_ISLNK(dest_stat.st_mode))
Denis Vlasenkofa050742007-09-11 10:39:13 +0000269 ) {
270 dst_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, source_stat.st_mode);
271 } else /* safe way: */
272 dst_fd = open(dest, O_WRONLY|O_CREAT|O_EXCL, source_stat.st_mode);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000273 if (dst_fd == -1) {
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000274 ovr = ask_and_unlink(dest, flags);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000275 if (ovr <= 0) {
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000276 close(src_fd);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000277 return ovr;
278 }
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000279 /* It shouldn't exist. If it exists, do not open (symlink attack?) */
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000280 dst_fd = open3_or_warn(dest, O_WRONLY|O_CREAT|O_EXCL, source_stat.st_mode);
281 if (dst_fd < 0) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000282 close(src_fd);
283 return -1;
Matt Kraai91b28552001-04-23 18:53:07 +0000284 }
285 }
286
Denis Vlasenko49622d72007-03-10 16:58:49 +0000287#if ENABLE_SELINUX
288 if (((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT)
289 || (flags & FILEUTILS_SET_SECURITY_CONTEXT))
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000290 && is_selinux_enabled() > 0
291 ) {
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000292 security_context_t con;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000293 if (getfscreatecon(&con) == -1) {
294 bb_perror_msg("getfscreatecon");
295 return -1;
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000296 }
Denis Vlasenko49622d72007-03-10 16:58:49 +0000297 if (con) {
Denis Vlasenko51742f42007-04-12 00:32:05 +0000298 if (setfilecon(dest, con) == -1) {
Denis Vlasenko49622d72007-03-10 16:58:49 +0000299 bb_perror_msg("setfilecon:%s,%s", dest, con);
300 freecon(con);
301 return -1;
302 }
303 freecon(con);
304 }
305 }
306#endif
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000307 if (bb_copyfd_eof(src_fd, dst_fd) == -1)
Denis Vlasenko30bab712007-08-25 21:14:55 +0000308 retval = -1;
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000309 /* Ok, writing side I can understand... */
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000310 if (close(dst_fd) < 0) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000311 bb_perror_msg("cannot close '%s'", dest);
Denis Vlasenko30bab712007-08-25 21:14:55 +0000312 retval = -1;
Matt Kraai91b28552001-04-23 18:53:07 +0000313 }
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000314 /* ...but read size is already checked by bb_copyfd_eof */
315 close(src_fd);
Denis Vlasenko30bab712007-08-25 21:14:55 +0000316 goto preserve_mode_ugid_time;
317 }
Eric Andersen403a73a2002-09-16 09:23:22 +0000318
Denis Vlasenko30bab712007-08-25 21:14:55 +0000319 /* Source is a symlink or a special file */
320 /* We are lazy here, a bit lax with races... */
321 if (dest_exists) {
322 errno = EEXIST;
323 ovr = ask_and_unlink(dest, flags);
324 if (ovr <= 0)
325 return ovr;
326 }
327 if (S_ISLNK(source_stat.st_mode)) {
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000328 char *lpath = xmalloc_readlink_or_warn(source);
329 if (lpath) {
330 int r = symlink(lpath, dest);
Rob Landley2f309322005-11-01 21:55:14 +0000331 free(lpath);
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000332 if (r < 0) {
333 bb_perror_msg("cannot create symlink '%s'", dest);
334 return -1;
335 }
336 if (flags & FILEUTILS_PRESERVE_STATUS)
337 if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
338 bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest);
Denis Vlasenko30bab712007-08-25 21:14:55 +0000339 }
Denis Vlasenko30bab712007-08-25 21:14:55 +0000340 /* _Not_ jumping to preserve_mode_ugid_time:
341 * symlinks don't have those */
342 return 0;
343 }
344 if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode)
345 || S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode)
346 ) {
347 if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
348 bb_perror_msg("cannot create '%s'", dest);
349 return -1;
Rob Landley2f309322005-11-01 21:55:14 +0000350 }
Eric Andersena9a220b2002-09-17 08:42:21 +0000351 } else {
Denis Vlasenko30bab712007-08-25 21:14:55 +0000352 bb_error_msg("unrecognized file '%s' with mode %x", source, source_stat.st_mode);
Eric Andersena9a220b2002-09-17 08:42:21 +0000353 return -1;
Eric Andersenaad1a882001-03-16 22:47:14 +0000354 }
355
Denis Vlasenko30bab712007-08-25 21:14:55 +0000356 preserve_mode_ugid_time:
Matt Kraai91b28552001-04-23 18:53:07 +0000357
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000358 if (flags & FILEUTILS_PRESERVE_STATUS
359 /* Cannot happen: */
360 /* && !(flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) */
361 ) {
Matt Kraai91b28552001-04-23 18:53:07 +0000362 struct utimbuf times;
363
364 times.actime = source_stat.st_atime;
365 times.modtime = source_stat.st_mtime;
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000366 /* BTW, utimes sets usec-precision time - just FYI */
Matt Kraai91b28552001-04-23 18:53:07 +0000367 if (utime(dest, &times) < 0)
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000368 bb_perror_msg("cannot preserve %s of '%s'", "times", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000369 if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
370 source_stat.st_mode &= ~(S_ISUID | S_ISGID);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000371 bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000372 }
373 if (chmod(dest, source_stat.st_mode) < 0)
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000374 bb_perror_msg("cannot preserve %s of '%s'", "permissions", dest);
Eric Andersenaad1a882001-03-16 22:47:14 +0000375 }
376
Denis Vlasenko30bab712007-08-25 21:14:55 +0000377 return retval;
Eric Andersenaad1a882001-03-16 22:47:14 +0000378}