1 /* vi: set sw=4 ts=4: */
3 * Mini copy_file implementation for busybox
5 * Copyright (C) 2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
6 * SELinux support by Yuichi Nakamura <ynakam@hitachisoft.jp>
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
14 // 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.
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...
23 #define DO_POSIX_CP 0 /* 1 - POSIX behavior, 0 - safe behavior */
25 // errno must be set to relevant value ("why we cannot create dest?")
26 // for POSIX mode to give reasonable error message
27 static int ask_and_unlink(const char *dest, int flags)
30 if (!(flags & (FILEUTILS_FORCE|FILEUTILS_INTERACTIVE))) {
31 // Either it exists, or the *path* doesnt exist
32 bb_perror_msg("cannot create '%s'", dest);
36 // If !DO_POSIX_CP, act as if -f is always in effect - we don't want
37 // "cannot create" msg, we want unlink to be done (silently unless -i).
39 // TODO: maybe we should do it only if ctty is present?
40 if (flags & FILEUTILS_INTERACTIVE) {
41 // We would not do POSIX insanity. -i asks,
42 // then _unlinks_ the offender. Presto.
43 // (No "opening without O_EXCL", no "unlink only if -f")
44 // Or else we will end up having 3 open()s!
45 fprintf(stderr, "%s: overwrite '%s'? ", applet_name, dest);
46 if (!bb_ask_confirmation())
47 return 0; // not allowed to overwrite
49 if (unlink(dest) < 0) {
50 bb_perror_msg("cannot remove '%s'", dest);
53 return 1; // ok (to try again)
57 * -1 error, copy not made
58 * 0 copy is made or user answered "no" in interactive mode
59 * (failures to preserve mode/owner/times are not reported in exit code)
61 int copy_file(const char *source, const char *dest, int flags)
63 /* This is a recursive function, try to minimize stack usage */
64 /* NB: each struct stat is ~100 bytes */
65 struct stat source_stat;
66 struct stat dest_stat;
67 signed char retval = 0;
68 signed char dest_exists = 0;
71 #define FLAGS_DEREF (flags & FILEUTILS_DEREFERENCE)
73 if ((FLAGS_DEREF ? stat : lstat)(source, &source_stat) < 0) {
74 // This may be a dangling symlink.
75 // Making [sym]links to dangling symlinks works, so...
76 if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK))
78 bb_perror_msg("cannot stat '%s'", source);
82 if (lstat(dest, &dest_stat) < 0) {
83 if (errno != ENOENT) {
84 bb_perror_msg("cannot stat '%s'", dest);
88 if (source_stat.st_dev == dest_stat.st_dev
89 && source_stat.st_ino == dest_stat.st_ino
91 bb_error_msg("'%s' and '%s' are the same file", source, dest);
98 if ((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) && is_selinux_enabled() > 0) {
99 security_context_t con;
100 if (lgetfilecon(source, &con) >= 0) {
101 if (setfscreatecon(con) < 0) {
102 bb_perror_msg("cannot set setfscreatecon %s", con);
106 } else if (errno == ENOTSUP || errno == ENODATA) {
107 setfscreatecon_or_die(NULL);
109 bb_perror_msg("cannot lgetfilecon %s", source);
115 if (S_ISDIR(source_stat.st_mode)) {
119 mode_t saved_umask = 0;
121 if (!(flags & FILEUTILS_RECUR)) {
122 bb_error_msg("omitting directory '%s'", source);
126 /* Did we ever create source ourself before? */
127 tp = is_in_ino_dev_hashtable(&source_stat);
129 /* We did! it's a recursion! man the lifeboats... */
130 bb_error_msg("recursion detected, omitting directory '%s'",
137 if (!S_ISDIR(dest_stat.st_mode)) {
138 bb_error_msg("target '%s' is not a directory", dest);
143 saved_umask = umask(0);
145 mode = source_stat.st_mode;
146 if (!(flags & FILEUTILS_PRESERVE_STATUS))
147 mode = source_stat.st_mode & ~saved_umask;
148 /* Allow owner to access new dir (at least for now) */
150 if (mkdir(dest, mode) < 0) {
152 bb_perror_msg("cannot create directory '%s'", dest);
156 /* need stat info for add_to_ino_dev_hashtable */
157 if (lstat(dest, &dest_stat) < 0) {
158 bb_perror_msg("cannot stat '%s'", dest);
162 /* remember (dev,inode) of each created dir.
163 * NULL: name is not remembered */
164 add_to_ino_dev_hashtable(&dest_stat, NULL);
166 /* Recursively copy files in SOURCE */
167 dp = opendir(source);
170 goto preserve_mode_ugid_time;
173 while ((d = readdir(dp)) != NULL) {
174 char *new_source, *new_dest;
176 new_source = concat_subpath_file(source, d->d_name);
177 if (new_source == NULL)
179 new_dest = concat_path_file(dest, d->d_name);
180 if (copy_file(new_source, new_dest, flags) < 0)
188 && chmod(dest, source_stat.st_mode & ~saved_umask) < 0
190 bb_perror_msg("cannot preserve %s of '%s'", "permissions", dest);
191 /* retval = -1; - WRONG! copy *WAS* made */
193 goto preserve_mode_ugid_time;
196 if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) {
197 int (*lf)(const char *oldpath, const char *newpath);
200 // if (DEREF && MAKE_SOFTLINK) source = realpath(source) ?
201 // (but realpath returns NULL on dangling symlinks...)
202 lf = (flags & FILEUTILS_MAKE_SOFTLINK) ? symlink : link;
203 if (lf(source, dest) < 0) {
204 ovr = ask_and_unlink(dest, flags);
207 if (lf(source, dest) < 0) {
208 bb_perror_msg("cannot create link '%s'", dest);
212 /* _Not_ jumping to preserve_mode_ugid_time:
213 * hard/softlinks don't have those */
217 if (S_ISREG(source_stat.st_mode)
218 /* DEREF uses stat, which never returns S_ISLNK() == true. */
219 /* || (FLAGS_DEREF && S_ISLNK(source_stat.st_mode)) */
224 if (ENABLE_FEATURE_PRESERVE_HARDLINKS && !FLAGS_DEREF) {
225 const char *link_target;
226 link_target = is_in_ino_dev_hashtable(&source_stat);
228 if (link(link_target, dest) < 0) {
229 ovr = ask_and_unlink(dest, flags);
232 if (link(link_target, dest) < 0) {
233 bb_perror_msg("cannot create link '%s'", dest);
239 add_to_ino_dev_hashtable(&source_stat, dest);
242 src_fd = open_or_warn(source, O_RDONLY);
246 #if DO_POSIX_CP /* POSIX way (a security problem versus symlink attacks!): */
247 dst_fd = open(dest, (flags & FILEUTILS_INTERACTIVE)
248 ? O_WRONLY|O_CREAT|O_EXCL
249 : O_WRONLY|O_CREAT|O_TRUNC, source_stat.st_mode);
250 #else /* safe way: */
251 dst_fd = open(dest, O_WRONLY|O_CREAT|O_EXCL, source_stat.st_mode);
254 ovr = ask_and_unlink(dest, flags);
259 /* It shouldn't exist. If it exists, do not open (symlink attack?) */
260 dst_fd = open3_or_warn(dest, O_WRONLY|O_CREAT|O_EXCL, source_stat.st_mode);
268 if (((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT)
269 || (flags & FILEUTILS_SET_SECURITY_CONTEXT))
270 && is_selinux_enabled() > 0
272 security_context_t con;
273 if (getfscreatecon(&con) == -1) {
274 bb_perror_msg("getfscreatecon");
278 if (setfilecon(dest, con) == -1) {
279 bb_perror_msg("setfilecon:%s,%s", dest, con);
287 if (bb_copyfd_eof(src_fd, dst_fd) == -1)
289 /* Ok, writing side I can understand... */
290 if (close(dst_fd) < 0) {
291 bb_perror_msg("cannot close '%s'", dest);
294 /* ...but read size is already checked by bb_copyfd_eof */
296 goto preserve_mode_ugid_time;
299 /* Source is a symlink or a special file */
300 /* We are lazy here, a bit lax with races... */
303 ovr = ask_and_unlink(dest, flags);
307 if (S_ISLNK(source_stat.st_mode)) {
308 char *lpath = xmalloc_readlink_or_warn(source);
310 int r = symlink(lpath, dest);
313 bb_perror_msg("cannot create symlink '%s'", dest);
316 if (flags & FILEUTILS_PRESERVE_STATUS)
317 if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
318 bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest);
320 /* _Not_ jumping to preserve_mode_ugid_time:
321 * symlinks don't have those */
324 if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode)
325 || S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode)
327 if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
328 bb_perror_msg("cannot create '%s'", dest);
332 bb_error_msg("unrecognized file '%s' with mode %x", source, source_stat.st_mode);
336 preserve_mode_ugid_time:
338 if (flags & FILEUTILS_PRESERVE_STATUS
340 /* && !(flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) */
342 struct utimbuf times;
344 times.actime = source_stat.st_atime;
345 times.modtime = source_stat.st_mtime;
346 /* BTW, utimes sets usec-precision time - just FYI */
347 if (utime(dest, ×) < 0)
348 bb_perror_msg("cannot preserve %s of '%s'", "times", dest);
349 if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
350 source_stat.st_mode &= ~(S_ISUID | S_ISGID);
351 bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest);
353 if (chmod(dest, source_stat.st_mode) < 0)
354 bb_perror_msg("cannot preserve %s of '%s'", "permissions", dest);