branchy recurses!
[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 print "list of whackees: @to_delete\n";
67
68   # we store the deleted files in a directory under the temporary directory.
69   if (! -d $TMP) { 
70     mkdir "$TMP", 0700;
71     if (! -d $TMP) {
72       die "the TMP directory $TMP could not be created!\n";
73     }
74   }
75   $temp_subdir = $TMP . "/zz_safedel_keep";
76   if (! -d $temp_subdir) {
77     mkdir "$temp_subdir", 0700;
78       # create the subdirectory under temp if missing.
79     if (! -d $temp_subdir) {
80       die "the directory $temp_subdir could not be created!\n";
81     }
82   }
83
84   # reset the list of objects actually whacked.
85   local(@deleted) = ();
86
87   # iterate over the files that we have been told to nuke.
88   foreach $file (@to_delete) {
89     # go through each object that should be deleted...
90     $file = &remove_trailing_slashes($file);
91     if (substr($file, length($file) - 1, 1) eq ":") {
92       die "removing the root directory of a drive is not permitted!";
93     }
94
95 #print "file to whack: '$file'\n";
96
97     if ( ($file =~ /^.*\/\.$/) || ($file =~ /^.*\/\.\.$/) ) {
98       print "ignoring attempt to remove current or parent directory.\n";
99       next;
100     }
101
102 #hmmm: extract this shared bit of code as new method (also in shared snarfer)
103     $date_tool = "date";
104     local($datestamp) = `$date_tool +%Y-%m-%d-%H%M`;
105     while ($datestamp =~ /[\r\n]$/) { chop $datestamp; }
106     $archive_file = $temp_subdir . "/del-$number-" . $datestamp;
107 #print "archive_file is $archive_file; file is $file.\n";
108
109     if (-d $file) {
110       # ensure there aren't any read only files.
111       system("chmod -R u+rw '$file'");
112       # store the directory in the trash storage.
113       system("$zip -rm $archive_file '$file' $DEV_NULL");
114         # zip up the files into the safekeeper directory.
115       # recursively unlink in case zip doesn't remove the empty dir.
116       if (-d $file) {
117         # remove the directory itself if possible, since zip did not.
118         &recursively_zap_dirs($file);
119       }
120       push(@deleted, "\"$file\"");
121     } elsif (-f $file) {
122 #print "about to chmod file\n";
123       # make the file writable by our user if possible (which resets any
124       # prior permissions as long as we're the owner).
125       system("chmod u+rw '$file'");
126       # store the file in the trash storage.
127 #print "about to run: system [$zip -m $archive_file '$file' $DEV_NULL]";
128       system("$zip -m $archive_file '$file' $DEV_NULL");
129       push(@deleted, "\"$file\"");
130     } else {
131       print "$0 cannot find \"$file\" to delete it.\n";
132     }
133   }
134   if (@deleted) {
135     if ($DEBUG_FEISTY_MEOW != "") {
136       print "Trashed [@deleted].\n";
137     }
138     open(REPORT, ">>$TMP/zz_safedel_report.txt");
139
140     local($printable_date) = scalar(localtime());
141 #&ctime(time);
142     $printable_date =~ s/\n//g;
143     local($just_archived_filename) = `basename "$archive_file"`;
144     while ($just_archived_filename =~ /[\r\n]$/) { chop $just_archived_filename; }
145     print REPORT "\n";
146     print REPORT $printable_date . " -- created \"" . $just_archived_filename . ".zip\"\n";
147     print REPORT $printable_date . " -- from [@deleted]\n";
148     close(REPORT);
149   } else {
150     print "No files were deleted.\n";
151   }
152 }
153