From 8810bdbba359eb2348cc371f12347dd7f41dda18 Mon Sep 17 00:00:00 2001 From: Matt Kraai Date: Tue, 24 Apr 2001 20:04:18 +0000 Subject: Rewrote rm. --- Makefile | 3 +- coreutils/mv.c | 17 +------ coreutils/rm.c | 124 +++++++++++++------------------------------------- include/libbb.h | 1 + libbb/libbb.h | 1 + libbb/remove_file.c | 127 ++++++++++++++++++++++++++++++++++++++++++++++++++++ mv.c | 17 +------ rm.c | 124 +++++++++++++------------------------------------- 8 files changed, 195 insertions(+), 219 deletions(-) create mode 100644 libbb/remove_file.c diff --git a/Makefile b/Makefile index 791357fa9..28cd3403a 100644 --- a/Makefile +++ b/Makefile @@ -247,7 +247,8 @@ parse_mode.c parse_number.c perror_msg.c perror_msg_and_die.c print_file.c \ process_escape_sequence.c read_package_field.c read_text_file_to_buffer.c \ recursive_action.c safe_read.c safe_strncpy.c seek_ared_file.c syscalls.c \ syslog_msg_with_name.c time_string.c trim.c untar.c unzip.c vdprintf.c \ -verror_msg.c vperror_msg.c wfopen.c xfuncs.c xgetcwd.c xregcomp.c interface.c +verror_msg.c vperror_msg.c wfopen.c xfuncs.c xgetcwd.c xregcomp.c interface.c \ +remove_file.c LIBBB_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC)) LIBBB_CFLAGS = -I$(LIBBB) diff --git a/coreutils/mv.c b/coreutils/mv.c index efc4ae6d8..b890abf6e 100644 --- a/coreutils/mv.c +++ b/coreutils/mv.c @@ -32,20 +32,6 @@ static int flags; -static int remove_file(const char *path, struct stat *statbuf, void *junk) -{ - if (unlink(path) < 0) - return FALSE; - return TRUE; -} - -static int remove_directory(const char *path, struct stat *statbuf, void *junk) -{ - if (rmdir(path) < 0) - return FALSE; - return TRUE; -} - static int manual_rename(const char *source, const char *dest) { struct stat source_stat; @@ -92,8 +78,7 @@ static int manual_rename(const char *source, const char *dest) FILEUTILS_PRESERVE_SYMLINKS) < 0) return -1; - if (!recursive_action(source, TRUE, FALSE, TRUE, remove_file, - remove_directory, NULL)) + if (remove_file(source, FILEUTILS_RECUR | FILEUTILS_FORCE) < 0) return -1; return 0; diff --git a/coreutils/rm.c b/coreutils/rm.c index ea7f86ce1..f8e16625c 100644 --- a/coreutils/rm.c +++ b/coreutils/rm.c @@ -3,12 +3,8 @@ * Mini rm implementation for busybox * * - * Copyright (C) 1999,2000,2001 by Lineo, inc. - * Written by Erik Andersen , + * Copyright (C) 2001 Matt Kraai * - * INTERACTIVE feature Copyright (C) 2001 by Alcove - * written by Christophe Boyanique - * for Ipanema Technologies * * 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 @@ -36,103 +32,45 @@ #include #include "busybox.h" -static int recursiveFlag = FALSE; -static int forceFlag = FALSE; -#ifdef BB_FEATURE_RM_INTERACTIVE - static int interactiveFlag = FALSE; -#endif -static const char *srcName; - - -static int fileAction(const char *fileName, struct stat *statbuf, void* junk) -{ -#ifdef BB_FEATURE_RM_INTERACTIVE - if (interactiveFlag == TRUE) { - printf("rm: remove `%s'? ", fileName); - if (ask_confirmation() == 0) - return (TRUE); - } -#endif - if (unlink(fileName) < 0) { - perror_msg("%s", fileName); - return (FALSE); - } - return (TRUE); -} - -static int dirAction(const char *fileName, struct stat *statbuf, void* junk) -{ - if (recursiveFlag == FALSE) { - errno = EISDIR; - perror_msg("%s", fileName); - return (FALSE); - } -#ifdef BB_FEATURE_RM_INTERACTIVE - if (interactiveFlag == TRUE) { - printf("rm: remove directory `%s'? ", fileName); - if (ask_confirmation() == 0) - return (TRUE); - } -#endif - if (rmdir(fileName) < 0) { - perror_msg("%s", fileName); - return (FALSE); - } - return (TRUE); -} - extern int rm_main(int argc, char **argv) { + int status = 0; int opt; - int status = EXIT_SUCCESS; - struct stat statbuf; - - - /* do normal option parsing */ - while ((opt = getopt(argc, argv, "Rrf-" -#ifdef BB_FEATURE_RM_INTERACTIVE -"i" -#endif -)) > 0) { - switch (opt) { - case 'R': - case 'r': - recursiveFlag = TRUE; - break; - case 'f': - forceFlag = TRUE; -#ifdef BB_FEATURE_RM_INTERACTIVE + int flags = 0; + int i; - interactiveFlag = FALSE; -#endif - break; -#ifdef BB_FEATURE_RM_INTERACTIVE - case 'i': - interactiveFlag = TRUE; - forceFlag = FALSE; - break; -#endif - default: - show_usage(); + while ((opt = getopt(argc, argv, "fiRr")) != -1) { + switch (opt) { + case 'f': + flags &= ~FILEUTILS_INTERACTIVE; + flags |= FILEUTILS_FORCE; + break; + case 'i': + flags &= ~FILEUTILS_FORCE; + flags |= FILEUTILS_INTERACTIVE; + break; + case 'R': + case 'r': + flags |= FILEUTILS_RECUR; + break; } } - - if (argc == optind && forceFlag == FALSE) { + + if (!(flags & FILEUTILS_FORCE) && optind == argc) show_usage(); - } - while (optind < argc) { - srcName = argv[optind]; - if (forceFlag == TRUE && lstat(srcName, &statbuf) != 0 - && errno == ENOENT) { - /* do not reports errors for non-existent files if -f, just skip them */ - } else { - if (recursive_action(srcName, recursiveFlag, FALSE, - TRUE, fileAction, dirAction, NULL) == FALSE) { - status = EXIT_FAILURE; - } + for (i = optind; i < argc; i++) { + char *base = get_last_path_component(argv[i]); + + if (strcmp(base, ".") == 0 || strcmp(base, "..") == 0) { + error_msg("cannot remove `.' or `..'"); + status = 1; + continue; } - optind++; + + if (remove_file(argv[i], flags) < 0) + status = 1; } + return status; } diff --git a/include/libbb.h b/include/libbb.h index 404d7076c..bbfffda6d 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -93,6 +93,7 @@ int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name); void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name); void reset_ino_dev_hashtable(void); +int remove_file(const char *path, int flags); int copy_file(const char *source, const char *dest, int flags); int copy_file_chunk(FILE *src_file, FILE *dst_file, unsigned long long chunksize); char *buildName(const char *dirName, const char *fileName); diff --git a/libbb/libbb.h b/libbb/libbb.h index 404d7076c..bbfffda6d 100644 --- a/libbb/libbb.h +++ b/libbb/libbb.h @@ -93,6 +93,7 @@ int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name); void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name); void reset_ino_dev_hashtable(void); +int remove_file(const char *path, int flags); int copy_file(const char *source, const char *dest, int flags); int copy_file_chunk(FILE *src_file, FILE *dst_file, unsigned long long chunksize); char *buildName(const char *dirName, const char *fileName); diff --git a/libbb/remove_file.c b/libbb/remove_file.c new file mode 100644 index 000000000..52b3211e6 --- /dev/null +++ b/libbb/remove_file.c @@ -0,0 +1,127 @@ +/* vi: set sw=4 ts=4: */ +/* + * Mini remove_file implementation for busybox + * + * + * Copyright (C) 2001 Matt Kraai + * + * + * 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 + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include "libbb.h" + +extern int remove_file(const char *path, int flags) +{ + struct stat path_stat; + int path_exists = 1; + + if (stat(path, &path_stat) < 0) { + if (errno != ENOENT) { + perror_msg("unable to stat `%s'", path); + return -1; + } + + path_exists = 0; + } + + if (!path_exists) { + if (!(flags & FILEUTILS_FORCE)) { + perror_msg("cannot remove `%s'", path); + return -1; + } + return 0; + } + + if (S_ISDIR(path_stat.st_mode)) { + DIR *dp; + struct dirent *d; + int status = 0; + + if (!(flags & FILEUTILS_RECUR)) { + error_msg("%s: is a directory", path); + return -1; + } + + if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 && + isatty(0)) || + (flags & FILEUTILS_INTERACTIVE)) { + fprintf(stderr, "%s: descend into directory `%s'? ", applet_name, + path); + if (!ask_confirmation()) + return 0; + } + + if ((dp = opendir(path)) == NULL) { + perror_msg("unable to open `%s'", path); + return -1; + } + + while ((d = readdir(dp)) != NULL) { + char *new_path; + + if (strcmp(d->d_name, ".") == 0 || + strcmp(d->d_name, "..") == 0) + continue; + + new_path = concat_path_file(path, d->d_name); + if (remove_file(new_path, flags) < 0) + status = -1; + free(new_path); + } + + if (closedir(dp) < 0) { + perror_msg("unable to close `%s'", path); + return -1; + } + + if (flags & FILEUTILS_INTERACTIVE) { + fprintf(stderr, "%s: remove directory `%s'? ", applet_name, path); + if (!ask_confirmation()) + return status; + } + + if (rmdir(path) < 0) { + perror_msg("unable to remove `%s'", path); + return -1; + } + + return status; + } else { + if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 && + isatty(0)) || + (flags & FILEUTILS_INTERACTIVE)) { + fprintf(stderr, "%s: remove `%s'? ", applet_name, path); + if (!ask_confirmation()) + return 0; + } + + if (unlink(path) < 0) { + perror_msg("unable to remove `%s'", path); + return -1; + } + + return 0; + } +} diff --git a/mv.c b/mv.c index efc4ae6d8..b890abf6e 100644 --- a/mv.c +++ b/mv.c @@ -32,20 +32,6 @@ static int flags; -static int remove_file(const char *path, struct stat *statbuf, void *junk) -{ - if (unlink(path) < 0) - return FALSE; - return TRUE; -} - -static int remove_directory(const char *path, struct stat *statbuf, void *junk) -{ - if (rmdir(path) < 0) - return FALSE; - return TRUE; -} - static int manual_rename(const char *source, const char *dest) { struct stat source_stat; @@ -92,8 +78,7 @@ static int manual_rename(const char *source, const char *dest) FILEUTILS_PRESERVE_SYMLINKS) < 0) return -1; - if (!recursive_action(source, TRUE, FALSE, TRUE, remove_file, - remove_directory, NULL)) + if (remove_file(source, FILEUTILS_RECUR | FILEUTILS_FORCE) < 0) return -1; return 0; diff --git a/rm.c b/rm.c index ea7f86ce1..f8e16625c 100644 --- a/rm.c +++ b/rm.c @@ -3,12 +3,8 @@ * Mini rm implementation for busybox * * - * Copyright (C) 1999,2000,2001 by Lineo, inc. - * Written by Erik Andersen , + * Copyright (C) 2001 Matt Kraai * - * INTERACTIVE feature Copyright (C) 2001 by Alcove - * written by Christophe Boyanique - * for Ipanema Technologies * * 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 @@ -36,103 +32,45 @@ #include #include "busybox.h" -static int recursiveFlag = FALSE; -static int forceFlag = FALSE; -#ifdef BB_FEATURE_RM_INTERACTIVE - static int interactiveFlag = FALSE; -#endif -static const char *srcName; - - -static int fileAction(const char *fileName, struct stat *statbuf, void* junk) -{ -#ifdef BB_FEATURE_RM_INTERACTIVE - if (interactiveFlag == TRUE) { - printf("rm: remove `%s'? ", fileName); - if (ask_confirmation() == 0) - return (TRUE); - } -#endif - if (unlink(fileName) < 0) { - perror_msg("%s", fileName); - return (FALSE); - } - return (TRUE); -} - -static int dirAction(const char *fileName, struct stat *statbuf, void* junk) -{ - if (recursiveFlag == FALSE) { - errno = EISDIR; - perror_msg("%s", fileName); - return (FALSE); - } -#ifdef BB_FEATURE_RM_INTERACTIVE - if (interactiveFlag == TRUE) { - printf("rm: remove directory `%s'? ", fileName); - if (ask_confirmation() == 0) - return (TRUE); - } -#endif - if (rmdir(fileName) < 0) { - perror_msg("%s", fileName); - return (FALSE); - } - return (TRUE); -} - extern int rm_main(int argc, char **argv) { + int status = 0; int opt; - int status = EXIT_SUCCESS; - struct stat statbuf; - - - /* do normal option parsing */ - while ((opt = getopt(argc, argv, "Rrf-" -#ifdef BB_FEATURE_RM_INTERACTIVE -"i" -#endif -)) > 0) { - switch (opt) { - case 'R': - case 'r': - recursiveFlag = TRUE; - break; - case 'f': - forceFlag = TRUE; -#ifdef BB_FEATURE_RM_INTERACTIVE + int flags = 0; + int i; - interactiveFlag = FALSE; -#endif - break; -#ifdef BB_FEATURE_RM_INTERACTIVE - case 'i': - interactiveFlag = TRUE; - forceFlag = FALSE; - break; -#endif - default: - show_usage(); + while ((opt = getopt(argc, argv, "fiRr")) != -1) { + switch (opt) { + case 'f': + flags &= ~FILEUTILS_INTERACTIVE; + flags |= FILEUTILS_FORCE; + break; + case 'i': + flags &= ~FILEUTILS_FORCE; + flags |= FILEUTILS_INTERACTIVE; + break; + case 'R': + case 'r': + flags |= FILEUTILS_RECUR; + break; } } - - if (argc == optind && forceFlag == FALSE) { + + if (!(flags & FILEUTILS_FORCE) && optind == argc) show_usage(); - } - while (optind < argc) { - srcName = argv[optind]; - if (forceFlag == TRUE && lstat(srcName, &statbuf) != 0 - && errno == ENOENT) { - /* do not reports errors for non-existent files if -f, just skip them */ - } else { - if (recursive_action(srcName, recursiveFlag, FALSE, - TRUE, fileAction, dirAction, NULL) == FALSE) { - status = EXIT_FAILURE; - } + for (i = optind; i < argc; i++) { + char *base = get_last_path_component(argv[i]); + + if (strcmp(base, ".") == 0 || strcmp(base, "..") == 0) { + error_msg("cannot remove `.' or `..'"); + status = 1; + continue; } - optind++; + + if (remove_file(argv[i], flags) < 0) + status = 1; } + return status; } -- cgit v1.2.3