blob: 72b0791c24c7ee749854c68fd74a407906abea29 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Erik Andersenfac10d72000-02-07 05:29:42 +00002/*
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>
Erik Andersen029011b2000-03-04 21:19:32 +000040#include <setjmp.h> /* Ok to use this since `ash' does, therefore it's in the libc subset already. */
41#include <string.h>
42#include <unistd.h>
43#include <errno.h>
Erik Andersenfac10d72000-02-07 05:29:42 +000044
45#define is_cp 0
46#define is_mv 1
Erik Andersen029011b2000-03-04 21:19:32 +000047static int dz_i; /* index into cp_mv_usage */
Erik Andersene49d5ec2000-02-08 19:58:47 +000048static const char *dz; /* dollar zero, .bss */
Erik Andersene49d5ec2000-02-08 19:58:47 +000049static const char *cp_mv_usage[] = /* .rodata */
Erik Andersenfac10d72000-02-07 05:29:42 +000050{
Erik Andersene49d5ec2000-02-08 19:58:47 +000051 "cp [OPTION]... SOURCE DEST\n"
52 " or: cp [OPTION]... SOURCE... DIRECTORY\n\n"
53 "Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.\n"
54 "\n"
55 "\t-a\tsame as -dpR\n"
56 "\t-d\tpreserve links\n"
57 "\t-p\tpreserve file attributes if possible\n"
58 "\t-R\tcopy directories recursively\n",
59 "mv SOURCE DEST\n"
60 " or: mv SOURCE... DIRECTORY\n\n"
61 "Rename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n"
Erik Andersenfac10d72000-02-07 05:29:42 +000062};
63
Erik Andersen029011b2000-03-04 21:19:32 +000064static int recursiveFlag;
65static int followLinks;
66static int preserveFlag;
67
68static const char *baseSrcName;
69static int srcDirFlag;
70static struct stat srcStatBuf;
71
72static char baseDestName[PATH_MAX + 1];
73static size_t baseDestLen;
74static int destDirFlag;
75static struct stat destStatBuf;
76
77static jmp_buf catch;
78static volatile int mv_Action_first_time;
79
80static void name_too_long__exit (void) __attribute__((noreturn));
81
82static
83void name_too_long__exit (void)
84{
85 fprintf(stderr, name_too_long, dz);
86 exit FALSE;
87}
88
89static void
90fill_baseDest_buf(char *_buf, size_t * _buflen) {
91 const char *srcBasename;
92 if ((srcBasename = strrchr(baseSrcName, '/')) == NULL) {
93 srcBasename = baseSrcName;
94 if (_buf[*_buflen - 1] != '/') {
95 if (++(*_buflen) > PATH_MAX)
96 name_too_long__exit();
97 strcat(_buf, "/");
98 }
99 }
100 if (*_buflen + strlen(srcBasename) > PATH_MAX)
101 name_too_long__exit();
102 strcat(_buf, srcBasename);
103 return;
104
105}
106
107static int
Erik Andersen3364d782000-03-28 00:58:14 +0000108cp_mv_Action(const char *fileName, struct stat *statbuf, void* junk)
Erik Andersen029011b2000-03-04 21:19:32 +0000109{
110 char destName[PATH_MAX + 1];
111 size_t destLen;
112 const char *srcBasename;
113 char *name;
114
115 strcpy(destName, baseDestName);
116 destLen = strlen(destName);
117
118 if (srcDirFlag == TRUE) {
119 if (recursiveFlag == FALSE) {
120 fprintf(stderr, omitting_directory, dz, baseSrcName);
121 return TRUE;
122 }
123 srcBasename = (strstr(fileName, baseSrcName)
124 + strlen(baseSrcName));
125
126 if (destLen + strlen(srcBasename) > PATH_MAX) {
127 fprintf(stderr, name_too_long, dz);
128 return FALSE;
129 }
130 strcat(destName, srcBasename);
131 }
132 else if (destDirFlag == TRUE) {
133 fill_baseDest_buf(&destName[0], &destLen);
134 }
135 else {
136 srcBasename = baseSrcName;
137 }
138 if (mv_Action_first_time && (dz_i == is_mv)) {
139 mv_Action_first_time = errno = 0;
140 if (rename(fileName, destName) < 0 && errno != EXDEV) {
141 fprintf(stderr, "%s: rename(%s, %s): %s\n",
142 dz, fileName, destName, strerror(errno));
143 goto do_copyFile; /* Try anyway... */
144 }
145 else if (errno == EXDEV)
146 goto do_copyFile;
147 else
148 longjmp(catch, 1); /* succeeded with rename() */
149 }
150 do_copyFile:
151 if (preserveFlag == TRUE && statbuf->st_nlink > 1) {
152 if (is_in_ino_dev_hashtable(statbuf, &name)) {
153 if (link(name, destName) < 0) {
154 fprintf(stderr, "%s: link(%s, %s): %s\n",
155 dz, name, destName, strerror(errno));
156 return FALSE;
157 }
158 return TRUE;
159 }
160 else {
161 add_to_ino_dev_hashtable(statbuf, destName);
162 }
163 }
164 return copyFile(fileName, destName, preserveFlag, followLinks);
165}
166
167static int
Erik Andersen3364d782000-03-28 00:58:14 +0000168rm_Action(const char *fileName, struct stat *statbuf, void* junk)
Erik Andersen029011b2000-03-04 21:19:32 +0000169{
170 int status = TRUE;
171
172 if (S_ISDIR(statbuf->st_mode)) {
173 if (rmdir(fileName) < 0) {
174 fprintf(stderr, "%s: rmdir(%s): %s\n", dz, fileName, strerror(errno));
175 status = FALSE;
176 }
177 } else if (unlink(fileName) < 0) {
178 fprintf(stderr, "%s: unlink(%s): %s\n", dz, fileName, strerror(errno));
179 status = FALSE;
180 }
181 return status;
182}
183
Erik Andersenfac10d72000-02-07 05:29:42 +0000184extern int cp_mv_main(int argc, char **argv)
185{
Erik Andersen029011b2000-03-04 21:19:32 +0000186 dz = *argv; /* already basename'd by busybox.c:main */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000187 if (*dz == 'c' && *(dz + 1) == 'p')
188 dz_i = is_cp;
189 else
190 dz_i = is_mv;
191 if (argc < 3)
192 usage(cp_mv_usage[dz_i]);
193 argc--;
194 argv++;
195
196 if (dz_i == is_cp) {
197 recursiveFlag = preserveFlag = FALSE;
198 followLinks = TRUE;
199 while (**argv == '-') {
200 while (*++(*argv)) {
201 switch (**argv) {
202 case 'a':
203 followLinks = FALSE;
204 preserveFlag = TRUE;
205 recursiveFlag = TRUE;
206 break;
207 case 'd':
208 followLinks = FALSE;
209 break;
210 case 'p':
211 preserveFlag = TRUE;
212 break;
213 case 'R':
214 recursiveFlag = TRUE;
215 break;
216 default:
217 usage(cp_mv_usage[is_cp]);
218 }
219 }
220 argc--;
221 argv++;
222 }
223 } else { /* (dz_i == is_mv) */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000224 recursiveFlag = preserveFlag = TRUE;
225 followLinks = FALSE;
226 }
227
228 if (strlen(argv[argc - 1]) > PATH_MAX) {
229 fprintf(stderr, name_too_long, "cp");
230 goto exit_false;
231 }
232 strcpy(baseDestName, argv[argc - 1]);
233 baseDestLen = strlen(baseDestName);
234 if (baseDestLen == 0)
235 goto exit_false;
236
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000237 destDirFlag = isDirectory(baseDestName, TRUE, &destStatBuf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000238 if ((argc > 3) && destDirFlag == FALSE) {
239 fprintf(stderr, not_a_directory, "cp", baseDestName);
240 goto exit_false;
241 }
242
243 while (argc-- > 1) {
244 size_t srcLen;
Erik Andersen029011b2000-03-04 21:19:32 +0000245 volatile int flags_memo;
246 int status;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000247
248 baseSrcName = *(argv++);
249
250 if ((srcLen = strlen(baseSrcName)) > PATH_MAX)
Erik Andersen029011b2000-03-04 21:19:32 +0000251 name_too_long__exit();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000252
Erik Andersen029011b2000-03-04 21:19:32 +0000253 if (srcLen == 0) continue; /* "" */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000254
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000255 srcDirFlag = isDirectory(baseSrcName, followLinks, &srcStatBuf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000256
257 if ((flags_memo = (recursiveFlag == TRUE &&
258 srcDirFlag == TRUE && destDirFlag == TRUE))) {
Erik Andersen029011b2000-03-04 21:19:32 +0000259
260 struct stat sb;
261 int state = 0;
262 char *pushd, *d, *p;
263
264 if ((pushd = getcwd(NULL, PATH_MAX + 1)) == NULL) {
265 fprintf(stderr, "%s: getcwd(): %s\n", dz, strerror(errno));
266 continue;
267 }
268 if (chdir(baseDestName) < 0) {
269 fprintf(stderr, "%s: chdir(%s): %s\n", dz, baseSrcName, strerror(errno));
270 continue;
271 }
272 if ((d = getcwd(NULL, PATH_MAX + 1)) == NULL) {
273 fprintf(stderr, "%s: getcwd(): %s\n", dz, strerror(errno));
274 continue;
275 }
276 while (!state && *d != '\0') {
277 if (stat(d, &sb) < 0) { /* stat not lstat - always dereference targets */
278 fprintf(stderr, "%s: stat(%s) :%s\n", dz, d, strerror(errno));
279 state = -1;
280 continue;
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000281 }
Erik Andersen029011b2000-03-04 21:19:32 +0000282 if ((sb.st_ino == srcStatBuf.st_ino) &&
283 (sb.st_dev == srcStatBuf.st_dev)) {
284 fprintf(stderr,
285 "%s: Cannot %s `%s' "
286 "into a subdirectory of itself, `%s/%s'\n",
287 dz, dz, baseSrcName, baseDestName, baseSrcName);
288 state = -1;
289 continue;
290 }
291 if ((p = strrchr(d, '/')) != NULL) {
292 *p = '\0';
293 }
294 }
295 if (chdir(pushd) < 0) {
296 fprintf(stderr, "%s: chdir(%s): %s\n", dz, pushd, strerror(errno));
297 free(pushd);
298 free(d);
299 continue;
300 }
301 free(pushd);
302 free(d);
303 if (state < 0)
304 continue;
305 else
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000306 fill_baseDest_buf(baseDestName, &baseDestLen);
Erik Andersenfac10d72000-02-07 05:29:42 +0000307 }
Erik Andersen029011b2000-03-04 21:19:32 +0000308 status = setjmp(catch);
309 if (status == 0) {
310 mv_Action_first_time = 1;
311 if (recursiveAction(baseSrcName,
312 recursiveFlag, followLinks, FALSE,
Erik Andersen3364d782000-03-28 00:58:14 +0000313 cp_mv_Action, cp_mv_Action, NULL) == FALSE) goto exit_false;
Erik Andersen029011b2000-03-04 21:19:32 +0000314 if (dz_i == is_mv &&
315 recursiveAction(baseSrcName,
316 recursiveFlag, followLinks, TRUE,
Erik Andersen3364d782000-03-28 00:58:14 +0000317 rm_Action, rm_Action, NULL) == FALSE) goto exit_false;
Erik Andersen029011b2000-03-04 21:19:32 +0000318 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000319 if (flags_memo)
320 *(baseDestName + baseDestLen) = '\0';
Erik Andersenfac10d72000-02-07 05:29:42 +0000321 }
Erik Andersen029011b2000-03-04 21:19:32 +0000322// exit_true:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000323 exit TRUE;
Erik Andersen029011b2000-03-04 21:19:32 +0000324 exit_false:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000325 exit FALSE;
Erik Andersenfac10d72000-02-07 05:29:42 +0000326}
327
Erik Andersen029011b2000-03-04 21:19:32 +0000328/*
329Local Variables:
330c-file-style: "linux"
331c-basic-offset: 4
332tab-width: 4
333End:
334*/