Code deduplication fin find_src_files()
authorDavid Riebenbauer <davrieb@liegesta.at>
Sat, 30 Jan 2010 17:12:01 +0000 (18:12 +0100)
committerDavid Riebenbauer <davrieb@liegesta.at>
Tue, 2 Feb 2010 13:04:28 +0000 (14:04 +0100)
This also has the advantage that I can use the resulting new function
elsewhere.

IkiWiki/Render.pm

index 3ebb1a324d8a3118b54f0d7c6360587892917b65..44b2fb9c235f0c1d81b04cf054074661d30dfcd3 100644 (file)
@@ -280,6 +280,27 @@ sub srcdir_check () {
        
 }
 
+sub verify_src_file ($$) {
+       my $file=decode_utf8(shift);
+       my $dir=shift;
+
+       return if -l $file || -d $file;
+       $file=~s/^\Q$dir\E\/?//;
+       return if ! length $file;
+       my $page = pagename($file);
+       if (! exists $pagesources{$page} &&
+               file_pruned($file)) {
+               $File::Find::prune=1;
+               return;
+       }
+
+       my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
+       if (! defined $f) {
+               warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
+       }
+       return ($file,$page,$f);
+}
+
 sub find_src_files () {
        my @files;
        my %pages;
@@ -288,22 +309,9 @@ sub find_src_files () {
        find({
                no_chdir => 1,
                wanted => sub {
-                       my $file=decode_utf8($_);
-                       $file=~s/^\Q$config{srcdir}\E\/?//;
-                       return if -l $_ || -d _ || ! length $file;
-                       my $page = pagename($file);
-                       if (! exists $pagesources{$page} &&
-                           file_pruned($file)) {
-                               $File::Find::prune=1;
-                               return;
-                       }
-
-                       my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
-                       if (! defined $f) {
-                               warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
-                       }
-                       else {
-                               push @files, $f;
+                       my ($file,$page,$f) = verify_src_file($_,$config{srcdir});
+                       if ($file) {
+                               push @files, $file;
                                if ($pages{$page}) {
                                        debug(sprintf(gettext("%s has multiple possible source pages"), $page));
                                }
@@ -315,24 +323,11 @@ sub find_src_files () {
                find({
                        no_chdir => 1,
                        wanted => sub {
-                               my $file=decode_utf8($_);
-                               $file=~s/^\Q$dir\E\/?//;
-                               return if -l $_ || -d _ || ! length $file;
-                               my $page=pagename($file);
-                               if (! exists $pagesources{$page} &&
-                                   file_pruned($file)) {
-                                       $File::Find::prune=1;
-                                       return;
-                               }
-
-                               my ($f) = $file =~ /$config{wiki_file_regexp}/; # untaint
-                               if (! defined $f) {
-                                       warn(sprintf(gettext("skipping bad filename %s"), $file)."\n");
-                               }
-                               else {
+                               my ($file,$page,$f) = verify_src_file($_,$dir);
+                               if ($f) {
                                        # avoid underlaydir override
                                        # attacks; see security.mdwn
-                                       if (! -l "$config{srcdir}/$f" && 
+                                       if (! -l "$config{srcdir}/$f" &&
                                            ! -e _) {
                                                if (! $pages{$page}) {
                                                        push @files, $f;