blob: 55483505f550bccf6adeb43b5e52ba4d2584fbb6 [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
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>
Erik Andersenfac10d72000-02-07 05:29:42 +000045
Mark Whitley59ab0252001-01-23 22:30:04 +000046static const int is_cp = 0;
47static const int is_mv = 1;
Erik Andersen029011b2000-03-04 21:19:32 +000048static int dz_i; /* index into cp_mv_usage */
Matt Kraai3bd8bd82000-07-14 23:28:47 +000049
Erik Andersene49d5ec2000-02-08 19:58:47 +000050static const char *cp_mv_usage[] = /* .rodata */
Erik Andersenfac10d72000-02-07 05:29:42 +000051{
Matt Kraai3bd8bd82000-07-14 23:28:47 +000052 cp_usage,
53 mv_usage
Erik Andersenfac10d72000-02-07 05:29:42 +000054};
55
Erik Andersen029011b2000-03-04 21:19:32 +000056static int recursiveFlag;
57static int followLinks;
58static int preserveFlag;
Erik Andersen59b9e872000-05-10 05:05:45 +000059static int forceFlag;
Erik Andersen029011b2000-03-04 21:19:32 +000060
61static const char *baseSrcName;
62static int srcDirFlag;
63static struct stat srcStatBuf;
64
Mark Whitley59ab0252001-01-23 22:30:04 +000065static char *pBaseDestName;
Erik Andersen029011b2000-03-04 21:19:32 +000066static size_t baseDestLen;
67static int destDirFlag;
68static struct stat destStatBuf;
69
70static jmp_buf catch;
71static volatile int mv_Action_first_time;
72
73static void name_too_long__exit (void) __attribute__((noreturn));
74
75static
76void name_too_long__exit (void)
77{
Mark Whitleyf57c9442000-12-07 19:56:48 +000078 error_msg_and_die(name_too_long);
Erik Andersen029011b2000-03-04 21:19:32 +000079}
80
81static void
82fill_baseDest_buf(char *_buf, size_t * _buflen) {
83 const char *srcBasename;
84 if ((srcBasename = strrchr(baseSrcName, '/')) == NULL) {
85 srcBasename = baseSrcName;
86 if (_buf[*_buflen - 1] != '/') {
Erik Andersen4f3f7572000-04-28 00:18:56 +000087 if (++(*_buflen) > BUFSIZ)
Erik Andersen029011b2000-03-04 21:19:32 +000088 name_too_long__exit();
89 strcat(_buf, "/");
90 }
91 }
Erik Andersen4f3f7572000-04-28 00:18:56 +000092 if (*_buflen + strlen(srcBasename) > BUFSIZ)
Erik Andersen029011b2000-03-04 21:19:32 +000093 name_too_long__exit();
94 strcat(_buf, srcBasename);
95 return;
96
97}
98
99static int
Erik Andersen3364d782000-03-28 00:58:14 +0000100cp_mv_Action(const char *fileName, struct stat *statbuf, void* junk)
Erik Andersen029011b2000-03-04 21:19:32 +0000101{
Erik Andersen4f3f7572000-04-28 00:18:56 +0000102 char destName[BUFSIZ + 1];
Erik Andersen029011b2000-03-04 21:19:32 +0000103 size_t destLen;
104 const char *srcBasename;
105 char *name;
106
Mark Whitley59ab0252001-01-23 22:30:04 +0000107 strcpy(destName, pBaseDestName);
Erik Andersen029011b2000-03-04 21:19:32 +0000108 destLen = strlen(destName);
109
110 if (srcDirFlag == TRUE) {
111 if (recursiveFlag == FALSE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000112 error_msg(omitting_directory, baseSrcName);
Erik Andersen029011b2000-03-04 21:19:32 +0000113 return TRUE;
114 }
115 srcBasename = (strstr(fileName, baseSrcName)
116 + strlen(baseSrcName));
117
Erik Andersen4f3f7572000-04-28 00:18:56 +0000118 if (destLen + strlen(srcBasename) > BUFSIZ) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000119 error_msg(name_too_long);
Erik Andersen029011b2000-03-04 21:19:32 +0000120 return FALSE;
121 }
122 strcat(destName, srcBasename);
123 }
124 else if (destDirFlag == TRUE) {
125 fill_baseDest_buf(&destName[0], &destLen);
126 }
127 else {
128 srcBasename = baseSrcName;
129 }
130 if (mv_Action_first_time && (dz_i == is_mv)) {
131 mv_Action_first_time = errno = 0;
132 if (rename(fileName, destName) < 0 && errno != EXDEV) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000133 perror_msg("rename(%s, %s)", fileName, destName);
Erik Andersen029011b2000-03-04 21:19:32 +0000134 goto do_copyFile; /* Try anyway... */
135 }
136 else if (errno == EXDEV)
137 goto do_copyFile;
138 else
139 longjmp(catch, 1); /* succeeded with rename() */
140 }
141 do_copyFile:
142 if (preserveFlag == TRUE && statbuf->st_nlink > 1) {
143 if (is_in_ino_dev_hashtable(statbuf, &name)) {
144 if (link(name, destName) < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000145 perror_msg("link(%s, %s)", name, destName);
Erik Andersen029011b2000-03-04 21:19:32 +0000146 return FALSE;
147 }
148 return TRUE;
149 }
150 else {
151 add_to_ino_dev_hashtable(statbuf, destName);
152 }
153 }
Mark Whitleyf57c9442000-12-07 19:56:48 +0000154 return copy_file(fileName, destName, preserveFlag, followLinks, forceFlag);
Erik Andersen029011b2000-03-04 21:19:32 +0000155}
156
157static int
Erik Andersen3364d782000-03-28 00:58:14 +0000158rm_Action(const char *fileName, struct stat *statbuf, void* junk)
Erik Andersen029011b2000-03-04 21:19:32 +0000159{
160 int status = TRUE;
161
162 if (S_ISDIR(statbuf->st_mode)) {
163 if (rmdir(fileName) < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000164 perror_msg("rmdir(%s)", fileName);
Erik Andersen029011b2000-03-04 21:19:32 +0000165 status = FALSE;
166 }
167 } else if (unlink(fileName) < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000168 perror_msg("unlink(%s)", fileName);
Erik Andersen029011b2000-03-04 21:19:32 +0000169 status = FALSE;
170 }
171 return status;
172}
173
Erik Andersenfac10d72000-02-07 05:29:42 +0000174extern int cp_mv_main(int argc, char **argv)
175{
Eric Andersene7413a92000-07-14 06:19:41 +0000176 volatile int i;
Pavel Roskin93c0d9f2000-07-14 15:18:57 +0000177 int c;
Mark Whitley59ab0252001-01-23 22:30:04 +0000178 char baseDestName[BUFSIZ + 1]; /* not declared globally == less bss used */
179 pBaseDestName = baseDestName; /* but available globally */
Eric Andersenf4c02262000-07-13 18:42:58 +0000180
Matt Kraaie714bce2000-07-12 00:53:06 +0000181 if (*applet_name == 'c' && *(applet_name + 1) == 'p')
Erik Andersene49d5ec2000-02-08 19:58:47 +0000182 dz_i = is_cp;
183 else
184 dz_i = is_mv;
185 if (argc < 3)
186 usage(cp_mv_usage[dz_i]);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000187
188 if (dz_i == is_cp) {
Erik Andersen59b9e872000-05-10 05:05:45 +0000189 recursiveFlag = preserveFlag = forceFlag = FALSE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000190 followLinks = TRUE;
Eric Andersenf4c02262000-07-13 18:42:58 +0000191 while ((c = getopt(argc, argv, "adpRf")) != EOF) {
192 switch (c) {
Pavel Roskinb3d235f2000-06-08 18:06:37 +0000193 case 'a':
194 followLinks = FALSE;
195 preserveFlag = TRUE;
196 recursiveFlag = TRUE;
197 break;
198 case 'd':
199 followLinks = FALSE;
200 break;
201 case 'p':
202 preserveFlag = TRUE;
203 break;
204 case 'R':
205 recursiveFlag = TRUE;
206 break;
207 case 'f':
208 forceFlag = TRUE;
209 break;
210 default:
211 usage(cp_mv_usage[is_cp]);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000212 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000213 }
Eric Andersenf4c02262000-07-13 18:42:58 +0000214 if ((argc - optind) < 2) {
Pavel Roskinb3d235f2000-06-08 18:06:37 +0000215 usage(cp_mv_usage[dz_i]);
Eric Andersen815e9042000-06-06 16:15:23 +0000216 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000217 } else { /* (dz_i == is_mv) */
Eric Andersen2da90d32000-09-23 20:55:59 +0000218 /* Initialize optind to 1, since in libc5 optind
219 * is not initialized until getopt() is called
220 * (or until sneaky programmers force it...). */
221 optind = 1;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000222 recursiveFlag = preserveFlag = TRUE;
223 followLinks = FALSE;
224 }
Eric Andersen815e9042000-06-06 16:15:23 +0000225
Erik Andersene49d5ec2000-02-08 19:58:47 +0000226
Erik Andersen4f3f7572000-04-28 00:18:56 +0000227 if (strlen(argv[argc - 1]) > BUFSIZ) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000228 error_msg(name_too_long);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000229 goto exit_false;
230 }
231 strcpy(baseDestName, argv[argc - 1]);
232 baseDestLen = strlen(baseDestName);
233 if (baseDestLen == 0)
234 goto exit_false;
235
Mark Whitleyf57c9442000-12-07 19:56:48 +0000236 destDirFlag = is_directory(baseDestName, TRUE, &destStatBuf);
Matt Kraaiadbf0182000-08-06 15:36:50 +0000237 if (argc - optind > 2 && destDirFlag == FALSE) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000238 error_msg(not_a_directory, baseDestName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000239 goto exit_false;
240 }
241
Eric Andersenf4c02262000-07-13 18:42:58 +0000242 for (i = optind; i < (argc-1); i++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000243 size_t srcLen;
Erik Andersen029011b2000-03-04 21:19:32 +0000244 volatile int flags_memo;
245 int status;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000246
Eric Andersenf4c02262000-07-13 18:42:58 +0000247 baseSrcName=argv[i];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000248
Erik Andersen4f3f7572000-04-28 00:18:56 +0000249 if ((srcLen = strlen(baseSrcName)) > BUFSIZ)
Erik Andersen029011b2000-03-04 21:19:32 +0000250 name_too_long__exit();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000251
Erik Andersen029011b2000-03-04 21:19:32 +0000252 if (srcLen == 0) continue; /* "" */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000253
Mark Whitleyf57c9442000-12-07 19:56:48 +0000254 srcDirFlag = is_directory(baseSrcName, followLinks, &srcStatBuf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000255
256 if ((flags_memo = (recursiveFlag == TRUE &&
257 srcDirFlag == TRUE && destDirFlag == TRUE))) {
Erik Andersen029011b2000-03-04 21:19:32 +0000258
259 struct stat sb;
260 int state = 0;
261 char *pushd, *d, *p;
262
Erik Andersen4f3f7572000-04-28 00:18:56 +0000263 if ((pushd = getcwd(NULL, BUFSIZ + 1)) == NULL) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000264 perror_msg("getcwd()");
Erik Andersen029011b2000-03-04 21:19:32 +0000265 continue;
266 }
267 if (chdir(baseDestName) < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000268 perror_msg("chdir(%s)", baseSrcName);
Erik Andersen029011b2000-03-04 21:19:32 +0000269 continue;
270 }
Erik Andersen4f3f7572000-04-28 00:18:56 +0000271 if ((d = getcwd(NULL, BUFSIZ + 1)) == NULL) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000272 perror_msg("getcwd()");
Erik Andersen029011b2000-03-04 21:19:32 +0000273 continue;
274 }
275 while (!state && *d != '\0') {
276 if (stat(d, &sb) < 0) { /* stat not lstat - always dereference targets */
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000277 perror_msg("stat(%s)", d);
Erik Andersen029011b2000-03-04 21:19:32 +0000278 state = -1;
279 continue;
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000280 }
Erik Andersen029011b2000-03-04 21:19:32 +0000281 if ((sb.st_ino == srcStatBuf.st_ino) &&
282 (sb.st_dev == srcStatBuf.st_dev)) {
Mark Whitleyf57c9442000-12-07 19:56:48 +0000283 error_msg("Cannot %s `%s' into a subdirectory of itself, "
Matt Kraaid537a952000-07-14 01:51:25 +0000284 "`%s/%s'\n", applet_name, baseSrcName,
285 baseDestName, baseSrcName);
Erik Andersen029011b2000-03-04 21:19:32 +0000286 state = -1;
287 continue;
288 }
289 if ((p = strrchr(d, '/')) != NULL) {
290 *p = '\0';
291 }
292 }
293 if (chdir(pushd) < 0) {
Matt Kraai1fa1ade2000-12-18 03:57:16 +0000294 perror_msg("chdir(%s)", pushd);
Erik Andersen029011b2000-03-04 21:19:32 +0000295 free(pushd);
296 free(d);
297 continue;
298 }
299 free(pushd);
300 free(d);
301 if (state < 0)
302 continue;
303 else
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000304 fill_baseDest_buf(baseDestName, &baseDestLen);
Erik Andersenfac10d72000-02-07 05:29:42 +0000305 }
Erik Andersen029011b2000-03-04 21:19:32 +0000306 status = setjmp(catch);
307 if (status == 0) {
308 mv_Action_first_time = 1;
Mark Whitleyf57c9442000-12-07 19:56:48 +0000309 if (recursive_action(baseSrcName,
Erik Andersen029011b2000-03-04 21:19:32 +0000310 recursiveFlag, followLinks, FALSE,
Erik Andersen3364d782000-03-28 00:58:14 +0000311 cp_mv_Action, cp_mv_Action, NULL) == FALSE) goto exit_false;
Erik Andersen029011b2000-03-04 21:19:32 +0000312 if (dz_i == is_mv &&
Mark Whitleyf57c9442000-12-07 19:56:48 +0000313 recursive_action(baseSrcName,
Erik Andersen029011b2000-03-04 21:19:32 +0000314 recursiveFlag, followLinks, TRUE,
Erik Andersen3364d782000-03-28 00:58:14 +0000315 rm_Action, rm_Action, NULL) == FALSE) goto exit_false;
Erik Andersen029011b2000-03-04 21:19:32 +0000316 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000317 if (flags_memo)
318 *(baseDestName + baseDestLen) = '\0';
Erik Andersenfac10d72000-02-07 05:29:42 +0000319 }
Matt Kraai3e856ce2000-12-01 02:55:13 +0000320 return EXIT_SUCCESS;
Erik Andersen029011b2000-03-04 21:19:32 +0000321 exit_false:
Matt Kraai3e856ce2000-12-01 02:55:13 +0000322 return EXIT_FAILURE;
Erik Andersenfac10d72000-02-07 05:29:42 +0000323}
324
Erik Andersen029011b2000-03-04 21:19:32 +0000325/*
326Local Variables:
327c-file-style: "linux"
328c-basic-offset: 4
329tab-width: 4
330End:
331*/