2000-02-09 01:28:47 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2000-02-07 10:59:42 +05:30
|
|
|
/*
|
|
|
|
* Mini `cp' and `mv' implementation for BusyBox.
|
|
|
|
*
|
|
|
|
*
|
2001-01-27 15:03:39 +05:30
|
|
|
* Copyright (C) 1999,2000,2001 by Lineo, inc.
|
2000-02-07 10:59:42 +05:30
|
|
|
* Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
|
|
|
|
*
|
|
|
|
* Copyright (C) 2000 by BitterSweet Enterprises, LLC. (GPL)
|
|
|
|
* Extensively modified and rewritten by Karl M. Hegbloom <karlheg@debian.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2000-09-26 03:15:58 +05:30
|
|
|
#include "busybox.h"
|
2000-02-07 10:59:42 +05:30
|
|
|
#define BB_DECLARE_EXTERN
|
|
|
|
#define bb_need_name_too_long
|
|
|
|
#define bb_need_omitting_directory
|
|
|
|
#define bb_need_not_a_directory
|
|
|
|
#include "messages.c"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <utime.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <sys/param.h>
|
2000-04-13 06:48:56 +05:30
|
|
|
#include <setjmp.h>
|
2000-03-05 02:49:32 +05:30
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
2000-07-14 00:12:58 +05:30
|
|
|
#include <getopt.h>
|
2001-01-27 13:54:39 +05:30
|
|
|
#include <stdlib.h>
|
2000-02-07 10:59:42 +05:30
|
|
|
|
2001-01-24 04:00:04 +05:30
|
|
|
static const int is_cp = 0;
|
|
|
|
static const int is_mv = 1;
|
2000-03-05 02:49:32 +05:30
|
|
|
static int dz_i; /* index into cp_mv_usage */
|
2000-07-15 04:58:47 +05:30
|
|
|
|
|
|
|
static const char *cp_mv_usage[] = /* .rodata */
|
|
|
|
{
|
|
|
|
cp_usage,
|
|
|
|
mv_usage
|
2000-02-07 10:59:42 +05:30
|
|
|
};
|
|
|
|
|
2000-03-05 02:49:32 +05:30
|
|
|
static int recursiveFlag;
|
|
|
|
static int followLinks;
|
|
|
|
static int preserveFlag;
|
2000-05-10 10:35:45 +05:30
|
|
|
static int forceFlag;
|
2000-03-05 02:49:32 +05:30
|
|
|
|
|
|
|
static const char *baseSrcName;
|
|
|
|
static int srcDirFlag;
|
|
|
|
static struct stat srcStatBuf;
|
|
|
|
|
2001-01-24 04:00:04 +05:30
|
|
|
static char *pBaseDestName;
|
2000-03-05 02:49:32 +05:30
|
|
|
static size_t baseDestLen;
|
|
|
|
static int destDirFlag;
|
|
|
|
static struct stat destStatBuf;
|
|
|
|
|
|
|
|
static jmp_buf catch;
|
|
|
|
static volatile int mv_Action_first_time;
|
|
|
|
|
|
|
|
static void name_too_long__exit (void) __attribute__((noreturn));
|
|
|
|
|
|
|
|
static
|
|
|
|
void name_too_long__exit (void)
|
2000-02-07 10:59:42 +05:30
|
|
|
{
|
2000-12-08 01:26:48 +05:30
|
|
|
error_msg_and_die(name_too_long);
|
2000-03-05 02:49:32 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
fill_baseDest_buf(char *_buf, size_t * _buflen) {
|
|
|
|
const char *srcBasename;
|
|
|
|
if ((srcBasename = strrchr(baseSrcName, '/')) == NULL) {
|
|
|
|
srcBasename = baseSrcName;
|
|
|
|
if (_buf[*_buflen - 1] != '/') {
|
2000-04-28 05:48:56 +05:30
|
|
|
if (++(*_buflen) > BUFSIZ)
|
2000-03-05 02:49:32 +05:30
|
|
|
name_too_long__exit();
|
|
|
|
strcat(_buf, "/");
|
2000-02-09 01:28:47 +05:30
|
|
|
}
|
|
|
|
}
|
2000-04-28 05:48:56 +05:30
|
|
|
if (*_buflen + strlen(srcBasename) > BUFSIZ)
|
2000-03-05 02:49:32 +05:30
|
|
|
name_too_long__exit();
|
|
|
|
strcat(_buf, srcBasename);
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
2000-02-07 10:59:42 +05:30
|
|
|
|
2000-03-05 02:49:32 +05:30
|
|
|
static int
|
2000-03-28 06:28:14 +05:30
|
|
|
cp_mv_Action(const char *fileName, struct stat *statbuf, void* junk)
|
2000-03-05 02:49:32 +05:30
|
|
|
{
|
2000-04-28 05:48:56 +05:30
|
|
|
char destName[BUFSIZ + 1];
|
2000-03-05 02:49:32 +05:30
|
|
|
size_t destLen;
|
|
|
|
const char *srcBasename;
|
|
|
|
char *name;
|
2000-02-09 01:28:47 +05:30
|
|
|
|
2001-01-24 04:00:04 +05:30
|
|
|
strcpy(destName, pBaseDestName);
|
2000-03-05 02:49:32 +05:30
|
|
|
destLen = strlen(destName);
|
2000-02-09 01:28:47 +05:30
|
|
|
|
2000-03-05 02:49:32 +05:30
|
|
|
if (srcDirFlag == TRUE) {
|
|
|
|
if (recursiveFlag == FALSE) {
|
2000-12-08 01:26:48 +05:30
|
|
|
error_msg(omitting_directory, baseSrcName);
|
2000-03-05 02:49:32 +05:30
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
srcBasename = (strstr(fileName, baseSrcName)
|
|
|
|
+ strlen(baseSrcName));
|
2000-02-09 01:28:47 +05:30
|
|
|
|
2000-04-28 05:48:56 +05:30
|
|
|
if (destLen + strlen(srcBasename) > BUFSIZ) {
|
2000-12-08 01:26:48 +05:30
|
|
|
error_msg(name_too_long);
|
2000-03-05 02:49:32 +05:30
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
strcat(destName, srcBasename);
|
|
|
|
}
|
|
|
|
else if (destDirFlag == TRUE) {
|
|
|
|
fill_baseDest_buf(&destName[0], &destLen);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
srcBasename = baseSrcName;
|
|
|
|
}
|
|
|
|
if (mv_Action_first_time && (dz_i == is_mv)) {
|
|
|
|
mv_Action_first_time = errno = 0;
|
|
|
|
if (rename(fileName, destName) < 0 && errno != EXDEV) {
|
2000-12-18 09:27:16 +05:30
|
|
|
perror_msg("rename(%s, %s)", fileName, destName);
|
2000-03-05 02:49:32 +05:30
|
|
|
goto do_copyFile; /* Try anyway... */
|
|
|
|
}
|
|
|
|
else if (errno == EXDEV)
|
|
|
|
goto do_copyFile;
|
|
|
|
else
|
|
|
|
longjmp(catch, 1); /* succeeded with rename() */
|
|
|
|
}
|
|
|
|
do_copyFile:
|
|
|
|
if (preserveFlag == TRUE && statbuf->st_nlink > 1) {
|
|
|
|
if (is_in_ino_dev_hashtable(statbuf, &name)) {
|
|
|
|
if (link(name, destName) < 0) {
|
2000-12-18 09:27:16 +05:30
|
|
|
perror_msg("link(%s, %s)", name, destName);
|
2000-02-12 03:25:04 +05:30
|
|
|
return FALSE;
|
2000-02-09 01:28:47 +05:30
|
|
|
}
|
2000-03-05 02:49:32 +05:30
|
|
|
return TRUE;
|
2000-02-09 01:28:47 +05:30
|
|
|
}
|
2000-03-05 02:49:32 +05:30
|
|
|
else {
|
|
|
|
add_to_ino_dev_hashtable(statbuf, destName);
|
2000-02-07 10:59:42 +05:30
|
|
|
}
|
|
|
|
}
|
2000-12-08 01:26:48 +05:30
|
|
|
return copy_file(fileName, destName, preserveFlag, followLinks, forceFlag);
|
2000-03-05 02:49:32 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2000-03-28 06:28:14 +05:30
|
|
|
rm_Action(const char *fileName, struct stat *statbuf, void* junk)
|
2000-03-05 02:49:32 +05:30
|
|
|
{
|
|
|
|
int status = TRUE;
|
2000-02-07 10:59:42 +05:30
|
|
|
|
2000-03-05 02:49:32 +05:30
|
|
|
if (S_ISDIR(statbuf->st_mode)) {
|
2000-02-09 01:28:47 +05:30
|
|
|
if (rmdir(fileName) < 0) {
|
2000-12-18 09:27:16 +05:30
|
|
|
perror_msg("rmdir(%s)", fileName);
|
2000-03-05 02:49:32 +05:30
|
|
|
status = FALSE;
|
2000-02-07 10:59:42 +05:30
|
|
|
}
|
2000-03-05 02:49:32 +05:30
|
|
|
} else if (unlink(fileName) < 0) {
|
2000-12-18 09:27:16 +05:30
|
|
|
perror_msg("unlink(%s)", fileName);
|
2000-03-05 02:49:32 +05:30
|
|
|
status = FALSE;
|
2000-02-07 10:59:42 +05:30
|
|
|
}
|
2000-03-05 02:49:32 +05:30
|
|
|
return status;
|
|
|
|
}
|
2000-02-07 10:59:42 +05:30
|
|
|
|
2000-03-05 02:49:32 +05:30
|
|
|
extern int cp_mv_main(int argc, char **argv)
|
|
|
|
{
|
2000-07-14 11:49:41 +05:30
|
|
|
volatile int i;
|
2000-07-14 20:48:57 +05:30
|
|
|
int c;
|
2001-01-26 05:19:09 +05:30
|
|
|
RESERVE_BB_BUFFER(baseDestName,BUFSIZ + 1);
|
|
|
|
pBaseDestName = baseDestName; /* available globally */
|
2000-07-14 00:12:58 +05:30
|
|
|
|
2000-07-12 06:23:06 +05:30
|
|
|
if (*applet_name == 'c' && *(applet_name + 1) == 'p')
|
2000-02-09 01:28:47 +05:30
|
|
|
dz_i = is_cp;
|
|
|
|
else
|
|
|
|
dz_i = is_mv;
|
|
|
|
if (argc < 3)
|
|
|
|
usage(cp_mv_usage[dz_i]);
|
|
|
|
|
|
|
|
if (dz_i == is_cp) {
|
2000-05-10 10:35:45 +05:30
|
|
|
recursiveFlag = preserveFlag = forceFlag = FALSE;
|
2000-02-09 01:28:47 +05:30
|
|
|
followLinks = TRUE;
|
2000-07-14 00:12:58 +05:30
|
|
|
while ((c = getopt(argc, argv, "adpRf")) != EOF) {
|
|
|
|
switch (c) {
|
2000-06-08 23:36:37 +05:30
|
|
|
case 'a':
|
|
|
|
followLinks = FALSE;
|
|
|
|
preserveFlag = TRUE;
|
|
|
|
recursiveFlag = TRUE;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
followLinks = FALSE;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
preserveFlag = TRUE;
|
|
|
|
break;
|
|
|
|
case 'R':
|
|
|
|
recursiveFlag = TRUE;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
forceFlag = TRUE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage(cp_mv_usage[is_cp]);
|
2000-02-09 01:28:47 +05:30
|
|
|
}
|
2000-02-07 10:59:42 +05:30
|
|
|
}
|
2000-07-14 00:12:58 +05:30
|
|
|
if ((argc - optind) < 2) {
|
2000-06-08 23:36:37 +05:30
|
|
|
usage(cp_mv_usage[dz_i]);
|
2000-06-06 21:45:23 +05:30
|
|
|
}
|
2000-02-09 01:28:47 +05:30
|
|
|
} else { /* (dz_i == is_mv) */
|
2000-09-24 02:25:59 +05:30
|
|
|
/* Initialize optind to 1, since in libc5 optind
|
|
|
|
* is not initialized until getopt() is called
|
|
|
|
* (or until sneaky programmers force it...). */
|
|
|
|
optind = 1;
|
2000-02-09 01:28:47 +05:30
|
|
|
recursiveFlag = preserveFlag = TRUE;
|
|
|
|
followLinks = FALSE;
|
|
|
|
}
|
2000-06-06 21:45:23 +05:30
|
|
|
|
2000-02-07 10:59:42 +05:30
|
|
|
|
2000-04-28 05:48:56 +05:30
|
|
|
if (strlen(argv[argc - 1]) > BUFSIZ) {
|
2000-12-08 01:26:48 +05:30
|
|
|
error_msg(name_too_long);
|
2000-02-09 01:28:47 +05:30
|
|
|
goto exit_false;
|
|
|
|
}
|
|
|
|
strcpy(baseDestName, argv[argc - 1]);
|
|
|
|
baseDestLen = strlen(baseDestName);
|
|
|
|
if (baseDestLen == 0)
|
|
|
|
goto exit_false;
|
|
|
|
|
2000-12-08 01:26:48 +05:30
|
|
|
destDirFlag = is_directory(baseDestName, TRUE, &destStatBuf);
|
2000-08-06 21:06:50 +05:30
|
|
|
if (argc - optind > 2 && destDirFlag == FALSE) {
|
2000-12-08 01:26:48 +05:30
|
|
|
error_msg(not_a_directory, baseDestName);
|
2000-02-09 01:28:47 +05:30
|
|
|
goto exit_false;
|
|
|
|
}
|
2000-02-07 10:59:42 +05:30
|
|
|
|
2000-07-14 00:12:58 +05:30
|
|
|
for (i = optind; i < (argc-1); i++) {
|
2000-02-09 01:28:47 +05:30
|
|
|
size_t srcLen;
|
2000-03-05 02:49:32 +05:30
|
|
|
volatile int flags_memo;
|
|
|
|
int status;
|
2000-02-07 10:59:42 +05:30
|
|
|
|
2000-07-14 00:12:58 +05:30
|
|
|
baseSrcName=argv[i];
|
2000-02-07 10:59:42 +05:30
|
|
|
|
2000-04-28 05:48:56 +05:30
|
|
|
if ((srcLen = strlen(baseSrcName)) > BUFSIZ)
|
2000-03-05 02:49:32 +05:30
|
|
|
name_too_long__exit();
|
2000-02-07 10:59:42 +05:30
|
|
|
|
2000-03-05 02:49:32 +05:30
|
|
|
if (srcLen == 0) continue; /* "" */
|
2000-02-07 10:59:42 +05:30
|
|
|
|
2000-12-08 01:26:48 +05:30
|
|
|
srcDirFlag = is_directory(baseSrcName, followLinks, &srcStatBuf);
|
2000-02-07 10:59:42 +05:30
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
if ((flags_memo = (recursiveFlag == TRUE &&
|
|
|
|
srcDirFlag == TRUE && destDirFlag == TRUE))) {
|
2000-03-05 02:49:32 +05:30
|
|
|
|
|
|
|
struct stat sb;
|
|
|
|
int state = 0;
|
|
|
|
char *pushd, *d, *p;
|
|
|
|
|
2000-04-28 05:48:56 +05:30
|
|
|
if ((pushd = getcwd(NULL, BUFSIZ + 1)) == NULL) {
|
2000-12-18 09:27:16 +05:30
|
|
|
perror_msg("getcwd()");
|
2000-03-05 02:49:32 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (chdir(baseDestName) < 0) {
|
2000-12-18 09:27:16 +05:30
|
|
|
perror_msg("chdir(%s)", baseSrcName);
|
2000-03-05 02:49:32 +05:30
|
|
|
continue;
|
|
|
|
}
|
2000-04-28 05:48:56 +05:30
|
|
|
if ((d = getcwd(NULL, BUFSIZ + 1)) == NULL) {
|
2000-12-18 09:27:16 +05:30
|
|
|
perror_msg("getcwd()");
|
2000-03-05 02:49:32 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
while (!state && *d != '\0') {
|
|
|
|
if (stat(d, &sb) < 0) { /* stat not lstat - always dereference targets */
|
2000-12-18 09:27:16 +05:30
|
|
|
perror_msg("stat(%s)", d);
|
2000-03-05 02:49:32 +05:30
|
|
|
state = -1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((sb.st_ino == srcStatBuf.st_ino) &&
|
|
|
|
(sb.st_dev == srcStatBuf.st_dev)) {
|
2000-12-08 01:26:48 +05:30
|
|
|
error_msg("Cannot %s `%s' into a subdirectory of itself, "
|
2000-07-14 07:21:25 +05:30
|
|
|
"`%s/%s'\n", applet_name, baseSrcName,
|
|
|
|
baseDestName, baseSrcName);
|
2000-03-05 02:49:32 +05:30
|
|
|
state = -1;
|
|
|
|
continue;
|
2000-02-12 03:25:04 +05:30
|
|
|
}
|
2000-03-05 02:49:32 +05:30
|
|
|
if ((p = strrchr(d, '/')) != NULL) {
|
|
|
|
*p = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (chdir(pushd) < 0) {
|
2000-12-18 09:27:16 +05:30
|
|
|
perror_msg("chdir(%s)", pushd);
|
2000-03-05 02:49:32 +05:30
|
|
|
free(pushd);
|
|
|
|
free(d);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
free(pushd);
|
|
|
|
free(d);
|
|
|
|
if (state < 0)
|
|
|
|
continue;
|
|
|
|
else
|
2000-02-12 03:25:04 +05:30
|
|
|
fill_baseDest_buf(baseDestName, &baseDestLen);
|
2000-02-09 01:28:47 +05:30
|
|
|
}
|
2000-03-05 02:49:32 +05:30
|
|
|
status = setjmp(catch);
|
|
|
|
if (status == 0) {
|
|
|
|
mv_Action_first_time = 1;
|
2000-12-08 01:26:48 +05:30
|
|
|
if (recursive_action(baseSrcName,
|
2000-03-05 02:49:32 +05:30
|
|
|
recursiveFlag, followLinks, FALSE,
|
2000-03-28 06:28:14 +05:30
|
|
|
cp_mv_Action, cp_mv_Action, NULL) == FALSE) goto exit_false;
|
2000-03-05 02:49:32 +05:30
|
|
|
if (dz_i == is_mv &&
|
2000-12-08 01:26:48 +05:30
|
|
|
recursive_action(baseSrcName,
|
2000-03-05 02:49:32 +05:30
|
|
|
recursiveFlag, followLinks, TRUE,
|
2000-03-28 06:28:14 +05:30
|
|
|
rm_Action, rm_Action, NULL) == FALSE) goto exit_false;
|
2000-03-05 02:49:32 +05:30
|
|
|
}
|
2000-02-09 01:28:47 +05:30
|
|
|
if (flags_memo)
|
|
|
|
*(baseDestName + baseDestLen) = '\0';
|
2000-02-07 10:59:42 +05:30
|
|
|
}
|
2000-12-01 08:25:13 +05:30
|
|
|
return EXIT_SUCCESS;
|
2000-03-05 02:49:32 +05:30
|
|
|
exit_false:
|
2000-12-01 08:25:13 +05:30
|
|
|
return EXIT_FAILURE;
|
2000-02-07 10:59:42 +05:30
|
|
|
}
|
|
|
|
|
2000-03-05 02:49:32 +05:30
|
|
|
/*
|
|
|
|
Local Variables:
|
|
|
|
c-file-style: "linux"
|
|
|
|
c-basic-offset: 4
|
|
|
|
tab-width: 4
|
|
|
|
End:
|
|
|
|
*/
|