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 = opendir(source)) == NULL) {
96 bb_perror_msg("unable to open directory `%s'", source);
101 while ((d = readdir(dp)) != NULL) {
102 char *new_source, *new_dest;
104 new_source = concat_subpath_file(source, d->d_name);
105 if(new_source == NULL)
107 new_dest = concat_path_file(dest, d->d_name);
108 if (copy_file(new_source, new_dest, flags) < 0)
113 /* closedir have only EBADF error, but "dp" not changes */
117 chmod(dest, source_stat.st_mode & ~saved_umask) < 0) {
118 bb_perror_msg("unable to change permissions of `%s'", dest);
121 } else if (S_ISREG(source_stat.st_mode) || (flags & FILEUTILS_DEREFERENCE))
125 #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS
128 if (!(flags & FILEUTILS_DEREFERENCE) &&
129 is_in_ino_dev_hashtable(&source_stat, &link_name)) {
130 if (link(link_name, dest) < 0) {
131 bb_perror_msg("unable to link `%s'", dest);
137 add_to_ino_dev_hashtable(&source_stat, dest);
139 src_fd = open(source, O_RDONLY);
141 bb_perror_msg("unable to open `%s'", source);
146 if (flags & FILEUTILS_INTERACTIVE) {
147 fprintf(stderr, "%s: overwrite `%s'? ", bb_applet_name, dest);
148 if (!bb_ask_confirmation()) {
154 dst_fd = open(dest, O_WRONLY|O_TRUNC);
156 if (!(flags & FILEUTILS_FORCE)) {
157 bb_perror_msg("unable to open `%s'", dest);
162 if (unlink(dest) < 0) {
163 bb_perror_msg("unable to remove `%s'", dest);
172 dst_fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode);
174 bb_perror_msg("unable to open `%s'", dest);
180 if (bb_copyfd_eof(src_fd, dst_fd) == -1)
183 if (close(dst_fd) < 0) {
184 bb_perror_msg("unable to close `%s'", dest);
188 if (close(src_fd) < 0) {
189 bb_perror_msg("unable to close `%s'", source);
192 } else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) ||
193 S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode) ||
194 S_ISLNK(source_stat.st_mode)) {
197 if((flags & FILEUTILS_FORCE) == 0) {
198 fprintf(stderr, "`%s' exists\n", dest);
201 if(unlink(dest) < 0) {
202 bb_perror_msg("unable to remove `%s'", dest);
206 if (S_ISFIFO(source_stat.st_mode)) {
207 if (mkfifo(dest, source_stat.st_mode) < 0) {
208 bb_perror_msg("cannot create fifo `%s'", dest);
211 } else if (S_ISLNK(source_stat.st_mode)) {
214 lpath = xreadlink(source);
215 if (symlink(lpath, dest) < 0) {
216 bb_perror_msg("cannot create symlink `%s'", dest);
221 if (flags & FILEUTILS_PRESERVE_STATUS)
222 if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
223 bb_perror_msg("unable to preserve ownership of `%s'", dest);
228 if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
229 bb_perror_msg("unable to create `%s'", dest);
234 bb_error_msg("internal error: unrecognized file type");
240 if (flags & FILEUTILS_PRESERVE_STATUS) {
241 struct utimbuf times;
242 char *msg="unable to preserve %s of `%s'";
244 times.actime = source_stat.st_atime;
245 times.modtime = source_stat.st_mtime;
246 if (utime(dest, ×) < 0)
247 bb_perror_msg(msg, "times", dest);
248 if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
249 source_stat.st_mode &= ~(S_ISUID | S_ISGID);
250 bb_perror_msg(msg, "ownership", dest);
252 if (chmod(dest, source_stat.st_mode) < 0)
253 bb_perror_msg(msg, "permissions", dest);