blob: b43a6d31fcc7539fcf35722b1936744a9b3ccc08 [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 Andersen61677fe2000-04-13 01:18:56 +000040#include <setjmp.h>
Erik Andersen029011b2000-03-04 21:19:32 +000041#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"
Erik Andersen1d1d9502000-04-21 01:26:49 +000052 " or: cp [OPTION]... SOURCE... DIRECTORY\n"
53#ifndef BB_FEATURE_TRIVIAL_HELP
54 "\nCopies SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.\n"
Erik Andersene49d5ec2000-02-08 19:58:47 +000055 "\n"
Erik Andersen1d1d9502000-04-21 01:26:49 +000056 "\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 ,
Erik Andersene49d5ec2000-02-08 19:58:47 +000062 "mv SOURCE DEST\n"
Erik Andersen1d1d9502000-04-21 01:26:49 +000063 " 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
Erik Andersenfac10d72000-02-07 05:29:42 +000067};
68
Erik Andersen029011b2000-03-04 21:19:32 +000069static int recursiveFlag;
70static int followLinks;
71static int preserveFlag;
72
73static const char *baseSrcName;
74static int srcDirFlag;
75static struct stat srcStatBuf;
76
Erik Andersen4f3f7572000-04-28 00:18:56 +000077static char baseDestName[BUFSIZ + 1];
Erik Andersen029011b2000-03-04 21:19:32 +000078static size_t baseDestLen;
79static int destDirFlag;
80static struct stat destStatBuf;
81
82static jmp_buf catch;
83static volatile int mv_Action_first_time;
84
85static void name_too_long__exit (void) __attribute__((noreturn));
86
87static
88void name_too_long__exit (void)
89{
90 fprintf(stderr, name_too_long, dz);
91 exit FALSE;
92}
93
94static void
95fill_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] != '/') {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000100 if (++(*_buflen) > BUFSIZ)
Erik Andersen029011b2000-03-04 21:19:32 +0000101 name_too_long__exit();
102 strcat(_buf, "/");
103 }
104 }
Erik Andersen4f3f7572000-04-28 00:18:56 +0000105 if (*_buflen + strlen(srcBasename) > BUFSIZ)
Erik Andersen029011b2000-03-04 21:19:32 +0000106 name_too_long__exit();
107 strcat(_buf, srcBasename);
108 return;
109
110}
111
112static int
Erik Andersen3364d782000-03-28 00:58:14 +0000113cp_mv_Action(const char *fileName, struct stat *statbuf, void* junk)
Erik Andersen029011b2000-03-04 21:19:32 +0000114{
Erik Andersen4f3f7572000-04-28 00:18:56 +0000115 char destName[BUFSIZ + 1];
Erik Andersen029011b2000-03-04 21:19:32 +0000116 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
Erik Andersen4f3f7572000-04-28 00:18:56 +0000131 if (destLen + strlen(srcBasename) > BUFSIZ) {
Erik Andersen029011b2000-03-04 21:19:32 +0000132 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
172static int
Erik Andersen3364d782000-03-28 00:58:14 +0000173rm_Action(const char *fileName, struct stat *statbuf, void* junk)
Erik Andersen029011b2000-03-04 21:19:32 +0000174{
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
Erik Andersenfac10d72000-02-07 05:29:42 +0000189extern int cp_mv_main(int argc, char **argv)
190{
Erik Andersen029011b2000-03-04 21:19:32 +0000191 dz = *argv; /* already basename'd by busybox.c:main */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000192 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) */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000229 recursiveFlag = preserveFlag = TRUE;
230 followLinks = FALSE;
231 }
232
Erik Andersen4f3f7572000-04-28 00:18:56 +0000233 if (strlen(argv[argc - 1]) > BUFSIZ) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000234 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
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000242 destDirFlag = isDirectory(baseDestName, TRUE, &destStatBuf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000243 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;
Erik Andersen029011b2000-03-04 21:19:32 +0000250 volatile int flags_memo;
251 int status;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000252
253 baseSrcName = *(argv++);
254
Erik Andersen4f3f7572000-04-28 00:18:56 +0000255 if ((srcLen = strlen(baseSrcName)) > BUFSIZ)
Erik Andersen029011b2000-03-04 21:19:32 +0000256 name_too_long__exit();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000257
Erik Andersen029011b2000-03-04 21:19:32 +0000258 if (srcLen == 0) continue; /* "" */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000259
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000260 srcDirFlag = isDirectory(baseSrcName, followLinks, &srcStatBuf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000261
262 if ((flags_memo = (recursiveFlag == TRUE &&
263 srcDirFlag == TRUE && destDirFlag == TRUE))) {
Erik Andersen029011b2000-03-04 21:19:32 +0000264
265 struct stat sb;
266 int state = 0;
267 char *pushd, *d, *p;
268
Erik Andersen4f3f7572000-04-28 00:18:56 +0000269 if ((pushd = getcwd(NULL, BUFSIZ + 1)) == NULL) {
Erik Andersen029011b2000-03-04 21:19:32 +0000270 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 }
Erik Andersen4f3f7572000-04-28 00:18:56 +0000277 if ((d = getcwd(NULL, BUFSIZ + 1)) == NULL) {
Erik Andersen029011b2000-03-04 21:19:32 +0000278 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;
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000286 }
Erik Andersen029011b2000-03-04 21:19:32 +0000287 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
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000311 fill_baseDest_buf(baseDestName, &baseDestLen);
Erik Andersenfac10d72000-02-07 05:29:42 +0000312 }
Erik Andersen029011b2000-03-04 21:19:32 +0000313 status = setjmp(catch);
314 if (status == 0) {
315 mv_Action_first_time = 1;
316 if (recursiveAction(baseSrcName,
317 recursiveFlag, followLinks, FALSE,
Erik Andersen3364d782000-03-28 00:58:14 +0000318 cp_mv_Action, cp_mv_Action, NULL) == FALSE) goto exit_false;
Erik Andersen029011b2000-03-04 21:19:32 +0000319 if (dz_i == is_mv &&
320 recursiveAction(baseSrcName,
321 recursiveFlag, followLinks, TRUE,
Erik Andersen3364d782000-03-28 00:58:14 +0000322 rm_Action, rm_Action, NULL) == FALSE) goto exit_false;
Erik Andersen029011b2000-03-04 21:19:32 +0000323 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000324 if (flags_memo)
325 *(baseDestName + baseDestLen) = '\0';
Erik Andersenfac10d72000-02-07 05:29:42 +0000326 }
Erik Andersen029011b2000-03-04 21:19:32 +0000327// exit_true:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000328 exit TRUE;
Erik Andersen029011b2000-03-04 21:19:32 +0000329 exit_false:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000330 exit FALSE;
Erik Andersenfac10d72000-02-07 05:29:42 +0000331}
332
Erik Andersen029011b2000-03-04 21:19:32 +0000333/*
334Local Variables:
335c-file-style: "linux"
336c-basic-offset: 4
337tab-width: 4
338End:
339*/