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 source tree.
12 // FEATURE_NON_POSIX_CP:
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 (sym)link, we overwrite link 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 /* Called if open of destination, link creation etc fails.
24 * errno must be set to relevant value ("why we cannot create dest?")
25 * to give reasonable error message */
26 static int ask_and_unlink(const char *dest
, int flags
)
30 #if !ENABLE_FEATURE_NON_POSIX_CP
31 if (!(flags
& (FILEUTILS_FORCE
|FILEUTILS_INTERACTIVE
))) {
32 /* Either it exists, or the *path* doesnt exist */
33 bb_perror_msg("can't create '%s'", dest
);
37 // else: act as if -f is always in effect.
38 // We don't want "can't create" msg, we want unlink to be done
39 // (silently unless -i). Why? POSIX cp usually succeeds with
40 // O_TRUNC open of existing file, and user is left ignorantly happy.
41 // With above block unconditionally enabled, non-POSIX cp
42 // will complain a lot more than POSIX one.
44 /* TODO: maybe we should do it only if ctty is present? */
45 if (flags
& FILEUTILS_INTERACTIVE
) {
46 // We would not do POSIX insanity. -i asks,
47 // then _unlinks_ the offender. Presto.
48 // (No "opening without O_EXCL", no "unlink only if -f")
49 // Or else we will end up having 3 open()s!
50 fprintf(stderr
, "%s: overwrite '%s'? ", applet_name
, dest
);
51 if (!bb_ask_confirmation())
52 return 0; /* not allowed to overwrite */
54 if (unlink(dest
) < 0) {
55 #if ENABLE_FEATURE_VERBOSE_CP_MESSAGE
56 if (e
== errno
&& e
== ENOENT
) {
57 /* e == ENOTDIR is similar: path has non-dir component,
58 * but in this case we don't even reach copy_file() */
59 bb_error_msg("can't create '%s': Path does not exist", dest
);
60 return -1; /* error */
63 errno
= e
; /* do not use errno from unlink */
64 bb_perror_msg("can't create '%s'", dest
);
65 return -1; /* error */
67 return 1; /* ok (to try again) */
71 * -1 error, copy not made
72 * 0 copy is made or user answered "no" in interactive mode
73 * (failures to preserve mode/owner/times are not reported in exit code)
75 int FAST_FUNC
copy_file(const char *source
, const char *dest
, int flags
)
77 /* This is a recursive function, try to minimize stack usage */
78 /* NB: each struct stat is ~100 bytes */
79 struct stat source_stat
;
80 struct stat dest_stat
;
82 smallint dest_exists
= 0;
85 /* Inverse of cp -d ("cp without -d") */
86 #define FLAGS_DEREF (flags & (FILEUTILS_DEREFERENCE + FILEUTILS_DEREFERENCE_L0))
88 if ((FLAGS_DEREF
? stat
: lstat
)(source
, &source_stat
) < 0) {
89 /* This may be a dangling symlink.
90 * Making [sym]links to dangling symlinks works, so... */
91 if (flags
& (FILEUTILS_MAKE_SOFTLINK
|FILEUTILS_MAKE_HARDLINK
))
93 bb_perror_msg("can't stat '%s'", source
);
97 if (lstat(dest
, &dest_stat
) < 0) {
98 if (errno
!= ENOENT
) {
99 bb_perror_msg("can't stat '%s'", dest
);
103 if (source_stat
.st_dev
== dest_stat
.st_dev
104 && source_stat
.st_ino
== dest_stat
.st_ino
106 bb_error_msg("'%s' and '%s' are the same file", source
, dest
);
113 if ((flags
& FILEUTILS_PRESERVE_SECURITY_CONTEXT
) && is_selinux_enabled() > 0) {
114 security_context_t con
;
115 if (lgetfilecon(source
, &con
) >= 0) {
116 if (setfscreatecon(con
) < 0) {
117 bb_perror_msg("can't set setfscreatecon %s", con
);
121 } else if (errno
== ENOTSUP
|| errno
== ENODATA
) {
122 setfscreatecon_or_die(NULL
);
124 bb_perror_msg("can't lgetfilecon %s", source
);
130 if (S_ISDIR(source_stat
.st_mode
)) {
134 mode_t saved_umask
= 0;
136 if (!(flags
& FILEUTILS_RECUR
)) {
137 bb_error_msg("omitting directory '%s'", source
);
141 /* Did we ever create source ourself before? */
142 tp
= is_in_ino_dev_hashtable(&source_stat
);
144 /* We did! it's a recursion! man the lifeboats... */
145 bb_error_msg("recursion detected, omitting directory '%s'",
151 if (!S_ISDIR(dest_stat
.st_mode
)) {
152 bb_error_msg("target '%s' is not a directory", dest
);
155 /* race here: user can substitute a symlink between
156 * this check and actual creation of files inside dest */
160 saved_umask
= umask(0);
162 mode
= source_stat
.st_mode
;
163 if (!(flags
& FILEUTILS_PRESERVE_STATUS
))
164 mode
= source_stat
.st_mode
& ~saved_umask
;
165 /* Allow owner to access new dir (at least for now) */
167 if (mkdir(dest
, mode
) < 0) {
169 bb_perror_msg("can't create directory '%s'", dest
);
173 /* need stat info for add_to_ino_dev_hashtable */
174 if (lstat(dest
, &dest_stat
) < 0) {
175 bb_perror_msg("can't stat '%s'", dest
);
179 /* remember (dev,inode) of each created dir.
180 * NULL: name is not remembered */
181 add_to_ino_dev_hashtable(&dest_stat
, NULL
);
183 /* Recursively copy files in SOURCE */
184 dp
= opendir(source
);
187 goto preserve_mode_ugid_time
;
190 while ((d
= readdir(dp
)) != NULL
) {
191 char *new_source
, *new_dest
;
193 new_source
= concat_subpath_file(source
, d
->d_name
);
194 if (new_source
== NULL
)
196 new_dest
= concat_path_file(dest
, d
->d_name
);
197 if (copy_file(new_source
, new_dest
, flags
& ~FILEUTILS_DEREFERENCE_L0
) < 0)
205 && chmod(dest
, source_stat
.st_mode
& ~saved_umask
) < 0
207 bb_perror_msg("can't preserve %s of '%s'", "permissions", dest
);
208 /* retval = -1; - WRONG! copy *WAS* made */
210 goto preserve_mode_ugid_time
;
213 if (flags
& (FILEUTILS_MAKE_SOFTLINK
|FILEUTILS_MAKE_HARDLINK
)) {
214 int (*lf
)(const char *oldpath
, const char *newpath
);
217 * if (DEREF && MAKE_SOFTLINK) source = realpath(source) ?
218 * (but realpath returns NULL on dangling symlinks...) */
219 lf
= (flags
& FILEUTILS_MAKE_SOFTLINK
) ? symlink
: link
;
220 if (lf(source
, dest
) < 0) {
221 ovr
= ask_and_unlink(dest
, flags
);
224 if (lf(source
, dest
) < 0) {
225 bb_perror_msg("can't create link '%s'", dest
);
229 /* _Not_ jumping to preserve_mode_ugid_time:
230 * (sym)links don't have those */
234 if (/* "cp thing1 thing2" without -R: just open and read() from thing1 */
235 !(flags
& FILEUTILS_RECUR
)
236 /* "cp [-opts] regular_file thing2" */
237 || S_ISREG(source_stat
.st_mode
)
238 /* DEREF uses stat, which never returns S_ISLNK() == true.
239 * So the below is never true: */
240 /* || (FLAGS_DEREF && S_ISLNK(source_stat.st_mode)) */
246 if (!FLAGS_DEREF
&& S_ISLNK(source_stat
.st_mode
)) {
247 /* "cp -d symlink dst": create a link */
251 if (ENABLE_FEATURE_PRESERVE_HARDLINKS
&& !FLAGS_DEREF
) {
252 const char *link_target
;
253 link_target
= is_in_ino_dev_hashtable(&source_stat
);
255 if (link(link_target
, dest
) < 0) {
256 ovr
= ask_and_unlink(dest
, flags
);
259 if (link(link_target
, dest
) < 0) {
260 bb_perror_msg("can't create link '%s'", dest
);
266 add_to_ino_dev_hashtable(&source_stat
, dest
);
269 src_fd
= open_or_warn(source
, O_RDONLY
);
273 /* Do not try to open with weird mode fields */
274 new_mode
= source_stat
.st_mode
;
275 if (!S_ISREG(source_stat
.st_mode
))
278 // POSIX way is a security problem versus (sym)link attacks
279 if (!ENABLE_FEATURE_NON_POSIX_CP
) {
280 dst_fd
= open(dest
, O_WRONLY
|O_CREAT
|O_TRUNC
, new_mode
);
281 } else { /* safe way: */
282 dst_fd
= open(dest
, O_WRONLY
|O_CREAT
|O_EXCL
, new_mode
);
285 ovr
= ask_and_unlink(dest
, flags
);
290 /* It shouldn't exist. If it exists, do not open (symlink attack?) */
291 dst_fd
= open3_or_warn(dest
, O_WRONLY
|O_CREAT
|O_EXCL
, new_mode
);
299 if ((flags
& (FILEUTILS_PRESERVE_SECURITY_CONTEXT
|FILEUTILS_SET_SECURITY_CONTEXT
))
300 && is_selinux_enabled() > 0
302 security_context_t con
;
303 if (getfscreatecon(&con
) == -1) {
304 bb_perror_msg("getfscreatecon");
308 if (setfilecon(dest
, con
) == -1) {
309 bb_perror_msg("setfilecon:%s,%s", dest
, con
);
317 if (bb_copyfd_eof(src_fd
, dst_fd
) == -1)
319 /* Careful with writing... */
320 if (close(dst_fd
) < 0) {
321 bb_perror_msg("error writing to '%s'", dest
);
324 /* ...but read size is already checked by bb_copyfd_eof */
326 /* "cp /dev/something new_file" should not
327 * copy mode of /dev/something */
328 if (!S_ISREG(source_stat
.st_mode
))
330 goto preserve_mode_ugid_time
;
334 /* Source is a symlink or a special file */
335 /* We are lazy here, a bit lax with races... */
338 ovr
= ask_and_unlink(dest
, flags
);
342 if (S_ISLNK(source_stat
.st_mode
)) {
343 char *lpath
= xmalloc_readlink_or_warn(source
);
345 int r
= symlink(lpath
, dest
);
348 bb_perror_msg("can't create symlink '%s'", dest
);
351 if (flags
& FILEUTILS_PRESERVE_STATUS
)
352 if (lchown(dest
, source_stat
.st_uid
, source_stat
.st_gid
) < 0)
353 bb_perror_msg("can't preserve %s of '%s'", "ownership", dest
);
355 /* _Not_ jumping to preserve_mode_ugid_time:
356 * symlinks don't have those */
359 if (S_ISBLK(source_stat
.st_mode
) || S_ISCHR(source_stat
.st_mode
)
360 || S_ISSOCK(source_stat
.st_mode
) || S_ISFIFO(source_stat
.st_mode
)
362 if (mknod(dest
, source_stat
.st_mode
, source_stat
.st_rdev
) < 0) {
363 bb_perror_msg("can't create '%s'", dest
);
367 bb_error_msg("unrecognized file '%s' with mode %x", source
, source_stat
.st_mode
);
371 preserve_mode_ugid_time
:
373 if (flags
& FILEUTILS_PRESERVE_STATUS
375 /* && !(flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) */
377 struct timeval times
[2];
379 times
[1].tv_sec
= times
[0].tv_sec
= source_stat
.st_mtime
;
380 times
[1].tv_usec
= times
[0].tv_usec
= 0;
381 /* BTW, utimes sets usec-precision time - just FYI */
382 if (utimes(dest
, times
) < 0)
383 bb_perror_msg("can't preserve %s of '%s'", "times", dest
);
384 if (chown(dest
, source_stat
.st_uid
, source_stat
.st_gid
) < 0) {
385 source_stat
.st_mode
&= ~(S_ISUID
| S_ISGID
);
386 bb_perror_msg("can't preserve %s of '%s'", "ownership", dest
);
388 if (chmod(dest
, source_stat
.st_mode
) < 0)
389 bb_perror_msg("can't preserve %s of '%s'", "permissions", dest
);