blob: 92534a1c5b50e9c9471d87ea347639c4e92f5608 [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
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 a2949aa2001-06-29 18:59:32 +000017#include <string.h>
Matt Kraai8810bdb2001-04-24 20:04:18 +000018#include <getopt.h>
19#include "libbb.h"
20
Rob Landleydfba7412006-03-06 20:47:33 +000021int remove_file(const char *path, int flags)
Matt Kraai8810bdb2001-04-24 20:04:18 +000022{
23 struct stat path_stat;
24 int path_exists = 1;
25
Matt Kraaif3e79ba2001-05-11 02:35:36 +000026 if (lstat(path, &path_stat) < 0) {
Matt Kraai8810bdb2001-04-24 20:04:18 +000027 if (errno != ENOENT) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000028 bb_perror_msg("unable to stat `%s'", path);
Matt Kraai8810bdb2001-04-24 20:04:18 +000029 return -1;
30 }
31
32 path_exists = 0;
33 }
34
35 if (!path_exists) {
36 if (!(flags & FILEUTILS_FORCE)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000037 bb_perror_msg("cannot remove `%s'", path);
Matt Kraai8810bdb2001-04-24 20:04:18 +000038 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 cad53642003-03-19 09:13:01 +000049 bb_error_msg("%s: is a directory", path);
Matt Kraai8810bdb2001-04-24 20:04:18 +000050 return -1;
51 }
52
53 if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 &&
54 isatty(0)) ||
55 (flags & FILEUTILS_INTERACTIVE)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000056 fprintf(stderr, "%s: descend into directory `%s'? ", bb_applet_name,
Matt Kraai8810bdb2001-04-24 20:04:18 +000057 path);
Manuel Novoa III cad53642003-03-19 09:13:01 +000058 if (!bb_ask_confirmation())
Matt Kraai8810bdb2001-04-24 20:04:18 +000059 return 0;
60 }
61
Rob Landleyd921b2e2006-08-03 15:41:12 +000062 if ((dp = opendir(path)) == NULL) {
Matt Kraai8810bdb2001-04-24 20:04:18 +000063 return -1;
64 }
65
66 while ((d = readdir(dp)) != NULL) {
67 char *new_path;
68
Glenn L McGrath393183d2003-05-26 14:07:50 +000069 new_path = concat_subpath_file(path, d->d_name);
70 if(new_path == NULL)
Matt Kraai8810bdb2001-04-24 20:04:18 +000071 continue;
Matt Kraai8810bdb2001-04-24 20:04:18 +000072 if (remove_file(new_path, flags) < 0)
73 status = -1;
74 free(new_path);
75 }
76
77 if (closedir(dp) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000078 bb_perror_msg("unable to close `%s'", path);
Matt Kraai8810bdb2001-04-24 20:04:18 +000079 return -1;
80 }
81
82 if (flags & FILEUTILS_INTERACTIVE) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000083 fprintf(stderr, "%s: remove directory `%s'? ", bb_applet_name, path);
84 if (!bb_ask_confirmation())
Matt Kraai8810bdb2001-04-24 20:04:18 +000085 return status;
86 }
87
88 if (rmdir(path) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000089 bb_perror_msg("unable to remove `%s'", path);
Matt Kraai8810bdb2001-04-24 20:04:18 +000090 return -1;
91 }
92
93 return status;
94 } else {
95 if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 &&
Matt Kraaif3e79ba2001-05-11 02:35:36 +000096 !S_ISLNK(path_stat.st_mode) &&
Matt Kraai8810bdb2001-04-24 20:04:18 +000097 isatty(0)) ||
98 (flags & FILEUTILS_INTERACTIVE)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000099 fprintf(stderr, "%s: remove `%s'? ", bb_applet_name, path);
100 if (!bb_ask_confirmation())
Matt Kraai8810bdb2001-04-24 20:04:18 +0000101 return 0;
102 }
103
104 if (unlink(path) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000105 bb_perror_msg("unable to remove `%s'", path);
Matt Kraai8810bdb2001-04-24 20:04:18 +0000106 return -1;
107 }
108
109 return 0;
110 }
111}