ae6f81387451a48e52010e45688187a26288b0cf
[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);
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 # The zip program has slightly different parameters depending on the
48 # version that will be run.  The DOS version needs to see a -P to remember
49 # the directory names.
50 $use_path = '';
51 $wildcard = "";  # used for reasonable zips, like os/2 or unix.
52
53 # set the filename used for numbering.
54 local($NUMBER_FILE) = "$TMP/aa_safedel.num";
55
56 # Retrieve the current deleted file number.
57 $number = &get_number($NUMBER_FILE);
58
59 # Skip to the next one to ensure we're the only ones that ever have this one.
60 &next_number($NUMBER_FILE);
61
62 # Chomp on all the files specified.
63 &safedel(@ARGV);
64
65 exit 0;
66
67 # The safedel procedure does most of the work.
68
69 sub safedel {
70   # get the list of files and directories to whack.
71   local(@to_delete) = &glob_list(@_);
72 #  print "final list of whackees: @to_delete\n";
73
74   # we store the deleted files in a directory under the temporary directory.
75   if (! -d $TMP) { 
76     mkdir "$TMP", 0700;
77     if (! -d $TMP) {
78       die "the TMP directory $TMP could not be created!\n";
79     }
80   }
81   $temp_subdir = $TMP . "/zz_safedel_keep";
82   if (! -d $temp_subdir) {
83     mkdir "$temp_subdir", 0700;
84       # create the subdirectory under temp if missing.
85     if (! -d $temp_subdir) {
86       die "the directory $temp_subdir could not be created!\n";
87     }
88   }
89
90   # reset the list of objects actually whacked.
91   local(@deleted) = ();
92 #  print "deleted list is @deleted\n";
93
94   # iterate over the files that we have been told to nuke.
95   foreach $file (@to_delete) {
96     # go through each object that should be deleted...
97     $file = &remove_trailing_slashes($file);
98     if (substr($file, length($file) - 1, 1) eq ":") {
99       die "removing the root directory of a drive is not permitted!";
100     }
101     if ( ($file =~ /^.*\/\.$/) || ($file =~ /^.*\/\.\.$/) ) {
102       print "ignoring attempt to remove current or parent directory.\n";
103       next;
104     }
105     $tempfile = $temp_subdir . "/temp" . "$number";
106 #print "tempfile is $tempfile; file is $file.\n";
107     if (-d $file) {
108       # ensure there aren't any read only files.
109       system("chmod -R u+rw \"$file\"");
110       # store the directory in the trash storage.
111       system("$zip -rm $use_path $tempfile \"$file$wildcard\" $DEV_NULL");
112         # zip up the files into the safekeeper directory.
113       # recursively unlink in case zip doesn't remove the empty dir.
114       if (-d $file) {
115         # remove the directory itself if possible, since zip did not.
116         &recursively_zap_dirs($file);
117       }
118       push(@deleted, "$file");
119     } elsif (-f $file) {
120       # store the file in the trash storage.
121       system("chmod u+rw \"$file\"");
122
123 #print "about to run: system [$zip -m$use_path $tempfile '$file' $DEV_NULL]";
124       system("$zip -m$use_path $tempfile \"$file\" $DEV_NULL");
125       push(@deleted, "$file");
126     } else {
127       print "$0 cannot find \"$file\" to delete it.\n";
128     }
129   }
130   if (@deleted) {
131     print "Trashed [@deleted].\n";
132     open(REPORT, ">>$TMP/zz_safedel.rpt");
133
134     local($printable_date) = scalar(localtime());
135 #&ctime(time);
136     $printable_date =~ s/\n//g;
137     print REPORT $printable_date . " -- safedel: \"temp" . $number . ".zip\" <= [@deleted]\n";
138     close(REPORT);
139   } else {
140     print "No files were deleted.\n";
141   }
142 }
143