blob: 6550e1c39b73942c998ed173e073ad98ca7dd3e8 [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>
Eric Andersenf4c02262000-07-13 18:42:58 +000044#include <getopt.h>
Erik Andersenfac10d72000-02-07 05:29:42 +000045
46#define is_cp 0
47#define is_mv 1
Erik Andersen029011b2000-03-04 21:19:32 +000048static int dz_i; /* index into cp_mv_usage */
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"
Erik Andersen94f5e0b2000-05-01 19:10:52 +000059 "\t-f\tforce (implied; ignored) - always set\n"
Erik Andersen1d1d9502000-04-21 01:26:49 +000060 "\t-R\tCopies directories recursively\n"
61#endif
62 ,
Erik Andersene49d5ec2000-02-08 19:58:47 +000063 "mv SOURCE DEST\n"
Erik Andersen1d1d9502000-04-21 01:26:49 +000064 " or: mv SOURCE... DIRECTORY\n"
65#ifndef BB_FEATURE_TRIVIAL_HELP
66 "\nRename SOURCE to DEST, or move SOURCE(s) to DIRECTORY.\n"
67#endif
Erik Andersenfac10d72000-02-07 05:29:42 +000068};
69
Erik Andersen029011b2000-03-04 21:19:32 +000070static int recursiveFlag;
71static int followLinks;
72static int preserveFlag;
Erik Andersen59b9e872000-05-10 05:05:45 +000073static int forceFlag;
Erik Andersen029011b2000-03-04 21:19:32 +000074
75static const char *baseSrcName;
76static int srcDirFlag;
77static struct stat srcStatBuf;
78
Erik Andersen4f3f7572000-04-28 00:18:56 +000079static char baseDestName[BUFSIZ + 1];
Erik Andersen029011b2000-03-04 21:19:32 +000080static size_t baseDestLen;
81static int destDirFlag;
82static struct stat destStatBuf;
83
84static jmp_buf catch;
85static volatile int mv_Action_first_time;
86
87static void name_too_long__exit (void) __attribute__((noreturn));
88
89static
90void name_too_long__exit (void)
91{
Matt Kraaid537a952000-07-14 01:51:25 +000092 fatalError(name_too_long);
Erik Andersen029011b2000-03-04 21:19:32 +000093}
94
95static void
96fill_baseDest_buf(char *_buf, size_t * _buflen) {
97 const char *srcBasename;
98 if ((srcBasename = strrchr(baseSrcName, '/')) == NULL) {
99 srcBasename = baseSrcName;
100 if (_buf[*_buflen - 1] != '/') {
Erik Andersen4f3f7572000-04-28 00:18:56 +0000101 if (++(*_buflen) > BUFSIZ)
Erik Andersen029011b2000-03-04 21:19:32 +0000102 name_too_long__exit();
103 strcat(_buf, "/");
104 }
105 }
Erik Andersen4f3f7572000-04-28 00:18:56 +0000106 if (*_buflen + strlen(srcBasename) > BUFSIZ)
Erik Andersen029011b2000-03-04 21:19:32 +0000107 name_too_long__exit();
108 strcat(_buf, srcBasename);
109 return;
110
111}
112
113static int
Erik Andersen3364d782000-03-28 00:58:14 +0000114cp_mv_Action(const char *fileName, struct stat *statbuf, void* junk)
Erik Andersen029011b2000-03-04 21:19:32 +0000115{
Erik Andersen4f3f7572000-04-28 00:18:56 +0000116 char destName[BUFSIZ + 1];
Erik Andersen029011b2000-03-04 21:19:32 +0000117 size_t destLen;
118 const char *srcBasename;
119 char *name;
120
121 strcpy(destName, baseDestName);
122 destLen = strlen(destName);
123
124 if (srcDirFlag == TRUE) {
125 if (recursiveFlag == FALSE) {
Matt Kraaid537a952000-07-14 01:51:25 +0000126 errorMsg(omitting_directory, baseSrcName);
Erik Andersen029011b2000-03-04 21:19:32 +0000127 return TRUE;
128 }
129 srcBasename = (strstr(fileName, baseSrcName)
130 + strlen(baseSrcName));
131
Erik Andersen4f3f7572000-04-28 00:18:56 +0000132 if (destLen + strlen(srcBasename) > BUFSIZ) {
Matt Kraaid537a952000-07-14 01:51:25 +0000133 errorMsg(name_too_long);
Erik Andersen029011b2000-03-04 21:19:32 +0000134 return FALSE;
135 }
136 strcat(destName, srcBasename);
137 }
138 else if (destDirFlag == TRUE) {
139 fill_baseDest_buf(&destName[0], &destLen);
140 }
141 else {
142 srcBasename = baseSrcName;
143 }
144 if (mv_Action_first_time && (dz_i == is_mv)) {
145 mv_Action_first_time = errno = 0;
146 if (rename(fileName, destName) < 0 && errno != EXDEV) {
Matt Kraaid537a952000-07-14 01:51:25 +0000147 errorMsg("rename(%s, %s): %s\n", fileName, destName,
148 strerror(errno));
Erik Andersen029011b2000-03-04 21:19:32 +0000149 goto do_copyFile; /* Try anyway... */
150 }
151 else if (errno == EXDEV)
152 goto do_copyFile;
153 else
154 longjmp(catch, 1); /* succeeded with rename() */
155 }
156 do_copyFile:
157 if (preserveFlag == TRUE && statbuf->st_nlink > 1) {
158 if (is_in_ino_dev_hashtable(statbuf, &name)) {
159 if (link(name, destName) < 0) {
Matt Kraaid537a952000-07-14 01:51:25 +0000160 errorMsg("link(%s, %s): %s\n", name, destName, strerror(errno));
Erik Andersen029011b2000-03-04 21:19:32 +0000161 return FALSE;
162 }
163 return TRUE;
164 }
165 else {
166 add_to_ino_dev_hashtable(statbuf, destName);
167 }
168 }
Erik Andersen59b9e872000-05-10 05:05:45 +0000169 return copyFile(fileName, destName, preserveFlag, followLinks, forceFlag);
Erik Andersen029011b2000-03-04 21:19:32 +0000170}
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) {
Matt Kraaid537a952000-07-14 01:51:25 +0000179 errorMsg("rmdir(%s): %s\n", fileName, strerror(errno));
Erik Andersen029011b2000-03-04 21:19:32 +0000180 status = FALSE;
181 }
182 } else if (unlink(fileName) < 0) {
Matt Kraaid537a952000-07-14 01:51:25 +0000183 errorMsg("unlink(%s): %s\n", fileName, strerror(errno));
Erik Andersen029011b2000-03-04 21:19:32 +0000184 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{
Eric Andersenf4c02262000-07-13 18:42:58 +0000191 int i;
192 char c;
193
Matt Kraaie714bce2000-07-12 00:53:06 +0000194 if (*applet_name == 'c' && *(applet_name + 1) == 'p')
Erik Andersene49d5ec2000-02-08 19:58:47 +0000195 dz_i = is_cp;
196 else
197 dz_i = is_mv;
198 if (argc < 3)
199 usage(cp_mv_usage[dz_i]);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000200
201 if (dz_i == is_cp) {
Erik Andersen59b9e872000-05-10 05:05:45 +0000202 recursiveFlag = preserveFlag = forceFlag = FALSE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000203 followLinks = TRUE;
Eric Andersenf4c02262000-07-13 18:42:58 +0000204 while ((c = getopt(argc, argv, "adpRf")) != EOF) {
205 switch (c) {
Pavel Roskinb3d235f2000-06-08 18:06:37 +0000206 case 'a':
207 followLinks = FALSE;
208 preserveFlag = TRUE;
209 recursiveFlag = TRUE;
210 break;
211 case 'd':
212 followLinks = FALSE;
213 break;
214 case 'p':
215 preserveFlag = TRUE;
216 break;
217 case 'R':
218 recursiveFlag = TRUE;
219 break;
220 case 'f':
221 forceFlag = TRUE;
222 break;
223 default:
224 usage(cp_mv_usage[is_cp]);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000225 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000226 }
Eric Andersenf4c02262000-07-13 18:42:58 +0000227 if ((argc - optind) < 2) {
Pavel Roskinb3d235f2000-06-08 18:06:37 +0000228 usage(cp_mv_usage[dz_i]);
Eric Andersen815e9042000-06-06 16:15:23 +0000229 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000230 } else { /* (dz_i == is_mv) */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000231 recursiveFlag = preserveFlag = TRUE;
232 followLinks = FALSE;
233 }
Eric Andersen815e9042000-06-06 16:15:23 +0000234
Erik Andersene49d5ec2000-02-08 19:58:47 +0000235
Erik Andersen4f3f7572000-04-28 00:18:56 +0000236 if (strlen(argv[argc - 1]) > BUFSIZ) {
Matt Kraaid537a952000-07-14 01:51:25 +0000237 errorMsg(name_too_long);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000238 goto exit_false;
239 }
240 strcpy(baseDestName, argv[argc - 1]);
241 baseDestLen = strlen(baseDestName);
242 if (baseDestLen == 0)
243 goto exit_false;
244
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000245 destDirFlag = isDirectory(baseDestName, TRUE, &destStatBuf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000246 if ((argc > 3) && destDirFlag == FALSE) {
Matt Kraaid537a952000-07-14 01:51:25 +0000247 errorMsg(not_a_directory, baseDestName);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000248 goto exit_false;
249 }
250
Eric Andersenf4c02262000-07-13 18:42:58 +0000251 for (i = optind; i < (argc-1); i++) {
Erik Andersene49d5ec2000-02-08 19:58:47 +0000252 size_t srcLen;
Erik Andersen029011b2000-03-04 21:19:32 +0000253 volatile int flags_memo;
254 int status;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000255
Eric Andersenf4c02262000-07-13 18:42:58 +0000256 baseSrcName=argv[i];
Erik Andersene49d5ec2000-02-08 19:58:47 +0000257
Erik Andersen4f3f7572000-04-28 00:18:56 +0000258 if ((srcLen = strlen(baseSrcName)) > BUFSIZ)
Erik Andersen029011b2000-03-04 21:19:32 +0000259 name_too_long__exit();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000260
Erik Andersen029011b2000-03-04 21:19:32 +0000261 if (srcLen == 0) continue; /* "" */
Erik Andersene49d5ec2000-02-08 19:58:47 +0000262
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000263 srcDirFlag = isDirectory(baseSrcName, followLinks, &srcStatBuf);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000264
265 if ((flags_memo = (recursiveFlag == TRUE &&
266 srcDirFlag == TRUE && destDirFlag == TRUE))) {
Erik Andersen029011b2000-03-04 21:19:32 +0000267
268 struct stat sb;
269 int state = 0;
270 char *pushd, *d, *p;
271
Erik Andersen4f3f7572000-04-28 00:18:56 +0000272 if ((pushd = getcwd(NULL, BUFSIZ + 1)) == NULL) {
Matt Kraaid537a952000-07-14 01:51:25 +0000273 errorMsg("getcwd(): %s\n", strerror(errno));
Erik Andersen029011b2000-03-04 21:19:32 +0000274 continue;
275 }
276 if (chdir(baseDestName) < 0) {
Matt Kraaid537a952000-07-14 01:51:25 +0000277 errorMsg("chdir(%s): %s\n", baseSrcName, strerror(errno));
Erik Andersen029011b2000-03-04 21:19:32 +0000278 continue;
279 }
Erik Andersen4f3f7572000-04-28 00:18:56 +0000280 if ((d = getcwd(NULL, BUFSIZ + 1)) == NULL) {
Matt Kraaid537a952000-07-14 01:51:25 +0000281 errorMsg("getcwd(): %s\n", strerror(errno));
Erik Andersen029011b2000-03-04 21:19:32 +0000282 continue;
283 }
284 while (!state && *d != '\0') {
285 if (stat(d, &sb) < 0) { /* stat not lstat - always dereference targets */
Matt Kraaid537a952000-07-14 01:51:25 +0000286 errorMsg("stat(%s): %s\n", d, strerror(errno));
Erik Andersen029011b2000-03-04 21:19:32 +0000287 state = -1;
288 continue;
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000289 }
Erik Andersen029011b2000-03-04 21:19:32 +0000290 if ((sb.st_ino == srcStatBuf.st_ino) &&
291 (sb.st_dev == srcStatBuf.st_dev)) {
Matt Kraaid537a952000-07-14 01:51:25 +0000292 errorMsg("Cannot %s `%s' into a subdirectory of itself, "
293 "`%s/%s'\n", applet_name, baseSrcName,
294 baseDestName, baseSrcName);
Erik Andersen029011b2000-03-04 21:19:32 +0000295 state = -1;
296 continue;
297 }
298 if ((p = strrchr(d, '/')) != NULL) {
299 *p = '\0';
300 }
301 }
302 if (chdir(pushd) < 0) {
Matt Kraaid537a952000-07-14 01:51:25 +0000303 errorMsg("chdir(%s): %s\n", pushd, strerror(errno));
Erik Andersen029011b2000-03-04 21:19:32 +0000304 free(pushd);
305 free(d);
306 continue;
307 }
308 free(pushd);
309 free(d);
310 if (state < 0)
311 continue;
312 else
Erik Andersen9ffdaa62000-02-11 21:55:04 +0000313 fill_baseDest_buf(baseDestName, &baseDestLen);
Erik Andersenfac10d72000-02-07 05:29:42 +0000314 }
Erik Andersen029011b2000-03-04 21:19:32 +0000315 status = setjmp(catch);
316 if (status == 0) {
317 mv_Action_first_time = 1;
318 if (recursiveAction(baseSrcName,
319 recursiveFlag, followLinks, FALSE,
Erik Andersen3364d782000-03-28 00:58:14 +0000320 cp_mv_Action, cp_mv_Action, NULL) == FALSE) goto exit_false;
Erik Andersen029011b2000-03-04 21:19:32 +0000321 if (dz_i == is_mv &&
322 recursiveAction(baseSrcName,
323 recursiveFlag, followLinks, TRUE,
Erik Andersen3364d782000-03-28 00:58:14 +0000324 rm_Action, rm_Action, NULL) == FALSE) goto exit_false;
Erik Andersen029011b2000-03-04 21:19:32 +0000325 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000326 if (flags_memo)
327 *(baseDestName + baseDestLen) = '\0';
Erik Andersenfac10d72000-02-07 05:29:42 +0000328 }
Eric Andersenb6106152000-06-19 17:25:40 +0000329 return( TRUE);
Erik Andersen029011b2000-03-04 21:19:32 +0000330 exit_false:
Eric Andersenb6106152000-06-19 17:25:40 +0000331 return( FALSE);
Erik Andersenfac10d72000-02-07 05:29:42 +0000332}
333
Erik Andersen029011b2000-03-04 21:19:32 +0000334/*
335Local Variables:
336c-file-style: "linux"
337c-basic-offset: 4
338tab-width: 4
339End:
340*/