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 int copy_file(const char *source, const char *dest, int flags)
25 struct stat source_stat;
26 struct stat dest_stat;
30 if ((!(flags & FILEUTILS_DEREFERENCE) &&
31 lstat(source, &source_stat) < 0) ||
32 ((flags & FILEUTILS_DEREFERENCE) &&
33 stat(source, &source_stat) < 0)) {
34 bb_perror_msg("%s", source);
38 if (lstat(dest, &dest_stat) < 0) {
39 if (errno != ENOENT) {
40 bb_perror_msg("unable to stat `%s'", dest);
44 if (source_stat.st_dev == dest_stat.st_dev &&
45 source_stat.st_ino == dest_stat.st_ino)
47 bb_error_msg("`%s' and `%s' are the same file", source, dest);
53 if (S_ISDIR(source_stat.st_mode)) {
56 mode_t saved_umask = 0;
58 if (!(flags & FILEUTILS_RECUR)) {
59 bb_error_msg("%s: omitting directory", source);
65 if (!S_ISDIR(dest_stat.st_mode)) {
66 bb_error_msg("`%s' is not a directory", dest);
71 saved_umask = umask(0);
73 mode = source_stat.st_mode;
74 if (!(flags & FILEUTILS_PRESERVE_STATUS))
75 mode = source_stat.st_mode & ~saved_umask;
78 if (mkdir(dest, mode) < 0) {
80 bb_perror_msg("cannot create directory `%s'", dest);
87 /* Recursively copy files in SOURCE. */
88 if ((dp = bb_opendir(source)) == NULL) {
93 while ((d = readdir(dp)) != NULL) {
94 char *new_source, *new_dest;
96 new_source = concat_subpath_file(source, d->d_name);
97 if(new_source == NULL)
99 new_dest = concat_path_file(dest, d->d_name);
100 if (copy_file(new_source, new_dest, flags) < 0)
105 /* closedir have only EBADF error, but "dp" not changes */
109 chmod(dest, source_stat.st_mode & ~saved_umask) < 0) {
110 bb_perror_msg("unable to change permissions of `%s'", dest);
113 } else if (S_ISREG(source_stat.st_mode) || (flags & FILEUTILS_DEREFERENCE))
117 #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS
120 if (!(flags & FILEUTILS_DEREFERENCE) &&
121 is_in_ino_dev_hashtable(&source_stat, &link_name)) {
122 if (link(link_name, dest) < 0) {
123 bb_perror_msg("unable to link `%s'", dest);
129 add_to_ino_dev_hashtable(&source_stat, dest);
131 src_fd = open(source, O_RDONLY);
133 bb_perror_msg("unable to open `%s'", source);
138 if (flags & FILEUTILS_INTERACTIVE) {
139 fprintf(stderr, "%s: overwrite `%s'? ", bb_applet_name, dest);
140 if (!bb_ask_confirmation()) {
146 dst_fd = open(dest, O_WRONLY|O_TRUNC);
148 if (!(flags & FILEUTILS_FORCE)) {
149 bb_perror_msg("unable to open `%s'", dest);
154 if (unlink(dest) < 0) {
155 bb_perror_msg("unable to remove `%s'", dest);
164 dst_fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode);
166 bb_perror_msg("unable to open `%s'", dest);
172 if (bb_copyfd_eof(src_fd, dst_fd) == -1)
175 if (close(dst_fd) < 0) {
176 bb_perror_msg("unable to close `%s'", dest);
180 if (close(src_fd) < 0) {
181 bb_perror_msg("unable to close `%s'", source);
184 } else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) ||
185 S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode) ||
186 S_ISLNK(source_stat.st_mode)) {
189 if((flags & FILEUTILS_FORCE) == 0) {
190 fprintf(stderr, "`%s' exists\n", dest);
193 if(unlink(dest) < 0) {
194 bb_perror_msg("unable to remove `%s'", dest);
198 if (S_ISFIFO(source_stat.st_mode)) {
199 if (mkfifo(dest, source_stat.st_mode) < 0) {
200 bb_perror_msg("cannot create fifo `%s'", dest);
203 } else if (S_ISLNK(source_stat.st_mode)) {
206 lpath = xreadlink(source);
207 if (symlink(lpath, dest) < 0) {
208 bb_perror_msg("cannot create symlink `%s'", dest);
213 if (flags & FILEUTILS_PRESERVE_STATUS)
214 if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
215 bb_perror_msg("unable to preserve ownership of `%s'", dest);
220 if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
221 bb_perror_msg("unable to create `%s'", dest);
226 bb_error_msg("internal error: unrecognized file type");
232 if (flags & FILEUTILS_PRESERVE_STATUS) {
233 struct utimbuf times;
234 char *msg="unable to preserve %s of `%s'";
236 times.actime = source_stat.st_atime;
237 times.modtime = source_stat.st_mtime;
238 if (utime(dest, ×) < 0)
239 bb_perror_msg(msg, "times", dest);
240 if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
241 source_stat.st_mode &= ~(S_ISUID | S_ISGID);
242 bb_perror_msg(msg, "ownership", dest);
244 if (chmod(dest, source_stat.st_mode) < 0)
245 bb_perror_msg(msg, "permissions", dest);