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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 8 | */ |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 9 | #include "libbb.h" |
| 10 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 11 | /* Used from NOFORK applets. Must not allocate anything */ |
| 12 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 13 | int FAST_FUNC remove_file(const char *path, int flags) |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 14 | { |
| 15 | struct stat path_stat; |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 16 | |
Matt Kraai | f3e79ba | 2001-05-11 02:35:36 +0000 | [diff] [blame] | 17 | if (lstat(path, &path_stat) < 0) { |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 18 | if (errno != ENOENT) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 19 | bb_perror_msg("can't stat '%s'", path); |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 20 | return -1; |
| 21 | } |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 22 | if (!(flags & FILEUTILS_FORCE)) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 23 | bb_perror_msg("can't remove '%s'", path); |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 24 | return -1; |
| 25 | } |
| 26 | return 0; |
| 27 | } |
| 28 | |
| 29 | if (S_ISDIR(path_stat.st_mode)) { |
| 30 | DIR *dp; |
| 31 | struct dirent *d; |
| 32 | int status = 0; |
| 33 | |
| 34 | if (!(flags & FILEUTILS_RECUR)) { |
Simon B | 44642d1 | 2012-05-06 13:18:35 +0200 | [diff] [blame] | 35 | bb_error_msg("'%s' is a directory", path); |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 36 | return -1; |
| 37 | } |
| 38 | |
Denis Vlasenko | f4d40c8 | 2007-03-26 23:14:38 +0000 | [diff] [blame] | 39 | if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 && isatty(0)) |
| 40 | || (flags & FILEUTILS_INTERACTIVE) |
| 41 | ) { |
Denys Vlasenko | bae8fc4 | 2018-04-07 15:21:35 +0200 | [diff] [blame] | 42 | fprintf(stderr, "%s: descend into directory '%s'? ", |
| 43 | applet_name, path); |
Denys Vlasenko | 77cb6b9 | 2018-04-07 15:08:12 +0200 | [diff] [blame] | 44 | if (!bb_ask_y_confirmation()) |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 45 | return 0; |
| 46 | } |
| 47 | |
Denis Vlasenko | f4d40c8 | 2007-03-26 23:14:38 +0000 | [diff] [blame] | 48 | dp = opendir(path); |
| 49 | if (dp == NULL) { |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 50 | return -1; |
| 51 | } |
| 52 | |
| 53 | while ((d = readdir(dp)) != NULL) { |
| 54 | char *new_path; |
| 55 | |
Glenn L McGrath | 393183d | 2003-05-26 14:07:50 +0000 | [diff] [blame] | 56 | new_path = concat_subpath_file(path, d->d_name); |
Denis Vlasenko | f4d40c8 | 2007-03-26 23:14:38 +0000 | [diff] [blame] | 57 | if (new_path == NULL) |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 58 | continue; |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 59 | if (remove_file(new_path, flags) < 0) |
| 60 | status = -1; |
| 61 | free(new_path); |
| 62 | } |
| 63 | |
| 64 | if (closedir(dp) < 0) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 65 | bb_perror_msg("can't close '%s'", path); |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 66 | return -1; |
| 67 | } |
| 68 | |
| 69 | if (flags & FILEUTILS_INTERACTIVE) { |
Denys Vlasenko | bae8fc4 | 2018-04-07 15:21:35 +0200 | [diff] [blame] | 70 | fprintf(stderr, "%s: remove directory '%s'? ", |
| 71 | applet_name, path); |
Denys Vlasenko | 77cb6b9 | 2018-04-07 15:08:12 +0200 | [diff] [blame] | 72 | if (!bb_ask_y_confirmation()) |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 73 | return status; |
| 74 | } |
| 75 | |
| 76 | if (rmdir(path) < 0) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 77 | bb_perror_msg("can't remove '%s'", path); |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 78 | return -1; |
| 79 | } |
| 80 | |
Denys Vlasenko | 17f8418 | 2014-05-19 16:23:50 +0200 | [diff] [blame] | 81 | if (flags & FILEUTILS_VERBOSE) { |
| 82 | printf("removed directory: '%s'\n", path); |
| 83 | } |
| 84 | |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 85 | return status; |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 86 | } |
Denis Vlasenko | f4d40c8 | 2007-03-26 23:14:38 +0000 | [diff] [blame] | 87 | |
| 88 | /* !ISDIR */ |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 89 | if ((!(flags & FILEUTILS_FORCE) |
| 90 | && access(path, W_OK) < 0 |
| 91 | && !S_ISLNK(path_stat.st_mode) |
| 92 | && isatty(0)) |
Denis Vlasenko | f4d40c8 | 2007-03-26 23:14:38 +0000 | [diff] [blame] | 93 | || (flags & FILEUTILS_INTERACTIVE) |
| 94 | ) { |
| 95 | fprintf(stderr, "%s: remove '%s'? ", applet_name, path); |
Denys Vlasenko | 77cb6b9 | 2018-04-07 15:08:12 +0200 | [diff] [blame] | 96 | if (!bb_ask_y_confirmation()) |
Denis Vlasenko | f4d40c8 | 2007-03-26 23:14:38 +0000 | [diff] [blame] | 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | if (unlink(path) < 0) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 101 | bb_perror_msg("can't remove '%s'", path); |
Denis Vlasenko | f4d40c8 | 2007-03-26 23:14:38 +0000 | [diff] [blame] | 102 | return -1; |
| 103 | } |
| 104 | |
Denys Vlasenko | 17f8418 | 2014-05-19 16:23:50 +0200 | [diff] [blame] | 105 | if (flags & FILEUTILS_VERBOSE) { |
| 106 | printf("removed '%s'\n", path); |
| 107 | } |
| 108 | |
Denis Vlasenko | f4d40c8 | 2007-03-26 23:14:38 +0000 | [diff] [blame] | 109 | return 0; |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 110 | } |