[LON-CAPA-cvs] cvs: loncom /publisher loncfile.pm

raeburn lon-capa-cvs@mail.lon-capa.org
Thu, 14 Jul 2005 22:49:28 -0000


raeburn		Thu Jul 14 18:49:28 2005 EDT

  Modified files:              
    /loncom/publisher	loncfile.pm 
  Log:
  Bug 1860.  Allow deletion of a directory which only contains .save, .meta and .log files associated with published, obsoleted, then deleted resource(s).    
  
  
Index: loncom/publisher/loncfile.pm
diff -u loncom/publisher/loncfile.pm:1.69 loncom/publisher/loncfile.pm:1.70
--- loncom/publisher/loncfile.pm:1.69	Mon May 30 12:56:46 2005
+++ loncom/publisher/loncfile.pm	Thu Jul 14 18:49:26 2005
@@ -9,7 +9,7 @@
 #  and displays a page showing the results of the action.
 #
 #
-# $Id: loncfile.pm,v 1.69 2005/05/30 16:56:46 www Exp $
+# $Id: loncfile.pm,v 1.70 2005/07/14 22:49:26 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -194,7 +194,32 @@
     }
 }
 
-
+# see if directory is empty
+# ignores any .meta, .save and .log files created for a previously
+# published file, which has since been marked obsolete and deleted.
+sub empty_directory {
+    my ($dirname,$phase) = @_;
+    if (opendir DIR, $dirname) {
+        my @files = grep(!/^\.\.?$/, readdir(DIR)); # ignore . and ..
+        if (@files) { 
+            my @orphans = grep(/\.(meta|save|log)$/,@files);
+            if (scalar(@files) - scalar(@orphans) > 0) { 
+                return 0;
+            } else {
+                if (($phase eq 'Delete2') && (@orphans > 0)) {
+                    foreach my $file (@orphans) {
+                        if ($file =~ /\.(meta|save|log)$/) {
+                            unlink($dirname.$file);
+                        }
+                    }
+                }
+            }
+        }
+        closedir(DIR);
+        return 1;
+    }
+    return 0;
+}
 
 =pod
 
@@ -519,11 +544,20 @@
     if( -e $fn) {
 	$request->print('<input type="hidden" name="newfilename" value="'.
 			$fn.'"/>');
-	unless (&obsolete_unpub($user,$domain,$fn)) {
-	    $request->print('<h3>'.&mt('Cannot delete non-obsolete published file').'</h3>'.
+        if (-d $fn) {
+            unless (&empty_directory($fn,'Delete1')) {
+                $request->print('<h3>'.&mt('Only empty directories may be deleted.').'</h3>'.
+                            'You must delete the contents of the directory first.<br />'.
+                            '<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a>');
+                return;
+            }
+        } else { 
+	    unless (&obsolete_unpub($user,$domain,$fn)) {
+	        $request->print('<h3>'.&mt('Cannot delete non-obsolete published file').'</h3>'.
 			    '<br /><a href="'.&url($fn).'">'.&mt('Cancel').'</a>');
-	    return;
-	}
+	        return;
+	    }
+        }
 	$request->print('<p>'.&mt('Delete').' '.&display($fn).'?</p>');
 	&CloseForm1($request, $fn);
     } else {
@@ -938,10 +972,8 @@
 
 sub Delete2 {
     my ($request, $user, $filename) = @_;
-    if(opendir DIR, $filename) { 
-	my @files=readdir(DIR);
-	shift @files; shift @files; # takes off . and ..
-	if(@files) { 
+    if (-d $filename) { 
+	unless (&empty_directory($filename,'Delete2')) { 
 	    $request->print('<font color="red"> '.&mt('Error: Directory Non Empty').'</font>'); 
 	    return 0;
 	} else {