Vodz, last_patch_86
[platform/upstream/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
69                 if (!(flags & FILEUTILS_RECUR)) {
70                         bb_error_msg("%s: omitting directory", source);
71                         return -1;
72                 }
73
74                 /* Create DEST.  */
75                 if (dest_exists) {
76                         if (!S_ISDIR(dest_stat.st_mode)) {
77                                 bb_error_msg("`%s' is not a directory", dest);
78                                 return -1;
79                         }
80                 } else {
81                         mode_t mode;
82                         saved_umask = umask(0);
83
84                         mode = source_stat.st_mode;
85                         if (!(flags & FILEUTILS_PRESERVE_STATUS))
86                                 mode = source_stat.st_mode & ~saved_umask;
87                         mode |= S_IRWXU;
88
89                         if (mkdir(dest, mode) < 0) {
90                                 umask(saved_umask);
91                                 bb_perror_msg("cannot create directory `%s'", dest);
92                                 return -1;
93                         }
94
95                         umask(saved_umask);
96                 }
97
98                 /* Recursively copy files in SOURCE.  */
99                 if ((dp = opendir(source)) == NULL) {
100                         bb_perror_msg("unable to open directory `%s'", source);
101                         status = -1;
102                         goto end;
103                 }
104
105                 while ((d = readdir(dp)) != NULL) {
106                         char *new_source, *new_dest;
107
108                         new_source = concat_subpath_file(source, d->d_name);
109                         if(new_source == NULL)
110                                 continue;
111                         new_dest = concat_path_file(dest, d->d_name);
112                         if (copy_file(new_source, new_dest, flags) < 0)
113                                 status = -1;
114                         free(new_source);
115                         free(new_dest);
116                 }
117                 /* closedir have only EBADF error, but "dp" not changes */
118                 closedir(dp);
119
120                 if (!dest_exists &&
121                                 chmod(dest, source_stat.st_mode & ~saved_umask) < 0) {
122                         bb_perror_msg("unable to change permissions of `%s'", dest);
123                         status = -1;
124                 }
125         } else if (S_ISREG(source_stat.st_mode)) {
126                 FILE *sfp, *dfp=NULL;
127 #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS
128                 char *link_name;
129
130                 if (!(flags & FILEUTILS_DEREFERENCE) &&
131                                 is_in_ino_dev_hashtable(&source_stat, &link_name)) {
132                         if (link(link_name, dest) < 0) {
133                                 bb_perror_msg("unable to link `%s'", dest);
134                                 return -1;
135                         }
136
137                         return 0;
138                 }
139 #endif
140
141                 if ((sfp = bb_wfopen(source, "r")) == NULL) {
142                         return -1;
143                 }
144
145                 if (dest_exists) {
146                         if (flags & FILEUTILS_INTERACTIVE) {
147                                 fprintf(stderr, "%s: overwrite `%s'? ", bb_applet_name, dest);
148                                 if (!bb_ask_confirmation()) {
149                                         fclose (sfp);
150                                         return 0;
151                                 }
152                         }
153
154                         if ((dfp = fopen(dest, "w")) == NULL) {
155                                 if (!(flags & FILEUTILS_FORCE)) {
156                                         bb_perror_msg("unable to open `%s'", dest);
157                                         fclose (sfp);
158                                         return -1;
159                                 }
160
161                                 if (unlink(dest) < 0) {
162                                         bb_perror_msg("unable to remove `%s'", dest);
163                                         fclose (sfp);
164                                         return -1;
165                                 }
166
167                                 dest_exists = 0;
168                         }
169                 }
170
171                 if (!dest_exists) {
172                         int fd;
173
174                         if ((fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode)) < 0 ||
175                                         (dfp = fdopen(fd, "w")) == NULL) {
176                                 if (fd >= 0)
177                                         close(fd);
178                                 bb_perror_msg("unable to open `%s'", dest);
179                                 fclose (sfp);
180                                 return -1;
181                         }
182                 }
183
184                 if (bb_copyfd(fileno(sfp), fileno(dfp), 0) == -1)
185                         status = -1;
186
187                 if (fclose(dfp) < 0) {
188                         bb_perror_msg("unable to close `%s'", dest);
189                         status = -1;
190                 }
191
192                 if (fclose(sfp) < 0) {
193                         bb_perror_msg("unable to close `%s'", source);
194                         status = -1;
195                 }
196                         }
197         else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) ||
198             S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode) ||
199             S_ISLNK(source_stat.st_mode)) {
200
201                 if (dest_exists &&
202                        ((flags & FILEUTILS_FORCE) == 0 || unlink(dest) < 0)) {
203                                 bb_perror_msg("unable to remove `%s'", dest);
204                                 return -1;
205
206                         }
207         } else {
208                 bb_error_msg("internal error: unrecognized file type");
209                 return -1;
210                 }
211         if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) ||
212             S_ISSOCK(source_stat.st_mode)) {
213                 if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
214                         bb_perror_msg("unable to create `%s'", dest);
215                         return -1;
216                 }
217         } else if (S_ISFIFO(source_stat.st_mode)) {
218                 if (mkfifo(dest, source_stat.st_mode) < 0) {
219                         bb_perror_msg("cannot create fifo `%s'", dest);
220                         return -1;
221                 }
222         } else if (S_ISLNK(source_stat.st_mode)) {
223                 char *lpath;
224
225                 lpath = xreadlink(source);
226                 if (symlink(lpath, dest) < 0) {
227                         bb_perror_msg("cannot create symlink `%s'", dest);
228                         return -1;
229                 }
230                 free(lpath);
231
232 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
233                 if (flags & FILEUTILS_PRESERVE_STATUS)
234                         if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
235                                 bb_perror_msg("unable to preserve ownership of `%s'", dest);
236 #endif
237
238 #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS
239                 add_to_ino_dev_hashtable(&source_stat, dest);
240 #endif
241
242                 return 0;
243         }
244
245 #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS
246         add_to_ino_dev_hashtable(&source_stat, dest);
247 #endif
248
249 end:
250
251         if (flags & FILEUTILS_PRESERVE_STATUS) {
252                 struct utimbuf times;
253
254                 times.actime = source_stat.st_atime;
255                 times.modtime = source_stat.st_mtime;
256                 if (utime(dest, &times) < 0)
257                         bb_perror_msg("unable to preserve times of `%s'", dest);
258                 if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
259                         source_stat.st_mode &= ~(S_ISUID | S_ISGID);
260                         bb_perror_msg("unable to preserve ownership of `%s'", dest);
261                 }
262                 if (chmod(dest, source_stat.st_mode) < 0)
263                         bb_perror_msg("unable to preserve permissions of `%s'", dest);
264         }
265
266         return status;
267 }