source: trunk/athena/bin/delete/expunge.c @ 23660

Revision 23660, 11.0 KB checked in by broder, 15 years ago (diff)
In delete: * Apply patches from jik: - Change the ENOMATCH error constant to DELETE_ENOMATCH to avoid conflicting with a system error constant of the same name
RevLine 
[1704]1/*
[12350]2 * $Id: expunge.c,v 1.25 1999-01-22 23:08:59 ghudson Exp $
[1704]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.
[4505]9 * For copying and distribution information, see the file "mit-copying.h."
[1704]10 */
11
12#if (!defined(lint) && !defined(SABER))
[12350]13     static char rcsid_expunge_c[] = "$Id: expunge.c,v 1.25 1999-01-22 23:08:59 ghudson Exp $";
[1704]14#endif
15
16#include <stdio.h>
17#include <sys/types.h>
18#include <sys/time.h>
[5049]19#include <dirent.h>
[1704]20#include <sys/param.h>
[3049]21#include <string.h>
[2179]22#include <com_err.h>
23#include <errno.h>
[1732]24#include "col.h"
[1704]25#include "util.h"
[4415]26#include "directories.h"
[1704]27#include "pattern.h"
28#include "expunge.h"
[2179]29#include "shell_regexp.h"
[4505]30#include "mit-copying.h"
[2179]31#include "delete_errs.h"
32#include "errors.h"
[1704]33
[2221]34extern time_t current_time;
[1704]35
[12144]36extern char *whoami;
[1704]37
[2221]38time_t timev;           /* minimum mod time before undeletion */
39
40int  interactive,       /* query before each expunge */
[1704]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 */
[1732]45     listfiles,         /* list files at toplevel */
[2221]46     yield,             /* print yield of expunge at end */
47     f_links,           /* follow symbolic links */
48     f_mounts;          /* follow mount points */
[1704]49
[4633]50int space_removed = 0;
[1704]51
52
53
54
55main(argc, argv)
56int argc;
57char *argv[];
58{
59     extern char *optarg;
60     extern int optind;
61     int arg;
62
[23271]63#if defined(__APPLE__) && defined(__MACH__)
64     add_error_table(&et_del_error_table);
65#else
[2179]66     initialize_del_error_table();
[23271]67#endif
[2179]68     
[1704]69     whoami = lastpart(argv[0]);
70     if (*whoami == 'p') { /* we're doing a purge */
[2519]71          if (argc > 1) {
72               set_error(PURGE_TOO_MANY_ARGS);
73               error("");
74               exit(1);
75          }
[2179]76          if (purge())
77               error("purge");
78          exit(error_occurred ? 1 : 0);
[1704]79     }
[1732]80     timev = 0;
81     yield = interactive = recursive = noop = verbose = listfiles = force = 0;
[2221]82     while ((arg = getopt(argc, argv, "t:irfnvlysm")) != EOF) {
[1704]83          switch (arg) {
84          case 't':
[1732]85               timev = atoi(optarg);
[1704]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;
[1732]105          case 'y':
106               yield++;
107               break;
[2221]108          case 's':
109               f_links++;
110               break;
111          case 'm':
112               f_mounts++;
113               break;
[1704]114          default:
115               usage();
116               exit(1);
117          }
118     }
[2221]119     report_errors = ! force;
120     
[1704]121     if (optind == argc) {
122          char *dir;
[2179]123          dir = "."; /* current working directory */
124          if (expunge(&dir, 1))
125               error("expunging .");
[1704]126     }
[2179]127     else if (expunge(&argv[optind], argc - optind))
128          error("expunge");
129
130     exit((error_occurred && (! force)) ? 1 : 0);
[1704]131}
132
133
134
135
136
137purge()
138{
[2179]139     char *home;
140     int retval;
141     
[2367]142     home = Malloc((unsigned) MAXPATHLEN);
[2179]143     if (! home) {
144          set_error(errno);
145          error("purge");
146          return error_code;
[1704]147     }
[1732]148     timev = interactive = noop = verbose = force = 0;
149     yield = listfiles = recursive = 1;
[2179]150     if (retval = get_home(home)) {
151          error("purge");
152          return retval;
[1704]153     }
154
155     printf("Please be patient.... this may take a while.\n\n");
[2179]156
157     if (retval = expunge(&home, 1)) {
158          error("expunge");
159          return retval;
160     }
[2221]161     return 0;
[1704]162}
163
164
165
166
167usage()
168{
[2221]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");
[1704]182}
183
184
185
186
187
[2179]188int expunge(files, num)
[1704]189char **files;
190int num;
191{
192     char **found_files;
193     int num_found;
194     int status = 0;
195     int total = 0;
196     filerec *current;
[2179]197     int retval;
[1704]198     
199     if (initialize_tree())
200          exit(1);
201
202     for ( ; num ; num--) {
[2221]203          retval = get_the_files(files[num - 1], &num_found, &found_files);
[2179]204          if (retval) {
205               error(files[num - 1]);
206               return retval;
207          }
208               
209          if (num_found) {
[1732]210               num_found = process_files(found_files, num_found);
[2179]211               if (num_found < 0) {
212                    error("process_files");
213                    return error_code;
214               }
215          }
216         
[1732]217          total += num_found;
218          if (! num_found) if (! force) {
[1753]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                */
[2221]229               if (no_wildcards(files[num - 1])) {
[1753]230                    if (! directory_exists(files[num - 1])) {
[2179]231                         set_error(ENOENT);
232                         error(files[num - 1]);
[1753]233                    }
234               }
235               else {
[23660]236                    set_error(DELETE_ENOMATCH);
[2179]237                    error(files[num - 1]);
[1753]238               }
[1704]239          }
240     }
241     if (total && listfiles) {
[2179]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          }
[1704]250     }
[1732]251     current = get_root_tree();
[2179]252     if (current) {
253          if (retval = expunge_specified(current)) {
254               error("expunge_specified");
255               status = retval;
256          }
257     }
[1732]258     current = get_cwd_tree();
[2179]259     if (current) {
260          if (retval = expunge_specified(current)) {
261               error("expunge_specified");
262               status = retval;
263          }
264     }
[1732]265     if (yield) {
266          if (noop)
267               printf("Total that would be expunged: %dk\n",
[4633]268                      space_to_k(space_removed));
[1732]269          else
[4633]270               printf("Total expunged: %dk\n", space_to_k(space_removed));
[1732]271     }
[2179]272     return status;
[1704]273}
274
275
276
[1732]277expunge_specified(leaf)
278filerec *leaf;
279{
280     int status = 0;
[2179]281     int do_it = 1;
282     int retval;
283     
284     if ((leaf->specified) && ((leaf->specs.st_mode & S_IFMT) == S_IFDIR)) {
[6420]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];
[1704]290
[2179]291          if (retval = get_leaf_path(leaf, buf)) {
292               error("get_leaf_path");
293               return retval;
294          }
[2221]295          (void) convert_to_user_name(buf, buf);
[2179]296
297          if (interactive) {
298               printf("%s: Expunge directory %s? ", whoami, buf);
299               status = (! (do_it = yes()));
300          }
[1850]301     }
[2179]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          }
[1732]315     }
[2179]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
[1732]329     free_leaf(leaf);
[2179]330     return status;
[1732]331}
[1704]332
[1732]333
[1704]334process_files(files, num)
335char **files;
336int num;
337{
[1831]338     int i, skipped = 0;
[1732]339     filerec *leaf;
340     
[1704]341     for (i = 0; i < num; i++) {
[2179]342          if (add_path_to_tree(files[i], &leaf)) {
343               error("add_path_to_tree");
344               return -1;
[1704]345          }
[1732]346          free(files[i]);
347          if (! timed_out(leaf, current_time, timev)) {
348               free_leaf(leaf);
[1831]349               skipped++;
[1704]350          }
351     }
[2179]352     free((char *) files);
[1831]353     return(num-skipped);
[1704]354}
355
356
357
358
359
360
361
362
363
364really_do_expunge(file_ent)
365filerec *file_ent;
366{
367     char real[MAXPATHLEN], user[MAXPATHLEN];
368     int status;
[2179]369     int retval;
[1704]370     
[2179]371     if (retval = get_leaf_path(file_ent, real)) {
372          error("get_leaf_path");
373          return retval;
374     }
[2221]375     (void) convert_to_user_name(real, user);
[1704]376
377     if (interactive) {
[1732]378          printf ("%s: Expunge %s (%dk)? ", whoami, user,
[4633]379                  specs_to_k(file_ent->specs));
[2179]380          if (! yes()) {
381               set_status(EXPUNGE_NOT_EXPUNGED);
382               return error_code;
383          }
[1704]384     }
385
386     if (noop) {
[4633]387          space_removed += specs_to_space(file_ent->specs);
[1732]388          printf("%s: %s (%dk) would be expunged (%dk total)\n", whoami, user,
[4633]389                 specs_to_k(file_ent->specs),
390                 space_to_k(space_removed));
[2179]391          return 0;
[1704]392     }
393
[1732]394     if ((file_ent->specs.st_mode & S_IFMT) == S_IFDIR)
[1704]395          status = rmdir(real);
396     else
397          status = unlink(real);
398     if (! status) {
[4633]399          space_removed += specs_to_space(file_ent->specs);
[1704]400          if (verbose)
[1732]401               printf("%s: %s (%dk) expunged (%dk total)\n", whoami, user,
[4633]402                      specs_to_k(file_ent->specs),
403                      space_to_k(space_removed));
[2179]404          return 0;
[1704]405     }
406     else {
[2179]407          set_error(errno);
408          error(real);
409          return error_code;
[1704]410     }
411}
412
413
414
415
416
417
418
419
420
421top_level()
422{
423     if (interactive) {
424printf("The above files, which have been marked for deletion, are about to be\n");
425printf("expunged forever!  You will be asked for confirmation before each file is\n");
[1757]426printf("deleted.  Do you wish to continue [return = no]? ");
[1704]427     }
428     else {
429printf("The above files, which have been marked for deletion, are about to be\n");
430printf("expunged forever!  Make sure you don't need any of them before continuing.\n");
[1757]431printf("Do you wish to continue [return = no]? ");
[1704]432     }
433     return (yes());
434}
435
[1732]436
437
438
439
[1704]440list_files()
441{
442     filerec *current;
[1732]443     char **strings;
444     int num;
[2179]445     int retval;
[1704]446     
[2367]447     strings = (char **) Malloc(sizeof(char *));
[1732]448     num = 0;
449     if (! strings) {
[2179]450          set_error(errno);
[2367]451          error("Malloc");
[2179]452          return error_code;
[1732]453     }
[2179]454
[1704]455     printf("The following deleted files are going to be expunged: \n\n");
456
457     current = get_root_tree();
[2179]458     if (retval = accumulate_names(current, &strings, &num)) {
459          error("accumulate_names");
460          return retval;
461     }
[1704]462     current = get_cwd_tree();
[2179]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     
[1704]473     printf("\n");
474     return(0);
475}
476     
477
478
479
480
[2221]481int get_the_files(name, num_found, found)
482char *name;
[1704]483int *num_found;
[2179]484char ***found;
[1704]485{
[2179]486     int retval;
[2221]487     int options;
[1704]488     
[2221]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;
[1704]496     
[2221]497     retval = find_matches(name, num_found, found, options);
498     if (retval) {
[2179]499          error("find_matches");
500          return retval;
501     }
[2221]502
[2179]503     return 0;
[1704]504}
Note: See TracBrowser for help on using the repository browser.