Fix stupid bugs. update was segfaulting. mknod minor # was always 0
[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
45 #define is_cp 0
46 #define is_mv 1
47 static int         dz_i;                /* index into cp_mv_usage */
48 static const char *dz;                  /* dollar zero, .bss */
49 static const char *cp_mv_usage[] =      /* .rodata */
50 {
51         "cp [OPTION]... SOURCE DEST\n"
52                 "   or: cp [OPTION]... SOURCE... DIRECTORY\n"
53 #ifndef BB_FEATURE_TRIVIAL_HELP
54                 "\nCopies SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.\n"
55                 "\n"
56                 "\t-a\tSame as -dpR\n"
57                 "\t-d\tPreserves links\n"
58                 "\t-p\tPreserves file attributes if possible\n"
59                 "\t-R\tCopies directories recursively\n"
60 #endif
61                 ,
62         "mv SOURCE DEST\n"
63                 "   or: mv SOURCE... DIRECTORY\n"
64 #ifndef BB_FEATURE_TRIVIAL_HELP
65                 "\nRename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n"
66 #endif
67 };
68
69 static int recursiveFlag;
70 static int followLinks;
71 static int preserveFlag;
72
73 static const char *baseSrcName;
74 static int                 srcDirFlag;
75 static struct stat srcStatBuf;
76
77 static char                baseDestName[BUFSIZ + 1];
78 static size_t      baseDestLen;
79 static int                 destDirFlag;
80 static struct stat destStatBuf;
81
82 static jmp_buf      catch;
83 static volatile int mv_Action_first_time;
84
85 static void name_too_long__exit (void) __attribute__((noreturn));
86
87 static
88 void name_too_long__exit (void)
89 {
90         fprintf(stderr, name_too_long, dz);
91         exit FALSE;
92 }
93
94 static void
95 fill_baseDest_buf(char *_buf, size_t * _buflen) {
96         const char *srcBasename;
97         if ((srcBasename = strrchr(baseSrcName, '/')) == NULL) {
98                 srcBasename = baseSrcName;
99                 if (_buf[*_buflen - 1] != '/') {
100                         if (++(*_buflen) > BUFSIZ)
101                                 name_too_long__exit();
102                         strcat(_buf, "/");
103                 }
104         }
105         if (*_buflen + strlen(srcBasename) > BUFSIZ)
106                 name_too_long__exit();
107         strcat(_buf, srcBasename);
108         return;
109         
110 }
111
112 static int
113 cp_mv_Action(const char *fileName, struct stat *statbuf, void* junk)
114 {
115         char            destName[BUFSIZ + 1];
116         size_t          destLen;
117         const char *srcBasename;
118         char       *name;
119
120         strcpy(destName, baseDestName);
121         destLen = strlen(destName);
122
123         if (srcDirFlag == TRUE) {
124                 if (recursiveFlag == FALSE) {
125                         fprintf(stderr, omitting_directory, dz, baseSrcName);
126                         return TRUE;
127                 }
128                 srcBasename = (strstr(fileName, baseSrcName)
129                                            + strlen(baseSrcName));
130
131                 if (destLen + strlen(srcBasename) > BUFSIZ) {
132                         fprintf(stderr, name_too_long, dz);
133                         return FALSE;
134                 }
135                 strcat(destName, srcBasename);
136         }
137         else if (destDirFlag == TRUE) {
138                 fill_baseDest_buf(&destName[0], &destLen);
139         }
140         else {
141                 srcBasename = baseSrcName;
142         }
143         if (mv_Action_first_time && (dz_i == is_mv)) {
144                 mv_Action_first_time = errno = 0;
145                 if (rename(fileName, destName) < 0 && errno != EXDEV) {
146                         fprintf(stderr, "%s: rename(%s, %s): %s\n",
147                                         dz, fileName, destName, strerror(errno));
148                         goto do_copyFile;       /* Try anyway... */
149                 }
150                 else if (errno == EXDEV)
151                         goto do_copyFile;
152                 else
153                         longjmp(catch, 1);      /* succeeded with rename() */
154         }
155  do_copyFile:
156         if (preserveFlag == TRUE && statbuf->st_nlink > 1) {
157                 if (is_in_ino_dev_hashtable(statbuf, &name)) {
158                         if (link(name, destName) < 0) {
159                                 fprintf(stderr, "%s: link(%s, %s): %s\n",
160                                                 dz, name, destName, strerror(errno));
161                                 return FALSE;
162                         }
163                         return TRUE;
164                 }
165                 else {
166                         add_to_ino_dev_hashtable(statbuf, destName);
167                 }
168         }
169         return copyFile(fileName, destName, preserveFlag, followLinks);
170 }
171
172 static int
173 rm_Action(const char *fileName, struct stat *statbuf, void* junk)
174 {
175         int status = TRUE;
176
177         if (S_ISDIR(statbuf->st_mode)) {
178                 if (rmdir(fileName) < 0) {
179                         fprintf(stderr, "%s: rmdir(%s): %s\n", dz, fileName, strerror(errno));
180                         status = FALSE;
181                 }
182         } else if (unlink(fileName) < 0) {
183                 fprintf(stderr, "%s: unlink(%s): %s\n", dz, fileName, strerror(errno));
184                 status = FALSE;
185         }
186         return status;
187 }
188
189 extern int cp_mv_main(int argc, char **argv)
190 {
191         dz = *argv;                                     /* already basename'd by busybox.c:main */
192         if (*dz == 'c' && *(dz + 1) == 'p')
193                 dz_i = is_cp;
194         else
195                 dz_i = is_mv;
196         if (argc < 3)
197                 usage(cp_mv_usage[dz_i]);
198         argc--;
199         argv++;
200
201         if (dz_i == is_cp) {
202                 recursiveFlag = preserveFlag = FALSE;
203                 followLinks = TRUE;
204                 while (**argv == '-') {
205                         while (*++(*argv)) {
206                                 switch (**argv) {
207                                 case 'a':
208                                         followLinks = FALSE;
209                                         preserveFlag = TRUE;
210                                         recursiveFlag = TRUE;
211                                         break;
212                                 case 'd':
213                                         followLinks = FALSE;
214                                         break;
215                                 case 'p':
216                                         preserveFlag = TRUE;
217                                         break;
218                                 case 'R':
219                                         recursiveFlag = TRUE;
220                                         break;
221                                 default:
222                                         usage(cp_mv_usage[is_cp]);
223                                 }
224                         }
225                         argc--;
226                         argv++;
227                 }
228         } else {                                        /* (dz_i == is_mv) */
229                 recursiveFlag = preserveFlag = TRUE;
230                 followLinks = FALSE;
231         }
232
233         if (strlen(argv[argc - 1]) > BUFSIZ) {
234                 fprintf(stderr, name_too_long, "cp");
235                 goto exit_false;
236         }
237         strcpy(baseDestName, argv[argc - 1]);
238         baseDestLen = strlen(baseDestName);
239         if (baseDestLen == 0)
240                 goto exit_false;
241
242         destDirFlag = isDirectory(baseDestName, TRUE, &destStatBuf);
243         if ((argc > 3) && destDirFlag == FALSE) {
244                 fprintf(stderr, not_a_directory, "cp", baseDestName);
245                 goto exit_false;
246         }
247
248         while (argc-- > 1) {
249                 size_t srcLen;
250                 volatile int flags_memo;
251                 int        status;
252
253                 baseSrcName = *(argv++);
254
255                 if ((srcLen = strlen(baseSrcName)) > BUFSIZ)
256                         name_too_long__exit();
257
258                 if (srcLen == 0) continue; /* "" */
259
260                 srcDirFlag = isDirectory(baseSrcName, followLinks, &srcStatBuf);
261
262                 if ((flags_memo = (recursiveFlag == TRUE &&
263                                                    srcDirFlag == TRUE && destDirFlag == TRUE))) {
264
265                         struct stat sb;
266                         int                     state = 0;
267                         char            *pushd, *d, *p;
268
269                         if ((pushd = getcwd(NULL, BUFSIZ + 1)) == NULL) {
270                                 fprintf(stderr, "%s: getcwd(): %s\n", dz, strerror(errno));
271                                 continue;
272                         }
273                         if (chdir(baseDestName) < 0) {
274                                 fprintf(stderr, "%s: chdir(%s): %s\n", dz, baseSrcName, strerror(errno));
275                                 continue;
276                         }
277                         if ((d = getcwd(NULL, BUFSIZ + 1)) == NULL) {
278                                 fprintf(stderr, "%s: getcwd(): %s\n", dz, strerror(errno));
279                                 continue;
280                         }
281                         while (!state && *d != '\0') {
282                                 if (stat(d, &sb) < 0) { /* stat not lstat - always dereference targets */
283                                         fprintf(stderr, "%s: stat(%s) :%s\n", dz, d, strerror(errno));
284                                         state = -1;
285                                         continue;
286                                 }
287                                 if ((sb.st_ino == srcStatBuf.st_ino) &&
288                                         (sb.st_dev == srcStatBuf.st_dev)) {
289                                         fprintf(stderr,
290                                                         "%s: Cannot %s `%s' "
291                                                         "into a subdirectory of itself, `%s/%s'\n",
292                                                         dz, dz, baseSrcName, baseDestName, baseSrcName);
293                                         state = -1;
294                                         continue;
295                                 }
296                                 if ((p = strrchr(d, '/')) != NULL) {
297                                         *p = '\0';
298                                 }
299                         }
300                         if (chdir(pushd) < 0) {
301                                 fprintf(stderr, "%s: chdir(%s): %s\n", dz, pushd, strerror(errno));
302                                 free(pushd);
303                                 free(d);
304                                 continue;
305                         }
306                         free(pushd);
307                         free(d);
308                         if (state < 0)
309                                 continue;
310                         else
311                                 fill_baseDest_buf(baseDestName, &baseDestLen);
312                 }
313                 status = setjmp(catch);
314                 if (status == 0) {
315                         mv_Action_first_time = 1;
316                         if (recursiveAction(baseSrcName,
317                                                                 recursiveFlag, followLinks, FALSE,
318                                                                 cp_mv_Action, cp_mv_Action, NULL) == FALSE) goto exit_false;
319                         if (dz_i == is_mv &&
320                                 recursiveAction(baseSrcName,
321                                                                 recursiveFlag, followLinks, TRUE,
322                                                                 rm_Action, rm_Action, NULL) == FALSE) goto exit_false;
323                 }               
324                 if (flags_memo)
325                         *(baseDestName + baseDestLen) = '\0';
326         }
327 // exit_true:
328         exit TRUE;
329  exit_false:
330         exit FALSE;
331 }
332
333 /*
334 Local Variables:
335 c-file-style: "linux"
336 c-basic-offset: 4
337 tab-width: 4
338 End:
339 */