Rev 3296 | Rev 3338 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3296 | Rev 3337 | ||
---|---|---|---|
1 | /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> |
1 | /* Copyright (c) 2008, Tim Post <tinkertim@gmail.com> |
2 | * All rights reserved. |
2 | * All rights reserved. |
3 | * |
3 | * |
4 | * Redistribution and use in source and binary forms, with or without |
4 | * Redistribution and use in source and binary forms, with or without |
5 | * modification, are permitted provided that the following conditions are met: |
5 | * modification, are permitted provided that the following conditions are met: |
6 | * |
6 | * |
7 | * Redistributions of source code must retain the above copyright notice, this |
7 | * Redistributions of source code must retain the above copyright notice, this |
8 | * list of conditions and the following disclaimer. |
8 | * list of conditions and the following disclaimer. |
9 | * |
9 | * |
10 | * Redistributions in binary form must reproduce the above copyright notice, |
10 | * Redistributions in binary form must reproduce the above copyright notice, |
11 | * this list of conditions and the following disclaimer in the documentation |
11 | * this list of conditions and the following disclaimer in the documentation |
12 | * and/or other materials provided with the distribution. |
12 | * and/or other materials provided with the distribution. |
13 | * |
13 | * |
14 | * Neither the name of the original program's authors nor the names of its |
14 | * Neither the name of the original program's authors nor the names of its |
15 | * contributors may be used to endorse or promote products derived from this |
15 | * contributors may be used to endorse or promote products derived from this |
16 | * software without specific prior written permission. |
16 | * software without specific prior written permission. |
17 | * |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
22 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
23 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
24 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
25 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
26 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
27 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
28 | * POSSIBILITY OF SUCH DAMAGE. |
28 | * POSSIBILITY OF SUCH DAMAGE. |
29 | */ |
29 | */ |
30 | 30 | ||
31 | #include <stdio.h> |
31 | #include <stdio.h> |
32 | #include <stdlib.h> |
32 | #include <stdlib.h> |
33 | #include <unistd.h> |
33 | #include <unistd.h> |
34 | #include <fcntl.h> |
34 | #include <fcntl.h> |
35 | #include <dirent.h> |
35 | #include <dirent.h> |
- | 36 | #include <assert.h> |
|
- | 37 | #include <getopt.h> |
|
- | 38 | ||
36 | #include "config.h" |
39 | #include "config.h" |
37 | #include "errors.h" |
40 | #include "errors.h" |
38 | #include "util.h" |
41 | #include "util.h" |
39 | #include "entry.h" |
42 | #include "entry.h" |
40 | #include "rm.h" |
43 | #include "rm.h" |
41 | #include "cmds.h" |
44 | #include "cmds.h" |
42 | 45 | ||
43 | static char *cmdname = "rm"; |
46 | static char *cmdname = "rm"; |
- | 47 | #define RM_VERSION "0.0.1" |
|
- | 48 | ||
- | 49 | static rm_job_t rm; |
|
- | 50 | ||
- | 51 | static struct option long_options[] = { |
|
- | 52 | { "help", no_argument, 0, 'h' }, |
|
- | 53 | { "version", no_argument, 0, 'v' }, |
|
- | 54 | { "recursive", no_argument, 0, 'r' }, |
|
- | 55 | { "force", no_argument, 0, 'f' }, |
|
- | 56 | { "safe", no_argument, 0, 's' }, |
|
- | 57 | { 0, 0, 0, 0 } |
|
- | 58 | }; |
|
- | 59 | ||
- | 60 | unsigned int rm_start(rm_job_t *rm) |
|
- | 61 | { |
|
- | 62 | rm->recursive = 0; |
|
- | 63 | rm->force = 0; |
|
- | 64 | rm->safe = 0; |
|
- | 65 | ||
- | 66 | /* Make sure we can allocate enough memory to store |
|
- | 67 | * what is needed in the job structure */ |
|
- | 68 | if (NULL == (rm->nwd = (char *) malloc(PATH_MAX))) |
|
- | 69 | return 0; |
|
- | 70 | memset(rm->nwd, 0, sizeof(rm->nwd)); |
|
- | 71 | ||
- | 72 | if (NULL == (rm->owd = (char *) malloc(PATH_MAX))) |
|
- | 73 | return 0; |
|
- | 74 | memset(rm->owd, 0, sizeof(rm->owd)); |
|
- | 75 | ||
- | 76 | if (NULL == (rm->cwd = (char *) malloc(PATH_MAX))) |
|
- | 77 | return 0; |
|
- | 78 | memset(rm->cwd, 0, sizeof(rm->cwd)); |
|
- | 79 | ||
- | 80 | chdir("."); |
|
- | 81 | ||
- | 82 | if (NULL == (getcwd(rm->owd, PATH_MAX))) |
|
- | 83 | return 0; |
|
- | 84 | ||
- | 85 | return 1; |
|
- | 86 | } |
|
- | 87 | ||
- | 88 | void rm_end(rm_job_t *rm) |
|
- | 89 | { |
|
- | 90 | if (NULL != rm->nwd) |
|
- | 91 | free(rm->nwd); |
|
- | 92 | ||
- | 93 | if (NULL != rm->owd) |
|
- | 94 | free(rm->owd); |
|
- | 95 | ||
- | 96 | if (NULL != rm->cwd) |
|
- | 97 | free(rm->cwd); |
|
- | 98 | ||
- | 99 | return; |
|
- | 100 | } |
|
44 | 101 | ||
45 | unsigned int rm_recursive(const char *path) |
102 | unsigned int rm_recursive(const char *path) |
46 | { |
103 | { |
- | 104 | int rc; |
|
- | 105 | ||
- | 106 | /* First see if it will just go away */ |
|
- | 107 | rc = rmdir(path); |
|
- | 108 | if (rc == 0) |
|
- | 109 | return 0; |
|
- | 110 | ||
- | 111 | /* Its not empty, recursively scan it */ |
|
- | 112 | cli_error(CL_ENOTSUP, |
|
47 | cli_error(CL_ENOTSUP, "I do not yet remove directories"); |
113 | "Can not remove %s, directory not empty", path); |
48 | return 1; |
114 | return 1; |
49 | } |
115 | } |
50 | 116 | ||
51 | unsigned int rm_single(const char *path) |
117 | unsigned int rm_single(const char *path) |
52 | { |
118 | { |
53 | if (unlink(path)) { |
119 | if (unlink(path)) { |
54 | cli_error(CL_EFAIL, "rm: could not remove file %s", path); |
120 | cli_error(CL_EFAIL, "rm: could not remove file %s", path); |
55 | return 1; |
121 | return 1; |
56 | } |
122 | } |
57 | return 0; |
123 | return 0; |
58 | } |
124 | } |
59 | 125 | ||
60 | unsigned int rm_scope(const char *path) |
126 | unsigned int rm_scope(const char *path) |
61 | { |
127 | { |
62 | int fd; |
128 | int fd; |
63 | DIR *dirp; |
129 | DIR *dirp; |
64 | 130 | ||
65 | dirp = opendir(path); |
131 | dirp = opendir(path); |
66 | if (dirp) { |
132 | if (dirp) { |
67 | closedir(dirp); |
133 | closedir(dirp); |
68 | return RM_DIR; |
134 | return RM_DIR; |
69 | } |
135 | } |
70 | 136 | ||
71 | fd = open(path, O_RDONLY); |
137 | fd = open(path, O_RDONLY); |
72 | if (fd > 0) { |
138 | if (fd > 0) { |
73 | close(fd); |
139 | close(fd); |
74 | return RM_FILE; |
140 | return RM_FILE; |
75 | } |
141 | } |
76 | 142 | ||
77 | return RM_BOGUS; |
143 | return RM_BOGUS; |
78 | } |
144 | } |
79 | 145 | ||
80 | /* Dispays help for rm in various levels */ |
146 | /* Dispays help for rm in various levels */ |
81 | void * help_cmd_rm(unsigned int level) |
147 | void * help_cmd_rm(unsigned int level) |
82 | { |
148 | { |
83 | if (level == HELP_SHORT) { |
149 | if (level == HELP_SHORT) { |
84 | printf("`%s' removes files and directories.\n", cmdname); |
150 | printf("`%s' removes files and directories.\n", cmdname); |
85 | } else { |
151 | } else { |
86 | help_cmd_rm(HELP_SHORT); |
152 | help_cmd_rm(HELP_SHORT); |
- | 153 | printf( |
|
- | 154 | "Usage: %s [options] <path>\n" |
|
- | 155 | "Options:\n" |
|
- | 156 | " -h, --help A short option summary\n" |
|
- | 157 | " -v, --version Print version information and exit\n" |
|
- | 158 | " -r, --recursive Recursively remove sub directories\n" |
|
- | 159 | " -f, --force Do not prompt prior to removing files\n" |
|
87 | printf(" %s <path> If <path> is a directory, all files and " |
160 | " -s, --safe Stop if directories change during removal\n\n" |
88 | "directories\n within will be unconditionally removed.\n", |
161 | "Currently, %s is under development, some options don't work.\n", |
89 | cmdname); |
162 | cmdname, cmdname); |
90 | } |
163 | } |
91 | return CMD_VOID; |
164 | return CMD_VOID; |
92 | } |
165 | } |
93 | 166 | ||
94 | /* Main entry point for rm, accepts an array of arguments */ |
167 | /* Main entry point for rm, accepts an array of arguments */ |
95 | int * cmd_rm(char **argv) |
168 | int * cmd_rm(char **argv) |
96 | { |
169 | { |
97 | unsigned int argc; |
170 | unsigned int argc; |
98 | unsigned int i, scope, ret = 0; |
171 | unsigned int i, scope, ret = 0; |
- | 172 | int c, opt_ind; |
|
- | 173 | size_t len; |
|
99 | char *buff; |
174 | char *buff = NULL; |
100 | 175 | ||
101 | /* Count the arguments */ |
- | |
102 | for (argc = 0; argv[argc] != NULL; argc ++); |
176 | for (argc = 0; argv[argc] != NULL; argc ++); |
103 | - | ||
104 | if (argc < 2) { |
177 | if (argc < 2) { |
105 | cli_error(CL_EFAIL, |
178 | cli_error(CL_EFAIL, |
106 | "%s - insufficient arguments. Try `help %s extended'", |
179 | "%s: insufficient arguments. Try %s --help", cmdname, cmdname); |
- | 180 | return CMD_FAILURE; |
|
- | 181 | } |
|
- | 182 | ||
- | 183 | if (!rm_start(&rm)) { |
|
- | 184 | cli_error(CL_ENOMEM, "%s: could not initialize", cmdname); |
|
- | 185 | rm_end(&rm); |
|
- | 186 | return CMD_FAILURE; |
|
- | 187 | } |
|
- | 188 | ||
- | 189 | for (c = 0, optind = 0, opt_ind = 0; c != -1;) { |
|
- | 190 | c = getopt_long(argc, argv, "hvrfs", long_options, &opt_ind); |
|
- | 191 | switch (c) { |
|
- | 192 | case 'h': |
|
107 | cmdname, cmdname); |
193 | help_cmd_rm(HELP_LONG); |
- | 194 | return CMD_SUCCESS; |
|
- | 195 | case 'v': |
|
- | 196 | printf("%s\n", RM_VERSION); |
|
- | 197 | return CMD_SUCCESS; |
|
- | 198 | case 'r': |
|
- | 199 | rm.recursive = 1; |
|
- | 200 | break; |
|
- | 201 | case 'f': |
|
- | 202 | rm.force = 1; |
|
- | 203 | break; |
|
- | 204 | case 's': |
|
- | 205 | rm.safe = 1; |
|
- | 206 | break; |
|
- | 207 | } |
|
- | 208 | } |
|
- | 209 | ||
- | 210 | if (optind == argc) { |
|
- | 211 | cli_error(CL_EFAIL, |
|
- | 212 | "%s: insufficient arguments. Try %s --help", cmdname, cmdname); |
|
- | 213 | rm_end(&rm); |
|
108 | return CMD_FAILURE; |
214 | return CMD_FAILURE; |
109 | } |
215 | } |
110 | 216 | ||
- | 217 | i = optind; |
|
111 | for (i = 1; i < argc; i++) { |
218 | while (NULL != argv[i]) { |
112 | buff = cli_strdup(argv[i]); |
219 | len = strlen(argv[i]) + 2; |
- | 220 | buff = (char *) realloc(buff, len); |
|
- | 221 | assert(buff != NULL); |
|
- | 222 | memset(buff, 0, sizeof(buff)); |
|
- | 223 | snprintf(buff, len, argv[i]); |
|
- | 224 | ||
113 | scope = rm_scope(buff); |
225 | scope = rm_scope(buff); |
114 | switch (scope) { |
226 | switch (scope) { |
115 | case RM_BOGUS: |
227 | case RM_BOGUS: /* FIXME */ |
116 | cli_error(CL_ENOENT, "Can not unlink %s", buff); |
- | |
117 | break; |
- | |
118 | case RM_FILE: |
228 | case RM_FILE: |
119 | ret += rm_single(buff); |
229 | ret += rm_single(buff); |
120 | break; |
230 | break; |
121 | case RM_DIR: |
231 | case RM_DIR: |
- | 232 | if (! rm.recursive) { |
|
- | 233 | printf("%s is a directory, use -r to remove it.\n", buff); |
|
- | 234 | ret ++; |
|
- | 235 | } else { |
|
122 | ret += rm_recursive(buff); |
236 | ret += rm_recursive(buff); |
- | 237 | } |
|
123 | break; |
238 | break; |
124 | } |
239 | } |
125 | free(buff); |
240 | i++; |
126 | } |
241 | } |
127 | 242 | ||
- | 243 | if (NULL != buff) |
|
- | 244 | free(buff); |
|
- | 245 | ||
- | 246 | rm_end(&rm); |
|
- | 247 | ||
128 | if (ret) |
248 | if (ret) |
129 | return CMD_FAILURE; |
249 | return CMD_FAILURE; |
130 | else |
250 | else |
131 | return CMD_SUCCESS; |
251 | return CMD_SUCCESS; |
132 | } |
252 | } |
133 | 253 | ||
134 | 254 |