[vhffs-dev] [1408] Recursion is evil so let's fix it

[ Thread Index | Date Index | More vhffs.org/vhffs-dev Archives ]


Revision: 1408
Author:   beuss
Date:     2009-05-26 13:20:35 +0200 (Tue, 26 May 2009)

Log Message:
-----------
Recursion is evil so let's fix it

Modified Paths:
--------------
    trunk/vhffs-api/src/Vhffs/Functions.pm


Modified: trunk/vhffs-api/src/Vhffs/Functions.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Functions.pm	2009-05-26 10:47:03 UTC (rev 1407)
+++ trunk/vhffs-api/src/Vhffs/Functions.pm	2009-05-26 11:20:35 UTC (rev 1408)
@@ -188,39 +188,37 @@
 	return( quotemeta( $string ) );
 }
 
+=head2 chmod_recur
 
-sub chmod_recur
-{
-    my $dir;
-    my $fmod;
-    my $dmod;
+    Vhffs::Functions::chmod_recur($dir, $fmod, $dmod);
 
-    $dir  = shift;
-    $fmod = shift;
-    $dmod = shift;
+Changes permissions on files and directories recursively.
+C<$fmod> and C<$dmod> are the mod to apply to files and
+directory, respectively. See the documentation of the
+original C<chmod> function for more information.
 
-    if( -d $dir )
-    {
-	chmod( $dmod , $dir );
-	opendir( DIR , $dir );
-	my @files = readdir( DIR );
-	my $file;
-	my $cfile;
+=cut
 
-	foreach $file ( @files )
-	{
-	    next if( ( $file eq '.' ) or ( $file eq '..' ) );
-	    $cfile = $dir . "/" . $file;
-	    chmod_recur( $cfile , $fmod , $dmod );
-	}
+sub chmod_recur {
+    my ($dir, $fmod, $dmod) = @_;
 
-	close( DIR );
+    my @files = ( $dir );
+    my $file;
+
+    while( $file = shift @files ) {
+        if( -f $file ) {
+            chmod( $fmod, $file );
+        } elsif( -d $file ) {
+            chmod( $dmod, $file );
+            opendir DIRECTORY, $file;
+            my @content = readdir( DIRECTORY );
+            closedir( DIRECTORY );
+            foreach my $item ( @content ) {
+                next if( $item eq '' or $item eq '..' or $item eq '.' );
+                push @files, $file.'/'.$item;
+            }
+        }
     }
-    elsif( -f $dir )
-    {
-	chmod( $fmod , $dir );
-    }
-    
 }
 
 # Change the owner and groupe recursivly for a dir
@@ -241,7 +239,7 @@
             closedir( DIRECTORY );
             foreach my $item ( @content ) {
                 next if( $item eq '' or $item eq '..' or $item eq '.' );
-		push @files, $f.'/'.$item;
+                push @files, $f.'/'.$item;
             }
         }
     }


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/