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 static int retry_overwrite(const char *dest, int flags)
16 if (!(flags & (FILEUTILS_FORCE|FILEUTILS_INTERACTIVE))) {
17 fprintf(stderr, "'%s' exists\n", dest);
20 if (flags & FILEUTILS_INTERACTIVE) {
21 fprintf(stderr, "%s: overwrite '%s'? ", applet_name, dest);
22 if (!bb_ask_confirmation())
23 return 0; // not allowed to overwrite
25 if (unlink(dest) < 0) {
26 bb_perror_msg("cannot remove '%s'", dest);
29 return 1; // ok (to try again)
32 int copy_file(const char *source, const char *dest, int flags)
34 struct stat source_stat;
35 struct stat dest_stat;
37 signed char dest_exists = 0;
40 #define FLAGS_DEREF (flags & FILEUTILS_DEREFERENCE)
42 if ((FLAGS_DEREF ? stat : lstat)(source, &source_stat) < 0) {
43 // This may be a dangling symlink.
44 // Making [sym]links to dangling symlinks works, so...
45 if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK))
47 bb_perror_msg("cannot stat '%s'", source);
51 if (lstat(dest, &dest_stat) < 0) {
52 if (errno != ENOENT) {
53 bb_perror_msg("cannot stat '%s'", dest);
57 if (source_stat.st_dev == dest_stat.st_dev
58 && source_stat.st_ino == dest_stat.st_ino
60 bb_error_msg("'%s' and '%s' are the same file", source, dest);
67 if ((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT) && is_selinux_enabled() > 0) {
68 security_context_t con;
69 if (lgetfilecon(source, &con) >= 0) {
70 if (setfscreatecon(con) < 0) {
71 bb_perror_msg("cannot set setfscreatecon %s", con);
76 if (errno == ENOTSUP || errno == ENODATA) {
79 bb_perror_msg("cannot lgetfilecon %s", source);
86 if (S_ISDIR(source_stat.st_mode)) {
89 mode_t saved_umask = 0;
91 if (!(flags & FILEUTILS_RECUR)) {
92 bb_error_msg("omitting directory '%s'", source);
98 if (!S_ISDIR(dest_stat.st_mode)) {
99 bb_error_msg("target '%s' is not a directory", dest);
104 saved_umask = umask(0);
106 mode = source_stat.st_mode;
107 if (!(flags & FILEUTILS_PRESERVE_STATUS))
108 mode = source_stat.st_mode & ~saved_umask;
111 if (mkdir(dest, mode) < 0) {
113 bb_perror_msg("cannot create directory '%s'", dest);
120 /* Recursively copy files in SOURCE. */
121 dp = opendir(source);
124 goto preserve_status;
127 while ((d = readdir(dp)) != NULL) {
128 char *new_source, *new_dest;
130 new_source = concat_subpath_file(source, d->d_name);
131 if (new_source == NULL)
133 new_dest = concat_path_file(dest, d->d_name);
134 if (copy_file(new_source, new_dest, flags) < 0)
142 && chmod(dest, source_stat.st_mode & ~saved_umask) < 0
144 bb_perror_msg("cannot change permissions of '%s'", dest);
148 } else if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) {
149 int (*lf)(const char *oldpath, const char *newpath);
152 // if (DEREF && MAKE_SOFTLINK) source = realpath(source) ?
153 // (but realpath returns NULL on dangling symlinks...)
154 lf = (flags & FILEUTILS_MAKE_SOFTLINK) ? symlink : link;
155 if (lf(source, dest) < 0) {
156 ovr = retry_overwrite(dest, flags);
159 if (lf(source, dest) < 0) {
160 bb_perror_msg("cannot create link '%s'", dest);
166 } else if (S_ISREG(source_stat.st_mode)
167 // Huh? DEREF uses stat, which never returns links IIRC...
168 || (FLAGS_DEREF && S_ISLNK(source_stat.st_mode))
172 if (ENABLE_FEATURE_PRESERVE_HARDLINKS) {
176 && is_in_ino_dev_hashtable(&source_stat, &link_name)
178 if (link(link_name, dest) < 0) {
179 ovr = retry_overwrite(dest, flags);
182 if (link(link_name, dest) < 0) {
183 bb_perror_msg("cannot create link '%s'", dest);
189 // TODO: probably is_in_.. and add_to_...
190 // can be combined: find_or_add_...
191 add_to_ino_dev_hashtable(&source_stat, dest);
194 src_fd = open(source, O_RDONLY);
196 bb_perror_msg("cannot open '%s'", source);
200 // POSIX: if exists and -i, ask (w/o -i assume yes).
201 // Then open w/o EXCL.
202 // If open still fails and -f, try unlink, then try open again.
204 // If dest is a softlink, we overwrite softlink's destination!
205 // (or fail, if it points to dir/nonexistent location/etc).
206 // This is strange, but POSIX-correct.
207 // coreutils cp has --remove-destination to override this...
208 dst_fd = open(dest, (flags & FILEUTILS_INTERACTIVE)
209 ? O_WRONLY|O_CREAT|O_TRUNC|O_EXCL
210 : O_WRONLY|O_CREAT|O_TRUNC, source_stat.st_mode);
212 // We would not do POSIX insanity. -i asks,
213 // then _unlinks_ the offender. Presto.
214 // Or else we will end up having 3 open()s!
215 ovr = retry_overwrite(dest, flags);
220 dst_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, source_stat.st_mode);
222 bb_perror_msg("cannot open '%s'", dest);
229 if (((flags & FILEUTILS_PRESERVE_SECURITY_CONTEXT)
230 || (flags & FILEUTILS_SET_SECURITY_CONTEXT))
231 && is_selinux_enabled() > 0) {
232 security_context_t con;
233 if (getfscreatecon(&con) == -1) {
234 bb_perror_msg("getfscreatecon");
238 if(setfilecon(dest, con) == -1) {
239 bb_perror_msg("setfilecon:%s,%s", dest, con);
247 if (bb_copyfd_eof(src_fd, dst_fd) == -1)
249 if (close(dst_fd) < 0) {
250 bb_perror_msg("cannot close '%s'", dest);
253 if (close(src_fd) < 0) {
254 bb_perror_msg("cannot close '%s'", source);
258 } else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode)
259 || S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode)
260 || S_ISLNK(source_stat.st_mode)
262 // We are lazy here, a bit lax with races...
264 ovr = retry_overwrite(dest, flags);
268 if (S_ISFIFO(source_stat.st_mode)) {
269 if (mkfifo(dest, source_stat.st_mode) < 0) {
270 bb_perror_msg("cannot create fifo '%s'", dest);
273 } else if (S_ISLNK(source_stat.st_mode)) {
276 lpath = xmalloc_readlink_or_warn(source);
277 if (symlink(lpath, dest) < 0) {
278 bb_perror_msg("cannot create symlink '%s'", dest);
284 if (flags & FILEUTILS_PRESERVE_STATUS)
285 if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
286 bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest);
291 if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
292 bb_perror_msg("cannot create '%s'", dest);
297 bb_error_msg("internal error: unrecognized file type");
303 if (flags & FILEUTILS_PRESERVE_STATUS
305 /* && !(flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) */
307 struct utimbuf times;
309 times.actime = source_stat.st_atime;
310 times.modtime = source_stat.st_mtime;
311 if (utime(dest, ×) < 0)
312 bb_perror_msg("cannot preserve %s of '%s'", "times", dest);
313 if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
314 source_stat.st_mode &= ~(S_ISUID | S_ISGID);
315 bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest);
317 if (chmod(dest, source_stat.st_mode) < 0)
318 bb_perror_msg("cannot preserve %s of '%s'", "permissions", dest);