Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 2 | /* |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 3 | * rmdir implementation for busybox |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 4 | * |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 5 | * Copyright (C) 2003 Manuel Novoa III <mjn3@codepoet.org> |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 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. |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 8 | */ |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 9 | //config:config RMDIR |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 10 | //config: bool "rmdir (3.4 kb)" |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 11 | //config: default y |
| 12 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 13 | //config: rmdir is used to remove empty directories. |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 14 | //config: |
| 15 | //config:config FEATURE_RMDIR_LONG_OPTIONS |
| 16 | //config: bool "Enable long options" |
| 17 | //config: default y |
| 18 | //config: depends on RMDIR && LONG_OPTS |
| 19 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 20 | //config: Support long options for the rmdir applet, including |
| 21 | //config: --ignore-fail-on-non-empty for compatibility with GNU rmdir. |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 22 | |
| 23 | //applet:IF_RMDIR(APPLET_NOFORK(rmdir, rmdir, BB_DIR_BIN, BB_SUID_DROP, rmdir)) |
| 24 | |
| 25 | //kbuild:lib-$(CONFIG_RMDIR) += rmdir.o |
Eric Andersen | f6be944 | 1999-10-13 21:12:06 +0000 | [diff] [blame] | 26 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 27 | /* BB_AUDIT SUSv3 compliant */ |
| 28 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/rmdir.html */ |
Matt Kraai | 1350666 | 2001-08-29 21:18:47 +0000 | [diff] [blame] | 29 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 30 | //usage:#define rmdir_trivial_usage |
| 31 | //usage: "[OPTIONS] DIRECTORY..." |
| 32 | //usage:#define rmdir_full_usage "\n\n" |
| 33 | //usage: "Remove DIRECTORY if it is empty\n" |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 34 | //usage: IF_FEATURE_RMDIR_LONG_OPTIONS( |
| 35 | //usage: "\n -p|--parents Include parents" |
| 36 | //usage: "\n --ignore-fail-on-non-empty" |
| 37 | //usage: ) |
| 38 | //usage: IF_NOT_FEATURE_RMDIR_LONG_OPTIONS( |
| 39 | //usage: "\n -p Include parents" |
| 40 | //usage: ) |
| 41 | //usage: |
| 42 | //usage:#define rmdir_example_usage |
| 43 | //usage: "# rmdir /tmp/foo\n" |
| 44 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 45 | #include "libbb.h" |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 46 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 47 | /* This is a NOFORK applet. Be very careful! */ |
| 48 | |
| 49 | |
Simon B | 3698ed1 | 2012-05-06 15:03:32 +0200 | [diff] [blame] | 50 | #define PARENTS (1 << 0) |
Denys Vlasenko | 17f8418 | 2014-05-19 16:23:50 +0200 | [diff] [blame] | 51 | #define VERBOSE ((1 << 1) * ENABLE_FEATURE_VERBOSE) |
Simon B | 3698ed1 | 2012-05-06 15:03:32 +0200 | [diff] [blame] | 52 | #define IGNORE_NON_EMPTY (1 << 2) |
Denis Vlasenko | 52feee9 | 2008-02-24 14:56:10 +0000 | [diff] [blame] | 53 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 54 | int rmdir_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 55 | int rmdir_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 56 | { |
Matt Kraai | 9a71af5 | 2000-11-22 01:09:38 +0000 | [diff] [blame] | 57 | int status = EXIT_SUCCESS; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 58 | int flags; |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 59 | char *path; |
Matt Kraai | 9a71af5 | 2000-11-22 01:09:38 +0000 | [diff] [blame] | 60 | |
Denis Vlasenko | 52feee9 | 2008-02-24 14:56:10 +0000 | [diff] [blame] | 61 | #if ENABLE_FEATURE_RMDIR_LONG_OPTIONS |
| 62 | static const char rmdir_longopts[] ALIGN1 = |
| 63 | "parents\0" No_argument "p" |
| 64 | /* Debian etch: many packages fail to be purged or installed |
| 65 | * because they desperately want this option: */ |
| 66 | "ignore-fail-on-non-empty\0" No_argument "\xff" |
Denys Vlasenko | 17f8418 | 2014-05-19 16:23:50 +0200 | [diff] [blame] | 67 | IF_FEATURE_VERBOSE( |
| 68 | "verbose\0" No_argument "v" |
| 69 | ) |
Denis Vlasenko | 52feee9 | 2008-02-24 14:56:10 +0000 | [diff] [blame] | 70 | ; |
| 71 | applet_long_options = rmdir_longopts; |
| 72 | #endif |
Simon B | 3698ed1 | 2012-05-06 15:03:32 +0200 | [diff] [blame] | 73 | flags = getopt32(argv, "pv"); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 74 | argv += optind; |
Matt Kraai | 1350666 | 2001-08-29 21:18:47 +0000 | [diff] [blame] | 75 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 76 | if (!*argv) { |
| 77 | bb_show_usage(); |
| 78 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 79 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 80 | do { |
| 81 | path = *argv; |
| 82 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 83 | while (1) { |
Denys Vlasenko | 17f8418 | 2014-05-19 16:23:50 +0200 | [diff] [blame] | 84 | if (flags & VERBOSE) { |
| 85 | printf("rmdir: removing directory, '%s'\n", path); |
| 86 | } |
| 87 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 88 | if (rmdir(path) < 0) { |
Denis Vlasenko | 52feee9 | 2008-02-24 14:56:10 +0000 | [diff] [blame] | 89 | #if ENABLE_FEATURE_RMDIR_LONG_OPTIONS |
| 90 | if ((flags & IGNORE_NON_EMPTY) && errno == ENOTEMPTY) |
| 91 | break; |
| 92 | #endif |
Denys Vlasenko | e4dcba1 | 2010-10-28 18:57:19 +0200 | [diff] [blame] | 93 | bb_perror_msg("'%s'", path); /* Match gnu rmdir msg. */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 94 | status = EXIT_FAILURE; |
Denis Vlasenko | 52feee9 | 2008-02-24 14:56:10 +0000 | [diff] [blame] | 95 | } else if (flags & PARENTS) { |
| 96 | /* Note: path was not "" since rmdir succeeded. */ |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 97 | path = dirname(path); |
Denis Vlasenko | 52feee9 | 2008-02-24 14:56:10 +0000 | [diff] [blame] | 98 | /* Path is now just the parent component. Dirname |
| 99 | * returns "." if there are no parents. |
Denis Vlasenko | 89f0b34 | 2006-11-18 22:04:09 +0000 | [diff] [blame] | 100 | */ |
Denis Vlasenko | 52feee9 | 2008-02-24 14:56:10 +0000 | [diff] [blame] | 101 | if (NOT_LONE_CHAR(path, '.')) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 102 | continue; |
| 103 | } |
| 104 | } |
| 105 | break; |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 106 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 107 | } while (*++argv); |
Matt Kraai | 1350666 | 2001-08-29 21:18:47 +0000 | [diff] [blame] | 108 | |
Matt Kraai | 9a71af5 | 2000-11-22 01:09:38 +0000 | [diff] [blame] | 109 | return status; |
Eric Andersen | cc8ed39 | 1999-10-05 16:24:54 +0000 | [diff] [blame] | 110 | } |