Commits

shl...@6f433eb9-a002-0410-945b-ee5462c261ee  committed 594fa2e

- Now handling directories that cannot be opendir()'ed in a graceful
manner - just not traversing them further.

  • Participants
  • Parent commits d0b748b

Comments (0)

Files changed (3)

+0.1.0
+    - Now handling directories that cannot be opendir()'ed in a graceful
+    manner - just not traversing them further.
+
 0.0.9 - Fri Feb 22 20:04:03 IST 2008
     - Now running canonpath() on the targets in the call to 
       File::Find::Object->new. That way, trailing slashes are omitted in

File lib/File/Find/Object.pm

     $current->_last_dir_scanned($current->dir());
 
     my $handle;
+    my @files;
     if (!opendir($handle, $current->dir()))
     {
-        return $current->_open_dir_ret(undef);
+        # Handle this error gracefully.        
     }
-    my @files = (sort { $a cmp $b } File::Spec->no_upwards(readdir($handle)));
-    closedir($handle);
-
+    else
+    {
+        @files = (sort { $a cmp $b } File::Spec->no_upwards(readdir($handle)));
+        closedir($handle);
+    }
+    
     $current->_files(
         [ @files ]
     );
     $current->_traverse_to(
         [ @files ]
     );
-
     
     my @st = stat($current->dir());
     $current->inode($st[1]);
     $current->dev($st[0]);
 
-
     return $current->_open_dir_ret(1);
 }
 

File t/03traverse.t

 use strict;
 use warnings;
 
-use Test::More tests => 3;
+use Test::More tests => 5;
 
 BEGIN
 {
 
     rmtree($t->get_path("./$test_dir"))
 }
+
+{
+    my $test_id = "handle-non-accessible-dirs-gracefully";
+    my $test_dir = "t/sample-data/$test_id";
+    my $tree =
+    {
+        'name' => "$test_id/",
+        'subs' =>
+        [   
+            {
+                'name' => "a/",
+                subs =>
+                [
+                    {
+                        'name' => "b.doc",
+                        'contents' => "This file was spotted in the wild.",
+                    },
+                ],
+            },
+            {
+                'name' => "c/",
+                subs =>
+                [
+                    {
+                        'name' => "d.doc",
+                        'contents' => "This file was spotted in the wild.",
+                    },
+                ],
+            },
+            
+            {
+                'name' => "foo/",
+                'subs' =>
+                [
+                    {
+                        'name' => "yet/",
+                    },
+                ],
+            },
+            {
+                'name' => "bar/",
+                'subs' =>
+                [
+                    {
+                        name => "myfile.txt",
+                        content => "Hello World",
+                    },
+                    {
+                        'name' => "zamda/",
+                    },
+                ],
+            },
+            {
+                'name' => "daps/",
+            },
+        ],
+    };
+
+    my $t = File::Find::Object::TreeCreate->new();
+    $t->create_tree("./t/sample-data/", $tree);
+    chmod (0000, $t->get_path("$test_dir/bar"));
+    eval
+    {
+        my $ff = File::Find::Object->new({}, $t->get_path("$test_dir"));
+
+        my @results;
+        while (defined(my $result = $ff->next()))
+        {
+            push @results, $result;
+        }
+        # TEST
+        ok (scalar(grep { $_ eq $t->get_path("$test_dir/a")} @results),
+            "Found /a",
+        );
+    };
+    # TEST
+    is ($@, "", "Handle non-accessible directories gracefully");
+
+    chmod (0755, $t->get_path("$test_dir/bar"));
+    rmtree($t->get_path("./$test_dir"))
+}