| 1 | # Copyright 2010, 2011, 2012, 2013 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Distlinks. | ||
| 4 | # | ||
| 5 | # Distlinks is free software; you can redistribute it and/or modify it under | ||
| 6 | # the terms of the GNU General Public License as published by the Free | ||
| 7 | # Software Foundation; either version 3, or (at your option) any later | ||
| 8 | # version. | ||
| 9 | # | ||
| 10 | # Distlinks is distributed in the hope that it will be useful, but WITHOUT | ||
| 11 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
| 12 | # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
| 13 | # more details. | ||
| 14 | # | ||
| 15 | # You should have received a copy of the GNU General Public License along | ||
| 16 | # with Distlinks. If not, see <http://www.gnu.org/licenses/>. | ||
| 17 | |||
| 18 | package App::Distlinks::FileFind; | ||
| 19 | use 5.010; | ||
| 20 | use strict; | ||
| 21 | use warnings; | ||
| 22 | use File::Spec; | ||
| 23 | use Iterator::Simple; | ||
| 24 | |||
| 25 | our $VERSION = 11; | ||
| 26 | |||
| 27 | use constant _false => 0; | ||
| 28 | |||
| 29 | # dirs | ||
| 30 | # prune | ||
| 31 | # queue | ||
| 32 | # | ||
| 33 | sub new { | ||
| 34 | my ($class, %self) = @_; | ||
| 35 | $self{'queue_array'} ||= []; | ||
| 36 | if (defined (my $dir = delete $self{'dir'})) { | ||
| 37 | push @{$self{'queue_array'}}, $dir; | ||
| 38 | } | ||
| 39 | if (my $dirs = delete $self{'dirs'}) { | ||
| 40 | push @{$self{'queue_array'}}, @$dirs; | ||
| 41 | } | ||
| 42 | $self{'prune_pred'} ||= \&_false; | ||
| 43 | return Iterator::Simple::iterator (sub { _next(\%self) }); | ||
| 44 | } | ||
| 45 | |||
| 46 | sub _next { | ||
| 47 | my ($self) = @_; | ||
| 48 | my $q = $self->{'queue_array'}; | ||
| 49 | if (! @$q) { return; } | ||
| 50 | |||
| 51 | my $filename = shift @$q; | ||
| 52 | if (-d $filename) { | ||
| 53 | if (! $self->{'prune_pred'}->($filename)) { | ||
| 54 | if (opendir my $fh, $filename) { | ||
| 55 | unshift @$q, | ||
| 56 | map { File::Spec->rel2abs($_, $filename) } | ||
| 57 | grep { $_ ne '.' && $_ ne '..' } | ||
| 58 | readdir $fh; | ||
| 59 | } else { | ||
| 60 | # warn "Skipping unreadable directory $filename"; | ||
| 61 | } | ||
| 62 | } | ||
| 63 | } | ||
| 64 | return $filename; | ||
| 65 | } | ||
| 66 | |||
| 67 | 1; | ||
| 68 | __END__ |