summaryrefslogtreecommitdiff
path: root/lib/SlackBuild/Registry/Backend/FS.pm
blob: 61cbe7d450f85b419d3ba379367b99d658796687 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package SlackBuild::Registry::Backend::FS;
use strict;
use warnings;
use Carp;
use File::Basename;
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);
    $pat = new SlackBuild::Registry::Pattern(package => 'openssl',
                                             version => '1.0.2m');
    @a = $x->lookup($pat);

=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 %_;
    $self->scan;
    return $self;
}

my @suffixes = qw(.tgz .txz);

sub scan {
    my $self = shift;
    my $pat = "*-*-*-*";
    
    $self->{ls} = [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)
        }
    }
    map {
	my ($name,$path,$suffix) = fileparse($_, @suffixes);
	my $rec = SlackBuild::Registry::Record->split($name);
	if ($rec) {
	    my $st = stat($_);
	    if (S_ISREG($st->mode)) {
		$rec->date($st->mtime);
		$rec->filename(File::Spec->abs2rel($_, $self->{dir}));
	    } else {
		$rec = undef;
	    }
	}
	$rec
    } (glob File::Spec->catfile($self->{dir}, $pat))];

    $self->{index}{$self->{ls}[0]->package}[0] = 0;
    my $i;
    for ($i = 1; $i < @{$self->{ls}}; $i++) {
	unless ($self->{ls}[$i]->package eq $self->{ls}[$i-1]->package) {
	    $self->{index}{$self->{ls}[$i-1]->package}[1] = $i-1;
	    $self->{index}{$self->{ls}[$i]->package}[0] = $i;
	}
    }
    $self->{index}{$self->{ls}[$i-1]->package}[1] = $i;
}

=head2 lookup

    @a = $backend->lookup($pattern)

Returns a sorted array of SlackBuild::Registry::Record objects matching the
B<SlackBuild::Registry::Pattern> object B<$pattern>.
    
=cut
    
sub lookup {
    my ($self, $pred) = @_;
    my $pkg = $pred->package;

    my @result;
    if ($pkg) {
	if (my $idx = $self->{index}{$pkg}) {
	    @result = grep { $pred->matches($_) }
	    		@{$self->{ls}}[$idx->[0] .. $idx->[1]];
	}
    } else {
	@result = grep { $pred->matches($_) } @{$self->{ls}};
    }

    if (wantarray) {
	(@result)
    } else {
	shift @result;
    }
}

1;

Return to:

Send suggestions and report system problems to the System administrator.