summaryrefslogtreecommitdiffstats
path: root/lib/Sbuild/ChrootInfoSchroot.pm
blob: 92b4c03f54edaed9491732ee991dc41e769ea305 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#
# ChrootInfo.pm: chroot utility library for sbuild
# Copyright © 2005-2009 Roger Leigh <rleigh@debian.org>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see
# <http://www.gnu.org/licenses/>.
#
#######################################################################

package Sbuild::ChrootInfoSchroot;

use Sbuild::ChrootInfo;
use Sbuild::ChrootSchroot;

use strict;
use warnings;

BEGIN {
    use Exporter ();
    our (@ISA, @EXPORT);

    @ISA = qw(Exporter Sbuild::ChrootInfo);

    @EXPORT = qw();
}

sub new {
    my $class = shift;
    my $conf = shift;

    my $self = $class->SUPER::new($conf);
    bless($self, $class);

    return $self;
}

sub get_info_from_stream {
    my $self = shift;
    my $stream = shift;

    my $chroot_type = '';
    my %tmp = ('Namespace' => '',
	       'Name' => '',
	       'Priority' => 0,
	       'Location' => '',
	       'Session Purged' => 0);

    while (<$stream>) {
	chomp;

	last if ! $_;

	if (/^\s*(───|---) Chroot \1$/ &&
	    $tmp{'Namespace'} eq "") {
	    $tmp{'Namespace'} = 'chroot';
	}
	if (/^\s*(───|---) Session \1$/ &&
	    $tmp{'Namespace'} eq "") {
	    $tmp{'Namespace'} = 'session';
	}
	if (/^\s*(───|---) Source \1$/ &&
	    $tmp{'Namespace'} eq "") {
	    $tmp{'Namespace'} = 'source';
	}
	if (/^\s*Name:?\s+(.*)$/ &&
	    $tmp{'Name'} eq "") {
	    $tmp{'Name'} = $1;
	}
	if (/^\s*Type:?\s+(.*)$/) {
	    $chroot_type = $1;
	}
	if (/^\s*Location:?\s+(.*)$/ &&
	    $tmp{'Location'} eq "") {
	    $tmp{'Location'} = $1;
	}
	if (/^\s*Mount Location:?\s+(.*)$/ &&
	    $tmp{'Location'} eq "") {
	    $tmp{'Location'} = $1;
	}
	# Path takes priority over Location and Mount Location.
	if (/^\s*Path:?\s+(.*)$/) {
	    $tmp{'Location'} = $1;
	}
	if (/^\s*Priority:?\s+(\d+)$/) {
	    $tmp{'Priority'} = $1;
	}
	if (/^\s*Session Purged\s+(.*)$/) {
	    if ($1 eq "true") {
		$tmp{'Session Purged'} = 1;
	    }
	}
	if (/^\s*Aliases:?\s+(.*)$/) {
	    $tmp{'Aliases'} = $1;
	}
    }

    if ($self->get_conf('DEBUG') && $tmp{'Name'})  {
	print STDERR "Found schroot chroot: $tmp{'Namespace'}:$tmp{'Name'}\n";
	foreach (sort keys %tmp) {
	    print STDERR "  $_ $tmp{$_}\n";
	}
    }

    if (!$tmp{'Name'}) {
	return undef;
    }
    return \%tmp;
}

sub get_info {
    my $self = shift;
    my $chroot = shift;

    my $chroot_type = "";

    # If namespaces aren't supported, try to fall back to old style session.
    open CHROOT_DATA, '-|', $self->get_conf('SCHROOT'), '--info', '--chroot', "session:$chroot" or
	open CHROOT_DATA, '-|', $self->get_conf('SCHROOT'), '--info', '--chroot', $chroot or
	die 'Can\'t run ' . $self->get_conf('SCHROOT') . ' to get chroot data';

    my $tmp = $self->get_info_from_stream(\*CHROOT_DATA);

    if (!$tmp) {
	close CHROOT_DATA;
	return undef;
    }

    close CHROOT_DATA or die "Can't close schroot pipe getting chroot data";

    return $tmp;
}

sub get_info_all {
    my $self = shift;

    my $chroots = {};

    local %ENV;

    $ENV{'LC_ALL'} = 'C';
    $ENV{'LANGUAGE'} = 'C';

    open CHROOTS, '-|', $self->get_conf('SCHROOT'), '--info'
	or die 'Can\'t run ' . $self->get_conf('SCHROOT');
    my $tmp = undef;
    while (defined($tmp = $self->get_info_from_stream(\*CHROOTS))) {
	my $namespace = $tmp->{'Namespace'};
	$namespace = "chroot"
	    if !$tmp->{'Namespace'};
	$chroots->{$namespace} = {}
	    if (!exists($chroots->{$namespace}));
	$chroots->{$namespace}->{$tmp->{'Name'}} = $tmp;
	foreach my $alias (split(/\s+/, $tmp->{'Aliases'})) {
	    $chroots->{$namespace}->{$alias} = $tmp;
	}
    }
    close CHROOTS or die "Can't close schroot pipe";

    $self->set('Chroots', $chroots);
}

sub _create {
    my $self = shift;
    my $chroot_id = shift;

    my $chroot = undef;

    if (defined($chroot_id)) {
	$chroot = Sbuild::ChrootSchroot->new($self->get('Config'), $chroot_id);
    }

    return $chroot;
}

1;