blob: dbd2fddf56dae3e7aa5f844996afcab872125f20 [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 *
Eric Andersen8ec10a92001-01-27 09:33:39 +00006 * Copyright (C) 1999,2000,2001 by Lineo, inc.
Erik Andersenfac10d72000-02-07 05:29:42 +00007 * 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
Eric Andersen3570a342000-09-25 21:45:58 +000028#include "busybox.h"
Erik Andersenfac10d72000-02-07 05:29:42 +000029#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>
Eric Andersenf4c02262000-07-13 18:42:58 +000044#include <getopt.h>
Eric Andersened3ef502001-01-27 08:24:39 +000045#include <stdlib.h>
Erik Andersenfac10d72000-02-07 05:29:42 +000046
Mark Whitley59ab0252001-01-23 22:30:04 +000047static const int is_cp = 0;
48static const int is_mv = 1;
Erik Andersen029011b2000-03-04 21:19:32 +000049static int dz_i; /* index into cp_mv_usage */
Matt Kraai3bd8bd82000-07-14 23:28:47 +000050
Erik Andersene49d5ec2000-02-08 19:58:47 +000051static const char *cp_mv_usage[] = /* .rodata */
Erik Andersenfac10d72000-02-07 05:29:42 +000052{
Matt Kraai3bd8bd82000-07-14 23:28:47 +000053 cp_usage,
54 mv_usage
Erik Andersenfac10d72000-02-07 05:29:42 +000055};
56
Erik Andersen029011b2000-03-04 21:19:32 +000057static int recursiveFlag;
58static int followLinks;
59static int preserveFlag;
Erik Andersen59b9e872000-05-10 05:05:45 +000060static int forceFlag;
Erik Andersen029011b2000-03-04 21:19:32 +000061
62static const char *baseSrcName;
63static int srcDirFlag;
64static struct stat srcStatBuf;
65
Mark Whitley59ab0252001-01-23 22:30:04 +000066static char *pBaseDestName;
Erik Andersen029011b2000-03-04 21:19:32 +000067static size_t baseDestLen;
68static int destDirFlag;
69static struct stat destStatBuf;
70
71static jmp_buf catch;
72static volatile int mv_Action_first_time;
73
74static void name_too_long__exit (void) __attribute__((noreturn));
75
76static
77void name_too_long__exit (void)
78{
Mark Whitleyf57c9442000-12-07 19:56:48 +000079 error_msg_and_die(name_too_long);
Erik Andersen029011b2000-03-04 21:19:32 +000080}
81
82static void
83fill_baseDest_buf(char *_buf, size_t * _buflen) {
84 const char *srcBasename;
85 if ((srcBasename = strrchr(baseSrcName, '/')) == NULL) {
86 srcBasename = baseSrcName;
87 if (_buf[*_buflen - 1] != '/') {
Erik Andersen4f3f7572000-04-28 00:18:56 +000088 if (++(*_buflen) > BUFSIZ)
Erik Andersen029011b2000-03-04 21:19:32 +000089 name_too_long__exit();
90 strcat(_buf, "/");
91 }
92 }
Erik Andersen4f3f7572000-04-28 00:18:56 +000093 if (*_buflen + strlen(srcBasename) > BUFSIZ)
Erik Andersen029011b2000-03-04 21:19:32 +000094 name_too_long__exit();
95 strcat(_buf, srcBasename);
96 return;
97
98}
99
100static int
Erik Andersen3364d782000-03-28 00:58:14 +0000101cp_mv_Action(const char *fileName, struct stat *statbuf, void* junk)
Erik Andersen029011b2000-03-04 21:19:32 +0000102{
Erik Andersen4f3f7572000-04-28 00:18:56 +0000103 char destName[BUFSIZ + 1];
Erik Andersen029011b2000-03-04 21:19:32 +0000104 size_t destLen;
105 const char *srcBasename;
106 char *name;
107
Mark Whitley59ab0252001-01-23 22:30:04 +0000108 strcpy(destName, pBaseDestName);
Erik Andersen029011b2000-03-04 21:19:32 +0000109 destLen = strlen(destName);
110
111 if (srcDirFlag == TRUE) {
112 if (recursiveFlag == FALSE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000113 error_msg(omitting_directory, baseSrcName);
Erik Andersen029011b2000-03-04 21:19:32 +0000114 return TRUE;
115 }
116 srcBasename = (strstr(fileName, baseSrcName)
117 + strlen(baseSrcName));
118
Erik Andersen4f3f7572000-04-28 00:18:56 +0000119 if (destLen + strlen(srcBasename) > BUFSIZ) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000120 error_msg(name_too_long);
Erik Andersen029011b2000-03-04 21:19:32 +0000121 return FALSE;
122 }
123 strcat(destName, srcBasename);
124 }
125 else if (destDirFlag == TRUE) {
126 fill_baseDest_buf(&destName[0], &destLen);
127 }
128 else {
129 srcBasename = baseSrcName;
130 }
131 if (mv_Action_first_time && (dz_i == is_mv)) {
132 mv_Action_first_time = errno = 0;
133 if (rename(fileName, destName) < 0 && errno != EXDEV) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000134 perror_msg("rename(%s, %s)", fileName, destName);
Erik Andersen029011b2000-03-04 21:19:32 +0000135 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) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000146 perror_msg("link(%s, %s)", name, destName);
Erik Andersen029011b2000-03-04 21:19:32 +0000147 return FALSE;
148 }
149 return TRUE;
150 }
151 else {
152 add_to_ino_dev_hashtable(statbuf, destName);
153 }
154 }
Mark Whitleyf57c9442000-12-07 19:56:48 +0000155 return copy_file(fileName, destName, preserveFlag, followLinks, forceFlag);
Erik Andersen029011b2000-03-04 21:19:32 +0000156}
157
158static int
Erik Andersen3364d782000-03-28 00:58:14 +0000159rm_Action(const char *fileName, struct stat *statbuf, void* junk)
Erik Andersen029011b2000-03-04 21:19:32 +0000160{
161 int status = TRUE;
162
163 if (S_ISDIR(statbuf->st_mode)) {
164 if (rmdir(fileName) < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000165 perror_msg("rmdir(%s)", fileName);
Erik Andersen029011b2000-03-04 21:19:32 +0000166 status = FALSE;
167 }
168 } else if (unlink(fileName) < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000169 perror_msg("unlink(%s)", fileName);
Erik Andersen029011b2000-03-04 21:19:32 +0000170 status = FALSE;
171 }
172 return status;
173}
174
Erik Andersenfac10d72000-02-07 05:29:42 +0000175extern int cp_mv_main(int argc, char **argv)
176{
Eric Andersene7413a92000-07-14 06:19:41 +0000177 volatile int i;
Pavel Roskin93c0d9f2000-07-14 15:18:57 +0000178 int c;
Eric Andersend35c2152001-01-25 23:49:09 +0000179 RESERVE_BB_BUFFER(baseDestName,BUFSIZ + 1);
180 pBaseDestName = baseDestName; /* available globally */
Eric Andersenf4c02262000-07-13 18:42:58 +0000181
Matt Kraaie714bce2000-07-12 00:53:06 +0000182 if (*applet_name == 'c' && *(applet_name + 1) == 'p')
Erik Andersene49d5ec2000-02-08 19:58:47 +0000183 dz_i = is_cp;
184 else
185 dz_i = is_mv;
186 if (argc < 3)
187 usage(cp_mv_usage[dz_i]);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000188
189 if (dz_i == is_cp) {
Erik Andersen59b9e872000-05-10 05:05:45 +0000190 recursiveFlag = preserveFlag = forceFlag = FALSE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000191 followLinks = TRUE;
Eric Andersenf4c02262000-07-13 18:42:58 +0000192 while ((c = getopt(argc, argv, "adpRf")) != EOF) {
193 switch (c) {
Pavel Roskinb3d235f2000-06-08 18:06:37 +0000194 case 'a':
195 followLinks = FALSE;
196 preserveFlag = TRUE;
197 recursiveFlag = TRUE;
198 break;
199 case 'd':
200 followLinks = FALSE;
201 break;
202 case 'p':
203 preserveFlag = TRUE;
204 break;
205 case 'R':
206 recursiveFlag = TRUE;
207 break;
208 case 'f':
209 forceFlag = TRUE;
210 break;
211 default:
212 usage(cp_mv_usage[is_cp]);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000213 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000214 }
Eric Andersenf4c02262000-07-13 18:42:58 +0000215 if ((argc - optind) < 2) {
Pavel Roskinb3d235f2000-06-08 18:06:37 +0000216 usage(cp_mv_usage[dz_i]);
Eric Andersen815e9042000-06-06 16:15:23 +0000217 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000218 } else { /* (dz_i == is_mv) */
Eric Andersen2da90d32000-09-23 20:55:59 +0000219 /* Initialize optind to 1, since in libc5 optind
220 * is not initialized until getopt() is called
221 * (or until sneaky programmers force it...). */
222 optind = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000223 recursiveFlag = preserveFlag = TRUE;
224 followLinks = FALSE;
225 }
Eric Andersen815e9042000-06-06 16:15:23 +0000226
Erik Andersene49d5ec2000-02-08 19:58:47 +0000227
Erik Andersen4f3f7572000-04-28 00:18:56 +0000228 if (strlen(argv[argc - 1]) > BUFSIZ) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000229 error_msg(name_too_long);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000230 goto exit_false;
231 }
232 strcpy(baseDestName, argv[argc - 1]);
233 baseDestLen = strlen(baseDestName);
234 if (baseDestLen == 0)
235 goto exit_false;
236
Mark Whitleyf57c9442000-12-07 19:56:48 +0000237 destDirFlag = is_directory(baseDestName, TRUE, &destStatBuf);
Matt Kraaiadbf0182000-08-06 15:36:50 +0000238 if (argc - optind > 2 && destDirFlag == FALSE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000239 error_msg(not_a_directory, baseDestName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000240 goto exit_false;
241 }
242
Eric Andersenf4c02262000-07-13 18:42:58 +0000243 for (i = optind; i < (argc-1); i++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000244 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
Eric Andersenf4c02262000-07-13 18:42:58 +0000248 baseSrcName=argv[i];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000249
Erik Andersen4f3f7572000-04-28 00:18:56 +0000250 if ((srcLen = strlen(baseSrcName)) > BUFSIZ)
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
Mark Whitleyf57c9442000-12-07 19:56:48 +0000255 srcDirFlag = is_directory(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
Erik Andersen4f3f7572000-04-28 00:18:56 +0000264 if ((pushd = getcwd(NULL, BUFSIZ + 1)) == NULL) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000265 perror_msg("getcwd()");
Erik Andersen029011b2000-03-04 21:19:32 +0000266 continue;
267 }
268 if (chdir(baseDestName) < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000269 perror_msg("chdir(%s)", baseSrcName);
Erik Andersen029011b2000-03-04 21:19:32 +0000270 continue;
271 }
Erik Andersen4f3f7572000-04-28 00:18:56 +0000272 if ((d = getcwd(NULL, BUFSIZ + 1)) == NULL) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000273 perror_msg("getcwd()");
Erik Andersen029011b2000-03-04 21:19:32 +0000274 continue;
275 }
276 while (!state && *d != '\0') {
277 if (stat(d, &sb) < 0) { /* stat not lstat - always dereference targets */
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000278 perror_msg("stat(%s)", d);
Erik Andersen029011b2000-03-04 21:19:32 +0000279 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)) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000284 error_msg("Cannot %s `%s' into a subdirectory of itself, "
Matt Kraaid537a952000-07-14 01:51:25 +0000285 "`%s/%s'\n", applet_name, baseSrcName,
286 baseDestName, baseSrcName);
Erik Andersen029011b2000-03-04 21:19:32 +0000287 state = -1;
288 continue;
289 }
290 if ((p = strrchr(d, '/')) != NULL) {
291 *p = '\0';
292 }
293 }
294 if (chdir(pushd) < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000295 perror_msg("chdir(%s)", pushd);
Erik Andersen029011b2000-03-04 21:19:32 +0000296 free(pushd);
297 free(d);
298 continue;
299 }
300 free(pushd);
301 free(d);
302 if (state < 0)
303 continue;
304 else
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000305 fill_baseDest_buf(baseDestName, &baseDestLen);
Erik Andersenfac10d72000-02-07 05:29:42 +0000306 }
Erik Andersen029011b2000-03-04 21:19:32 +0000307 status = setjmp(catch);
308 if (status == 0) {
309 mv_Action_first_time = 1;
Mark Whitleyf57c9442000-12-07 19:56:48 +0000310 if (recursive_action(baseSrcName,
Erik Andersen029011b2000-03-04 21:19:32 +0000311 recursiveFlag, followLinks, FALSE,
Erik Andersen3364d782000-03-28 00:58:14 +0000312 cp_mv_Action, cp_mv_Action, NULL) == FALSE) goto exit_false;
Erik Andersen029011b2000-03-04 21:19:32 +0000313 if (dz_i == is_mv &&
Mark Whitleyf57c9442000-12-07 19:56:48 +0000314 recursive_action(baseSrcName,
Erik Andersen029011b2000-03-04 21:19:32 +0000315 recursiveFlag, followLinks, TRUE,
Erik Andersen3364d782000-03-28 00:58:14 +0000316 rm_Action, rm_Action, NULL) == FALSE) goto exit_false;
Erik Andersen029011b2000-03-04 21:19:32 +0000317 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000318 if (flags_memo)
319 *(baseDestName + baseDestLen) = '\0';
Erik Andersenfac10d72000-02-07 05:29:42 +0000320 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000321 return EXIT_SUCCESS;
Erik Andersen029011b2000-03-04 21:19:32 +0000322 exit_false:
Matt Kraai3e856ce2000-12-01 02:55:13 +0000323 return EXIT_FAILURE;
Erik Andersenfac10d72000-02-07 05:29:42 +0000324}
325
Erik Andersen029011b2000-03-04 21:19:32 +0000326/*
327Local Variables:
328c-file-style: "linux"
329c-basic-offset: 4
330tab-width: 4
331End:
332*/