blob: 86449f27c853b0b654939a07d0243479e4ce1db6 [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 Vlasenko54d14ca2007-03-15 13:33:37 +000032#if DO_POSIX_CP
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000033 if (!(flags & (FILEUTILS_FORCE|FILEUTILS_INTERACTIVE))) {
Denis Vlasenko3d829622007-08-24 14:23:57 +000034 // Either it exists, or the *path* doesnt exist
35 bb_perror_msg("cannot create '%s'", dest);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000036 return -1;
37 }
Denis Vlasenko54d14ca2007-03-15 13:33:37 +000038#endif
Denis Vlasenko3d829622007-08-24 14:23:57 +000039 // If !DO_POSIX_CP, act as if -f is always in effect - we don't want
40 // "cannot create" msg, we want unlink to be done (silently unless -i).
Denis Vlasenko54d14ca2007-03-15 13:33:37 +000041
42 // TODO: maybe we should do it only if ctty is present?
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000043 if (flags & FILEUTILS_INTERACTIVE) {
Denis Vlasenko54d14ca2007-03-15 13:33:37 +000044 // We would not do POSIX insanity. -i asks,
45 // then _unlinks_ the offender. Presto.
Denis Vlasenko3d829622007-08-24 14:23:57 +000046 // (No "opening without O_EXCL", no "unlink only if -f")
Denis Vlasenko54d14ca2007-03-15 13:33:37 +000047 // Or else we will end up having 3 open()s!
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000048 fprintf(stderr, "%s: overwrite '%s'? ", applet_name, dest);
49 if (!bb_ask_confirmation())
50 return 0; // not allowed to overwrite
51 }
52 if (unlink(dest) < 0) {
53 bb_perror_msg("cannot remove '%s'", dest);
54 return -1; // error
55 }
56 return 1; // ok (to try again)
57}
58
Denis Vlasenko54d14ca2007-03-15 13:33:37 +000059/* Return:
60 * -1 error, copy not made
61 * 0 copy is made or user answered "no" in interactive mode
62 * (failures to preserve mode/owner/times are not reported in exit code)
63 */
Matt Kraai91b28552001-04-23 18:53:07 +000064int copy_file(const char *source, const char *dest, int flags)
Eric Andersenaad1a882001-03-16 22:47:14 +000065{
Denis Vlasenko30bab712007-08-25 21:14:55 +000066 /* This is a recursive function, try to minimize stack usage */
67 /* NB: each struct stat is ~100 bytes */
Matt Kraai91b28552001-04-23 18:53:07 +000068 struct stat source_stat;
69 struct stat dest_stat;
Denis Vlasenko30bab712007-08-25 21:14:55 +000070 signed char retval = 0;
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000071 signed char dest_exists = 0;
72 signed char ovr;
Eric Andersenaad1a882001-03-16 22:47:14 +000073
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000074#define FLAGS_DEREF (flags & FILEUTILS_DEREFERENCE)
75
76 if ((FLAGS_DEREF ? stat : lstat)(source, &source_stat) < 0) {
77 // This may be a dangling symlink.
78 // Making [sym]links to dangling symlinks works, so...
79 if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK))
80 goto make_links;
81 bb_perror_msg("cannot stat '%s'", source);
Matt Kraai91b28552001-04-23 18:53:07 +000082 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +000083 }
Eric Andersenaad1a882001-03-16 22:47:14 +000084
Eric Andersen02b8dfc2002-09-16 10:23:38 +000085 if (lstat(dest, &dest_stat) < 0) {
Matt Kraai91b28552001-04-23 18:53:07 +000086 if (errno != ENOENT) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000087 bb_perror_msg("cannot stat '%s'", dest);
Matt Kraai91b28552001-04-23 18:53:07 +000088 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +000089 }
Eric Andersena9a220b2002-09-17 08:42:21 +000090 } else {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +000091 if (source_stat.st_dev == dest_stat.st_dev
92 && source_stat.st_ino == dest_stat.st_ino
93 ) {
94 bb_error_msg("'%s' and '%s' are the same file", source, dest);
Mike Frysinger2ed05ab2005-04-14 02:52:50 +000095 return -1;
96 }
Eric Andersena9a220b2002-09-17 08:42:21 +000097 dest_exists = 1;
98 }
Eric Andersenaad1a882001-03-16 22:47:14 +000099
Denis Vlasenko49622d72007-03-10 16:58:49 +0000100#if ENABLE_SELINUX
101 if ((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) && is_selinux_enabled() > 0) {
102 security_context_t con;
103 if (lgetfilecon(source, &con) >= 0) {
104 if (setfscreatecon(con) < 0) {
105 bb_perror_msg("cannot set setfscreatecon %s", con);
106 freecon(con);
107 return -1;
108 }
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000109 } else if (errno == ENOTSUP || errno == ENODATA) {
110 setfscreatecon_or_die(NULL);
Denis Vlasenko49622d72007-03-10 16:58:49 +0000111 } else {
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000112 bb_perror_msg("cannot lgetfilecon %s", source);
113 return -1;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000114 }
115 }
116#endif
117
Matt Kraai91b28552001-04-23 18:53:07 +0000118 if (S_ISDIR(source_stat.st_mode)) {
119 DIR *dp;
Denis Vlasenko0f214ec2007-08-27 17:02:19 +0000120 const char *tp;
Matt Kraai91b28552001-04-23 18:53:07 +0000121 struct dirent *d;
Matt Kraai218aa372001-04-30 17:32:43 +0000122 mode_t saved_umask = 0;
Matt Kraai91b28552001-04-23 18:53:07 +0000123
Matt Kraai01441032001-04-24 01:30:02 +0000124 if (!(flags & FILEUTILS_RECUR)) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000125 bb_error_msg("omitting directory '%s'", source);
Matt Kraai91b28552001-04-23 18:53:07 +0000126 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000127 }
Eric Andersenaad1a882001-03-16 22:47:14 +0000128
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000129 /* Did we ever create source ourself before? */
Denis Vlasenko0f214ec2007-08-27 17:02:19 +0000130 tp = is_in_ino_dev_hashtable(&source_stat);
131 if (tp) {
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000132 /* We did! it's a recursion! man the lifeboats... */
133 bb_error_msg("recursion detected, omitting directory '%s'",
Denis Vlasenko0f214ec2007-08-27 17:02:19 +0000134 source);
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000135 return -1;
136 }
137
Denis Vlasenko30bab712007-08-25 21:14:55 +0000138 /* Create DEST */
Matt Kraai91b28552001-04-23 18:53:07 +0000139 if (dest_exists) {
140 if (!S_ISDIR(dest_stat.st_mode)) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000141 bb_error_msg("target '%s' is not a directory", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000142 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000143 }
Matt Kraai91b28552001-04-23 18:53:07 +0000144 } else {
Matt Kraai218aa372001-04-30 17:32:43 +0000145 mode_t mode;
Matt Kraai91b28552001-04-23 18:53:07 +0000146 saved_umask = umask(0);
Eric Andersenaad1a882001-03-16 22:47:14 +0000147
Matt Kraai91b28552001-04-23 18:53:07 +0000148 mode = source_stat.st_mode;
Matt Kraai01441032001-04-24 01:30:02 +0000149 if (!(flags & FILEUTILS_PRESERVE_STATUS))
Matt Kraai91b28552001-04-23 18:53:07 +0000150 mode = source_stat.st_mode & ~saved_umask;
Denis Vlasenko30bab712007-08-25 21:14:55 +0000151 /* Allow owner to access new dir (at least for now) */
Matt Kraai91b28552001-04-23 18:53:07 +0000152 mode |= S_IRWXU;
Matt Kraai91b28552001-04-23 18:53:07 +0000153 if (mkdir(dest, mode) < 0) {
154 umask(saved_umask);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000155 bb_perror_msg("cannot create directory '%s'", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000156 return -1;
Glenn L McGrath4949faf2001-04-11 16:23:35 +0000157 }
Matt Kraai91b28552001-04-23 18:53:07 +0000158 umask(saved_umask);
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000159 /* need stat info for add_to_ino_dev_hashtable */
160 if (lstat(dest, &dest_stat) < 0) {
161 bb_perror_msg("cannot stat '%s'", dest);
162 return -1;
163 }
Eric Andersenaad1a882001-03-16 22:47:14 +0000164 }
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000165 /* remember (dev,inode) of each created dir.
166 * NULL: name is not remembered */
167 add_to_ino_dev_hashtable(&dest_stat, NULL);
Matt Kraaibf0a0102001-06-11 13:58:02 +0000168
Denis Vlasenko30bab712007-08-25 21:14:55 +0000169 /* Recursively copy files in SOURCE */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000170 dp = opendir(source);
171 if (dp == NULL) {
Denis Vlasenko30bab712007-08-25 21:14:55 +0000172 retval = -1;
173 goto preserve_mode_ugid_time;
Matt Kraai91b28552001-04-23 18:53:07 +0000174 }
175
176 while ((d = readdir(dp)) != NULL) {
177 char *new_source, *new_dest;
178
Glenn L McGrath393183d2003-05-26 14:07:50 +0000179 new_source = concat_subpath_file(source, d->d_name);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000180 if (new_source == NULL)
Matt Kraai91b28552001-04-23 18:53:07 +0000181 continue;
Matt Kraai91b28552001-04-23 18:53:07 +0000182 new_dest = concat_path_file(dest, d->d_name);
183 if (copy_file(new_source, new_dest, flags) < 0)
Denis Vlasenko30bab712007-08-25 21:14:55 +0000184 retval = -1;
Matt Kraai91b28552001-04-23 18:53:07 +0000185 free(new_source);
186 free(new_dest);
187 }
Eric Andersena9a220b2002-09-17 08:42:21 +0000188 closedir(dp);
Matt Kraai218aa372001-04-30 17:32:43 +0000189
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000190 if (!dest_exists
191 && chmod(dest, source_stat.st_mode & ~saved_umask) < 0
192 ) {
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000193 bb_perror_msg("cannot preserve %s of '%s'", "permissions", dest);
194 /* retval = -1; - WRONG! copy *WAS* made */
Matt Kraai218aa372001-04-30 17:32:43 +0000195 }
Denis Vlasenko30bab712007-08-25 21:14:55 +0000196 goto preserve_mode_ugid_time;
197 }
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000198
Denis Vlasenko30bab712007-08-25 21:14:55 +0000199 if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000200 int (*lf)(const char *oldpath, const char *newpath);
201 make_links:
202 // Hmm... maybe
203 // if (DEREF && MAKE_SOFTLINK) source = realpath(source) ?
204 // (but realpath returns NULL on dangling symlinks...)
205 lf = (flags & FILEUTILS_MAKE_SOFTLINK) ? symlink : link;
206 if (lf(source, dest) < 0) {
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000207 ovr = ask_and_unlink(dest, flags);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000208 if (ovr <= 0)
209 return ovr;
210 if (lf(source, dest) < 0) {
211 bb_perror_msg("cannot create link '%s'", dest);
212 return -1;
213 }
214 }
Denis Vlasenko30bab712007-08-25 21:14:55 +0000215 /* _Not_ jumping to preserve_mode_ugid_time:
216 * hard/softlinks don't have those */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000217 return 0;
Denis Vlasenko30bab712007-08-25 21:14:55 +0000218 }
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000219
Denis Vlasenko30bab712007-08-25 21:14:55 +0000220 if (S_ISREG(source_stat.st_mode)
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000221 /* DEREF uses stat, which never returns S_ISLNK() == true. */
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000222 /* || (FLAGS_DEREF && S_ISLNK(source_stat.st_mode)) */
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000223 ) {
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000224 int src_fd;
225 int dst_fd;
Denis Vlasenko30bab712007-08-25 21:14:55 +0000226
227 if (ENABLE_FEATURE_PRESERVE_HARDLINKS && !FLAGS_DEREF) {
Denis Vlasenko0f214ec2007-08-27 17:02:19 +0000228 const char *link_target;
Denis Vlasenko30bab712007-08-25 21:14:55 +0000229 link_target = is_in_ino_dev_hashtable(&source_stat);
230 if (link_target) {
231 if (link(link_target, dest) < 0) {
232 ovr = ask_and_unlink(dest, flags);
233 if (ovr <= 0)
234 return ovr;
Denis Vlasenko75103842007-06-20 14:49:47 +0000235 if (link(link_target, dest) < 0) {
Denis Vlasenko30bab712007-08-25 21:14:55 +0000236 bb_perror_msg("cannot create link '%s'", dest);
237 return -1;
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000238 }
Rob Landleyc9832742006-06-24 21:27:36 +0000239 }
Denis Vlasenko30bab712007-08-25 21:14:55 +0000240 return 0;
Matt Kraaiace02dc2001-12-17 15:26:36 +0000241 }
Rob Landleyc9832742006-06-24 21:27:36 +0000242 add_to_ino_dev_hashtable(&source_stat, dest);
Matt Kraaiace02dc2001-12-17 15:26:36 +0000243 }
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000244
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000245 src_fd = open_or_warn(source, O_RDONLY);
Denis Vlasenko30bab712007-08-25 21:14:55 +0000246 if (src_fd < 0)
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000247 return -1;
Matt Kraai14b7c5d2001-12-11 16:43:48 +0000248
Denis Vlasenkofa050742007-09-11 10:39:13 +0000249 /* POSIX way is a security problem versus symlink attacks,
250 * we do it only for dest's which are device nodes,
251 * and only for non-recursive, non-interactive cp. NB: it is still racy
252 * for "cp file /home/bad_user/device_node" case
253 * (user can rm device_node and create link to /etc/passwd) */
254 if (DO_POSIX_CP
255 || (dest_exists && !(flags & (FILEUTILS_RECUR|FILEUTILS_INTERACTIVE))
256 && (S_ISBLK(dest_stat.st_mode) || S_ISCHR(dest_stat.st_mode)))
257 ) {
258 dst_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, source_stat.st_mode);
259 } else /* safe way: */
260 dst_fd = open(dest, O_WRONLY|O_CREAT|O_EXCL, source_stat.st_mode);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000261 if (dst_fd == -1) {
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000262 ovr = ask_and_unlink(dest, flags);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000263 if (ovr <= 0) {
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000264 close(src_fd);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000265 return ovr;
266 }
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000267 /* It shouldn't exist. If it exists, do not open (symlink attack?) */
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000268 dst_fd = open3_or_warn(dest, O_WRONLY|O_CREAT|O_EXCL, source_stat.st_mode);
269 if (dst_fd < 0) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000270 close(src_fd);
271 return -1;
Matt Kraai91b28552001-04-23 18:53:07 +0000272 }
273 }
274
Denis Vlasenko49622d72007-03-10 16:58:49 +0000275#if ENABLE_SELINUX
276 if (((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT)
277 || (flags & FILEUTILS_SET_SECURITY_CONTEXT))
Denis Vlasenko54d14ca2007-03-15 13:33:37 +0000278 && is_selinux_enabled() > 0
279 ) {
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000280 security_context_t con;
Denis Vlasenko49622d72007-03-10 16:58:49 +0000281 if (getfscreatecon(&con) == -1) {
282 bb_perror_msg("getfscreatecon");
283 return -1;
Denis Vlasenkoc86e0522007-03-20 11:30:28 +0000284 }
Denis Vlasenko49622d72007-03-10 16:58:49 +0000285 if (con) {
Denis Vlasenko51742f42007-04-12 00:32:05 +0000286 if (setfilecon(dest, con) == -1) {
Denis Vlasenko49622d72007-03-10 16:58:49 +0000287 bb_perror_msg("setfilecon:%s,%s", dest, con);
288 freecon(con);
289 return -1;
290 }
291 freecon(con);
292 }
293 }
294#endif
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000295 if (bb_copyfd_eof(src_fd, dst_fd) == -1)
Denis Vlasenko30bab712007-08-25 21:14:55 +0000296 retval = -1;
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000297 /* Ok, writing side I can understand... */
Glenn L McGrathf62ea202003-12-20 04:38:01 +0000298 if (close(dst_fd) < 0) {
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000299 bb_perror_msg("cannot close '%s'", dest);
Denis Vlasenko30bab712007-08-25 21:14:55 +0000300 retval = -1;
Matt Kraai91b28552001-04-23 18:53:07 +0000301 }
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000302 /* ...but read size is already checked by bb_copyfd_eof */
303 close(src_fd);
Denis Vlasenko30bab712007-08-25 21:14:55 +0000304 goto preserve_mode_ugid_time;
305 }
Eric Andersen403a73a2002-09-16 09:23:22 +0000306
Denis Vlasenko30bab712007-08-25 21:14:55 +0000307 /* Source is a symlink or a special file */
308 /* We are lazy here, a bit lax with races... */
309 if (dest_exists) {
310 errno = EEXIST;
311 ovr = ask_and_unlink(dest, flags);
312 if (ovr <= 0)
313 return ovr;
314 }
315 if (S_ISLNK(source_stat.st_mode)) {
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000316 char *lpath = xmalloc_readlink_or_warn(source);
317 if (lpath) {
318 int r = symlink(lpath, dest);
Rob Landley2f309322005-11-01 21:55:14 +0000319 free(lpath);
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000320 if (r < 0) {
321 bb_perror_msg("cannot create symlink '%s'", dest);
322 return -1;
323 }
324 if (flags & FILEUTILS_PRESERVE_STATUS)
325 if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
326 bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest);
Denis Vlasenko30bab712007-08-25 21:14:55 +0000327 }
Denis Vlasenko30bab712007-08-25 21:14:55 +0000328 /* _Not_ jumping to preserve_mode_ugid_time:
329 * symlinks don't have those */
330 return 0;
331 }
332 if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode)
333 || S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode)
334 ) {
335 if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
336 bb_perror_msg("cannot create '%s'", dest);
337 return -1;
Rob Landley2f309322005-11-01 21:55:14 +0000338 }
Eric Andersena9a220b2002-09-17 08:42:21 +0000339 } else {
Denis Vlasenko30bab712007-08-25 21:14:55 +0000340 bb_error_msg("unrecognized file '%s' with mode %x", source, source_stat.st_mode);
Eric Andersena9a220b2002-09-17 08:42:21 +0000341 return -1;
Eric Andersenaad1a882001-03-16 22:47:14 +0000342 }
343
Denis Vlasenko30bab712007-08-25 21:14:55 +0000344 preserve_mode_ugid_time:
Matt Kraai91b28552001-04-23 18:53:07 +0000345
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000346 if (flags & FILEUTILS_PRESERVE_STATUS
347 /* Cannot happen: */
348 /* && !(flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) */
349 ) {
Matt Kraai91b28552001-04-23 18:53:07 +0000350 struct utimbuf times;
351
352 times.actime = source_stat.st_atime;
353 times.modtime = source_stat.st_mtime;
Denis Vlasenko8a5fab62007-08-27 16:51:30 +0000354 /* BTW, utimes sets usec-precision time - just FYI */
Matt Kraai91b28552001-04-23 18:53:07 +0000355 if (utime(dest, &times) < 0)
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000356 bb_perror_msg("cannot preserve %s of '%s'", "times", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000357 if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
358 source_stat.st_mode &= ~(S_ISUID | S_ISGID);
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000359 bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest);
Matt Kraai91b28552001-04-23 18:53:07 +0000360 }
361 if (chmod(dest, source_stat.st_mode) < 0)
Denis Vlasenkof24e1f42006-10-21 23:40:20 +0000362 bb_perror_msg("cannot preserve %s of '%s'", "permissions", dest);
Eric Andersenaad1a882001-03-16 22:47:14 +0000363 }
364
Denis Vlasenko30bab712007-08-25 21:14:55 +0000365 return retval;
Eric Andersenaad1a882001-03-16 22:47:14 +0000366}