add aliases == and [[ for = and [ to support more bash scripts
[people/mcb30/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                 int src_fd;
127                 int dst_fd;
128 #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS
129                 char *link_name;
130
131                 if (!(flags & FILEUTILS_DEREFERENCE) &&
132                                 is_in_ino_dev_hashtable(&source_stat, &link_name)) {
133                         if (link(link_name, dest) < 0) {
134                                 bb_perror_msg("unable to link `%s'", dest);
135                                 return -1;
136                         }
137
138                         return 0;
139                 }
140 #endif
141                 src_fd = open(source, O_RDONLY);
142                 if (src_fd == -1) {
143                         bb_perror_msg("unable to open `%s'", source);
144                         return(-1);
145                 }
146
147                 if (dest_exists) {
148                         if (flags & FILEUTILS_INTERACTIVE) {
149                                 bb_error_msg("overwrite `%s'? ", dest);
150                                 if (!bb_ask_confirmation()) {
151                                         close (src_fd);
152                                         return 0;
153                                 }
154                         }
155
156                         dst_fd = open(dest, O_WRONLY|O_TRUNC);
157                         if (dst_fd == -1) {
158                                 if (!(flags & FILEUTILS_FORCE)) {
159                                         bb_perror_msg("unable to open `%s'", dest);
160                                         close(src_fd);
161                                         return -1;
162                                 }
163
164                                 if (unlink(dest) < 0) {
165                                         bb_perror_msg("unable to remove `%s'", dest);
166                                         close(src_fd);
167                                         return -1;
168                                 }
169
170                                 dest_exists = 0;
171                         }
172                 }
173
174                 if (!dest_exists) {
175                         dst_fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode);
176                         if (dst_fd == -1) {
177                                 bb_perror_msg("unable to open `%s'", dest);
178                                 close(src_fd);
179                                 return(-1);
180                         }
181                 }
182
183                 if (bb_copyfd_eof(src_fd, dst_fd) == -1)
184                         status = -1;
185
186                 if (close(dst_fd) < 0) {
187                         bb_perror_msg("unable to close `%s'", dest);
188                         status = -1;
189                 }
190
191                 if (close(src_fd) < 0) {
192                         bb_perror_msg("unable to close `%s'", source);
193                         status = -1;
194                 }
195                         }
196         else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) ||
197             S_ISSOCK(source_stat.st_mode) || S_ISFIFO(source_stat.st_mode) ||
198             S_ISLNK(source_stat.st_mode)) {
199
200                 if (dest_exists &&
201                        ((flags & FILEUTILS_FORCE) == 0 || unlink(dest) < 0)) {
202                                 bb_perror_msg("unable to remove `%s'", dest);
203                                 return -1;
204
205                         }
206         } else {
207                 bb_error_msg("internal error: unrecognized file type");
208                 return -1;
209                 }
210         if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) ||
211             S_ISSOCK(source_stat.st_mode)) {
212                 if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
213                         bb_perror_msg("unable to create `%s'", dest);
214                         return -1;
215                 }
216         } else if (S_ISFIFO(source_stat.st_mode)) {
217                 if (mkfifo(dest, source_stat.st_mode) < 0) {
218                         bb_perror_msg("cannot create fifo `%s'", dest);
219                         return -1;
220                 }
221         } else if (S_ISLNK(source_stat.st_mode)) {
222                 char *lpath;
223
224                 lpath = xreadlink(source);
225                 if (symlink(lpath, dest) < 0) {
226                         bb_perror_msg("cannot create symlink `%s'", dest);
227                         return -1;
228                 }
229                 free(lpath);
230
231 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
232                 if (flags & FILEUTILS_PRESERVE_STATUS)
233                         if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
234                                 bb_perror_msg("unable to preserve ownership of `%s'", dest);
235 #endif
236
237 #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS
238                 add_to_ino_dev_hashtable(&source_stat, dest);
239 #endif
240
241                 return 0;
242         }
243
244 #ifdef CONFIG_FEATURE_PRESERVE_HARDLINKS
245         if (! S_ISDIR(source_stat.st_mode)) {
246                 add_to_ino_dev_hashtable(&source_stat, dest);
247         }
248 #endif
249
250 end:
251
252         if (flags & FILEUTILS_PRESERVE_STATUS) {
253                 struct utimbuf times;
254
255                 times.actime = source_stat.st_atime;
256                 times.modtime = source_stat.st_mtime;
257                 if (utime(dest, &times) < 0)
258                         bb_perror_msg("unable to preserve times of `%s'", dest);
259                 if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
260                         source_stat.st_mode &= ~(S_ISUID | S_ISGID);
261                         bb_perror_msg("unable to preserve ownership of `%s'", dest);
262                 }
263                 if (chmod(dest, source_stat.st_mode) < 0)
264                         bb_perror_msg("unable to preserve permissions of `%s'", dest);
265         }
266
267         return status;
268 }