Major rework of the directory structure and the entire build system.
[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 "libbb.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 = 1;
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                 perror_msg("%s", source);
47                 return -1;
48         }
49
50         if (stat(dest, &dest_stat) < 0) {
51                 if (errno != ENOENT) {
52                         perror_msg("unable to stat `%s'", dest);
53                         return -1;
54                 }
55                 dest_exists = 0;
56         }
57
58         if (dest_exists && source_stat.st_rdev == dest_stat.st_rdev &&
59                         source_stat.st_ino == dest_stat.st_ino) {
60                 error_msg("`%s' and `%s' are the same file", source, dest);
61                 return -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                         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                                 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                                 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                         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                         if (strcmp(d->d_name, ".") == 0 ||
109                                         strcmp(d->d_name, "..") == 0)
110                                 continue;
111
112                         new_source = concat_path_file(source, d->d_name);
113                         new_dest = concat_path_file(dest, d->d_name);
114                         if (copy_file(new_source, new_dest, flags) < 0)
115                                 status = -1;
116                         free(new_source);
117                         free(new_dest);
118                 }
119
120                 /* ??? What if an error occurs in readdir?  */
121
122                 if (closedir(dp) < 0) {
123                         perror_msg("unable to close directory `%s'", source);
124                         status = -1;
125                 }
126
127                 if (!dest_exists &&
128                                 chmod(dest, source_stat.st_mode & ~saved_umask) < 0) {
129                         perror_msg("unable to change permissions of `%s'", dest);
130                         status = -1;
131                 }
132         } else if (S_ISREG(source_stat.st_mode)) {
133                 FILE *sfp, *dfp=NULL;
134
135                 if (dest_exists) {
136                         if (flags & FILEUTILS_INTERACTIVE) {
137                                 fprintf(stderr, "%s: overwrite `%s'? ", applet_name, dest);
138                                 if (!ask_confirmation())
139                                         return 0;
140                         }
141
142                         if ((dfp = fopen(dest, "w")) == NULL) {
143                                 if (!(flags & FILEUTILS_FORCE)) {
144                                         perror_msg("unable to open `%s'", dest);
145                                         return -1;
146                                 }
147
148                                 if (unlink(dest) < 0) {
149                                         perror_msg("unable to remove `%s'", dest);
150                                         return -1;
151                                 }
152
153                                 dest_exists = 0;
154                         }
155                 }
156
157                 if (!dest_exists) {
158                         int fd;
159
160                         if ((fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode)) < 0 ||
161                                         (dfp = fdopen(fd, "w")) == NULL) {
162                                 if (fd >= 0)
163                                         close(fd);
164                                 perror_msg("unable to open `%s'", dest);
165                                 return -1;
166                         }
167                 }
168
169                 if ((sfp = fopen(source, "r")) == NULL) {
170                         fclose(dfp);
171                         perror_msg("unable to open `%s'", source);
172                         status = -1;
173                         goto end;
174                 }
175
176                 if (copy_file_chunk(sfp, dfp, -1) < 0)
177                         status = -1;
178
179                 if (fclose(dfp) < 0) {
180                         perror_msg("unable to close `%s'", dest);
181                         status = -1;
182                 }
183
184                 if (fclose(sfp) < 0) {
185                         perror_msg("unable to close `%s'", source);
186                         status = -1;
187                 }
188         } else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) ||
189                         S_ISSOCK(source_stat.st_mode)) {
190                 if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
191                         perror_msg("unable to create `%s'", dest);
192                         return -1;
193                 }
194         } else if (S_ISFIFO(source_stat.st_mode)) {
195                 if (mkfifo(dest, source_stat.st_mode) < 0) {
196                         perror_msg("cannot create fifo `%s'", dest);
197                         return -1;
198                 }
199         } else if (S_ISLNK(source_stat.st_mode)) {
200                 char *lpath = xreadlink(source);
201                 if (symlink(lpath, dest) < 0) {
202                         perror_msg("cannot create symlink `%s'", dest);
203                         return -1;
204                 }
205                 free(lpath);
206
207 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
208                 if (flags & FILEUTILS_PRESERVE_STATUS)
209                         if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
210                                 perror_msg("unable to preserve ownership of `%s'", dest);
211 #endif
212                 return 0;
213         } else {
214                 error_msg("internal error: unrecognized file type");
215                 return -1;
216         }
217
218 end:
219
220         if (flags & FILEUTILS_PRESERVE_STATUS) {
221                 struct utimbuf times;
222
223                 times.actime = source_stat.st_atime;
224                 times.modtime = source_stat.st_mtime;
225                 if (utime(dest, &times) < 0)
226                         perror_msg("unable to preserve times of `%s'", dest);
227                 if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
228                         source_stat.st_mode &= ~(S_ISUID | S_ISGID);
229                         perror_msg("unable to preserve ownership of `%s'", dest);
230                 }
231                 if (chmod(dest, source_stat.st_mode) < 0)
232                         perror_msg("unable to preserve permissions of `%s'", dest);
233         }
234
235         return status;
236 }