blob: da1488544ea392825480cf0c041789327b93bf3d [file] [log] [blame]
Matt Kraai8810bdb2001-04-24 20:04:18 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini remove_file implementation for busybox
4 *
Matt Kraai8810bdb2001-04-24 20:04:18 +00005 * Copyright (C) 2001 Matt Kraai <kraai@alumni.carnegiemellon.edu>
6 *
Bernhard Reutner-Fischercb448162006-04-12 07:35:12 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Matt Kraai8810bdb2001-04-24 20:04:18 +00008 */
9
Matt Kraai8810bdb2001-04-24 20:04:18 +000010#include "libbb.h"
11
Denis Vlasenko99912ca2007-04-10 15:43:37 +000012/* Used from NOFORK applets. Must not allocate anything */
13
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000014int FAST_FUNC remove_file(const char *path, int flags)
Matt Kraai8810bdb2001-04-24 20:04:18 +000015{
16 struct stat path_stat;
Matt Kraai8810bdb2001-04-24 20:04:18 +000017
Matt Kraaif3e79ba2001-05-11 02:35:36 +000018 if (lstat(path, &path_stat) < 0) {
Matt Kraai8810bdb2001-04-24 20:04:18 +000019 if (errno != ENOENT) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +010020 bb_perror_msg("can't stat '%s'", path);
Matt Kraai8810bdb2001-04-24 20:04:18 +000021 return -1;
22 }
Matt Kraai8810bdb2001-04-24 20:04:18 +000023 if (!(flags & FILEUTILS_FORCE)) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +010024 bb_perror_msg("can't remove '%s'", path);
Matt Kraai8810bdb2001-04-24 20:04:18 +000025 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 cad53642003-03-19 09:13:01 +000036 bb_error_msg("%s: is a directory", path);
Matt Kraai8810bdb2001-04-24 20:04:18 +000037 return -1;
38 }
39
Denis Vlasenkof4d40c82007-03-26 23:14:38 +000040 if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 && isatty(0))
41 || (flags & FILEUTILS_INTERACTIVE)
42 ) {
Denis Vlasenko89f0b342006-11-18 22:04:09 +000043 fprintf(stderr, "%s: descend into directory '%s'? ", applet_name,
Matt Kraai8810bdb2001-04-24 20:04:18 +000044 path);
Manuel Novoa III cad53642003-03-19 09:13:01 +000045 if (!bb_ask_confirmation())
Matt Kraai8810bdb2001-04-24 20:04:18 +000046 return 0;
47 }
48
Denis Vlasenkof4d40c82007-03-26 23:14:38 +000049 dp = opendir(path);
50 if (dp == NULL) {
Matt Kraai8810bdb2001-04-24 20:04:18 +000051 return -1;
52 }
53
54 while ((d = readdir(dp)) != NULL) {
55 char *new_path;
56
Glenn L McGrath393183d2003-05-26 14:07:50 +000057 new_path = concat_subpath_file(path, d->d_name);
Denis Vlasenkof4d40c82007-03-26 23:14:38 +000058 if (new_path == NULL)
Matt Kraai8810bdb2001-04-24 20:04:18 +000059 continue;
Matt Kraai8810bdb2001-04-24 20:04:18 +000060 if (remove_file(new_path, flags) < 0)
61 status = -1;
62 free(new_path);
63 }
64
65 if (closedir(dp) < 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +010066 bb_perror_msg("can't close '%s'", path);
Matt Kraai8810bdb2001-04-24 20:04:18 +000067 return -1;
68 }
69
70 if (flags & FILEUTILS_INTERACTIVE) {
Denis Vlasenko89f0b342006-11-18 22:04:09 +000071 fprintf(stderr, "%s: remove directory '%s'? ", applet_name, path);
Manuel Novoa III cad53642003-03-19 09:13:01 +000072 if (!bb_ask_confirmation())
Matt Kraai8810bdb2001-04-24 20:04:18 +000073 return status;
74 }
75
76 if (rmdir(path) < 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +010077 bb_perror_msg("can't remove '%s'", path);
Matt Kraai8810bdb2001-04-24 20:04:18 +000078 return -1;
79 }
80
81 return status;
Matt Kraai8810bdb2001-04-24 20:04:18 +000082 }
Denis Vlasenkof4d40c82007-03-26 23:14:38 +000083
84 /* !ISDIR */
Denis Vlasenkocf26ab72008-03-27 22:45:44 +000085 if ((!(flags & FILEUTILS_FORCE)
86 && access(path, W_OK) < 0
87 && !S_ISLNK(path_stat.st_mode)
88 && isatty(0))
Denis Vlasenkof4d40c82007-03-26 23:14:38 +000089 || (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) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +010097 bb_perror_msg("can't remove '%s'", path);
Denis Vlasenkof4d40c82007-03-26 23:14:38 +000098 return -1;
99 }
100
101 return 0;
Matt Kraai8810bdb2001-04-24 20:04:18 +0000102}