[LON-CAPA-cvs] cvs: loncom /debugging_tools move_construction_spaces.pl

raeburn raeburn at source.lon-capa.org
Thu Dec 8 22:01:44 EST 2011


raeburn		Fri Dec  9 03:01:44 2011 EDT

  Modified files:              
    /loncom/debugging_tools	move_construction_spaces.pl 
  Log:
  - Use closedir() to close directories opened using opendir()
  - Correct order of arguments for chmod()
  - When called in "undo" context use rmdir to remove empty domain directories
    in /home/httpd/html/priv/, then empty /home/httpd/html/priv directory.
  - Warning in "undo" context if /home/httpd/html/priv not removed, as
    access to /priv/uname/ (old CSTR location)  will not work until  
    priv directory is moved.
  
  
-------------- next part --------------
Index: loncom/debugging_tools/move_construction_spaces.pl
diff -u loncom/debugging_tools/move_construction_spaces.pl:1.6 loncom/debugging_tools/move_construction_spaces.pl:1.7
--- loncom/debugging_tools/move_construction_spaces.pl:1.6	Sun Oct 30 16:01:20 2011
+++ loncom/debugging_tools/move_construction_spaces.pl	Fri Dec  9 03:01:44 2011
@@ -5,7 +5,7 @@
 # Move Construction Spaces from /home/$user/public_html
 # to /home/httpd/html/priv/$domain/$user and vice versa
 #
