1 /* vi: set sw=4 ts=4: */
3 * Mini copy_file implementation for busybox
6 * Copyright (C) 2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <sys/types.h>
36 int copy_file(const char *source
, const char *dest
, int flags
)
38 struct stat source_stat
;
39 struct stat dest_stat
;
43 if (((flags
& FILEUTILS_PRESERVE_SYMLINKS
) &&
44 lstat(source
, &source_stat
) < 0) ||
45 (!(flags
& FILEUTILS_PRESERVE_SYMLINKS
) &&
46 stat(source
, &source_stat
) < 0)) {
47 perror_msg("%s", source
);
51 if (stat(dest
, &dest_stat
) < 0) {
52 if (errno
!= ENOENT
) {
53 perror_msg("unable to stat `%s'", dest
);
59 if (dest_exists
&& source_stat
.st_rdev
== dest_stat
.st_rdev
&&
60 source_stat
.st_ino
== dest_stat
.st_ino
) {
61 error_msg("`%s' and `%s' are the same file", source
, dest
);
65 if (S_ISDIR(source_stat
.st_mode
)) {
68 mode_t saved_umask
= 0;
70 if (!(flags
& FILEUTILS_RECUR
)) {
71 error_msg("%s: omitting directory", source
);
77 if (!S_ISDIR(dest_stat
.st_mode
)) {
78 error_msg("`%s' is not a directory", dest
);
83 saved_umask
= umask(0);
85 mode
= source_stat
.st_mode
;
86 if (!(flags
& FILEUTILS_PRESERVE_STATUS
))
87 mode
= source_stat
.st_mode
& ~saved_umask
;
90 if (mkdir(dest
, mode
) < 0) {
92 perror_msg("cannot create directory `%s'",
100 /* Recursively copy files in SOURCE. */
101 if ((dp
= opendir(source
)) == NULL
) {
102 perror_msg("unable to open directory `%s'", source
);
107 while ((d
= readdir(dp
)) != NULL
) {
108 char *new_source
, *new_dest
;
110 if (strcmp(d
->d_name
, ".") == 0 ||
111 strcmp(d
->d_name
, "..") == 0)
114 new_source
= concat_path_file(source
, d
->d_name
);
115 new_dest
= concat_path_file(dest
, d
->d_name
);
116 if (copy_file(new_source
, new_dest
, flags
) < 0)
122 /* ??? What if an error occurs in readdir? */
124 if (closedir(dp
) < 0) {
125 perror_msg("unable to close directory `%s'", source
);
130 chmod(dest
, source_stat
.st_mode
& ~saved_umask
) < 0) {
131 perror_msg("unable to change permissions of `%s'",
135 } else if (S_ISREG(source_stat
.st_mode
)) {
139 if ((dfp
= fopen(dest
, "w")) == NULL
) {
140 if (!(flags
& FILEUTILS_FORCE
)) {
141 perror_msg("unable to open `%s'", dest
);
145 if (unlink(dest
) < 0) {
146 perror_msg("unable to remove `%s'",
159 open(dest
, O_WRONLY
| O_CREAT
,
160 source_stat
.st_mode
)) < 0
161 || (dfp
= fdopen(fd
, "w")) == NULL
) {
164 perror_msg("unable to open `%s'", dest
);
169 if ((sfp
= fopen(source
, "r")) == NULL
) {
171 perror_msg("unable to open `%s'", source
);
176 if (copy_file_chunk(sfp
, dfp
, -1) < 0)
179 if (fclose(dfp
) < 0) {
180 perror_msg("unable to close `%s'", dest
);
184 if (fclose(sfp
) < 0) {
185 perror_msg("unable to close `%s'", source
);
188 } else if (S_ISBLK(source_stat
.st_mode
) || S_ISCHR(source_stat
.st_mode
)
189 || S_ISSOCK(source_stat
.st_mode
)) {
190 if (mknod(dest
, source_stat
.st_mode
, source_stat
.st_rdev
) < 0) {
191 perror_msg("unable to create `%s'", dest
);
194 } else if (S_ISFIFO(source_stat
.st_mode
)) {
195 if (mkfifo(dest
, source_stat
.st_mode
) < 0) {
196 perror_msg("cannot create fifo `%s'", dest
);
199 } else if (S_ISLNK(source_stat
.st_mode
)) {
200 char *lpath
= xreadlink(source
);
201 if (symlink(lpath
, dest
) < 0) {
202 perror_msg("cannot create symlink `%s'", dest
);
207 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
208 if (flags
& FILEUTILS_PRESERVE_STATUS
)
209 if (lchown(dest
, source_stat
.st_uid
, source_stat
.st_gid
)
212 ("unable to preserve ownership of `%s'",
217 error_msg("internal error: unrecognized file type");
223 if (flags
& FILEUTILS_PRESERVE_STATUS
) {
224 struct utimbuf times
;
226 times
.actime
= source_stat
.st_atime
;
227 times
.modtime
= source_stat
.st_mtime
;
228 if (utime(dest
, ×
) < 0)
229 perror_msg("unable to preserve times of `%s'", dest
);
230 if (chown(dest
, source_stat
.st_uid
, source_stat
.st_gid
) < 0) {
231 source_stat
.st_mode
&= ~(S_ISUID
| S_ISGID
);
232 perror_msg("unable to preserve ownership of `%s'",
235 if (chmod(dest
, source_stat
.st_mode
) < 0)
236 perror_msg("unable to preserve permissions of `%s'",