1 | /* |
---|
2 | * $Id: expunge.c,v 1.25 1999-01-22 23:08:59 ghudson Exp $ |
---|
3 | * |
---|
4 | * This program is part of a package including delete, undelete, |
---|
5 | * lsdel, expunge and purge. The software suite is meant as a |
---|
6 | * replacement for rm which allows for file recovery. |
---|
7 | * |
---|
8 | * Copyright (c) 1989 by the Massachusetts Institute of Technology. |
---|
9 | * For copying and distribution information, see the file "mit-copying.h." |
---|
10 | */ |
---|
11 | |
---|
12 | #if (!defined(lint) && !defined(SABER)) |
---|
13 | static char rcsid_expunge_c[] = "$Id: expunge.c,v 1.25 1999-01-22 23:08:59 ghudson Exp $"; |
---|
14 | #endif |
---|
15 | |
---|
16 | #include <stdio.h> |
---|
17 | #include <sys/types.h> |
---|
18 | #include <sys/time.h> |
---|
19 | #include <dirent.h> |
---|
20 | #include <sys/param.h> |
---|
21 | #include <string.h> |
---|
22 | #include <com_err.h> |
---|
23 | #include <errno.h> |
---|
24 | #include "col.h" |
---|
25 | #include "util.h" |
---|
26 | #include "directories.h" |
---|
27 | #include "pattern.h" |
---|
28 | #include "expunge.h" |
---|
29 | #include "shell_regexp.h" |
---|
30 | #include "mit-copying.h" |
---|
31 | #include "delete_errs.h" |
---|
32 | #include "errors.h" |
---|
33 | |
---|
34 | extern time_t current_time; |
---|
35 | |
---|
36 | extern char *whoami; |
---|
37 | |
---|
38 | time_t timev; /* minimum mod time before undeletion */ |
---|
39 | |
---|
40 | int interactive, /* query before each expunge */ |
---|
41 | recursive, /* expunge undeleted directories recursively */ |
---|
42 | noop, /* print what would be done instead of doing it */ |
---|
43 | verbose, /* print a line as each file is deleted */ |
---|
44 | force, /* do not ask for any confirmation */ |
---|
45 | listfiles, /* list files at toplevel */ |
---|
46 | yield, /* print yield of expunge at end */ |
---|
47 | f_links, /* follow symbolic links */ |
---|
48 | f_mounts; /* follow mount points */ |
---|
49 | |
---|
50 | int space_removed = 0; |
---|
51 | |
---|
52 | |
---|
53 | |
---|
54 | |
---|
55 | main(argc, argv) |
---|
56 | int argc; |
---|
57 | char *argv[]; |
---|
58 | { |
---|
59 | extern char *optarg; |
---|
60 | extern int optind; |
---|
61 | int arg; |
---|
62 | |
---|
63 | #if defined(__APPLE__) && defined(__MACH__) |
---|
64 | add_error_table(&et_del_error_table); |
---|
65 | #else |
---|
66 | initialize_del_error_table(); |
---|
67 | #endif |
---|
68 | |
---|
69 | whoami = lastpart(argv[0]); |
---|
70 | if (*whoami == 'p') { /* we're doing a purge */ |
---|
71 | if (argc > 1) { |
---|
72 | set_error(PURGE_TOO_MANY_ARGS); |
---|
73 | error(""); |
---|
74 | exit(1); |
---|
75 | } |
---|
76 | if (purge()) |
---|
77 | error("purge"); |
---|
78 | exit(error_occurred ? 1 : 0); |
---|
79 | } |
---|
80 | timev = 0; |
---|
81 | yield = interactive = recursive = noop = verbose = listfiles = force = 0; |
---|
82 | while ((arg = getopt(argc, argv, "t:irfnvlysm")) != EOF) { |
---|
83 | switch (arg) { |
---|
84 | case 't': |
---|
85 | timev = atoi(optarg); |
---|
86 | break; |
---|
87 | case 'i': |
---|
88 | interactive++; |
---|
89 | break; |
---|
90 | case 'r': |
---|
91 | recursive++; |
---|
92 | break; |
---|
93 | case 'f': |
---|
94 | force++; |
---|
95 | break; |
---|
96 | case 'n': |
---|
97 | noop++; |
---|
98 | break; |
---|
99 | case 'v': |
---|
100 | verbose++; |
---|
101 | break; |
---|
102 | case 'l': |
---|
103 | listfiles++; |
---|
104 | break; |
---|
105 | case 'y': |
---|
106 | yield++; |
---|
107 | break; |
---|
108 | case 's': |
---|
109 | f_links++; |
---|
110 | break; |
---|
111 | case 'm': |
---|
112 | f_mounts++; |
---|
113 | break; |
---|
114 | default: |
---|
115 | usage(); |
---|
116 | exit(1); |
---|
117 | } |
---|
118 | } |
---|
119 | report_errors = ! force; |
---|
120 | |
---|
121 | if (optind == argc) { |
---|
122 | char *dir; |
---|
123 | dir = "."; /* current working directory */ |
---|
124 | if (expunge(&dir, 1)) |
---|
125 | error("expunging ."); |
---|
126 | } |
---|
127 | else if (expunge(&argv[optind], argc - optind)) |
---|
128 | error("expunge"); |
---|
129 | |
---|
130 | exit((error_occurred && (! force)) ? 1 : 0); |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | |
---|
135 | |
---|
136 | |
---|
137 | purge() |
---|
138 | { |
---|
139 | char *home; |
---|
140 | int retval; |
---|
141 | |
---|
142 | home = Malloc((unsigned) MAXPATHLEN); |
---|
143 | if (! home) { |
---|
144 | set_error(errno); |
---|
145 | error("purge"); |
---|
146 | return error_code; |
---|
147 | } |
---|
148 | timev = interactive = noop = verbose = force = 0; |
---|
149 | yield = listfiles = recursive = 1; |
---|
150 | if (retval = get_home(home)) { |
---|
151 | error("purge"); |
---|
152 | return retval; |
---|
153 | } |
---|
154 | |
---|
155 | printf("Please be patient.... this may take a while.\n\n"); |
---|
156 | |
---|
157 | if (retval = expunge(&home, 1)) { |
---|
158 | error("expunge"); |
---|
159 | return retval; |
---|
160 | } |
---|
161 | return 0; |
---|
162 | } |
---|
163 | |
---|
164 | |
---|
165 | |
---|
166 | |
---|
167 | usage() |
---|
168 | { |
---|
169 | fprintf(stderr, "Usage: %s [ options ] [ filename [ ... ]]\n", whoami); |
---|
170 | fprintf(stderr, "Options are:\n"); |
---|
171 | fprintf(stderr, " -r recursive\n"); |
---|
172 | fprintf(stderr, " -i interactive\n"); |
---|
173 | fprintf(stderr, " -f force\n"); |
---|
174 | fprintf(stderr, " -t n n-day-or-older expunge\n"); |
---|
175 | fprintf(stderr, " -n noop\n"); |
---|
176 | fprintf(stderr, " -v verbose\n"); |
---|
177 | fprintf(stderr, " -l list files before expunging\n"); |
---|
178 | fprintf(stderr, " -s follow symbolic links to directories\n"); |
---|
179 | fprintf(stderr, " -m follow mount points\n"); |
---|
180 | fprintf(stderr, " -y print yield of expunge\n"); |
---|
181 | fprintf(stderr, " -- end options and start filenames\n"); |
---|
182 | } |
---|
183 | |
---|
184 | |
---|
185 | |
---|
186 | |
---|
187 | |
---|
188 | int expunge(files, num) |
---|
189 | char **files; |
---|
190 | int num; |
---|
191 | { |
---|
192 | char **found_files; |
---|
193 | int num_found; |
---|
194 | int status = 0; |
---|
195 | int total = 0; |
---|
196 | filerec *current; |
---|
197 | int retval; |
---|
198 | |
---|
199 | if (initialize_tree()) |
---|
200 | exit(1); |
---|
201 | |
---|
202 | for ( ; num ; num--) { |
---|
203 | retval = get_the_files(files[num - 1], &num_found, &found_files); |
---|
204 | if (retval) { |
---|
205 | error(files[num - 1]); |
---|
206 | return retval; |
---|
207 | } |
---|
208 | |
---|
209 | if (num_found) { |
---|
210 | num_found = process_files(found_files, num_found); |
---|
211 | if (num_found < 0) { |
---|
212 | error("process_files"); |
---|
213 | return error_code; |
---|
214 | } |
---|
215 | } |
---|
216 | |
---|
217 | total += num_found; |
---|
218 | if (! num_found) if (! force) { |
---|
219 | /* |
---|
220 | * There are three different situations here. Eiter we |
---|
221 | * are dealing with an existing directory with no |
---|
222 | * deleted files in it, or we are deleting with a |
---|
223 | * non-existing deleted file with wildcards, or we are |
---|
224 | * dealing with a non-existing deleted file without |
---|
225 | * wildcards. In the former case we print nothing, and |
---|
226 | * in the latter cases we print either "no match" or |
---|
227 | * "not found" respectively |
---|
228 | */ |
---|
229 | if (no_wildcards(files[num - 1])) { |
---|
230 | if (! directory_exists(files[num - 1])) { |
---|
231 | set_error(ENOENT); |
---|
232 | error(files[num - 1]); |
---|
233 | } |
---|
234 | } |
---|
235 | else { |
---|
236 | set_error(ENOMATCH); |
---|
237 | error(files[num - 1]); |
---|
238 | } |
---|
239 | } |
---|
240 | } |
---|
241 | if (total && listfiles) { |
---|
242 | if (retval = list_files()) { |
---|
243 | error("list_files"); |
---|
244 | return retval; |
---|
245 | } |
---|
246 | if (! force) if (! top_level()) { |
---|
247 | set_status(EXPUNGE_NOT_EXPUNGED); |
---|
248 | return error_code; |
---|
249 | } |
---|
250 | } |
---|
251 | current = get_root_tree(); |
---|
252 | if (current) { |
---|
253 | if (retval = expunge_specified(current)) { |
---|
254 | error("expunge_specified"); |
---|
255 | status = retval; |
---|
256 | } |
---|
257 | } |
---|
258 | current = get_cwd_tree(); |
---|
259 | if (current) { |
---|
260 | if (retval = expunge_specified(current)) { |
---|
261 | error("expunge_specified"); |
---|
262 | status = retval; |
---|
263 | } |
---|
264 | } |
---|
265 | if (yield) { |
---|
266 | if (noop) |
---|
267 | printf("Total that would be expunged: %dk\n", |
---|
268 | space_to_k(space_removed)); |
---|
269 | else |
---|
270 | printf("Total expunged: %dk\n", space_to_k(space_removed)); |
---|
271 | } |
---|
272 | return status; |
---|
273 | } |
---|
274 | |
---|
275 | |
---|
276 | |
---|
277 | expunge_specified(leaf) |
---|
278 | filerec *leaf; |
---|
279 | { |
---|
280 | int status = 0; |
---|
281 | int do_it = 1; |
---|
282 | int retval; |
---|
283 | |
---|
284 | if ((leaf->specified) && ((leaf->specs.st_mode & S_IFMT) == S_IFDIR)) { |
---|
285 | /* |
---|
286 | * This is static so that we don't create a copy of it for |
---|
287 | * every recursive invocation of expunge_specified. |
---|
288 | */ |
---|
289 | static char buf[MAXPATHLEN]; |
---|
290 | |
---|
291 | if (retval = get_leaf_path(leaf, buf)) { |
---|
292 | error("get_leaf_path"); |
---|
293 | return retval; |
---|
294 | } |
---|
295 | (void) convert_to_user_name(buf, buf); |
---|
296 | |
---|
297 | if (interactive) { |
---|
298 | printf("%s: Expunge directory %s? ", whoami, buf); |
---|
299 | status = (! (do_it = yes())); |
---|
300 | } |
---|
301 | } |
---|
302 | if (do_it) { |
---|
303 | if (leaf->dirs) { |
---|
304 | if (retval = expunge_specified(leaf->dirs)) { |
---|
305 | error("expunge_specified"); |
---|
306 | status = retval; |
---|
307 | } |
---|
308 | } |
---|
309 | if (leaf->files) { |
---|
310 | if (retval = expunge_specified(leaf->files)) { |
---|
311 | error("expunge_specified"); |
---|
312 | status = retval; |
---|
313 | } |
---|
314 | } |
---|
315 | } |
---|
316 | if (leaf->specified && (! status)) { |
---|
317 | if (retval = really_do_expunge(leaf)) { |
---|
318 | error("really_do_expunge"); |
---|
319 | status = retval; |
---|
320 | } |
---|
321 | } |
---|
322 | if (leaf->next) { |
---|
323 | if (retval = expunge_specified(leaf->next)) { |
---|
324 | error("expunge_specified"); |
---|
325 | status = retval; |
---|
326 | } |
---|
327 | } |
---|
328 | |
---|
329 | free_leaf(leaf); |
---|
330 | return status; |
---|
331 | } |
---|
332 | |
---|
333 | |
---|
334 | process_files(files, num) |
---|
335 | char **files; |
---|
336 | int num; |
---|
337 | { |
---|
338 | int i, skipped = 0; |
---|
339 | filerec *leaf; |
---|
340 | |
---|
341 | for (i = 0; i < num; i++) { |
---|
342 | if (add_path_to_tree(files[i], &leaf)) { |
---|
343 | error("add_path_to_tree"); |
---|
344 | return -1; |
---|
345 | } |
---|
346 | free(files[i]); |
---|
347 | if (! timed_out(leaf, current_time, timev)) { |
---|
348 | free_leaf(leaf); |
---|
349 | skipped++; |
---|
350 | } |
---|
351 | } |
---|
352 | free((char *) files); |
---|
353 | return(num-skipped); |
---|
354 | } |
---|
355 | |
---|
356 | |
---|
357 | |
---|
358 | |
---|
359 | |
---|
360 | |
---|
361 | |
---|
362 | |
---|
363 | |
---|
364 | really_do_expunge(file_ent) |
---|
365 | filerec *file_ent; |
---|
366 | { |
---|
367 | char real[MAXPATHLEN], user[MAXPATHLEN]; |
---|
368 | int status; |
---|
369 | int retval; |
---|
370 | |
---|
371 | if (retval = get_leaf_path(file_ent, real)) { |
---|
372 | error("get_leaf_path"); |
---|
373 | return retval; |
---|
374 | } |
---|
375 | (void) convert_to_user_name(real, user); |
---|
376 | |
---|
377 | if (interactive) { |
---|
378 | printf ("%s: Expunge %s (%dk)? ", whoami, user, |
---|
379 | specs_to_k(file_ent->specs)); |
---|
380 | if (! yes()) { |
---|
381 | set_status(EXPUNGE_NOT_EXPUNGED); |
---|
382 | return error_code; |
---|
383 | } |
---|
384 | } |
---|
385 | |
---|
386 | if (noop) { |
---|
387 | space_removed += specs_to_space(file_ent->specs); |
---|
388 | printf("%s: %s (%dk) would be expunged (%dk total)\n", whoami, user, |
---|
389 | specs_to_k(file_ent->specs), |
---|
390 | space_to_k(space_removed)); |
---|
391 | return 0; |
---|
392 | } |
---|
393 | |
---|
394 | if ((file_ent->specs.st_mode & S_IFMT) == S_IFDIR) |
---|
395 | status = rmdir(real); |
---|
396 | else |
---|
397 | status = unlink(real); |
---|
398 | if (! status) { |
---|
399 | space_removed += specs_to_space(file_ent->specs); |
---|
400 | if (verbose) |
---|
401 | printf("%s: %s (%dk) expunged (%dk total)\n", whoami, user, |
---|
402 | specs_to_k(file_ent->specs), |
---|
403 | space_to_k(space_removed)); |
---|
404 | return 0; |
---|
405 | } |
---|
406 | else { |
---|
407 | set_error(errno); |
---|
408 | error(real); |
---|
409 | return error_code; |
---|
410 | } |
---|
411 | } |
---|
412 | |
---|
413 | |
---|
414 | |
---|
415 | |
---|
416 | |
---|
417 | |
---|
418 | |
---|
419 | |
---|
420 | |
---|
421 | top_level() |
---|
422 | { |
---|
423 | if (interactive) { |
---|
424 | printf("The above files, which have been marked for deletion, are about to be\n"); |
---|
425 | printf("expunged forever! You will be asked for confirmation before each file is\n"); |
---|
426 | printf("deleted. Do you wish to continue [return = no]? "); |
---|
427 | } |
---|
428 | else { |
---|
429 | printf("The above files, which have been marked for deletion, are about to be\n"); |
---|
430 | printf("expunged forever! Make sure you don't need any of them before continuing.\n"); |
---|
431 | printf("Do you wish to continue [return = no]? "); |
---|
432 | } |
---|
433 | return (yes()); |
---|
434 | } |
---|
435 | |
---|
436 | |
---|
437 | |
---|
438 | |
---|
439 | |
---|
440 | list_files() |
---|
441 | { |
---|
442 | filerec *current; |
---|
443 | char **strings; |
---|
444 | int num; |
---|
445 | int retval; |
---|
446 | |
---|
447 | strings = (char **) Malloc(sizeof(char *)); |
---|
448 | num = 0; |
---|
449 | if (! strings) { |
---|
450 | set_error(errno); |
---|
451 | error("Malloc"); |
---|
452 | return error_code; |
---|
453 | } |
---|
454 | |
---|
455 | printf("The following deleted files are going to be expunged: \n\n"); |
---|
456 | |
---|
457 | current = get_root_tree(); |
---|
458 | if (retval = accumulate_names(current, &strings, &num)) { |
---|
459 | error("accumulate_names"); |
---|
460 | return retval; |
---|
461 | } |
---|
462 | current = get_cwd_tree(); |
---|
463 | if (retval = accumulate_names(current, &strings, &num)) { |
---|
464 | error("accumulate_names"); |
---|
465 | return retval; |
---|
466 | } |
---|
467 | if (retval = column_array(strings, num, DEF_SCR_WIDTH, 0, 0, 2, 1, 0, |
---|
468 | 1, stdout)) { |
---|
469 | error("column_array"); |
---|
470 | return retval; |
---|
471 | } |
---|
472 | |
---|
473 | printf("\n"); |
---|
474 | return(0); |
---|
475 | } |
---|
476 | |
---|
477 | |
---|
478 | |
---|
479 | |
---|
480 | |
---|
481 | int get_the_files(name, num_found, found) |
---|
482 | char *name; |
---|
483 | int *num_found; |
---|
484 | char ***found; |
---|
485 | { |
---|
486 | int retval; |
---|
487 | int options; |
---|
488 | |
---|
489 | options = FIND_DELETED | FIND_CONTENTS | RECURS_DELETED; |
---|
490 | if (recursive) |
---|
491 | options |= RECURS_FIND_DELETED; |
---|
492 | if (f_mounts) |
---|
493 | options |= FOLLW_MOUNTPOINTS; |
---|
494 | if (f_links) |
---|
495 | options |= FOLLW_LINKS; |
---|
496 | |
---|
497 | retval = find_matches(name, num_found, found, options); |
---|
498 | if (retval) { |
---|
499 | error("find_matches"); |
---|
500 | return retval; |
---|
501 | } |
---|
502 | |
---|
503 | return 0; |
---|
504 | } |
---|