-# $Id: move_construction_spaces.pl,v 1.6 2011/10/30 16:01:20 raeburn Exp $
+# $Id: move_construction_spaces.pl,v 1.7 2011/12/09 03:01:44 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -256,6 +256,7 @@
     my ($dir,$output);
     if (opendir($dir,$lonusersdir)) {
         my @contents = (grep(!/^\.{1,2}$/,readdir($dir)));
+        closedir($dir);
         foreach my $item (@contents) {
             if (-d "$lonusersdir/$item") {
                 if ($item =~ /^$match_domain$/) {
@@ -303,7 +304,6 @@
                 }
             }
         }
-        closedir($dir);
     } else {
         $output = &mt('Could not open [_1].',"'$lonusersdir'")."\n";
         print $output;
@@ -318,10 +318,12 @@
         my ($dir,$domdir);
         if (opendir($dir,"$londocroot/res")) {
             my @contents = (grep(!/^\.{1,2}$/,readdir($dir)));
+            closedir($dir);
             foreach my $dom (@contents) {
                 if ((grep(/^\Q$dom\E/, at machinedoms)) && (-d "$londocroot/res/$dom")) {
                     if (opendir($domdir,"$londocroot/res/$dom")) {
                         my @unames = (grep(!/^\.{1,2}$/,readdir($domdir)));
+                        closedir($domdir);
                         foreach my $uname (@unames) {
                             if ($uname =~ /^$match_username$/) {
                                 push(@{$pubusers{$uname}},$dom);
@@ -341,10 +343,12 @@
             my ($dir,$domdir);
             if (opendir($dir,"$londocroot/priv")) {
                 my @contents = (grep(!/^\.{1,2}/,readdir($dir)));
+                closedir($dir);
                 foreach my $dom (@contents) {
                     next if (!-d "$londocroot/priv/$dom");
                     if (opendir($domdir,"$londocroot/priv/$dom")) {
                         my @unames = (grep(!/^\.{1,2}$/,readdir($domdir)));
+                        closedir($domdir);
                         foreach my $uname (@unames) {
                             if ($uname =~ /^$match_username$/) {
                                 push(@{$privspaces{$uname}},$dom);
@@ -357,6 +361,7 @@
     }
     foreach my $uname (keys(%privspaces)) {
         if (ref($privspaces{$uname}) eq 'ARRAY') {
+            my $output;
             if (@{$privspaces{$uname}} > 1) {
                 my $displaydoms = join(', ',@{$privspaces{$uname}});
                 print &mt('Same username used for authors in multiple domains.')."\n".
@@ -367,29 +372,70 @@
                 my $choice=<STDIN>;
                 chomp($choice);
                 if (grep(/^\Q$choice\E$/,@{$privspaces{$uname}})) {
-                    my $output = &move_priv_to_home($londocroot,$uid,$gid,$uname,$choice);
-                    print $output;
-                    print $logfh $output;
+                    $output = &move_priv_to_home($londocroot,$uid,$gid,$uname,$choice);
                 } else {
                     print &mt('Invalid choice of domain:')." $choice\n";
-                    my $output = &mt('Skipping this user: [_1].',"'$uname'")."\n";
+                    $output = &mt('Skipping this user: [_1].',"'$uname'")."\n";
                     print $output;
                     print $logfh $output;
                     next;
                 }
             } elsif (@{$privspaces{$uname}} == 1) {
-                my $output = &move_priv_to_home($londocroot,$uid,$gid,$uname,$privspaces{$uname}[0]);
-                print $output;
-                print $logfh $output;
+                $output = &move_priv_to_home($londocroot,$uid,$gid,$uname,$privspaces{$uname}[0]);
             } else {
                 print &mt('Username [_1] found in [_2] was not within a domain',
                           "'$uname'","'$londocroot/priv'")."\n";
-                my $output = &mt('Skipping this user: [_1].',"'$uname'")."\n";
-                print $output;
-                print $logfh $output;
+                $output = &mt('Skipping this user: [_1].',"'$uname'")."\n";
             }
+            print $output;
+            print $logfh $output;
         }
     }
+    if (-d "$londocroot/priv") {
+        my $output;
+        if (opendir(my $dir,"$londocroot/priv")) {
+            my @doms = grep(!/^\.{1,2}/,readdir($dir));
+            closedir($dir);
+            foreach my $dom (@doms) {
+                if (opendir(my $domdir,"$londocroot/priv/$dom")) {
+                    my @contents =  grep(!/^\.{1,2}/,readdir($domdir));
+                    closedir($domdir);
+                    if (@contents == 0) {
+                        if (rmdir("$londocroot/priv/$dom")) {
+                            $output = &mt('Removed empty directory: [_1]',
+                                          "'$londocroot/priv/$dom'")."\n";
+                        } else {
+                            $output = &mt('Failed to remove directory: [_1]',
+                                          "'$londocroot/priv/$dom'")."\n";
+                        }
+                    }
+                }
+            }
+        }
+        my $warning = &mt('WARNING: Access to Construction Spaces in their old locations (i.e., in [_1]) via LON-CAPA with URLs of the form [_2] will not work until the directory at [_3] is moved or deleted.',"'/home/<user>/'","'/priv/<user>/'","'$londocroot/priv/'")."\n";
+        if (opendir(my $dir,"$londocroot/priv")) {
+            my @contents = (grep(!/^\.{1,2}/,readdir($dir)));
+            closedir($dir);
+            if (@contents == 0) {
+                if (rmdir("$londocroot/priv")) {
+                    $output .= &mt('Removed empty directory: [_1]',
+                                   "'$londocroot/priv'")."\n";
+                } else {
+                    $output .= &mt('Failed to remove directory: [_1]',
+                                   "'$londocroot/priv'")."\n".
+                               $warning."\n";
+                }
+            } else {
+                $output .= $warning."\n".
+                           &mt('The attempt to remove the directory failed, because it is not empty.')."\n";
+            }
+        } else {
+            $output .= $warning."\n".
+                       &mt('The attempt to open the directory to see its contents failed, hence no attempt was made to remove it.')."\n";
+        }
+        print $output;
+        print $logfh $output;
+    }
     &stop_logging($logfh);
     print "\n".&mt('Done')."\n";
     exit;
@@ -401,7 +447,8 @@
 
 # Iterate over directories in /home
 if (opendir(my $dir,"/home")) {
-    my @possibles = grep(!/^\.{1,2}$/,readdir($dir)); 
+    my @possibles = grep(!/^\.{1,2}$/,readdir($dir));
+    closedir($dir);
     foreach my $item (sort(@possibles)) {
         next if ($item eq 'www');
         if ((-d "/home/$item") && ($item ne '')) {
@@ -526,6 +573,7 @@
                                 if (opendir(my $homedir,"/home/$author")) {
                                     my @contents = 
                                         grep(!/^\.{1,2}$/,readdir($homedir));
+                                    closedir($homedir);
                                     if (@contents == 0) {
                                         if (rmdir("/home/$author/")) {
                                             $output .= &mt('Removed empty directory: [_1]',
@@ -651,7 +699,7 @@
                             if (-e $dompath) {
                                 if (move($source_path,$newpath)) {
                                     chown($uid,$gid,$newpath);
-                                    chmod($newpath,0750);
+                                    chmod(0750,$newpath);
                                     $output = &mt('Moved [_1] to [_2].',
                                                   "'$source_path'","'$newpath'")."\n";
                                 } else {
@@ -787,8 +835,12 @@
             if (!-e $target_path) {
                 move($source_path,$target_path);
                 chown($uid,$gid,$target_path);
-                chmod($target_path,2770);
-                $output = &mt('Moved [_1] to [_2].',"'$source_path'","'$target_path'")."\n";
+                chmod(0750,$target_path);
+                if (-e $target_path && !-e $source_path) {
+                    $output = &mt('Moved [_1] to [_2].',"'$source_path'","'$target_path'")."\n";
+                } else {
+                    $output = &mt('Failed to move [_1] to [_2].',"'$source_path'","'$target_path'")."\n";
+                }
             } else {
                 $output = &mt('Directory [_1] already exists -- not moving [_2].',
                               "'$target_path'","'$source_path'")."\n";
@@ -842,6 +894,7 @@
     my ($londaemons,$author,$domain) = @_;
     if (opendir(my $homedir,"/home/$author")) {
         my @contents = grep(!/^\.{1,2}$/,readdir($homedir));
+        closedir($homedir);
         if (@contents > 0) {
             if (grep(/^restore_\d+\.sh$/, at contents)) {
                 if (!-e "$londaemons/logs/moved_construction_spaces") { 


More information about the LON-CAPA-cvs mailing list