branchy recurses!
[feisty_meow.git] / scripts / files / safedel.pl
index 44288b414e9d1cd6dadffcf68e57b1083a25919f..010cd959d83976bb61263867142a85e291135605 100644 (file)
 ###############################################################################
 
 require Cwd;
-require "ctime.pl";
 require "filename_helper.pl";
 require "inc_num.pl";
-require "importenv.pl";
 require "zap_the_dir.pl";
 
+use Env qw(TMP OS DEBUG_FEISTY_MEOW);
+
 #hmmm: need a usage statement.
 
 if ($#ARGV < 0) {
   die "Too few arguments to command.";
 }
 
-$DEV_NULL = "&> /dev/null";
+$DEV_NULL = "> /dev/null 2> /dev/null";
 if ($OS eq "UNIX") {
   $FIND_ENDING = "';'";
   $zip = "zip -y ";
@@ -44,12 +44,6 @@ if ($OS eq "UNIX") {
   die "The Operating System variable (OS) is not set.\n";
 }
 
-# The zip program has slightly different parameters depending on the
-# version that will be run.  The DOS version needs to see a -P to remember
-# the directory names.
-$use_path = '';
-$wildcard = "";  # used for reasonable zips, like os/2 or unix.
-
 # set the filename used for numbering.
 local($NUMBER_FILE) = "$TMP/aa_safedel.num";
 
@@ -69,12 +63,18 @@ exit 0;
 sub safedel {
   # get the list of files and directories to whack.
   local(@to_delete) = &glob_list(@_);
-#  print "final list of whackees: @to_delete\n";
+print "list of whackees: @to_delete\n";
 
   # we store the deleted files in a directory under the temporary directory.
-  $temp_subdir = $TMP . "/zz_del_keep";
+  if (! -d $TMP) { 
+    mkdir "$TMP", 0700;
+    if (! -d $TMP) {
+      die "the TMP directory $TMP could not be created!\n";
+    }
+  }
+  $temp_subdir = $TMP . "/zz_safedel_keep";
   if (! -d $temp_subdir) {
-    mkdir "$temp_subdir", 0777;
+    mkdir "$temp_subdir", 0700;
       # create the subdirectory under temp if missing.
     if (! -d $temp_subdir) {
       die "the directory $temp_subdir could not be created!\n";
@@ -83,7 +83,6 @@ sub safedel {
 
   # reset the list of objects actually whacked.
   local(@deleted) = ();
-#  print "deleted list is @deleted\n";
 
   # iterate over the files that we have been told to nuke.
   foreach $file (@to_delete) {
@@ -92,40 +91,60 @@ sub safedel {
     if (substr($file, length($file) - 1, 1) eq ":") {
       die "removing the root directory of a drive is not permitted!";
     }
+
+#print "file to whack: '$file'\n";
+
     if ( ($file =~ /^.*\/\.$/) || ($file =~ /^.*\/\.\.$/) ) {
       print "ignoring attempt to remove current or parent directory.\n";
       next;
     }
-    $tempfile = $temp_subdir . "/temp" . "$number";
-#   print "tempfile is $tempfile; file is $file.\n";
+
+#hmmm: extract this shared bit of code as new method (also in shared snarfer)
+    $date_tool = "date";
+    local($datestamp) = `$date_tool +%Y-%m-%d-%H%M`;
+    while ($datestamp =~ /[\r\n]$/) { chop $datestamp; }
+    $archive_file = $temp_subdir . "/del-$number-" . $datestamp;
+#print "archive_file is $archive_file; file is $file.\n";
+
     if (-d $file) {
       # ensure there aren't any read only files.
-      system("chmod -R u+rw \"$file\"");
+      system("chmod -R u+rw '$file'");
       # store the directory in the trash storage.
-      system("$zip -rm$use_path $tempfile \"$file$wildcard\" $DEV_NULL");
+      system("$zip -rm $archive_file '$file' $DEV_NULL");
         # zip up the files into the safekeeper directory.
       # recursively unlink in case zip doesn't remove the empty dir.
       if (-d $file) {
         # remove the directory itself if possible, since zip did not.
         &recursively_zap_dirs($file);
       }
-      push(@deleted, "$file");
+      push(@deleted, "\"$file\"");
     } elsif (-f $file) {
+#print "about to chmod file\n";
+      # make the file writable by our user if possible (which resets any
+      # prior permissions as long as we're the owner).
+      system("chmod u+rw '$file'");
       # store the file in the trash storage.
-      system("chmod u+rw \"$file\"");
-      system("$zip -m$use_path $tempfile \"$file\" $DEV_NULL");
-      push(@deleted, "$file");
+#print "about to run: system [$zip -m $archive_file '$file' $DEV_NULL]";
+      system("$zip -m $archive_file '$file' $DEV_NULL");
+      push(@deleted, "\"$file\"");
     } else {
       print "$0 cannot find \"$file\" to delete it.\n";
     }
   }
   if (@deleted) {
-    print "Trashed [@deleted].\n";
-    open(REPORT, ">>$TMP/zz_safedel.rpt");
+    if ($DEBUG_FEISTY_MEOW != "") {
+      print "Trashed [@deleted].\n";
+    }
+    open(REPORT, ">>$TMP/zz_safedel_report.txt");
 
-    local($printable_date) = &ctime(time);
+    local($printable_date) = scalar(localtime());
+#&ctime(time);
     $printable_date =~ s/\n//g;
-    print REPORT $printable_date . " -- safedel: \"temp" . $number . ".zip\" <= [@deleted]\n";
+    local($just_archived_filename) = `basename "$archive_file"`;
+    while ($just_archived_filename =~ /[\r\n]$/) { chop $just_archived_filename; }
+    print REPORT "\n";
+    print REPORT $printable_date . " -- created \"" . $just_archived_filename . ".zip\"\n";
+    print REPORT $printable_date . " -- from [@deleted]\n";
     close(REPORT);
   } else {
     print "No files were deleted.\n";