blob: 1505e62183b5127da1baabe72b27a78d04ef360f [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Matt Kraai8810bdb2001-04-24 20:04:18 +00008 */
Matt Kraai8810bdb2001-04-24 20:04:18 +00009#include "libbb.h"
10
Denis Vlasenko99912ca2007-04-10 15:43:37 +000011/* Used from NOFORK applets. Must not allocate anything */
12
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000013int FAST_FUNC remove_file(const char *path, int flags)
Matt Kraai8810bdb2001-04-24 20:04:18 +000014{
15 struct stat path_stat;
Matt Kraai8810bdb2001-04-24 20:04:18 +000016
Matt Kraaif3e79ba2001-05-11 02:35:36 +000017 if (lstat(path, &path_stat) < 0) {
Matt Kraai8810bdb2001-04-24 20:04:18 +000018 if (errno != ENOENT) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +010019 bb_perror_msg("can't stat '%s'", path);
Matt Kraai8810bdb2001-04-24 20:04:18 +000020 return -1;
21 }
Matt Kraai8810bdb2001-04-24 20:04:18 +000022 if (!(flags & FILEUTILS_FORCE)) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +010023 bb_perror_msg("can't remove '%s'", path);
Matt Kraai8810bdb2001-04-24 20:04:18 +000024 return -1;
25 }
26 return 0;
27 }
28
29 if (S_ISDIR(path_stat.st_mode)) {
30 DIR *dp;
31 struct dirent *d;
32 int status = 0;
33
34 if (!(flags & FILEUTILS_RECUR)) {
Simon B44642d12012-05-06 13:18:35 +020035 bb_error_msg("'%s' is a directory", path);
Matt Kraai8810bdb2001-04-24 20:04:18 +000036 return -1;
37 }
38
Denis Vlasenkof4d40c82007-03-26 23:14:38 +000039 if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 && isatty(0))
40 || (flags & FILEUTILS_INTERACTIVE)
41 ) {
Denys Vlasenkobae8fc42018-04-07 15:21:35 +020042 fprintf(stderr, "%s: descend into directory '%s'? ",
43 applet_name, path);
Denys Vlasenko77cb6b92018-04-07 15:08:12 +020044 if (!bb_ask_y_confirmation())
Matt Kraai8810bdb2001-04-24 20:04:18 +000045 return 0;
46 }
47
Denis Vlasenkof4d40c82007-03-26 23:14:38 +000048 dp = opendir(path);
49 if (dp == NULL) {
Matt Kraai8810bdb2001-04-24 20:04:18 +000050 return -1;
51 }
52
53 while ((d = readdir(dp)) != NULL) {
54 char *new_path;
55
Glenn L McGrath393183d2003-05-26 14:07:50 +000056 new_path = concat_subpath_file(path, d->d_name);
Denis Vlasenkof4d40c82007-03-26 23:14:38 +000057 if (new_path == NULL)
Matt Kraai8810bdb2001-04-24 20:04:18 +000058 continue;
Matt Kraai8810bdb2001-04-24 20:04:18 +000059 if (remove_file(new_path, flags) < 0)
60 status = -1;
61 free(new_path);
62 }
Denys Vlasenko4a686972021-06-24 17:39:57 +020063 closedir(dp);
Matt Kraai8810bdb2001-04-24 20:04:18 +000064
65 if (flags & FILEUTILS_INTERACTIVE) {
Denys Vlasenkobae8fc42018-04-07 15:21:35 +020066 fprintf(stderr, "%s: remove directory '%s'? ",
67 applet_name, path);
Denys Vlasenko77cb6b92018-04-07 15:08:12 +020068 if (!bb_ask_y_confirmation())
Matt Kraai8810bdb2001-04-24 20:04:18 +000069 return status;
70 }
71
Chen Yu05b18062018-09-04 15:26:22 +080072 if (status == 0 && rmdir(path) < 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +010073 bb_perror_msg("can't remove '%s'", path);
Matt Kraai8810bdb2001-04-24 20:04:18 +000074 return -1;
75 }
76
Denys Vlasenko17f84182014-05-19 16:23:50 +020077 if (flags & FILEUTILS_VERBOSE) {
78 printf("removed directory: '%s'\n", path);
79 }
80
Matt Kraai8810bdb2001-04-24 20:04:18 +000081 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);
Denys Vlasenko77cb6b92018-04-07 15:08:12 +020092 if (!bb_ask_y_confirmation())
Denis Vlasenkof4d40c82007-03-26 23:14:38 +000093 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
Denys Vlasenko17f84182014-05-19 16:23:50 +0200101 if (flags & FILEUTILS_VERBOSE) {
102 printf("removed '%s'\n", path);
103 }
104
Denis Vlasenkof4d40c82007-03-26 23:14:38 +0000105 return 0;
Matt Kraai8810bdb2001-04-24 20:04:18 +0000106}