took out noisesome logging
[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 "filename_helper.pl";
24 require "inc_num.pl";
25 require "zap_the_dir.pl";
26
27 use Env qw(TMP OS DEBUG_FEISTY_MEOW);
28
29 #hmmm: need a usage statement.
30
31 if ($#ARGV < 0) {
32   die "Too few arguments to command.";
33 }
34
35 $DEV_NULL = "> /dev/null 2> /dev/null";
36 if ($OS eq "UNIX") {
37   $FIND_ENDING = "';'";
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   $zip = "zip ";
43 } else {
44   die "The Operating System variable (OS) is not set.\n";
45 }
46
47 # set the filename used for numbering.
48 local($NUMBER_FILE) = "$TMP/aa_safedel.num";
49
50 # Retrieve the current deleted file number.
51 $number = &get_number($NUMBER_FILE);
52
53 # Skip to the next one to ensure we're the only ones that ever have this one.
54 &next_number($NUMBER_FILE);
55
56 # Chomp on all the files specified.
57 &safedel(@ARGV);
58
59 exit 0;
60
61 # The safedel procedure does most of the work.
62
63 sub safedel {
64   # get the list of files and directories to whack.
65   local(@to_delete) = &glob_list(@_);
66 #hmmm: make this into a debug option. 
67 #print "list of whackees: @to_delete\n";
68
69   # we store the deleted files in a directory under the temporary directory.
70   if (! -d $TMP) { 
71     mkdir "$TMP", 0700;
72     if (! -d $TMP) {
73       die "the TMP directory $TMP could not be created!\n";
74     }
75   }
76   $temp_subdir = $TMP . "/zz_safedel_keep";
77   if (! -d $temp_subdir) {
78     mkdir "$temp_subdir", 0700;
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
88   # iterate over the files that we have been told to nuke.
89   foreach $file (@to_delete) {
90     # go through each object that should be deleted...
91     $file = &remove_trailing_slashes($file);
92     if (substr($file, length($file) - 1, 1) eq ":") {
93       die "removing the root directory of a drive is not permitted!";
94     }
95
96 #print "file to whack: '$file'\n";
97
98     if ( ($file =~ /^.*\/\.$/) || ($file =~ /^.*\/\.\.$/) ) {
99       print "ignoring attempt to remove current or parent directory.\n";
100       next;
101     }
102
103 #hmmm: extract this shared bit of code as new method (also in shared snarfer)
104     $date_tool = "date";
105     local($datestamp) = `$date_tool +%Y-%m-%d-%H%M`;
106     while ($datestamp =~ /[\r\n]$/) { chop $datestamp; }
107     $archive_file = $temp_subdir . "/del-$number-" . $datestamp;
108 #print "archive_file is $archive_file; file is $file.\n";
109
110     if (-d $file) {
111       # ensure there aren't any read only files.
112       system("chmod -R u+rw '$file'");
113       # store the directory in the trash storage.
114       system("$zip -rm $archive_file '$file' $DEV_NULL");
115         # zip up the files into the safekeeper directory.
116       # recursively unlink in case zip doesn't remove the empty dir.
117       if (-d $file) {
118         # remove the directory itself if possible, since zip did not.
119         &recursively_zap_dirs($file);
120       }
121       push(@deleted, "\"$file\"");
122     } elsif (-f $file) {
123 #print "about to chmod file\n";
124       # make the file writable by our user if possible (which resets any
125       # prior permissions as long as we're the owner).
126       system("chmod u+rw '$file'");
127       # store the file in the trash storage.
128 #print "about to run: system [$zip -m $archive_file '$file' $DEV_NULL]";
129       system("$zip -m $archive_file '$file' $DEV_NULL");
130       push(@deleted, "\"$file\"");
131     } else {
132       print "$0 cannot find \"$file\" to delete it.\n";
133     }
134   }
135   if (@deleted) {
136     if ($DEBUG_FEISTY_MEOW != "") {
137       print "Trashed [@deleted].\n";
138     }
139     open(REPORT, ">>$TMP/zz_safedel_report.txt");
140
141     local($printable_date) = scalar(localtime());
142 #&ctime(time);
143     $printable_date =~ s/\n//g;
144     local($just_archived_filename) = `basename "$archive_file"`;
145     while ($just_archived_filename =~ /[\r\n]$/) { chop $just_archived_filename; }
146     print REPORT "\n";
147     print REPORT $printable_date . " -- created \"" . $just_archived_filename . ".zip\"\n";
148     print REPORT $printable_date . " -- from [@deleted]\n";
149     close(REPORT);
150   } else {
151 #hmmm: oh good, and we should always bug people about nothing having been done?
152 #      this is especially tiresome when our own scripts cause safedel to be invoked,
153 #      since then they are automatically noisy and blathery.
154 #hmmm: make this into a debug option. 
155 #    print "No files were deleted.\n";
156   }
157 }
158