Chris Larson (kergoth) writes:
[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  * 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.
11  *
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.
16  *
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
20  *
21  */
22
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <unistd.h>
26 #include <fcntl.h>
27 #include <utime.h>
28 #include <errno.h>
29 #include <dirent.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "busybox.h"
34
35 int copy_file(const char *source, const char *dest, int flags)
36 {
37         struct stat source_stat;
38         struct stat dest_stat;
39         int dest_exists = 0;
40         int status = 0;
41
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);
47                 return -1;
48         }
49
50         if (lstat(dest, &dest_stat) < 0) {
51                 if (errno != ENOENT) {
52                         bb_perror_msg("unable to stat `%s'", dest);
53                         return -1;
54                 }
55         } else {
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);
59                 return -1;
60         }
61                 dest_exists = 1;
62         }
63
64         if (S_ISDIR(source_stat.st_mode)) {
65                 DIR *dp;
66                 struct dirent *d;
67                 mode_t saved_umask = 0;
68                 char *dstparent;
69                 struct stat dstparent_stat;
70
71                 if (!(flags & FILEUTILS_RECUR)) {
72                         bb_error_msg("%s: omitting directory", source);
73                         return -1;
74                 }
75
76                 dstparent = dirname(bb_xstrdup(dest));
77                 if (lstat(dstparent, &dstparent_stat) < 0) {
78                         bb_perror_msg("unable to stat `%s'", dstparent);
79                         free(dstparent);
80                         return -1;
81                 }
82                 free(dstparent);
83
84                 if (source_stat.st_dev == dstparent_stat.st_dev &&
85                         source_stat.st_ino == dstparent_stat.st_ino) {
86                         bb_error_msg("cannot copy a directory, `%s', into itself, `%s'", source, dest);
87                         return -1;
88                 }
89
90                 /* Create DEST.  */
91                 if (dest_exists) {
92                         if (!S_ISDIR(dest_stat.st_mode)) {
93                                 bb_error_msg("`%s' is not a directory", dest);
94                                 return -1;
95                         }
96                 } else {
97                         mode_t mode;
98                         saved_umask = umask(0);
99
100                         mode = source_stat.st_mode;
101                         if (!(flags & FILEUTILS_PRESERVE_STATUS))
102                                 mode = source_stat.st_mode & ~saved_umask;
103                         mode |= S_IRWXU;
104
105                         if (mkdir(dest, mode) < 0) {
106                                 umask(saved_umask);
107                                 bb_perror_msg("cannot create directory `%s'", dest);
108                                 return -1;
109                         }
110
111                         umask(saved_umask);
112                 }
113
114                 /* Recursively copy files in SOURCE.  */
115                 if ((dp = opendir(source)) == NULL) {
116                         bb_perror_msg("unable to open directory `%s'", source);
117                         status = -1;
118                         goto end;
119                 }
120
121                 while ((d = readdir(dp)) != NULL) {
122                         char *new_source, *new_dest;
123
124                         new_source = concat_subpath_file(source, d->d_name);
125                         if(new_source == NULL)
126                                 continue;
127                         new_dest = concat_path_file(dest, d->d_name);
128                         if (copy_file(new_source, new_dest, flags) < 0)
129                                 status = -1;
130                         free(new_source);
131                         free(new_dest);
132                 }
133                 /* closedir have only EBADF error, but "dp" not changes */
134                 closedir(dp);
135
136                 if (!dest_exists &&
137                                 chmod(dest, source_stat.st_mode & ~saved_umask) < 0) {
138                         bb_perror_msg("unable to change permissions of `%s'", dest);
139                         status = -1;
140                 }
141         } else if (S_ISREG(source_stat.st_mode)) {
142                 int src_fd;
143                 int dst_fd;
144 #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS
145                 char *link_name;
146
147                 if (!(flags & FILEUTILS_DEREFERENCE) &&
148                                 is_in_ino_dev_hashtable(&source_stat, &link_name)) {
149                         if (link(link_name, dest) < 0) {
150                                 bb_perror_msg("unable to link `%s'", dest);
151                                 return -1;
152                         }
153
154                         return 0;
155                 }
156 #endif
157                 src_fd = open(source, O_RDONLY);
158                 if (src_fd == -1) {
159                         bb_perror_msg("unable to open `%s'", source);
160                         return(-1);
161                 }
162
163                 if (dest_exists) {
164                         if (flags & FILEUTILS_INTERACTIVE) {
165                                 bb_error_msg("overwrite `%s'? ", dest);
166                                 if (!bb_ask_confirmation()) {
167                                         close (src_fd);
168                                         return 0;
169                                 }
170                         }
171
172                         dst_fd = open(dest, O_WRONLY|O_TRUNC);
173                         if (dst_fd == -1) {
174                                 if (!(flags & FILEUTILS_FORCE)) {
175                                         bb_perror_msg("unable to open `%s'", dest);
176                                         close(src_fd);
177                                         return -1;
178                                 }
179
180                                 if (unlink(dest) < 0) {
181                                         bb_perror_msg("unable to remove `%s'", dest);
182                                         close(src_fd);
183                                         return -1;
184                                 }
185
186                                 dest_exists = 0;
187                         }
188                 }
189
190                 if (!dest_exists) {
191                         dst_fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode);
192                         if (dst_fd == -1) {
193                                 bb_perror_msg("unable to open `%s'", dest);
194                                 close(src_fd);
195                                 return(-1);
196                         }
197                 }
198
199                 if (bb_copyfd_eof(src_fd, dst_fd) == -1)
200                         status = -1;
201
202                 if (close(dst_fd) < 0) {
203                         bb_perror_msg("unable to close `%s'", dest);
204                         status = -1;
205                 }
206
207                 if (close(src_fd) < 0) {
208                         bb_perror_msg("unable to close `%s'", source);
209                         status = -1;
210                 }
211                         }
212         else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) ||
213             S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode) ||
214             S_ISLNK(source_stat.st_mode)) {
215
216                 if (dest_exists &&
217                        ((flags & FILEUTILS_FORCE) == 0 || unlink(dest) < 0)) {
218                                 bb_perror_msg("unable to remove `%s'", dest);
219                                 return -1;
220
221                         }
222         } else {
223                 bb_error_msg("internal error: unrecognized file type");
224                 return -1;
225                 }
226         if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) ||
227             S_ISSOCK(source_stat.st_mode)) {
228                 if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
229                         bb_perror_msg("unable to create `%s'", dest);
230                         return -1;
231                 }
232         } else if (S_ISFIFO(source_stat.st_mode)) {
233                 if (mkfifo(dest, source_stat.st_mode) < 0) {
234                         bb_perror_msg("cannot create fifo `%s'", dest);
235                         return -1;
236                 }
237         } else if (S_ISLNK(source_stat.st_mode)) {
238                 char *lpath;
239
240                 lpath = xreadlink(source);
241                 if (symlink(lpath, dest) < 0) {
242                         bb_perror_msg("cannot create symlink `%s'", dest);
243                         return -1;
244                 }
245                 free(lpath);
246
247 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
248                 if (flags & FILEUTILS_PRESERVE_STATUS)
249                         if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
250                                 bb_perror_msg("unable to preserve ownership of `%s'", dest);
251 #endif
252
253 #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS
254                 add_to_ino_dev_hashtable(&source_stat, dest);
255 #endif
256
257                 return 0;
258         }
259
260 #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS
261         add_to_ino_dev_hashtable(&source_stat, dest);
262 #endif
263
264 end:
265
266         if (flags & FILEUTILS_PRESERVE_STATUS) {
267                 struct utimbuf times;
268
269                 times.actime = source_stat.st_atime;
270                 times.modtime = source_stat.st_mtime;
271                 if (utime(dest, &times) < 0)
272                         bb_perror_msg("unable to preserve times of `%s'", dest);
273                 if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
274                         source_stat.st_mode &= ~(S_ISUID | S_ISGID);
275                         bb_perror_msg("unable to preserve ownership of `%s'", dest);
276                 }
277                 if (chmod(dest, source_stat.st_mode) < 0)
278                         bb_perror_msg("unable to preserve permissions of `%s'", dest);
279         }
280
281         return status;
282 }