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 | * |
Bernhard Reutner-Fischer | cb44816 | 2006-04-12 07:35:12 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
| 11 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 12 | /* Used from NOFORK applets. Must not allocate anything */ |
| 13 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 14 | int remove_file(const char *path, int flags) |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 15 | { |
| 16 | struct stat path_stat; |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 17 | |
Matt Kraai | f3e79ba | 2001-05-11 02:35:36 +0000 | [diff] [blame] | 18 | if (lstat(path, &path_stat) < 0) { |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 19 | if (errno != ENOENT) { |
Denis Vlasenko | 89f0b34 | 2006-11-18 22:04:09 +0000 | [diff] [blame] | 20 | bb_perror_msg("cannot stat '%s'", path); |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 21 | return -1; |
| 22 | } |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 23 | if (!(flags & FILEUTILS_FORCE)) { |
Denis Vlasenko | 89f0b34 | 2006-11-18 22:04:09 +0000 | [diff] [blame] | 24 | bb_perror_msg("cannot remove '%s'", path); |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 25 | return -1; |
| 26 | } |
| 27 | return 0; |
| 28 | } |
| 29 | |
| 30 | if (S_ISDIR(path_stat.st_mode)) { |
| 31 | DIR *dp; |
| 32 | struct dirent *d; |
| 33 | int status = 0; |
| 34 | |
| 35 | if (!(flags & FILEUTILS_RECUR)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 36 | bb_error_msg("%s: is a directory", path); |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 37 | return -1; |
| 38 | } |
| 39 | |
Denis Vlasenko | f4d40c8 | 2007-03-26 23:14:38 +0000 | [diff] [blame] | 40 | if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 && isatty(0)) |
| 41 | || (flags & FILEUTILS_INTERACTIVE) |
| 42 | ) { |
Denis Vlasenko | 89f0b34 | 2006-11-18 22:04:09 +0000 | [diff] [blame] | 43 | fprintf(stderr, "%s: descend into directory '%s'? ", applet_name, |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 44 | path); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 45 | if (!bb_ask_confirmation()) |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 46 | return 0; |
| 47 | } |
| 48 | |
Denis Vlasenko | f4d40c8 | 2007-03-26 23:14:38 +0000 | [diff] [blame] | 49 | dp = opendir(path); |
| 50 | if (dp == NULL) { |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 51 | return -1; |
| 52 | } |
| 53 | |
| 54 | while ((d = readdir(dp)) != NULL) { |
| 55 | char *new_path; |
| 56 | |
Glenn L McGrath | 393183d | 2003-05-26 14:07:50 +0000 | [diff] [blame] | 57 | new_path = concat_subpath_file(path, d->d_name); |
Denis Vlasenko | f4d40c8 | 2007-03-26 23:14:38 +0000 | [diff] [blame] | 58 | if (new_path == NULL) |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 59 | continue; |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 60 | if (remove_file(new_path, flags) < 0) |
| 61 | status = -1; |
| 62 | free(new_path); |
| 63 | } |
| 64 | |
| 65 | if (closedir(dp) < 0) { |
Denis Vlasenko | 89f0b34 | 2006-11-18 22:04:09 +0000 | [diff] [blame] | 66 | bb_perror_msg("cannot close '%s'", path); |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 67 | return -1; |
| 68 | } |
| 69 | |
| 70 | if (flags & FILEUTILS_INTERACTIVE) { |
Denis Vlasenko | 89f0b34 | 2006-11-18 22:04:09 +0000 | [diff] [blame] | 71 | fprintf(stderr, "%s: remove directory '%s'? ", applet_name, path); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 72 | if (!bb_ask_confirmation()) |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 73 | return status; |
| 74 | } |
| 75 | |
| 76 | if (rmdir(path) < 0) { |
Denis Vlasenko | 89f0b34 | 2006-11-18 22:04:09 +0000 | [diff] [blame] | 77 | bb_perror_msg("cannot remove '%s'", path); |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 78 | return -1; |
| 79 | } |
| 80 | |
| 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); |
| 92 | if (!bb_ask_confirmation()) |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | if (unlink(path) < 0) { |
| 97 | bb_perror_msg("cannot remove '%s'", path); |
| 98 | return -1; |
| 99 | } |
| 100 | |
| 101 | return 0; |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 102 | } |