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.
11 #include <sys/types.h>
23 /* Compiler version-specific crap that should be in a header file somewhere. */
25 #if !((__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1))
30 int copy_file(const char *source, const char *dest, int flags)
32 struct stat source_stat;
33 struct stat dest_stat;
37 if ((!(flags & FILEUTILS_DEREFERENCE) &&
38 lstat(source, &source_stat) < 0) ||
39 ((flags & FILEUTILS_DEREFERENCE) &&
40 stat(source, &source_stat) < 0)) {
41 bb_perror_msg("%s", source);
45 if (lstat(dest, &dest_stat) < 0) {
46 if (errno != ENOENT) {
47 bb_perror_msg("unable to stat `%s'", dest);
51 if (source_stat.st_dev == dest_stat.st_dev &&
52 source_stat.st_ino == dest_stat.st_ino)
54 bb_error_msg("`%s' and `%s' are the same file", source, dest);
60 if (S_ISDIR(source_stat.st_mode)) {
63 mode_t saved_umask = 0;
65 if (!(flags & FILEUTILS_RECUR)) {
66 bb_error_msg("%s: omitting directory", source);
72 if (!S_ISDIR(dest_stat.st_mode)) {
73 bb_error_msg("`%s' is not a directory", dest);
78 saved_umask = umask(0);
80 mode = source_stat.st_mode;
81 if (!(flags & FILEUTILS_PRESERVE_STATUS))
82 mode = source_stat.st_mode & ~saved_umask;
85 if (mkdir(dest, mode) < 0) {
87 bb_perror_msg("cannot create directory `%s'", dest);
94 /* Recursively copy files in SOURCE. */
95 if ((dp = bb_opendir(source)) == NULL) {
100 while ((d = readdir(dp)) != NULL) {
101 char *new_source, *new_dest;
103 new_source = concat_subpath_file(source, d->d_name);
104 if(new_source == NULL)
106 new_dest = concat_path_file(dest, d->d_name);
107 if (copy_file(new_source, new_dest, flags) < 0)
112 /* closedir have only EBADF error, but "dp" not changes */
116 chmod(dest, source_stat.st_mode & ~saved_umask) < 0) {
117 bb_perror_msg("unable to change permissions of `%s'", dest);
120 } else if (S_ISREG(source_stat.st_mode) || (flags & FILEUTILS_DEREFERENCE))
124 #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS
127 if (!(flags & FILEUTILS_DEREFERENCE) &&
128 is_in_ino_dev_hashtable(&source_stat, &link_name)) {
129 if (link(link_name, dest) < 0) {
130 bb_perror_msg("unable to link `%s'", dest);
136 add_to_ino_dev_hashtable(&source_stat, dest);
138 src_fd = open(source, O_RDONLY);
140 bb_perror_msg("unable to open `%s'", source);
145 if (flags & FILEUTILS_INTERACTIVE) {
146 fprintf(stderr, "%s: overwrite `%s'? ", bb_applet_name, dest);
147 if (!bb_ask_confirmation()) {
153 dst_fd = open(dest, O_WRONLY|O_TRUNC);
155 if (!(flags & FILEUTILS_FORCE)) {
156 bb_perror_msg("unable to open `%s'", dest);
161 if (unlink(dest) < 0) {
162 bb_perror_msg("unable to remove `%s'", dest);
171 dst_fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode);
173 bb_perror_msg("unable to open `%s'", dest);
179 if (bb_copyfd_eof(src_fd, dst_fd) == -1)
182 if (close(dst_fd) < 0) {
183 bb_perror_msg("unable to close `%s'", dest);
187 if (close(src_fd) < 0) {
188 bb_perror_msg("unable to close `%s'", source);
191 } else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) ||
192 S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode) ||
193 S_ISLNK(source_stat.st_mode)) {
196 if((flags & FILEUTILS_FORCE) == 0) {
197 fprintf(stderr, "`%s' exists\n", dest);
200 if(unlink(dest) < 0) {
201 bb_perror_msg("unable to remove `%s'", dest);
205 if (S_ISFIFO(source_stat.st_mode)) {
206 if (mkfifo(dest, source_stat.st_mode) < 0) {
207 bb_perror_msg("cannot create fifo `%s'", dest);
210 } else if (S_ISLNK(source_stat.st_mode)) {
213 lpath = xreadlink(source);
214 if (symlink(lpath, dest) < 0) {
215 bb_perror_msg("cannot create symlink `%s'", dest);
220 if (flags & FILEUTILS_PRESERVE_STATUS)
221 if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
222 bb_perror_msg("unable to preserve ownership of `%s'", dest);
227 if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
228 bb_perror_msg("unable to create `%s'", dest);
233 bb_error_msg("internal error: unrecognized file type");
239 if (flags & FILEUTILS_PRESERVE_STATUS) {
240 struct utimbuf times;
241 char *msg="unable to preserve %s of `%s'";
243 times.actime = source_stat.st_atime;
244 times.modtime = source_stat.st_mtime;
245 if (utime(dest, ×) < 0)
246 bb_perror_msg(msg, "times", dest);
247 if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
248 source_stat.st_mode &= ~(S_ISUID | S_ISGID);
249 bb_perror_msg(msg, "ownership", dest);
251 if (chmod(dest, source_stat.st_mode) < 0)
252 bb_perror_msg(msg, "permissions", dest);