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>
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
13 static int retry_overwrite(const char *dest, int flags)
15 if (!(flags & (FILEUTILS_FORCE|FILEUTILS_INTERACTIVE))) {
16 fprintf(stderr, "'%s' exists\n", dest);
19 if (flags & FILEUTILS_INTERACTIVE) {
20 fprintf(stderr, "%s: overwrite '%s'? ", applet_name, dest);
21 if (!bb_ask_confirmation())
22 return 0; // not allowed to overwrite
24 if (unlink(dest) < 0) {
25 bb_perror_msg("cannot remove '%s'", dest);
28 return 1; // ok (to try again)
31 int copy_file(const char *source, const char *dest, int flags)
33 struct stat source_stat;
34 struct stat dest_stat;
36 signed char dest_exists = 0;
39 #define FLAGS_DEREF (flags & FILEUTILS_DEREFERENCE)
41 if ((FLAGS_DEREF ? stat : lstat)(source, &source_stat) < 0) {
42 // This may be a dangling symlink.
43 // Making [sym]links to dangling symlinks works, so...
44 if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK))
46 bb_perror_msg("cannot stat '%s'", source);
50 if (lstat(dest, &dest_stat) < 0) {
51 if (errno != ENOENT) {
52 bb_perror_msg("cannot stat '%s'", dest);
56 if (source_stat.st_dev == dest_stat.st_dev
57 && source_stat.st_ino == dest_stat.st_ino
59 bb_error_msg("'%s' and '%s' are the same file", source, dest);
65 if (S_ISDIR(source_stat.st_mode)) {
68 mode_t saved_umask = 0;
70 if (!(flags & FILEUTILS_RECUR)) {
71 bb_error_msg("omitting directory '%s'", source);
77 if (!S_ISDIR(dest_stat.st_mode)) {
78 bb_error_msg("target '%s' is not a directory", dest);
83 saved_umask = umask(0);
85 mode = source_stat.st_mode;
86 if (!(flags & FILEUTILS_PRESERVE_STATUS))
87 mode = source_stat.st_mode & ~saved_umask;
90 if (mkdir(dest, mode) < 0) {
92 bb_perror_msg("cannot create directory '%s'", dest);
99 /* Recursively copy files in SOURCE. */
100 dp = opendir(source);
103 goto preserve_status;
106 while ((d = readdir(dp)) != NULL) {
107 char *new_source, *new_dest;
109 new_source = concat_subpath_file(source, d->d_name);
110 if (new_source == NULL)
112 new_dest = concat_path_file(dest, d->d_name);
113 if (copy_file(new_source, new_dest, flags) < 0)
121 && chmod(dest, source_stat.st_mode & ~saved_umask) < 0
123 bb_perror_msg("cannot change permissions of '%s'", dest);
127 } else if (flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) {
128 int (*lf)(const char *oldpath, const char *newpath);
131 // if (DEREF && MAKE_SOFTLINK) source = realpath(source) ?
132 // (but realpath returns NULL on dangling symlinks...)
133 lf = (flags & FILEUTILS_MAKE_SOFTLINK) ? symlink : link;
134 if (lf(source, dest) < 0) {
135 ovr = retry_overwrite(dest, flags);
138 if (lf(source, dest) < 0) {
139 bb_perror_msg("cannot create link '%s'", dest);
145 } else if (S_ISREG(source_stat.st_mode)
146 // Huh? DEREF uses stat, which never returns links IIRC...
147 || (FLAGS_DEREF && S_ISLNK(source_stat.st_mode))
151 if (ENABLE_FEATURE_PRESERVE_HARDLINKS) {
155 && is_in_ino_dev_hashtable(&source_stat, &link_name)
157 if (link(link_name, dest) < 0) {
158 ovr = retry_overwrite(dest, flags);
161 if (link(link_name, dest) < 0) {
162 bb_perror_msg("cannot create link '%s'", dest);
168 // TODO: probably is_in_.. and add_to_...
169 // can be combined: find_or_add_...
170 add_to_ino_dev_hashtable(&source_stat, dest);
173 src_fd = open(source, O_RDONLY);
175 bb_perror_msg("cannot open '%s'", source);
179 // POSIX: if exists and -i, ask (w/o -i assume yes).
180 // Then open w/o EXCL.
181 // If open still fails and -f, try unlink, then try open again.
183 // If dest is a softlink, we overwrite softlink's destination!
184 // (or fail, if it points to dir/nonexistent location/etc).
185 // This is strange, but POSIX-correct.
186 // coreutils cp has --remove-destination to override this...
187 dst_fd = open(dest, (flags & FILEUTILS_INTERACTIVE)
188 ? O_WRONLY|O_CREAT|O_TRUNC|O_EXCL
189 : O_WRONLY|O_CREAT|O_TRUNC, source_stat.st_mode);
191 // We would not do POSIX insanity. -i asks,
192 // then _unlinks_ the offender. Presto.
193 // Or else we will end up having 3 open()s!
194 ovr = retry_overwrite(dest, flags);
199 dst_fd = open(dest, O_WRONLY|O_CREAT|O_TRUNC, source_stat.st_mode);
201 bb_perror_msg("cannot open '%s'", dest);
207 if (bb_copyfd_eof(src_fd, dst_fd) == -1)
209 if (close(dst_fd) < 0) {
210 bb_perror_msg("cannot close '%s'", dest);
213 if (close(src_fd) < 0) {
214 bb_perror_msg("cannot close '%s'", source);
218 } else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode)
219 || S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode)
220 || S_ISLNK(source_stat.st_mode)
222 // We are lazy here, a bit lax with races...
224 ovr = retry_overwrite(dest, flags);
228 if (S_ISFIFO(source_stat.st_mode)) {
229 if (mkfifo(dest, source_stat.st_mode) < 0) {
230 bb_perror_msg("cannot create fifo '%s'", dest);
233 } else if (S_ISLNK(source_stat.st_mode)) {
236 lpath = xreadlink(source);
237 if (symlink(lpath, dest) < 0) {
238 bb_perror_msg("cannot create symlink '%s'", dest);
244 if (flags & FILEUTILS_PRESERVE_STATUS)
245 if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
246 bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest);
251 if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
252 bb_perror_msg("cannot create '%s'", dest);
257 bb_error_msg("internal error: unrecognized file type");
263 if (flags & FILEUTILS_PRESERVE_STATUS
265 /* && !(flags & (FILEUTILS_MAKE_SOFTLINK|FILEUTILS_MAKE_HARDLINK)) */
267 struct utimbuf times;
269 times.actime = source_stat.st_atime;
270 times.modtime = source_stat.st_mtime;
271 if (utime(dest, ×) < 0)
272 bb_perror_msg("cannot preserve %s of '%s'", "times", dest);
273 if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
274 source_stat.st_mode &= ~(S_ISUID | S_ISGID);
275 bb_perror_msg("cannot preserve %s of '%s'", "ownership", dest);
277 if (chmod(dest, source_stat.st_mode) < 0)
278 bb_perror_msg("cannot preserve %s of '%s'", "permissions", dest);