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