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 | } |
Denys Vlasenko | 4a68697 | 2021-06-24 17:39:57 +0200 | [diff] [blame] | 63 | closedir(dp); |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 64 | |
| 65 | if (flags & FILEUTILS_INTERACTIVE) { |
Denys Vlasenko | bae8fc4 | 2018-04-07 15:21:35 +0200 | [diff] [blame] | 66 | fprintf(stderr, "%s: remove directory '%s'? ", |
| 67 | applet_name, path); |
Denys Vlasenko | 77cb6b9 | 2018-04-07 15:08:12 +0200 | [diff] [blame] | 68 | if (!bb_ask_y_confirmation()) |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 69 | return status; |
| 70 | } |
| 71 | |
Chen Yu | 05b1806 | 2018-09-04 15:26:22 +0800 | [diff] [blame] | 72 | if (status == 0 && rmdir(path) < 0) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 73 | bb_perror_msg("can't remove '%s'", path); |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 74 | return -1; |
| 75 | } |
| 76 | |
Denys Vlasenko | 17f8418 | 2014-05-19 16:23:50 +0200 | [diff] [blame] | 77 | if (flags & FILEUTILS_VERBOSE) { |
| 78 | printf("removed directory: '%s'\n", path); |
| 79 | } |
| 80 | |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 81 | return status; |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 82 | } |
Denis Vlasenko | f4d40c8 | 2007-03-26 23:14:38 +0000 | [diff] [blame] | 83 | |
| 84 | /* !ISDIR */ |
Denis Vlasenko | cf26ab7 | 2008-03-27 22:45:44 +0000 | [diff] [blame] | 85 | if ((!(flags & FILEUTILS_FORCE) |
| 86 | && access(path, W_OK) < 0 |
| 87 | && !S_ISLNK(path_stat.st_mode) |
| 88 | && isatty(0)) |
Denis Vlasenko | f4d40c8 | 2007-03-26 23:14:38 +0000 | [diff] [blame] | 89 | || (flags & FILEUTILS_INTERACTIVE) |
| 90 | ) { |
| 91 | fprintf(stderr, "%s: remove '%s'? ", applet_name, path); |
Denys Vlasenko | 77cb6b9 | 2018-04-07 15:08:12 +0200 | [diff] [blame] | 92 | if (!bb_ask_y_confirmation()) |
Denis Vlasenko | f4d40c8 | 2007-03-26 23:14:38 +0000 | [diff] [blame] | 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | if (unlink(path) < 0) { |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 97 | bb_perror_msg("can't remove '%s'", path); |
Denis Vlasenko | f4d40c8 | 2007-03-26 23:14:38 +0000 | [diff] [blame] | 98 | return -1; |
| 99 | } |
| 100 | |
Denys Vlasenko | 17f8418 | 2014-05-19 16:23:50 +0200 | [diff] [blame] | 101 | if (flags & FILEUTILS_VERBOSE) { |
| 102 | printf("removed '%s'\n", path); |
| 103 | } |
| 104 | |
Denis Vlasenko | f4d40c8 | 2007-03-26 23:14:38 +0000 | [diff] [blame] | 105 | return 0; |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 106 | } |