- drats.
[oweals/busybox.git] / libbb / copy_file.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini copy_file implementation for busybox
4  *
5  * Copyright (C) 2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  *
9  */
10
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <unistd.h>
14 #include <fcntl.h>
15 #include <utime.h>
16 #include <errno.h>
17 #include <dirent.h>
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include "libbb.h"
22
23 int copy_file(const char *source, const char *dest, int flags)
24 {
25         struct stat source_stat;
26         struct stat dest_stat;
27         int dest_exists = 0;
28         int status = 0;
29
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);
35                 return -1;
36         }
37
38         if (lstat(dest, &dest_stat) < 0) {
39                 if (errno != ENOENT) {
40                         bb_perror_msg("unable to stat `%s'", dest);
41                         return -1;
42                 }
43         } else {
44                 if (source_stat.st_dev == dest_stat.st_dev &&
45                         source_stat.st_ino == dest_stat.st_ino)
46                 {
47                         bb_error_msg("`%s' and `%s' are the same file", source, dest);
48                         return -1;
49                 }
50                 dest_exists = 1;
51         }
52
53         if (S_ISDIR(source_stat.st_mode)) {
54                 DIR *dp;
55                 struct dirent *d;
56                 mode_t saved_umask = 0;
57
58                 if (!(flags & FILEUTILS_RECUR)) {
59                         bb_error_msg("%s: omitting directory", source);
60                         return -1;
61                 }
62
63                 /* Create DEST.  */
64                 if (dest_exists) {
65                         if (!S_ISDIR(dest_stat.st_mode)) {
66                                 bb_error_msg("`%s' is not a directory", dest);
67                                 return -1;
68                         }
69                 } else {
70                         mode_t mode;
71                         saved_umask = umask(0);
72
73                         mode = source_stat.st_mode;
74                         if (!(flags & FILEUTILS_PRESERVE_STATUS))
75                                 mode = source_stat.st_mode & ~saved_umask;
76                         mode |= S_IRWXU;
77
78                         if (mkdir(dest, mode) < 0) {
79                                 umask(saved_umask);
80                                 bb_perror_msg("cannot create directory `%s'", dest);
81                                 return -1;
82                         }
83
84                         umask(saved_umask);
85                 }
86
87                 /* Recursively copy files in SOURCE.  */
88                 if ((dp = bb_opendir(source)) == NULL) {
89                         status = -1;
90                         goto preserve_status;
91                 }
92
93                 while ((d = readdir(dp)) != NULL) {
94                         char *new_source, *new_dest;
95
96                         new_source = concat_subpath_file(source, d->d_name);
97                         if(new_source == NULL)
98                                 continue;
99                         new_dest = concat_path_file(dest, d->d_name);
100                         if (copy_file(new_source, new_dest, flags) < 0)
101                                 status = -1;
102                         free(new_source);
103                         free(new_dest);
104                 }
105                 /* closedir have only EBADF error, but "dp" not changes */
106                 closedir(dp);
107
108                 if (!dest_exists &&
109                                 chmod(dest, source_stat.st_mode & ~saved_umask) < 0) {
110                         bb_perror_msg("unable to change permissions of `%s'", dest);
111                         status = -1;
112                 }
113         } else if (S_ISREG(source_stat.st_mode) || (flags & FILEUTILS_DEREFERENCE))
114         {
115                 int src_fd;
116                 int dst_fd;
117 #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS
118                 char *link_name;
119
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);
124                                 return -1;
125                         }
126
127                         return 0;
128                 }
129                 add_to_ino_dev_hashtable(&source_stat, dest);
130 #endif
131                 src_fd = open(source, O_RDONLY);
132                 if (src_fd == -1) {
133                         bb_perror_msg("unable to open `%s'", source);
134                         return(-1);
135                 }
136
137                 if (dest_exists) {
138                         if (flags & FILEUTILS_INTERACTIVE) {
139                                 fprintf(stderr, "%s: overwrite `%s'? ", bb_applet_name, dest);
140                                 if (!bb_ask_confirmation()) {
141                                         close (src_fd);
142                                         return 0;
143                                 }
144                         }
145
146                         dst_fd = open(dest, O_WRONLY|O_TRUNC);
147                         if (dst_fd == -1) {
148                                 if (!(flags & FILEUTILS_FORCE)) {
149                                         bb_perror_msg("unable to open `%s'", dest);
150                                         close(src_fd);
151                                         return -1;
152                                 }
153
154                                 if (unlink(dest) < 0) {
155                                         bb_perror_msg("unable to remove `%s'", dest);
156                                         close(src_fd);
157                                         return -1;
158                                 }
159
160                                 goto dest_removed;
161                         }
162                 } else {
163 dest_removed:
164                         dst_fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode);
165                         if (dst_fd == -1) {
166                                 bb_perror_msg("unable to open `%s'", dest);
167                                 close(src_fd);
168                                 return(-1);
169                         }
170                 }
171
172                 if (bb_copyfd_eof(src_fd, dst_fd) == -1)
173                         status = -1;
174
175                 if (close(dst_fd) < 0) {
176                         bb_perror_msg("unable to close `%s'", dest);
177                         status = -1;
178                 }
179
180                 if (close(src_fd) < 0) {
181                         bb_perror_msg("unable to close `%s'", source);
182                         status = -1;
183                 }
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)) {
187
188                 if (dest_exists) {
189                         if((flags & FILEUTILS_FORCE) == 0) {
190                                 fprintf(stderr, "`%s' exists\n", dest);
191                                 return -1;
192                         }
193                         if(unlink(dest) < 0) {
194                                 bb_perror_msg("unable to remove `%s'", dest);
195                                 return -1;
196                         }
197                 }
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);
201                                 return -1;
202                         }
203                 } else if (S_ISLNK(source_stat.st_mode)) {
204                         char *lpath;
205
206                         lpath = xreadlink(source);
207                         if (symlink(lpath, dest) < 0) {
208                                 bb_perror_msg("cannot create symlink `%s'", dest);
209                                 return -1;
210                         }
211                         free(lpath);
212
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);
216
217                         return 0;
218
219                 } else {
220                         if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
221                                 bb_perror_msg("unable to create `%s'", dest);
222                                 return -1;
223                         }
224                 }
225         } else {
226                 bb_error_msg("internal error: unrecognized file type");
227                 return -1;
228         }
229
230 preserve_status:
231
232         if (flags & FILEUTILS_PRESERVE_STATUS) {
233                 struct utimbuf times;
234                 char *msg="unable to preserve %s of `%s'";
235
236                 times.actime = source_stat.st_atime;
237                 times.modtime = source_stat.st_mtime;
238                 if (utime(dest, &times) < 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);
243                 }
244                 if (chmod(dest, source_stat.st_mode) < 0)
245                         bb_perror_msg(msg, "permissions", dest);
246         }
247
248         return status;
249 }