blob: 86c9a5c56bd24b9eb1049bcfb8ad8462933eb373 [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 }
63
64 if (closedir(dp) < 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +010065 bb_perror_msg("can't close '%s'", path);
Matt Kraai8810bdb2001-04-24 20:04:18 +000066 return -1;
67 }
68
69 if (flags & FILEUTILS_INTERACTIVE) {
Denys Vlasenkobae8fc42018-04-07 15:21:35 +020070 fprintf(stderr, "%s: remove directory '%s'? ",
71 applet_name, path);
Denys Vlasenko77cb6b92018-04-07 15:08:12 +020072 if (!bb_ask_y_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
Denys Vlasenko17f84182014-05-19 16:23:50 +020081 if (flags & FILEUTILS_VERBOSE) {
82 printf("removed directory: '%s'\n", path);
83 }
84
Matt Kraai8810bdb2001-04-24 20:04:18 +000085 return status;
Matt Kraai8810bdb2001-04-24 20:04:18 +000086 }
Denis Vlasenkof4d40c82007-03-26 23:14:38 +000087
88 /* !ISDIR */
Denis Vlasenkocf26ab72008-03-27 22:45:44 +000089 if ((!(flags & FILEUTILS_FORCE)
90 && access(path, W_OK) < 0
91 && !S_ISLNK(path_stat.st_mode)
92 && isatty(0))
Denis Vlasenkof4d40c82007-03-26 23:14:38 +000093 || (flags & FILEUTILS_INTERACTIVE)
94 ) {
95 fprintf(stderr, "%s: remove '%s'? ", applet_name, path);
Denys Vlasenko77cb6b92018-04-07 15:08:12 +020096 if (!bb_ask_y_confirmation())
Denis Vlasenkof4d40c82007-03-26 23:14:38 +000097 return 0;
98 }
99
100 if (unlink(path) < 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100101 bb_perror_msg("can't remove '%s'", path);
Denis Vlasenkof4d40c82007-03-26 23:14:38 +0000102 return -1;
103 }
104
Denys Vlasenko17f84182014-05-19 16:23:50 +0200105 if (flags & FILEUTILS_VERBOSE) {
106 printf("removed '%s'\n", path);
107 }
108
Denis Vlasenkof4d40c82007-03-26 23:14:38 +0000109 return 0;
Matt Kraai8810bdb2001-04-24 20:04:18 +0000110}