first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / scripts / files / safedel.pl
1 #!/usr/bin/perl
2
3 ###############################################################################
4 #                                                                             #
5 #  Name   : safedel                                                           #
6 #  Author : Chris Koeritz                                                     #
7 #  Rights : Copyright (C) 1996-$now by Author                                 #
8 #                                                                             #
9 #  Purpose:                                                                   #
10 #                                                                             #
11 #    This program moves all of the files specified on the command line        #
12 #  into the temporary storage directory, rather than just deleting them.      #
13 #                                                                             #
14 ###############################################################################
15 #  This program is free software; you can redistribute it and/or modify it    #
16 #  under the terms of the GNU General Public License as published by the Free #
17 #  Software Foundation; either version 2 of the License or (at your option)   #
18 #  any later version.  See: "http://www.gruntose.com/Info/GNU/GPL.html" for a #
19 #  version of the License.  Please send any updates to "fred@gruntose.com".   #
20 ###############################################################################
21
22 require Cwd;
23 require "ctime.pl";
24 require "filename_helper.pl";
25 require "inc_num.pl";
26 require "importenv.pl";
27 require "zap_the_dir.pl";
28
29 #hmmm: need a usage statement.
30
31 if ($#ARGV < 0) {
32   die "Too few arguments to command.";
33 }
34
35 if ($OS eq "UNIX") {
36   $FIND_ENDING = "';'";
37   $DEV_NULL = "> /dev/null";
38   $zip = "zip -y ";
39 } elsif ( ($OS eq "DOS") || ($OS eq "Windows_95")
40     || ($OS eq "Windows_98") || ($OS eq "Windows_NT") ) {
41   $FIND_ENDING = "';'";
42   $DEV_NULL = "> nul";
43   $zip = "zip ";
44 } else {
45   die "The Operating System variable (OS) is not set.\n";
46 }
47
48 # The zip program has slightly different parameters depending on the
49 # version that will be run.  The DOS version needs to see a -P to remember
50 # the directory names.
51 $use_path = '';
52 $wildcard = "";  # used for reasonable zips, like os/2 or unix.
53
54 # set the filename used for numbering.
55 local($NUMBER_FILE) = "$TMP/aa_safedel.num";
56
57 # Retrieve the current deleted file number.
58 $number = &get_number($NUMBER_FILE);
59
60 # Skip to the next one to ensure we're the only ones that ever have this one.
61 &next_number($NUMBER_FILE);
62
63 # Chomp on all the files specified.
64 &safedel(@ARGV);
65
66 exit 0;
67
68 # The safedel procedure does most of the work.
69
70 sub safedel {
71   # get the list of files and directories to whack.
72   local(@to_delete) = &glob_list(@_);
73 #  print "final list of whackees: @to_delete\n";
74
75   # we store the deleted files in a directory under the temporary directory.
76   $temp_subdir = $TMP . "/zz_del_keep";
77   if (! -d $temp_subdir) {
78     mkdir "$temp_subdir", 0777;
79       # create the subdirectory under temp if missing.
80     if (! -d $temp_subdir) {
81       die "the directory $temp_subdir could not be created!\n";
82     }
83   }
84
85   # reset the list of objects actually whacked.
86   local(@deleted) = ();
87 #  print "deleted list is @deleted\n";
88
89   # iterate over the files that we have been told to nuke.
90   foreach $file (@to_delete) {
91     # go through each object that should be deleted...
92     $file = &remove_trailing_slashes($file);
93     if (substr($file, length($file) - 1, 1) eq ":") {
94       die "removing the root directory of a drive is not permitted!";
95     }
96     if ( ($file =~ /^.*\/\.$/) || ($file =~ /^.*\/\.\.$/) ) {
97       print "ignoring attempt to remove current or parent directory.\n";
98       next;
99     }
100     $tempfile = $temp_subdir . "/temp" . "$number";
101 #   print "tempfile is $tempfile; file is $file.\n";
102     if (-d $file) {
103       # ensure there aren't any read only files.
104       system("chmod -R u+rw \"$file\"");
105       # store the directory in the trash storage.
106       system("$zip -rm$use_path $tempfile \"$file$wildcard\" $DEV_NULL");
107         # zip up the files into the safekeeper directory.
108       # recursively unlink in case zip doesn't remove the empty dir.
109       if (-d $file) {
110         # remove the directory itself if possible, since zip did not.
111         &recursively_zap_dirs($file);
112       }
113       push(@deleted, "$file");
114     } elsif (-f $file) {
115       # store the file in the trash storage.
116       system("chmod u+rw \"$file\"");
117       system("$zip -m$use_path $tempfile \"$file\" $DEV_NULL");
118       push(@deleted, "$file");
119     } else {
120       print "$0 cannot find \"$file\" to delete it.\n";
121     }
122   }
123   if (@deleted) {
124     print "Trashed [@deleted].\n";
125     open(REPORT, ">>$TMP/zz_safedel.rpt");
126
127     local($printable_date) = &ctime(time);
128     $printable_date =~ s/\n//g;
129     print REPORT $printable_date . " -- safedel: \"temp" . $number . ".zip\" <= [@deleted]\n";
130     close(REPORT);
131   } else {
132     print "No files were deleted.\n";
133   }
134 }
135