Rev 4296 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4296 | Rev 4537 | ||
---|---|---|---|
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 <getopt.h> |
36 | #include <getopt.h> |
- | 37 | #include <mem.h> |
|
- | 38 | #include <string.h> |
|
37 | 39 | ||
38 | #include "config.h" |
40 | #include "config.h" |
39 | #include "errors.h" |
41 | #include "errors.h" |
40 | #include "util.h" |
42 | #include "util.h" |
41 | #include "entry.h" |
43 | #include "entry.h" |
42 | #include "rm.h" |
44 | #include "rm.h" |
43 | #include "cmds.h" |
45 | #include "cmds.h" |
44 | 46 | ||
45 | static char *cmdname = "rm"; |
47 | static char *cmdname = "rm"; |
46 | #define RM_VERSION "0.0.1" |
48 | #define RM_VERSION "0.0.1" |
47 | 49 | ||
48 | static rm_job_t rm; |
50 | static rm_job_t rm; |
49 | 51 | ||
50 | static struct option const long_options[] = { |
52 | static struct option const long_options[] = { |
51 | { "help", no_argument, 0, 'h' }, |
53 | { "help", no_argument, 0, 'h' }, |
52 | { "version", no_argument, 0, 'v' }, |
54 | { "version", no_argument, 0, 'v' }, |
53 | { "recursive", no_argument, 0, 'r' }, |
55 | { "recursive", no_argument, 0, 'r' }, |
54 | { "force", no_argument, 0, 'f' }, |
56 | { "force", no_argument, 0, 'f' }, |
55 | { "safe", no_argument, 0, 's' }, |
57 | { "safe", no_argument, 0, 's' }, |
56 | { 0, 0, 0, 0 } |
58 | { 0, 0, 0, 0 } |
57 | }; |
59 | }; |
58 | 60 | ||
59 | static unsigned int rm_start(rm_job_t *rm) |
61 | static unsigned int rm_start(rm_job_t *rm) |
60 | { |
62 | { |
61 | rm->recursive = 0; |
63 | rm->recursive = 0; |
62 | rm->force = 0; |
64 | rm->force = 0; |
63 | rm->safe = 0; |
65 | rm->safe = 0; |
64 | 66 | ||
65 | /* Make sure we can allocate enough memory to store |
67 | /* Make sure we can allocate enough memory to store |
66 | * what is needed in the job structure */ |
68 | * what is needed in the job structure */ |
67 | if (NULL == (rm->nwd = (char *) malloc(PATH_MAX))) |
69 | if (NULL == (rm->nwd = (char *) malloc(PATH_MAX))) |
68 | return 0; |
70 | return 0; |
69 | memset(rm->nwd, 0, sizeof(rm->nwd)); |
71 | memset(rm->nwd, 0, sizeof(rm->nwd)); |
70 | 72 | ||
71 | if (NULL == (rm->owd = (char *) malloc(PATH_MAX))) |
73 | if (NULL == (rm->owd = (char *) malloc(PATH_MAX))) |
72 | return 0; |
74 | return 0; |
73 | memset(rm->owd, 0, sizeof(rm->owd)); |
75 | memset(rm->owd, 0, sizeof(rm->owd)); |
74 | 76 | ||
75 | if (NULL == (rm->cwd = (char *) malloc(PATH_MAX))) |
77 | if (NULL == (rm->cwd = (char *) malloc(PATH_MAX))) |
76 | return 0; |
78 | return 0; |
77 | memset(rm->cwd, 0, sizeof(rm->cwd)); |
79 | memset(rm->cwd, 0, sizeof(rm->cwd)); |
78 | 80 | ||
79 | chdir("."); |
81 | chdir("."); |
80 | 82 | ||
81 | if (NULL == (getcwd(rm->owd, PATH_MAX))) |
83 | if (NULL == (getcwd(rm->owd, PATH_MAX))) |
82 | return 0; |
84 | return 0; |
83 | 85 | ||
84 | return 1; |
86 | return 1; |
85 | } |
87 | } |
86 | 88 | ||
87 | static void rm_end(rm_job_t *rm) |
89 | static void rm_end(rm_job_t *rm) |
88 | { |
90 | { |
89 | if (NULL != rm->nwd) |
91 | if (NULL != rm->nwd) |
90 | free(rm->nwd); |
92 | free(rm->nwd); |
91 | 93 | ||
92 | if (NULL != rm->owd) |
94 | if (NULL != rm->owd) |
93 | free(rm->owd); |
95 | free(rm->owd); |
94 | 96 | ||
95 | if (NULL != rm->cwd) |
97 | if (NULL != rm->cwd) |
96 | free(rm->cwd); |
98 | free(rm->cwd); |
97 | 99 | ||
98 | return; |
100 | return; |
99 | } |
101 | } |
100 | 102 | ||
101 | static unsigned int rm_recursive(const char *path) |
103 | static unsigned int rm_recursive(const char *path) |
102 | { |
104 | { |
103 | int rc; |
105 | int rc; |
104 | 106 | ||
105 | /* First see if it will just go away */ |
107 | /* First see if it will just go away */ |
106 | rc = rmdir(path); |
108 | rc = rmdir(path); |
107 | if (rc == 0) |
109 | if (rc == 0) |
108 | return 0; |
110 | return 0; |
109 | 111 | ||
110 | /* Its not empty, recursively scan it */ |
112 | /* Its not empty, recursively scan it */ |
111 | cli_error(CL_ENOTSUP, |
113 | cli_error(CL_ENOTSUP, |
112 | "Can not remove %s, directory not empty", path); |
114 | "Can not remove %s, directory not empty", path); |
113 | return 1; |
115 | return 1; |
114 | } |
116 | } |
115 | 117 | ||
116 | static unsigned int rm_single(const char *path) |
118 | static unsigned int rm_single(const char *path) |
117 | { |
119 | { |
118 | if (unlink(path)) { |
120 | if (unlink(path)) { |
119 | cli_error(CL_EFAIL, "rm: could not remove file %s", path); |
121 | cli_error(CL_EFAIL, "rm: could not remove file %s", path); |
120 | return 1; |
122 | return 1; |
121 | } |
123 | } |
122 | return 0; |
124 | return 0; |
123 | } |
125 | } |
124 | 126 | ||
125 | static unsigned int rm_scope(const char *path) |
127 | static unsigned int rm_scope(const char *path) |
126 | { |
128 | { |
127 | int fd; |
129 | int fd; |
128 | DIR *dirp; |
130 | DIR *dirp; |
129 | 131 | ||
130 | dirp = opendir(path); |
132 | dirp = opendir(path); |
131 | if (dirp) { |
133 | if (dirp) { |
132 | closedir(dirp); |
134 | closedir(dirp); |
133 | return RM_DIR; |
135 | return RM_DIR; |
134 | } |
136 | } |
135 | 137 | ||
136 | fd = open(path, O_RDONLY); |
138 | fd = open(path, O_RDONLY); |
137 | if (fd > 0) { |
139 | if (fd > 0) { |
138 | close(fd); |
140 | close(fd); |
139 | return RM_FILE; |
141 | return RM_FILE; |
140 | } |
142 | } |
141 | 143 | ||
142 | return RM_BOGUS; |
144 | return RM_BOGUS; |
143 | } |
145 | } |
144 | 146 | ||
145 | /* Dispays help for rm in various levels */ |
147 | /* Dispays help for rm in various levels */ |
146 | void help_cmd_rm(unsigned int level) |
148 | void help_cmd_rm(unsigned int level) |
147 | { |
149 | { |
148 | if (level == HELP_SHORT) { |
150 | if (level == HELP_SHORT) { |
149 | printf("`%s' removes files and directories.\n", cmdname); |
151 | printf("`%s' removes files and directories.\n", cmdname); |
150 | } else { |
152 | } else { |
151 | help_cmd_rm(HELP_SHORT); |
153 | help_cmd_rm(HELP_SHORT); |
152 | printf( |
154 | printf( |
153 | "Usage: %s [options] <path>\n" |
155 | "Usage: %s [options] <path>\n" |
154 | "Options:\n" |
156 | "Options:\n" |
155 | " -h, --help A short option summary\n" |
157 | " -h, --help A short option summary\n" |
156 | " -v, --version Print version information and exit\n" |
158 | " -v, --version Print version information and exit\n" |
157 | " -r, --recursive Recursively remove sub directories\n" |
159 | " -r, --recursive Recursively remove sub directories\n" |
158 | " -f, --force Do not prompt prior to removing files\n" |
160 | " -f, --force Do not prompt prior to removing files\n" |
159 | " -s, --safe Stop if directories change during removal\n\n" |
161 | " -s, --safe Stop if directories change during removal\n\n" |
160 | "Currently, %s is under development, some options don't work.\n", |
162 | "Currently, %s is under development, some options don't work.\n", |
161 | cmdname, cmdname); |
163 | cmdname, cmdname); |
162 | } |
164 | } |
163 | return; |
165 | return; |
164 | } |
166 | } |
165 | 167 | ||
166 | /* Main entry point for rm, accepts an array of arguments */ |
168 | /* Main entry point for rm, accepts an array of arguments */ |
167 | int cmd_rm(char **argv) |
169 | int cmd_rm(char **argv) |
168 | { |
170 | { |
169 | unsigned int argc; |
171 | unsigned int argc; |
170 | unsigned int i, scope, ret = 0; |
172 | unsigned int i, scope, ret = 0; |
171 | int c, opt_ind; |
173 | int c, opt_ind; |
172 | size_t len; |
174 | size_t len; |
173 | char *buff = NULL; |
175 | char *buff = NULL; |
174 | 176 | ||
175 | argc = cli_count_args(argv); |
177 | argc = cli_count_args(argv); |
176 | 178 | ||
177 | if (argc < 2) { |
179 | if (argc < 2) { |
178 | cli_error(CL_EFAIL, |
180 | cli_error(CL_EFAIL, |
179 | "%s: insufficient arguments. Try %s --help", cmdname, cmdname); |
181 | "%s: insufficient arguments. Try %s --help", cmdname, cmdname); |
180 | return CMD_FAILURE; |
182 | return CMD_FAILURE; |
181 | } |
183 | } |
182 | 184 | ||
183 | if (!rm_start(&rm)) { |
185 | if (!rm_start(&rm)) { |
184 | cli_error(CL_ENOMEM, "%s: could not initialize", cmdname); |
186 | cli_error(CL_ENOMEM, "%s: could not initialize", cmdname); |
185 | rm_end(&rm); |
187 | rm_end(&rm); |
186 | return CMD_FAILURE; |
188 | return CMD_FAILURE; |
187 | } |
189 | } |
188 | 190 | ||
189 | for (c = 0, optind = 0, opt_ind = 0; c != -1;) { |
191 | for (c = 0, optind = 0, opt_ind = 0; c != -1;) { |
190 | c = getopt_long(argc, argv, "hvrfs", long_options, &opt_ind); |
192 | c = getopt_long(argc, argv, "hvrfs", long_options, &opt_ind); |
191 | switch (c) { |
193 | switch (c) { |
192 | case 'h': |
194 | case 'h': |
193 | help_cmd_rm(HELP_LONG); |
195 | help_cmd_rm(HELP_LONG); |
194 | return CMD_SUCCESS; |
196 | return CMD_SUCCESS; |
195 | case 'v': |
197 | case 'v': |
196 | printf("%s\n", RM_VERSION); |
198 | printf("%s\n", RM_VERSION); |
197 | return CMD_SUCCESS; |
199 | return CMD_SUCCESS; |
198 | case 'r': |
200 | case 'r': |
199 | rm.recursive = 1; |
201 | rm.recursive = 1; |
200 | break; |
202 | break; |
201 | case 'f': |
203 | case 'f': |
202 | rm.force = 1; |
204 | rm.force = 1; |
203 | break; |
205 | break; |
204 | case 's': |
206 | case 's': |
205 | rm.safe = 1; |
207 | rm.safe = 1; |
206 | break; |
208 | break; |
207 | } |
209 | } |
208 | } |
210 | } |
209 | 211 | ||
210 | if (optind == argc) { |
212 | if (optind == argc) { |
211 | cli_error(CL_EFAIL, |
213 | cli_error(CL_EFAIL, |
212 | "%s: insufficient arguments. Try %s --help", cmdname, cmdname); |
214 | "%s: insufficient arguments. Try %s --help", cmdname, cmdname); |
213 | rm_end(&rm); |
215 | rm_end(&rm); |
214 | return CMD_FAILURE; |
216 | return CMD_FAILURE; |
215 | } |
217 | } |
216 | 218 | ||
217 | i = optind; |
219 | i = optind; |
218 | while (NULL != argv[i]) { |
220 | while (NULL != argv[i]) { |
219 | len = str_size(argv[i]) + 2; |
221 | len = str_size(argv[i]) + 2; |
220 | buff = (char *) realloc(buff, len); |
222 | buff = (char *) realloc(buff, len); |
221 | if (buff == NULL) { |
223 | if (buff == NULL) { |
222 | printf("rm: out of memory\n"); |
224 | printf("rm: out of memory\n"); |
223 | ret = 1; |
225 | ret = 1; |
224 | break; |
226 | break; |
225 | } |
227 | } |
226 | memset(buff, 0, sizeof(buff)); |
228 | memset(buff, 0, sizeof(buff)); |
227 | snprintf(buff, len, argv[i]); |
229 | snprintf(buff, len, argv[i]); |
228 | 230 | ||
229 | scope = rm_scope(buff); |
231 | scope = rm_scope(buff); |
230 | switch (scope) { |
232 | switch (scope) { |
231 | case RM_BOGUS: /* FIXME */ |
233 | case RM_BOGUS: /* FIXME */ |
232 | case RM_FILE: |
234 | case RM_FILE: |
233 | ret += rm_single(buff); |
235 | ret += rm_single(buff); |
234 | break; |
236 | break; |
235 | case RM_DIR: |
237 | case RM_DIR: |
236 | if (! rm.recursive) { |
238 | if (! rm.recursive) { |
237 | printf("%s is a directory, use -r to remove it.\n", buff); |
239 | printf("%s is a directory, use -r to remove it.\n", buff); |
238 | ret ++; |
240 | ret ++; |
239 | } else { |
241 | } else { |
240 | ret += rm_recursive(buff); |
242 | ret += rm_recursive(buff); |
241 | } |
243 | } |
242 | break; |
244 | break; |
243 | } |
245 | } |
244 | i++; |
246 | i++; |
245 | } |
247 | } |
246 | 248 | ||
247 | if (NULL != buff) |
249 | if (NULL != buff) |
248 | free(buff); |
250 | free(buff); |
249 | 251 | ||
250 | rm_end(&rm); |
252 | rm_end(&rm); |
251 | 253 | ||
252 | if (ret) |
254 | if (ret) |
253 | return CMD_FAILURE; |
255 | return CMD_FAILURE; |
254 | else |
256 | else |
255 | return CMD_SUCCESS; |
257 | return CMD_SUCCESS; |
256 | } |
258 | } |
257 | 259 | ||
258 | 260 |