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 */
26 static int ask_and_unlink(const char *dest, int flags)
28 // If !DO_POSIX_CP, act as if -f is always in effect - we don't want
29 // "'file' exists" msg, we want unlink to be done (silently unless -i
30 // is also in effect).
31 // This prevents safe way from asking more questions than POSIX does.
33 if (!(flags & (FILEUTILS_FORCE|FILEUTILS_INTERACTIVE))) {
34 fprintf(stderr, "'%s' exists\n", dest);
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 unlinks 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 struct stat source_stat;
64 struct stat dest_stat;
66 signed char dest_exists = 0;
69 #define FLAGS_DEREF (flags & FILEUTILS_DEREFERENCE)
71 if ((FLAGS_DEREF ? stat : lstat)(source, &source_stat) < 0) {
72 // This may be a dangling symlink.
73 // Making [sym]links to dangling symlinks works, so...
74 if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK))
76 bb_perror_msg("cannot stat '%s'", source);
80 if (lstat(dest, &dest_stat) < 0) {
81 if (errno != ENOENT) {
82 bb_perror_msg("cannot stat '%s'", dest);
86 if (source_stat.st_dev == dest_stat.st_dev
87 && source_stat.st_ino == dest_stat.st_ino
89 bb_error_msg("'%s' and '%s' are the same file", source, dest);
96 if ((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) && is_selinux_enabled() > 0) {
97 security_context_t con;
98 if (lgetfilecon(source, &con) >= 0) {
99 if (setfscreatecon(con) < 0) {
100 bb_perror_msg("cannot set setfscreatecon %s", con);
104 } else if (errno == ENOTSUP || errno == ENODATA) {
105 setfscreatecon_or_die(NULL);
107 bb_perror_msg("cannot lgetfilecon %s", source);
113 if (S_ISDIR(source_stat.st_mode)) {
116 mode_t saved_umask = 0;
118 if (!(flags & FILEUTILS_RECUR)) {
119 bb_error_msg("omitting directory '%s'", source);
125 if (!S_ISDIR(dest_stat.st_mode)) {
126 bb_error_msg("target '%s' is not a directory", dest);
131 saved_umask = umask(0);
133 mode = source_stat.st_mode;
134 if (!(flags & FILEUTILS_PRESERVE_STATUS))
135 mode = source_stat.st_mode & ~saved_umask;
138 if (mkdir(dest, mode) < 0) {
140 bb_perror_msg("cannot create directory '%s'", dest);
147 /* Recursively copy files in SOURCE. */
148 dp = opendir(source);
151 goto preserve_status;
154 while ((d = readdir(dp)) != NULL) {
155 char *new_source, *new_dest;
157 new_source = concat_subpath_file(source, d->d_name);
158 if (new_source == NULL)
160 new_dest = concat_path_file(dest, d->d_name);
161 if (copy_file(new_source, new_dest, flags) < 0)
169 && chmod(dest, source_stat.st_mode & ~saved_umask) < 0
171 bb_perror_msg("cannot change permissions of '%s'", dest);
175 } else if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) {
176 int (*lf)(const char *oldpath, const char *newpath);
179 // if (DEREF && MAKE_SOFTLINK) source = realpath(source) ?
180 // (but realpath returns NULL on dangling symlinks...)
181 lf = (flags & FILEUTILS_MAKE_SOFTLINK) ? symlink : link;
182 if (lf(source, dest) < 0) {
183 ovr = ask_and_unlink(dest, flags);
186 if (lf(source, dest) < 0) {
187 bb_perror_msg("cannot create link '%s'", dest);
193 } else if (S_ISREG(source_stat.st_mode)
194 /* Huh? DEREF uses stat, which never returns links! */
195 /* || (FLAGS_DEREF && S_ISLNK(source_stat.st_mode)) */
199 if (ENABLE_FEATURE_PRESERVE_HARDLINKS) {
203 link_target = is_in_ino_dev_hashtable(&source_stat);
205 if (link(link_target, dest) < 0) {
206 ovr = ask_and_unlink(dest, flags);
209 if (link(link_target, dest) < 0) {
210 bb_perror_msg("cannot create link '%s'", dest);
217 add_to_ino_dev_hashtable(&source_stat, dest);
220 src_fd = open_or_warn(source, O_RDONLY);
225 #if DO_POSIX_CP /* POSIX way (a security problem versus symlink attacks!): */
226 dst_fd = open(dest, (flags & FILEUTILS_INTERACTIVE)
227 ? O_WRONLY|O_CREAT|O_EXCL
228 : O_WRONLY|O_CREAT|O_TRUNC, source_stat.st_mode);
229 #else /* safe way: */
230 dst_fd = open(dest, O_WRONLY|O_CREAT|O_EXCL, source_stat.st_mode);
233 ovr = ask_and_unlink(dest, flags);
238 /* It shouldn't exist. If it exists, do not open (symlink attack?) */
239 dst_fd = open3_or_warn(dest, O_WRONLY|O_CREAT|O_EXCL, source_stat.st_mode);
247 if (((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT)
248 || (flags & FILEUTILS_SET_SECURITY_CONTEXT))
249 && is_selinux_enabled() > 0
251 security_context_t con;
252 if (getfscreatecon(&con) == -1) {
253 bb_perror_msg("getfscreatecon");
257 if (setfilecon(dest, con) == -1) {
258 bb_perror_msg("setfilecon:%s,%s", dest, con);
266 if (bb_copyfd_eof(src_fd, dst_fd) == -1)
268 if (close(dst_fd) < 0) {
269 bb_perror_msg("cannot close '%s'", dest);
272 if (close(src_fd) < 0) {
273 bb_perror_msg("cannot close '%s'", source);
277 } else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode)
278 || S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode)
279 || S_ISLNK(source_stat.st_mode)
281 // We are lazy here, a bit lax with races...
283 ovr = ask_and_unlink(dest, flags);
287 if (S_ISFIFO(source_stat.st_mode)) {
288 if (mkfifo(dest, source_stat.st_mode) < 0) {
289 bb_perror_msg("cannot create fifo '%s'", dest);
292 } else if (S_ISLNK(source_stat.st_mode)) {
295 lpath = xmalloc_readlink_or_warn(source);
296 if (lpath && symlink(lpath, dest) < 0) {
297 bb_perror_msg("cannot create symlink '%s'", dest);
303 if (flags & FILEUTILS_PRESERVE_STATUS)
304 if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
305 bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest);
310 if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
311 bb_perror_msg("cannot create '%s'", dest);
316 bb_error_msg("internal error: unrecognized file type");
322 if (flags & FILEUTILS_PRESERVE_STATUS
324 /* && !(flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) */
326 struct utimbuf times;
328 times.actime = source_stat.st_atime;
329 times.modtime = source_stat.st_mtime;
330 if (utime(dest, ×) < 0)
331 bb_perror_msg("cannot preserve %s of '%s'", "times", dest);
332 if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
333 source_stat.st_mode &= ~(S_ISUID | S_ISGID);
334 bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest);
336 if (chmod(dest, source_stat.st_mode) < 0)
337 bb_perror_msg("cannot preserve %s of '%s'", "permissions", dest);