Major rework of the directory structure and the entire build system.
[oweals/busybox.git] / libbb / remove_file.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini remove_file implementation for busybox
4  *
5  * Copyright (C) 2001 Matt Kraai <kraai@alumni.carnegiemellon.edu>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21
22 #include <stdio.h>
23 #include <time.h>
24 #include <utime.h>
25 #include <dirent.h>
26 #include <errno.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <getopt.h>
31 #include "libbb.h"
32
33 extern int remove_file(const char *path, int flags)
34 {
35         struct stat path_stat;
36         int path_exists = 1;
37
38         if (lstat(path, &path_stat) < 0) {
39                 if (errno != ENOENT) {
40                         perror_msg("unable to stat `%s'", path);
41                         return -1;
42                 }
43
44                 path_exists = 0;
45         }
46
47         if (!path_exists) {
48                 if (!(flags & FILEUTILS_FORCE)) {
49                         perror_msg("cannot remove `%s'", path);
50                         return -1;
51                 }
52                 return 0;
53         }
54
55         if (S_ISDIR(path_stat.st_mode)) {
56                 DIR *dp;
57                 struct dirent *d;
58                 int status = 0;
59
60                 if (!(flags & FILEUTILS_RECUR)) {
61                         error_msg("%s: is a directory", path);
62                         return -1;
63                 }
64
65                 if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 &&
66                                         isatty(0)) ||
67                                 (flags & FILEUTILS_INTERACTIVE)) {
68                         fprintf(stderr, "%s: descend into directory `%s'? ", applet_name,
69                                         path);
70                         if (!ask_confirmation())
71                                 return 0;
72                 }
73
74                 if ((dp = opendir(path)) == NULL) {
75                         perror_msg("unable to open `%s'", path);
76                         return -1;
77                 }
78
79                 while ((d = readdir(dp)) != NULL) {
80                         char *new_path;
81
82                         if (strcmp(d->d_name, ".") == 0 ||
83                                         strcmp(d->d_name, "..") == 0)
84                                 continue;
85
86                         new_path = concat_path_file(path, d->d_name);
87                         if (remove_file(new_path, flags) < 0)
88                                 status = -1;
89                         free(new_path);
90                 }
91
92                 if (closedir(dp) < 0) {
93                         perror_msg("unable to close `%s'", path);
94                         return -1;
95                 }
96
97                 if (flags & FILEUTILS_INTERACTIVE) {
98                         fprintf(stderr, "%s: remove directory `%s'? ", applet_name, path);
99                         if (!ask_confirmation())
100                                 return status;
101                 }
102
103                 if (rmdir(path) < 0) {
104                         perror_msg("unable to remove `%s'", path);
105                         return -1;
106                 }
107
108                 return status;
109         } else {
110                 if ((!(flags & FILEUTILS_FORCE) && access(path, W_OK) < 0 &&
111                                         !S_ISLNK(path_stat.st_mode) &&
112                                         isatty(0)) ||
113                                 (flags & FILEUTILS_INTERACTIVE)) {
114                         fprintf(stderr, "%s: remove `%s'? ", applet_name, path);
115                         if (!ask_confirmation())
116                                 return 0;
117                 }
118
119                 if (unlink(path) < 0) {
120                         perror_msg("unable to remove `%s'", path);
121                         return -1;
122                 }
123
124                 return 0;
125         }
126 }