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 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <sys/types.h>
35 int copy_file(const char *source, const char *dest, int flags)
37 struct stat source_stat;
38 struct stat dest_stat;
42 if ((!(flags & FILEUTILS_DEREFERENCE) &&
43 lstat(source, &source_stat) < 0) ||
44 ((flags & FILEUTILS_DEREFERENCE) &&
45 stat(source, &source_stat) < 0)) {
46 bb_perror_msg("%s", source);
50 if (lstat(dest, &dest_stat) < 0) {
51 if (errno != ENOENT) {
52 bb_perror_msg("unable to stat `%s'", dest);
56 if (source_stat.st_dev == dest_stat.st_dev &&
57 source_stat.st_ino == dest_stat.st_ino) {
58 bb_error_msg("`%s' and `%s' are the same file", source, dest);
64 if (S_ISDIR(source_stat.st_mode)) {
67 mode_t saved_umask = 0;
69 if (!(flags & FILEUTILS_RECUR)) {
70 bb_error_msg("%s: omitting directory", source);
76 if (!S_ISDIR(dest_stat.st_mode)) {
77 bb_error_msg("`%s' is not a directory", dest);
82 saved_umask = umask(0);
84 mode = source_stat.st_mode;
85 if (!(flags & FILEUTILS_PRESERVE_STATUS))
86 mode = source_stat.st_mode & ~saved_umask;
89 if (mkdir(dest, mode) < 0) {
91 bb_perror_msg("cannot create directory `%s'", dest);
98 add_to_ino_dev_hashtable(&dest_stat, source);
100 /* Recursively copy files in SOURCE. */
101 if ((dp = opendir(source)) == NULL) {
102 bb_perror_msg("unable to open directory `%s'", source);
107 while ((d = readdir(dp)) != NULL) {
108 char *new_source, *new_dest;
110 new_source = concat_subpath_file(source, d->d_name);
111 if(new_source == NULL)
113 if (is_in_ino_dev_hashtable(&dest_stat, &new_source)) {
114 bb_error_msg("cannot copy a directory, `%s', into itself, `%s'", new_source, dest);
117 new_dest = concat_path_file(dest, d->d_name);
118 if (copy_file(new_source, new_dest, flags) < 0)
123 /* closedir have only EBADF error, but "dp" not changes */
127 chmod(dest, source_stat.st_mode & ~saved_umask) < 0) {
128 bb_perror_msg("unable to change permissions of `%s'", dest);
131 } else if (S_ISREG(source_stat.st_mode)) {
134 #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS
137 if (!(flags & FILEUTILS_DEREFERENCE) &&
138 is_in_ino_dev_hashtable(&source_stat, &link_name)) {
139 if (link(link_name, dest) < 0) {
140 bb_perror_msg("unable to link `%s'", dest);
147 src_fd = open(source, O_RDONLY);
149 bb_perror_msg("unable to open `%s'", source);
154 if (flags & FILEUTILS_INTERACTIVE) {
155 bb_error_msg("overwrite `%s'? ", dest);
156 if (!bb_ask_confirmation()) {
162 dst_fd = open(dest, O_WRONLY|O_TRUNC);
164 if (!(flags & FILEUTILS_FORCE)) {
165 bb_perror_msg("unable to open `%s'", dest);
170 if (unlink(dest) < 0) {
171 bb_perror_msg("unable to remove `%s'", dest);
181 dst_fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode);
183 bb_perror_msg("unable to open `%s'", dest);
189 if (bb_copyfd_eof(src_fd, dst_fd) == -1)
192 if (close(dst_fd) < 0) {
193 bb_perror_msg("unable to close `%s'", dest);
197 if (close(src_fd) < 0) {
198 bb_perror_msg("unable to close `%s'", source);
202 else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) ||
203 S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode) ||
204 S_ISLNK(source_stat.st_mode)) {
207 ((flags & FILEUTILS_FORCE) == 0 || unlink(dest) < 0)) {
208 bb_perror_msg("unable to remove `%s'", dest);
213 bb_error_msg("internal error: unrecognized file type");
216 if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) ||
217 S_ISSOCK(source_stat.st_mode)) {
218 if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
219 bb_perror_msg("unable to create `%s'", dest);
222 } else if (S_ISFIFO(source_stat.st_mode)) {
223 if (mkfifo(dest, source_stat.st_mode) < 0) {
224 bb_perror_msg("cannot create fifo `%s'", dest);
227 } else if (S_ISLNK(source_stat.st_mode)) {
230 lpath = xreadlink(source);
231 if (symlink(lpath, dest) < 0) {
232 bb_perror_msg("cannot create symlink `%s'", dest);
237 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
238 if (flags & FILEUTILS_PRESERVE_STATUS)
239 if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
240 bb_perror_msg("unable to preserve ownership of `%s'", dest);
243 #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS
244 add_to_ino_dev_hashtable(&source_stat, dest);
250 #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS
251 add_to_ino_dev_hashtable(&source_stat, dest);
256 if (flags & FILEUTILS_PRESERVE_STATUS) {
257 struct utimbuf times;
259 times.actime = source_stat.st_atime;
260 times.modtime = source_stat.st_mtime;
261 if (utime(dest, ×) < 0)
262 bb_perror_msg("unable to preserve times of `%s'", dest);
263 if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
264 source_stat.st_mode &= ~(S_ISUID | S_ISGID);
265 bb_perror_msg("unable to preserve ownership of `%s'", dest);
267 if (chmod(dest, source_stat.st_mode) < 0)
268 bb_perror_msg("unable to preserve permissions of `%s'", dest);