Rewrite cp and mv to be SUSv2 compliant.
[oweals/busybox.git] / libbb / copy_file.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini copy_file implementation for busybox
4  *
5  *
6  * Copyright (C) 2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21  *
22  */
23
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <utime.h>
29 #include <errno.h>
30 #include <dirent.h>
31 #include <stdlib.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 & CP_PRESERVE_SYMLINKS) && lstat(source, &source_stat) < 0) ||
43                         (!(flags & CP_PRESERVE_SYMLINKS) &&
44                          stat(source, &source_stat) < 0)) {
45                 perror_msg("%s", source);
46                 return -1;
47         }
48
49         if (stat(dest, &dest_stat) < 0) {
50                 if (errno != ENOENT) {
51                         perror_msg("unable to stat `%s'", dest);
52                         return -1;
53                 }
54                 dest_exists = 0;
55         }
56
57         if (dest_exists && source_stat.st_rdev == dest_stat.st_rdev &&
58                         source_stat.st_ino == dest_stat.st_ino) {
59                 error_msg("`%s' and `%s' are the same file", source, dest);
60                 return -1;
61         }
62
63         if (S_ISDIR(source_stat.st_mode)) {
64                 DIR *dp;
65                 struct dirent *d;
66
67                 if (!(flags & CP_RECUR)) {
68                         error_msg("%s: omitting directory", source);
69                         return -1;
70                 }
71
72                 /* Create DEST.  */
73                 if (dest_exists) {
74                         if (!S_ISDIR(dest_stat.st_mode)) {
75                                 error_msg("`%s' is not a directory", dest);
76                                 return -1;
77                         }
78                 } else {
79                         mode_t mode, saved_umask;
80                         saved_umask = umask(0);
81
82                         mode = source_stat.st_mode;
83                         if (!(flags & CP_PRESERVE_STATUS))
84                                 mode = source_stat.st_mode & ~saved_umask;
85                         mode |= S_IRWXU;
86
87                         if (mkdir(dest, mode) < 0) {
88                                 umask(saved_umask);
89                                 perror_msg("cannot create directory `%s'", dest);
90                                 return -1;
91                         }
92
93                         umask(saved_umask);
94                 }
95                 
96                 /* Recursively copy files in SOURCE.  */
97                 if ((dp = opendir(source)) == NULL) {
98                         perror_msg("unable to open directory `%s'", source);
99                         status = -1;
100                         goto end;
101                 }
102
103                 while ((d = readdir(dp)) != NULL) {
104                         char *new_source, *new_dest;
105
106                         if (strcmp(d->d_name, ".") == 0 ||
107                                         strcmp(d->d_name, "..") == 0)
108                                 continue;
109
110                         new_source = concat_path_file(source, d->d_name);
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                 
118                 /* ??? What if an error occurs in readdir?  */
119
120                 if (closedir(dp) < 0) {
121                         perror_msg("unable to close directory `%s'", source);
122                         status = -1;
123                 }
124         } else if (S_ISREG(source_stat.st_mode)) {
125                 FILE *sfp, *dfp;
126
127                 if (dest_exists) {
128                         if (flags & CP_INTERACTIVE) {
129                                 fprintf(stderr, "cp: overwrite `%s'? ", dest);
130                                 if (!ask_confirmation())
131                                         return 0;
132                         }
133
134                         if ((dfp = fopen(dest, "w")) == NULL) {
135                                 if (!(flags & CP_FORCE)) {
136                                         perror_msg("unable to open `%s'", dest);
137                                         return -1;
138                                 }
139
140                                 if (unlink(dest) < 0) {
141                                         perror_msg("unable to remove `%s'", dest);
142                                         return -1;
143                                 }
144
145                                 dest_exists = 0;
146                         }
147                 }
148
149                 if (!dest_exists) {
150                         int fd;
151
152                         if ((fd = open(dest, O_WRONLY|O_CREAT, source_stat.st_mode)) < 0 ||
153                                         (dfp = fdopen(fd, "w")) == NULL) {
154                                 if (fd >= 0)
155                                         close(fd);
156                                 perror_msg("unable to open `%s'", dest);
157                                 return -1;
158                         }
159                 }
160
161                 if ((sfp = fopen(source, "r")) == NULL) {
162                         fclose(dfp);
163                         perror_msg("unable to open `%s'", source);
164                         status = -1;
165                         goto end;
166                 }
167
168                 copy_file_chunk(sfp, dfp, source_stat.st_size);
169
170                 if (fclose(dfp) < 0) {
171                         perror_msg("unable to close `%s'", dest);
172                         status = -1;
173                 }
174
175                 if (fclose(sfp) < 0) {
176                         perror_msg("unable to close `%s'", source);
177                         status = -1;
178                 }
179         } else if (S_ISBLK(source_stat.st_mode) || S_ISCHR(source_stat.st_mode) ||
180                         S_ISSOCK(source_stat.st_mode)) {
181                 if (mknod(dest, source_stat.st_mode, source_stat.st_rdev) < 0) {
182                         perror_msg("unable to create `%s'", dest);
183                         return -1;
184                 }
185         } else if (S_ISFIFO(source_stat.st_mode)) {
186                 mode_t mode, saved_umask;
187                 saved_umask = umask(0);
188
189                 mode = source_stat.st_mode;
190                 if (!(flags & CP_PRESERVE_STATUS))
191                         mode = source_stat.st_mode & ~saved_umask;
192                 mode |= S_IRWXU;
193
194                 if (mkfifo(dest, mode) < 0) {
195                         umask(saved_umask);
196                         perror_msg("cannot create fifo `%s'", dest);
197                         return -1;
198                 }
199
200                 umask(saved_umask);
201         } else if (S_ISLNK(source_stat.st_mode)) {
202                 char buf[BUFSIZ + 1];
203
204                 if (readlink(source, buf, BUFSIZ) < 0) {
205                         perror_msg("cannot read `%s'", source);
206                         return -1;
207                 }
208                 buf[BUFSIZ] = '\0';
209
210                 if (symlink(buf, dest) < 0) {
211                         perror_msg("cannot create symlink `%s'", dest);
212                         return -1;
213                 }
214
215 #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
216                 if (flags & CP_PRESERVE_STATUS)
217                         if (lchown(dest, source_stat.st_uid, source_stat.st_gid) < 0)
218                                 perror_msg("unable to preserve ownership of `%s'", dest);
219 #endif
220                 return 0;
221         } else {
222                 error_msg("internal error: unrecognized file type");
223                 return -1;
224         }
225
226 end:
227
228         if (flags & CP_PRESERVE_STATUS) {
229                 struct utimbuf times;
230
231                 times.actime = source_stat.st_atime;
232                 times.modtime = source_stat.st_mtime;
233                 if (utime(dest, &times) < 0)
234                         perror_msg("unable to preserve times of `%s'", dest);
235                 if (chown(dest, source_stat.st_uid, source_stat.st_gid) < 0) {
236                         source_stat.st_mode &= ~(S_ISUID | S_ISGID);
237                         perror_msg("unable to preserve ownership of `%s'", dest);
238                 }
239                 if (chmod(dest, source_stat.st_mode) < 0)
240                         perror_msg("unable to preserve permissions of `%s'", dest);
241         }
242
243         return 0;
244 }