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.
15 int copy_file(const char *source, const char *dest, int flags)
17 struct stat source_stat;
18 struct stat dest_stat;
22 if ((!(flags & FILEUTILS_DEREFERENCE) &&
23 lstat(source, &source_stat) < 0) ||
24 ((flags & FILEUTILS_DEREFERENCE) &&
25 stat(source, &source_stat) < 0)) {
26 bb_perror_msg("%s", source);
30 if (lstat(dest, &dest_stat) < 0) {
31 if (errno != ENOENT) {
32 bb_perror_msg("unable to stat `%s'", dest);
36 if (source_stat.st_dev == dest_stat.st_dev &&
37 source_stat.st_ino == dest_stat.st_ino)
39 bb_error_msg("`%s' and `%s' are the same file", source, dest);
45 if (S_ISDIR(source_stat.st_mode)) {
48 mode_t saved_umask = 0;
50 if (!(flags & FILEUTILS_RECUR)) {
51 bb_error_msg("%s: omitting directory", source);
57 if (!S_ISDIR(dest_stat.st_mode)) {
58 bb_error_msg("`%s' is not a directory", dest);
63 saved_umask = umask(0);
65 mode = source_stat.st_mode;
66 if (!(flags & FILEUTILS_PRESERVE_STATUS))
67 mode = source_stat.st_mode & ~saved_umask;
70 if (mkdir(dest, mode) < 0) {
72 bb_perror_msg("cannot create directory `%s'", dest);
79 /* Recursively copy files in SOURCE. */
80 if ((dp = bb_opendir(source)) == NULL) {
85 while ((d = readdir(dp)) != NULL) {
86 char *new_source, *new_dest;
88 new_source = concat_subpath_file(source, d->d_name);
89 if(new_source == NULL)
91 new_dest = concat_path_file(dest, d->d_name);
92 if (copy_file(new_source, new_dest, flags) < 0)
97 /* closedir have only EBADF error, but "dp" not changes */
101 chmod(dest, source_stat.st_mode & ~saved_umask) < 0) {
102 bb_perror_msg("unable to change permissions of `%s'", dest);
105 } else if (S_ISREG(source_stat.st_mode) ||
106 (S_ISLNK(source_stat.st_mode) && (flags & FILEUTILS_DEREFERENCE)))
110 if (ENABLE_FEATURE_PRESERVE_HARDLINKS) {
113 if (!(flags & FILEUTILS_DEREFERENCE) &&
114 is_in_ino_dev_hashtable(&source_stat, &link_name)) {
115 if (link(link_name, dest) < 0) {
116 bb_perror_msg("unable to link `%s'", dest);
122 add_to_ino_dev_hashtable(&source_stat, dest);
124 src_fd = open(source, O_RDONLY);
126 bb_perror_msg("unable to open `%s'", source);
131 if (flags & FILEUTILS_INTERACTIVE) {
132 fprintf(stderr, "%s: overwrite `%s'? ", bb_applet_name, dest);
133 if (!bb_ask_confirmation()) {
139 dst_fd = open(dest, O_WRONLY|O_TRUNC);
141 if (!(flags & FILEUTILS_FORCE)) {
142 bb_perror_msg("unable to open `%s'", dest);
147 if (unlink(dest) < 0) {
148 bb_perror_msg("unable to remove `%s'", dest);
157 dst_fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode);
159 bb_perror_msg("unable to open `%s'", dest);
165 if (bb_copyfd_eof(src_fd, dst_fd) == -1)
168 if (close(dst_fd) < 0) {
169 bb_perror_msg("unable to close `%s'", dest);
173 if (close(src_fd) < 0) {
174 bb_perror_msg("unable to close `%s'", source);
177 } else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) ||
178 S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode) ||
179 S_ISLNK(source_stat.st_mode)) {
182 if((flags & FILEUTILS_FORCE) == 0) {
183 fprintf(stderr, "`%s' exists\n", dest);
186 if(unlink(dest) < 0) {
187 bb_perror_msg("unable to remove `%s'", dest);
191 if (S_ISFIFO(source_stat.st_mode)) {
192 if (mkfifo(dest, source_stat.st_mode) < 0) {
193 bb_perror_msg("cannot create fifo `%s'", dest);
196 } else if (S_ISLNK(source_stat.st_mode)) {
199 lpath = xreadlink(source);
200 if (symlink(lpath, dest) < 0) {
201 bb_perror_msg("cannot create symlink `%s'", dest);
206 if (flags & FILEUTILS_PRESERVE_STATUS)
207 if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
208 bb_perror_msg("unable to preserve ownership of `%s'", dest);
213 if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
214 bb_perror_msg("unable to create `%s'", dest);
219 bb_error_msg("internal error: unrecognized file type");
225 if (flags & FILEUTILS_PRESERVE_STATUS) {
226 struct utimbuf times;
227 char *msg="unable to preserve %s of `%s'";
229 times.actime = source_stat.st_atime;
230 times.modtime = source_stat.st_mtime;
231 if (utime(dest, ×) < 0)
232 bb_perror_msg(msg, "times", dest);
233 if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
234 source_stat.st_mode &= ~(S_ISUID | S_ISGID);
235 bb_perror_msg(msg, "ownership", dest);
237 if (chmod(dest, source_stat.st_mode) < 0)
238 bb_perror_msg(msg, "permissions", dest);