1 /* vi: set sw=4 ts=4: */
3 * Mini remove_file implementation for busybox
5 * Copyright (C) 2001 Matt Kraai <kraai@alumni.carnegiemellon.edu>
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
21 int remove_file(const char *path, int flags)
23 struct stat path_stat;
26 if (lstat(path, &path_stat) < 0) {
27 if (errno != ENOENT) {
28 bb_perror_msg("cannot stat '%s'", path);
36 if (!(flags & FILEUTILS_FORCE)) {
37 bb_perror_msg("cannot remove '%s'", path);
43 if (S_ISDIR(path_stat.st_mode)) {
48 if (!(flags & FILEUTILS_RECUR)) {
49 bb_error_msg("%s: is a directory", path);
53 if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 &&
55 (flags & FILEUTILS_INTERACTIVE)) {
56 fprintf(stderr, "%s: descend into directory '%s'? ", applet_name,
58 if (!bb_ask_confirmation())
62 if ((dp = opendir(path)) == NULL) {
66 while ((d = readdir(dp)) != NULL) {
69 new_path = concat_subpath_file(path, d->d_name);
72 if (remove_file(new_path, flags) < 0)
77 if (closedir(dp) < 0) {
78 bb_perror_msg("cannot close '%s'", path);
82 if (flags & FILEUTILS_INTERACTIVE) {
83 fprintf(stderr, "%s: remove directory '%s'? ", applet_name, path);
84 if (!bb_ask_confirmation())
88 if (rmdir(path) < 0) {
89 bb_perror_msg("cannot remove '%s'", path);
95 if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 &&
96 !S_ISLNK(path_stat.st_mode) &&
98 (flags & FILEUTILS_INTERACTIVE)) {
99 fprintf(stderr, "%s: remove '%s'? ", applet_name, path);
100 if (!bb_ask_confirmation())
104 if (unlink(path) < 0) {
105 bb_perror_msg("cannot remove '%s'", path);