From b30d967dc49238fcb1e20156bc3d2f53111e17c8 Mon Sep 17 00:00:00 2001 From: Maarten Vanraes Date: Sat, 6 Aug 2016 14:21:24 +0200 Subject: Part: add a closest helper function --- lib/ManaTools/Shared/disk_backend/Part.pm | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/lib/ManaTools/Shared/disk_backend/Part.pm b/lib/ManaTools/Shared/disk_backend/Part.pm index 39502ec0..44961628 100644 --- a/lib/ManaTools/Shared/disk_backend/Part.pm +++ b/lib/ManaTools/Shared/disk_backend/Part.pm @@ -364,6 +364,35 @@ sub find_parts { return @res; } +sub find_closest { + my $self = shift; + my $partstate = shift; + my $identify = shift; + my $parttype = shift; + my $parameters = shift; + my @tags = @_; + my @res = ($self); + while (scalar(@res) > 0) { + my @next = (); + for my $p (@res) { + my $links = $p->links(); + for my $link (@{$links}) { + if ($link->check($p, $parttype, @tags)) { + if (!defined $identify || $identify->($link->part(), $parameters)) { + # if it's the state we're looking for, just return it + return $link->part() if ($link->part()->is_state($partstate)); + } + + # add it to the next list to be checked + push @next, $link->part(); + } + } + } + @res = @next; + } + return undef; +} + sub find_part { my $self = shift; my $parttype = shift; -- cgit v1.2.1