[lintian] 01/05: L::Path: Add "child(BASENAME)" method
This is an automated email from the git hooks/post-receive script.
nthykier pushed a commit to branch master
in repository lintian.
commit 9303614ed29d8ea5f08dfebd344bb07c896241d7
Author: Niels Thykier <niels@thykier.net>
Date: Tue Sep 23 21:17:10 2014 +0200
L::Path: Add "child(BASENAME)" method
Add a method "child" for looking up a named child of a directory.
Note that BASENAME never ends with a slash (even for directories).
Signed-off-by: Niels Thykier <niels@thykier.net>
---
lib/Lintian/Collect/Package.pm | 10 +++++++---
lib/Lintian/Path.pm | 29 ++++++++++++++++++++++++++++-
2 files changed, 35 insertions(+), 4 deletions(-)
diff --git a/lib/Lintian/Collect/Package.pm b/lib/Lintian/Collect/Package.pm
index 4d0544c..b374f94 100644
--- a/lib/Lintian/Collect/Package.pm
+++ b/lib/Lintian/Collect/Package.pm
@@ -508,15 +508,19 @@ sub _fetch_index_data {
# Add them in reverse order - entries in a dir are made
# objects before the dir itself.
if ($idxh{$file}->{type} eq 'd') {
- my @children;
+ my %child_table;
for my $cname (sort(@{ $children{$file} })) {
my $child = $idxh{$cname};
+ my $basename = $child->basename;
# Insert dirname here to share the same storage with
# the hash key
$child->{'dirname'} = $file;
- push(@children, $child);
+ if (substr($basename, -1, 1) eq '/') {
+ $basename = substr($basename, 0, -1);
+ }
+ $child_table{$basename} = $child;
}
- $idxh{$file}{children} = \@children;
+ $idxh{$file}{children} = \%child_table;
}
# Insert name here to share the same storage with the hash key
$idxh{$file}{'name'} = $file;
diff --git a/lib/Lintian/Path.pm b/lib/Lintian/Path.pm
index 39aac38..1aafa46 100644
--- a/lib/Lintian/Path.pm
+++ b/lib/Lintian/Path.pm
@@ -197,7 +197,34 @@ NB: Returns the empty list for non-dir entries.
sub children {
my ($self) = @_;
- return @{ $self->{'children'} };
+ return sort(values(%{ $self->{'children'} })) if wantarray;
+ return values(%{ $self->{'children'} });
+}
+
+=item child(BASENAME)
+
+Returns the child named BASENAME if it is a child of this directory.
+Otherwise, this method returns C<undef>.
+
+For non-dirs, this method always returns C<undef>.
+
+=cut
+
+sub child {
+ my ($self, $basename) = @_;
+ my $children = $self->{'children'};
+ my ($child, $had_trailing_slash);
+
+ # Remove the trailing slash (for dirs)
+ if (substr($basename, -1, 1) eq '/') {
+ $basename = substr($basename, 0, -1);
+ $had_trailing_slash = 1;
+ }
+ return if not $children or not exists($children->{$basename});
+ $child = $children->{$basename};
+ # Only directories are allowed to be fetched with trailing slash.
+ return if $had_trailing_slash and not $child->is_dir;
+ return $child;
}
=item is_symlink
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/lintian/lintian.git
Reply to: