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