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