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 int copy_file(const char *source, const char *dest, int flags)
15 struct stat source_stat;
16 struct stat dest_stat;
20 if ((!(flags & FILEUTILS_DEREFERENCE) &&
21 lstat(source, &source_stat) < 0) ||
22 ((flags & FILEUTILS_DEREFERENCE) &&
23 stat(source, &source_stat) < 0)) {
24 bb_perror_msg("%s", source);
28 if (lstat(dest, &dest_stat) < 0) {
29 if (errno != ENOENT) {
30 bb_perror_msg("unable to stat `%s'", dest);
34 if (source_stat.st_dev == dest_stat.st_dev &&
35 source_stat.st_ino == dest_stat.st_ino)
37 bb_error_msg("`%s' and `%s' are the same file", source, dest);
43 if (S_ISDIR(source_stat.st_mode)) {
46 mode_t saved_umask = 0;
48 if (!(flags & FILEUTILS_RECUR)) {
49 bb_error_msg("%s: omitting directory", source);
55 if (!S_ISDIR(dest_stat.st_mode)) {
56 bb_error_msg("`%s' is not a directory", dest);
61 saved_umask = umask(0);
63 mode = source_stat.st_mode;
64 if (!(flags & FILEUTILS_PRESERVE_STATUS))
65 mode = source_stat.st_mode & ~saved_umask;
68 if (mkdir(dest, mode) < 0) {
70 bb_perror_msg("cannot create directory `%s'", dest);
77 /* Recursively copy files in SOURCE. */
78 if ((dp = opendir(source)) == NULL) {
83 while ((d = readdir(dp)) != NULL) {
84 char *new_source, *new_dest;
86 new_source = concat_subpath_file(source, d->d_name);
87 if(new_source == NULL)
89 new_dest = concat_path_file(dest, d->d_name);
90 if (copy_file(new_source, new_dest, flags) < 0)
95 /* closedir have only EBADF error, but "dp" not changes */
99 chmod(dest, source_stat.st_mode & ~saved_umask) < 0) {
100 bb_perror_msg("unable to change permissions of `%s'", dest);
103 } else if (S_ISREG(source_stat.st_mode) ||
104 (S_ISLNK(source_stat.st_mode) && (flags & FILEUTILS_DEREFERENCE)))
108 if (ENABLE_FEATURE_PRESERVE_HARDLINKS) {
111 if (!(flags & FILEUTILS_DEREFERENCE) &&
112 is_in_ino_dev_hashtable(&source_stat, &link_name)) {
113 if (link(link_name, dest) < 0) {
114 bb_perror_msg("unable to link `%s'", dest);
120 add_to_ino_dev_hashtable(&source_stat, dest);
122 src_fd = open(source, O_RDONLY);
124 bb_perror_msg("unable to open `%s'", source);
129 if (flags & FILEUTILS_INTERACTIVE) {
130 fprintf(stderr, "%s: overwrite `%s'? ", applet_name, dest);
131 if (!bb_ask_confirmation()) {
137 dst_fd = open(dest, O_WRONLY|O_TRUNC);
139 if (!(flags & FILEUTILS_FORCE)) {
140 bb_perror_msg("unable to open `%s'", dest);
145 if (unlink(dest) < 0) {
146 bb_perror_msg("unable to remove `%s'", dest);
155 dst_fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode);
157 bb_perror_msg("unable to open `%s'", dest);
163 if (bb_copyfd_eof(src_fd, dst_fd) == -1)
166 if (close(dst_fd) < 0) {
167 bb_perror_msg("unable to close `%s'", dest);
171 if (close(src_fd) < 0) {
172 bb_perror_msg("unable to close `%s'", source);
175 } else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) ||
176 S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode) ||
177 S_ISLNK(source_stat.st_mode)) {
180 if((flags & FILEUTILS_FORCE) == 0) {
181 fprintf(stderr, "`%s' exists\n", dest);
184 if(unlink(dest) < 0) {
185 bb_perror_msg("unable to remove `%s'", dest);
189 if (S_ISFIFO(source_stat.st_mode)) {
190 if (mkfifo(dest, source_stat.st_mode) < 0) {
191 bb_perror_msg("cannot create fifo `%s'", dest);
194 } else if (S_ISLNK(source_stat.st_mode)) {
197 lpath = xreadlink(source);
198 if (symlink(lpath, dest) < 0) {
199 bb_perror_msg("cannot create symlink `%s'", dest);
204 if (flags & FILEUTILS_PRESERVE_STATUS)
205 if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
206 bb_perror_msg("unable to preserve ownership of `%s'", dest);
211 if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
212 bb_perror_msg("unable to create `%s'", dest);
217 bb_error_msg("internal error: unrecognized file type");
223 if (flags & FILEUTILS_PRESERVE_STATUS) {
224 struct utimbuf times;
225 char *msg="unable to preserve %s of `%s'";
227 times.actime = source_stat.st_atime;
228 times.modtime = source_stat.st_mtime;
229 if (utime(dest, ×) < 0)
230 bb_perror_msg(msg, "times", dest);
231 if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
232 source_stat.st_mode &= ~(S_ISUID | S_ISGID);
233 bb_perror_msg(msg, "ownership", dest);
235 if (chmod(dest, source_stat.st_mode) < 0)
236 bb_perror_msg(msg, "permissions", dest);