Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Mini remove_file implementation for busybox |
| 4 | * |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 5 | * Copyright (C) 2001 Matt Kraai <kraai@alumni.carnegiemellon.edu> |
| 6 | * |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
| 22 | #include <stdio.h> |
| 23 | #include <time.h> |
| 24 | #include <utime.h> |
| 25 | #include <dirent.h> |
| 26 | #include <errno.h> |
| 27 | #include <unistd.h> |
| 28 | #include <stdlib.h> |
Manuel Novoa III | a2949aa | 2001-06-29 18:59:32 +0000 | [diff] [blame] | 29 | #include <string.h> |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 30 | #include <getopt.h> |
| 31 | #include "libbb.h" |
| 32 | |
| 33 | extern int remove_file(const char *path, int flags) |
| 34 | { |
| 35 | struct stat path_stat; |
| 36 | int path_exists = 1; |
| 37 | |
Matt Kraai | f3e79ba | 2001-05-11 02:35:36 +0000 | [diff] [blame] | 38 | if (lstat(path, &path_stat) < 0) { |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 39 | if (errno != ENOENT) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 40 | bb_perror_msg("unable to stat `%s'", path); |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 41 | return -1; |
| 42 | } |
| 43 | |
| 44 | path_exists = 0; |
| 45 | } |
| 46 | |
| 47 | if (!path_exists) { |
| 48 | if (!(flags & FILEUTILS_FORCE)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 49 | bb_perror_msg("cannot remove `%s'", path); |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 50 | return -1; |
| 51 | } |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | if (S_ISDIR(path_stat.st_mode)) { |
| 56 | DIR *dp; |
| 57 | struct dirent *d; |
| 58 | int status = 0; |
| 59 | |
| 60 | if (!(flags & FILEUTILS_RECUR)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 61 | bb_error_msg("%s: is a directory", path); |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 62 | return -1; |
| 63 | } |
| 64 | |
| 65 | if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 && |
| 66 | isatty(0)) || |
| 67 | (flags & FILEUTILS_INTERACTIVE)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 68 | fprintf(stderr, "%s: descend into directory `%s'? ", bb_applet_name, |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 69 | path); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 70 | if (!bb_ask_confirmation()) |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | if ((dp = opendir(path)) == NULL) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 75 | bb_perror_msg("unable to open `%s'", path); |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 76 | return -1; |
| 77 | } |
| 78 | |
| 79 | while ((d = readdir(dp)) != NULL) { |
| 80 | char *new_path; |
| 81 | |
Glenn L McGrath | 393183d | 2003-05-26 14:07:50 +0000 | [diff] [blame] | 82 | new_path = concat_subpath_file(path, d->d_name); |
| 83 | if(new_path == NULL) |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 84 | continue; |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 85 | if (remove_file(new_path, flags) < 0) |
| 86 | status = -1; |
| 87 | free(new_path); |
| 88 | } |
| 89 | |
| 90 | if (closedir(dp) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 91 | bb_perror_msg("unable to close `%s'", path); |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 92 | return -1; |
| 93 | } |
| 94 | |
| 95 | if (flags & FILEUTILS_INTERACTIVE) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 96 | fprintf(stderr, "%s: remove directory `%s'? ", bb_applet_name, path); |
| 97 | if (!bb_ask_confirmation()) |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 98 | return status; |
| 99 | } |
| 100 | |
| 101 | if (rmdir(path) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 102 | bb_perror_msg("unable to remove `%s'", path); |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 103 | return -1; |
| 104 | } |
| 105 | |
| 106 | return status; |
| 107 | } else { |
| 108 | if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 && |
Matt Kraai | f3e79ba | 2001-05-11 02:35:36 +0000 | [diff] [blame] | 109 | !S_ISLNK(path_stat.st_mode) && |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 110 | isatty(0)) || |
| 111 | (flags & FILEUTILS_INTERACTIVE)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 112 | fprintf(stderr, "%s: remove `%s'? ", bb_applet_name, path); |
| 113 | if (!bb_ask_confirmation()) |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | if (unlink(path) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 118 | bb_perror_msg("unable to remove `%s'", path); |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 119 | return -1; |
| 120 | } |
| 121 | |
| 122 | return 0; |
| 123 | } |
| 124 | } |