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);
173 dst_fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode);
175 bb_perror_msg("unable to open `%s'", dest);
181 if (bb_copyfd_eof(src_fd, dst_fd) == -1)
184 if (close(dst_fd) < 0) {
185 bb_perror_msg("unable to close `%s'", dest);
189 if (close(src_fd) < 0) {
190 bb_perror_msg("unable to close `%s'", source);
193 } else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) ||
194 S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode) ||
195 S_ISLNK(source_stat.st_mode)) {
198 if((flags & FILEUTILS_FORCE) == 0) {
199 fprintf(stderr, "`%s' exists\n", dest);
202 if(unlink(dest) < 0) {
203 bb_perror_msg("unable to remove `%s'", dest);
207 if (S_ISFIFO(source_stat.st_mode)) {
208 if (mkfifo(dest, source_stat.st_mode) < 0) {
209 bb_perror_msg("cannot create fifo `%s'", dest);
212 } else if (S_ISLNK(source_stat.st_mode)) {
215 lpath = xreadlink(source);
216 if (symlink(lpath, dest) < 0) {
217 bb_perror_msg("cannot create symlink `%s'", dest);
222 if (flags & FILEUTILS_PRESERVE_STATUS)
223 if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
224 bb_perror_msg("unable to preserve ownership of `%s'", dest);
229 if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
230 bb_perror_msg("unable to create `%s'", dest);
235 bb_error_msg("internal error: unrecognized file type");
241 if (flags & FILEUTILS_PRESERVE_STATUS) {
242 struct utimbuf times;
243 char *msg="unable to preserve %s of `%s'";
245 times.actime = source_stat.st_atime;
246 times.modtime = source_stat.st_mtime;
247 if (utime(dest, ×) < 0)
248 bb_perror_msg(msg, "times", dest);
249 if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
250 source_stat.st_mode &= ~(S_ISUID | S_ISGID);
251 bb_perror_msg(msg, "ownership", dest);
253 if (chmod(dest, source_stat.st_mode) < 0)
254 bb_perror_msg(msg, "permissions", dest);