summaryrefslogtreecommitdiff
path: root/lib/SlackBuild/Registry/Backend/FS.pm
blob: 5788eadb2a34b1b2ffed66f9cc63829164d6084f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package SlackBuild::Registry::Backend::FS;
use strict;
use warnings;
use Carp;
use File::Basename;
use List::Regexp;
use File::stat;
use File::Spec;
use Fcntl ':mode';
use SlackBuild::Registry::Record;

=head1 NAME
    
SlackBuild::Registry::Backend::FS - filesystem backend for slackbuild registry

=head1 SYNOPSIS

    $reg = new SlackBuild::Registry(dir => DIRECTORY);
    my @a = $x->lookup('openssl', version => '1.0.2m');

=head1 METHODS

=head2 new

    $x = new SlackBuild::Registry::Backend::FS(dir => DIRECTORY)
    
=cut
    
sub new {
    my $class = shift;
    my $self = bless {}, $class;
    local %_ = @_;
    if (my $dir = delete $_{dir}) {
	$self->{dir} = $dir;
    } else {
	croak "required parameter dir not present";
    }
    croak "too many arguments" if keys %_;
    return $self;
}

my @architectures = qw(i386 x86_64 arm noarch);
my @suffixes = qw(.tgz .txz);

=head2 lookup

    @a = $backend->lookup(PACKAGE, [version=>X], [arch=>Y], [build=>Z])

Returns a sorted array of SlackBuild::Registry::Record objects matching the
search criteria.    
    
=cut
    
sub lookup {
    my ($self, $pkg, %cond) = @_;
    my $v;

    my $pat = "$pkg-*-*-*";
    
    my $rx = '^' . qr($pkg) . '-'
	   . '(?<version>\d+(?:\.\d+)*.*?)-(?<arch>'
	   . regexp_opt(@architectures)
	   .= ')-(?<build>\d+)(?<rest>.*)$';
    my @result = sort {
	my $d;
	if ($d = ($a->package || '') cmp ($b->package || '')) {
	    $d
	} elsif ($d = $b->version <=> $a->version) {
	    $d
	} elsif ($a->arch && $b->arch
		 && ($d = $a->arch cmp $b->arch)) {
	    $d
        } else {
	    ($b->build || 1) <=> ($a->build || 1)
        }
    }
    grep { $_->matches(%cond) }
    map {
	my ($name,$path,$suffix) = fileparse($_, @suffixes);
	if ($name =~ m{$rx}) {
	    my $st = stat($_);
	    if (S_ISREG($st->mode)) {
		new SlackBuild::Registry::Record($pkg,
						 version => $+{version},
						 arch => $+{arch},
						 build => $+{build},
						 date => $st->mtime,
						 filename => File::Spec->abs2rel($_, $self->{dir}))
	    } else {
		()
	    }
	} else {
	    ()
	}
    } (glob File::Spec->catfile($self->{dir}, $pat));
    if (wantarray) {
	(@result)
    } else {
	shift @result;
    }
}

1;

Return to:

Send suggestions and report system problems to the System administrator.