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 | |
| 10 | #include <stdio.h> |
| 11 | #include <time.h> |
| 12 | #include <utime.h> |
| 13 | #include <dirent.h> |
| 14 | #include <errno.h> |
| 15 | #include <unistd.h> |
| 16 | #include <stdlib.h> |
Manuel Novoa III | a2949aa | 2001-06-29 18:59:32 +0000 | [diff] [blame] | 17 | #include <string.h> |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 18 | #include <getopt.h> |
| 19 | #include "libbb.h" |
| 20 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 21 | int remove_file(const char *path, int flags) |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 22 | { |
| 23 | struct stat path_stat; |
| 24 | int path_exists = 1; |
| 25 | |
Matt Kraai | f3e79ba | 2001-05-11 02:35:36 +0000 | [diff] [blame] | 26 | if (lstat(path, &path_stat) < 0) { |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 27 | if (errno != ENOENT) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 28 | bb_perror_msg("unable to stat `%s'", path); |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 29 | return -1; |
| 30 | } |
| 31 | |
| 32 | path_exists = 0; |
| 33 | } |
| 34 | |
| 35 | if (!path_exists) { |
| 36 | if (!(flags & FILEUTILS_FORCE)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 37 | bb_perror_msg("cannot remove `%s'", path); |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 38 | return -1; |
| 39 | } |
| 40 | return 0; |
| 41 | } |
| 42 | |
| 43 | if (S_ISDIR(path_stat.st_mode)) { |
| 44 | DIR *dp; |
| 45 | struct dirent *d; |
| 46 | int status = 0; |
| 47 | |
| 48 | if (!(flags & FILEUTILS_RECUR)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 49 | bb_error_msg("%s: is a directory", path); |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 50 | return -1; |
| 51 | } |
| 52 | |
| 53 | if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 && |
| 54 | isatty(0)) || |
| 55 | (flags & FILEUTILS_INTERACTIVE)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 56 | fprintf(stderr, "%s: descend into directory `%s'? ", bb_applet_name, |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 57 | path); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 58 | if (!bb_ask_confirmation()) |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 59 | return 0; |
| 60 | } |
| 61 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 62 | if ((dp = opendir(path)) == NULL) { |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 63 | return -1; |
| 64 | } |
| 65 | |
| 66 | while ((d = readdir(dp)) != NULL) { |
| 67 | char *new_path; |
| 68 | |
Glenn L McGrath | 393183d | 2003-05-26 14:07:50 +0000 | [diff] [blame] | 69 | new_path = concat_subpath_file(path, d->d_name); |
| 70 | if(new_path == NULL) |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 71 | continue; |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 72 | if (remove_file(new_path, flags) < 0) |
| 73 | status = -1; |
| 74 | free(new_path); |
| 75 | } |
| 76 | |
| 77 | if (closedir(dp) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 78 | bb_perror_msg("unable to close `%s'", path); |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 79 | return -1; |
| 80 | } |
| 81 | |
| 82 | if (flags & FILEUTILS_INTERACTIVE) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 83 | fprintf(stderr, "%s: remove directory `%s'? ", bb_applet_name, path); |
| 84 | if (!bb_ask_confirmation()) |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 85 | return status; |
| 86 | } |
| 87 | |
| 88 | if (rmdir(path) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 89 | bb_perror_msg("unable to remove `%s'", path); |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 90 | return -1; |
| 91 | } |
| 92 | |
| 93 | return status; |
| 94 | } else { |
| 95 | if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 && |
Matt Kraai | f3e79ba | 2001-05-11 02:35:36 +0000 | [diff] [blame] | 96 | !S_ISLNK(path_stat.st_mode) && |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 97 | isatty(0)) || |
| 98 | (flags & FILEUTILS_INTERACTIVE)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 99 | fprintf(stderr, "%s: remove `%s'? ", bb_applet_name, path); |
| 100 | if (!bb_ask_confirmation()) |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | if (unlink(path) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 105 | bb_perror_msg("unable to remove `%s'", path); |
Matt Kraai | 8810bdb | 2001-04-24 20:04:18 +0000 | [diff] [blame] | 106 | return -1; |
| 107 | } |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | } |