Initial implementation of wget, from Chip Rosenthal <chip@laserlink.net>.
[oweals/busybox.git] / cp_mv.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini `cp' and `mv' implementation for BusyBox.
4  *
5  *
6  * Copyright (C) 1999 by Lineo, inc.
7  * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
8  *
9  * Copyright (C) 2000 by BitterSweet Enterprises, LLC. (GPL)
10  * Extensively modified and rewritten by  Karl M. Hegbloom <karlheg@debian.org>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25  *
26  */
27
28 #include "internal.h"
29 #define BB_DECLARE_EXTERN
30 #define bb_need_name_too_long
31 #define bb_need_omitting_directory
32 #define bb_need_not_a_directory
33 #include "messages.c"
34
35 #include <stdio.h>
36 #include <time.h>
37 #include <utime.h>
38 #include <dirent.h>
39 #include <sys/param.h>
40 #include <setjmp.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <errno.h>
44 #include <getopt.h>
45
46 #define is_cp 0
47 #define is_mv 1
48 static int         dz_i;                /* index into cp_mv_usage */
49
50 static const char *cp_mv_usage[] =      /* .rodata */
51 {
52         cp_usage,
53         mv_usage
54 };
55
56 static int recursiveFlag;
57 static int followLinks;
58 static int preserveFlag;
59 static int forceFlag;
60
61 static const char *baseSrcName;
62 static int                 srcDirFlag;
63 static struct stat srcStatBuf;
64
65 static char                baseDestName[BUFSIZ + 1];
66 static size_t      baseDestLen;
67 static int                 destDirFlag;
68 static struct stat destStatBuf;
69
70 static jmp_buf      catch;
71 static volatile int mv_Action_first_time;
72
73 static void name_too_long__exit (void) __attribute__((noreturn));
74
75 static
76 void name_too_long__exit (void)
77 {
78         fatalError(name_too_long);
79 }
80
81 static void
82 fill_baseDest_buf(char *_buf, size_t * _buflen) {
83         const char *srcBasename;
84         if ((srcBasename = strrchr(baseSrcName, '/')) == NULL) {
85                 srcBasename = baseSrcName;
86                 if (_buf[*_buflen - 1] != '/') {
87                         if (++(*_buflen) > BUFSIZ)
88                                 name_too_long__exit();
89                         strcat(_buf, "/");
90                 }
91         }
92         if (*_buflen + strlen(srcBasename) > BUFSIZ)
93                 name_too_long__exit();
94         strcat(_buf, srcBasename);
95         return;
96         
97 }
98
99 static int
100 cp_mv_Action(const char *fileName, struct stat *statbuf, void* junk)
101 {
102         char            destName[BUFSIZ + 1];
103         size_t          destLen;
104         const char *srcBasename;
105         char       *name;
106
107         strcpy(destName, baseDestName);
108         destLen = strlen(destName);
109
110         if (srcDirFlag == TRUE) {
111                 if (recursiveFlag == FALSE) {
112                         errorMsg(omitting_directory, baseSrcName);
113                         return TRUE;
114                 }
115                 srcBasename = (strstr(fileName, baseSrcName)
116                                            + strlen(baseSrcName));
117
118                 if (destLen + strlen(srcBasename) > BUFSIZ) {
119                         errorMsg(name_too_long);
120                         return FALSE;
121                 }
122                 strcat(destName, srcBasename);
123         }
124         else if (destDirFlag == TRUE) {
125                 fill_baseDest_buf(&destName[0], &destLen);
126         }
127         else {
128                 srcBasename = baseSrcName;
129         }
130         if (mv_Action_first_time && (dz_i == is_mv)) {
131                 mv_Action_first_time = errno = 0;
132                 if (rename(fileName, destName) < 0 && errno != EXDEV) {
133                         errorMsg("rename(%s, %s): %s\n", fileName, destName, 
134                                         strerror(errno));
135                         goto do_copyFile;       /* Try anyway... */
136                 }
137                 else if (errno == EXDEV)
138                         goto do_copyFile;
139                 else
140                         longjmp(catch, 1);      /* succeeded with rename() */
141         }
142  do_copyFile:
143         if (preserveFlag == TRUE && statbuf->st_nlink > 1) {
144                 if (is_in_ino_dev_hashtable(statbuf, &name)) {
145                         if (link(name, destName) < 0) {
146                                 errorMsg("link(%s, %s): %s\n", name, destName, strerror(errno));
147                                 return FALSE;
148                         }
149                         return TRUE;
150                 }
151                 else {
152                         add_to_ino_dev_hashtable(statbuf, destName);
153                 }
154         }
155         return copyFile(fileName, destName, preserveFlag, followLinks, forceFlag);
156 }
157
158 static int
159 rm_Action(const char *fileName, struct stat *statbuf, void* junk)
160 {
161         int status = TRUE;
162
163         if (S_ISDIR(statbuf->st_mode)) {
164                 if (rmdir(fileName) < 0) {
165                         errorMsg("rmdir(%s): %s\n", fileName, strerror(errno));
166                         status = FALSE;
167                 }
168         } else if (unlink(fileName) < 0) {
169                 errorMsg("unlink(%s): %s\n", fileName, strerror(errno));
170                 status = FALSE;
171         }
172         return status;
173 }
174
175 extern int cp_mv_main(int argc, char **argv)
176 {
177         volatile int i;
178         int c;
179
180         if (*applet_name == 'c' && *(applet_name + 1) == 'p')
181                 dz_i = is_cp;
182         else
183                 dz_i = is_mv;
184         if (argc < 3)
185                 usage(cp_mv_usage[dz_i]);
186
187         if (dz_i == is_cp) {
188                 recursiveFlag = preserveFlag = forceFlag = FALSE;
189                 followLinks = TRUE;
190                 while ((c = getopt(argc, argv, "adpRf")) != EOF) {
191                                 switch (c) {
192                                 case 'a':
193                                         followLinks = FALSE;
194                                         preserveFlag = TRUE;
195                                         recursiveFlag = TRUE;
196                                         break;
197                                 case 'd':
198                                         followLinks = FALSE;
199                                         break;
200                                 case 'p':
201                                         preserveFlag = TRUE;
202                                         break;
203                                 case 'R':
204                                         recursiveFlag = TRUE;
205                                         break;
206                                 case 'f':
207                                         forceFlag = TRUE;
208                                         break;
209                                 default:
210                                         usage(cp_mv_usage[is_cp]);
211                                 }
212                 }
213                 if ((argc - optind) < 2) {
214                         usage(cp_mv_usage[dz_i]);
215                 }
216         } else {                                        /* (dz_i == is_mv) */
217                 recursiveFlag = preserveFlag = TRUE;
218                 followLinks = FALSE;
219         }
220         
221
222         if (strlen(argv[argc - 1]) > BUFSIZ) {
223                 errorMsg(name_too_long);
224                 goto exit_false;
225         }
226         strcpy(baseDestName, argv[argc - 1]);
227         baseDestLen = strlen(baseDestName);
228         if (baseDestLen == 0)
229                 goto exit_false;
230
231         destDirFlag = isDirectory(baseDestName, TRUE, &destStatBuf);
232         if (argc - optind > 2 && destDirFlag == FALSE) {
233                 errorMsg(not_a_directory, baseDestName);
234                 goto exit_false;
235         }
236
237         for (i = optind; i < (argc-1); i++) {
238                 size_t srcLen;
239                 volatile int flags_memo;
240                 int        status;
241
242                 baseSrcName=argv[i];
243
244                 if ((srcLen = strlen(baseSrcName)) > BUFSIZ)
245                         name_too_long__exit();
246
247                 if (srcLen == 0) continue; /* "" */
248
249                 srcDirFlag = isDirectory(baseSrcName, followLinks, &srcStatBuf);
250
251                 if ((flags_memo = (recursiveFlag == TRUE &&
252                                                    srcDirFlag == TRUE && destDirFlag == TRUE))) {
253
254                         struct stat sb;
255                         int                     state = 0;
256                         char            *pushd, *d, *p;
257
258                         if ((pushd = getcwd(NULL, BUFSIZ + 1)) == NULL) {
259                                 errorMsg("getcwd(): %s\n", strerror(errno));
260                                 continue;
261                         }
262                         if (chdir(baseDestName) < 0) {
263                                 errorMsg("chdir(%s): %s\n", baseSrcName, strerror(errno));
264                                 continue;
265                         }
266                         if ((d = getcwd(NULL, BUFSIZ + 1)) == NULL) {
267                                 errorMsg("getcwd(): %s\n", strerror(errno));
268                                 continue;
269                         }
270                         while (!state && *d != '\0') {
271                                 if (stat(d, &sb) < 0) { /* stat not lstat - always dereference targets */
272                                         errorMsg("stat(%s): %s\n", d, strerror(errno));
273                                         state = -1;
274                                         continue;
275                                 }
276                                 if ((sb.st_ino == srcStatBuf.st_ino) &&
277                                         (sb.st_dev == srcStatBuf.st_dev)) {
278                                         errorMsg("Cannot %s `%s' into a subdirectory of itself, "
279                                                         "`%s/%s'\n", applet_name, baseSrcName,
280                                                         baseDestName, baseSrcName);
281                                         state = -1;
282                                         continue;
283                                 }
284                                 if ((p = strrchr(d, '/')) != NULL) {
285                                         *p = '\0';
286                                 }
287                         }
288                         if (chdir(pushd) < 0) {
289                                 errorMsg("chdir(%s): %s\n", pushd, strerror(errno));
290                                 free(pushd);
291                                 free(d);
292                                 continue;
293                         }
294                         free(pushd);
295                         free(d);
296                         if (state < 0)
297                                 continue;
298                         else
299                                 fill_baseDest_buf(baseDestName, &baseDestLen);
300                 }
301                 status = setjmp(catch);
302                 if (status == 0) {
303                         mv_Action_first_time = 1;
304                         if (recursiveAction(baseSrcName,
305                                                                 recursiveFlag, followLinks, FALSE,
306                                                                 cp_mv_Action, cp_mv_Action, NULL) == FALSE) goto exit_false;
307                         if (dz_i == is_mv &&
308                                 recursiveAction(baseSrcName,
309                                                                 recursiveFlag, followLinks, TRUE,
310                                                                 rm_Action, rm_Action, NULL) == FALSE) goto exit_false;
311                 }               
312                 if (flags_memo)
313                         *(baseDestName + baseDestLen) = '\0';
314         }
315         return( TRUE);
316  exit_false:
317         return( FALSE);
318 }
319
320 /*
321 Local Variables:
322 c-file-style: "linux"
323 c-basic-offset: 4
324 tab-width: 4
325 End:
326 */