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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
|
#
# Sbuild.pm: library for sbuild
# Copyright © 2005 Ryan Murray <rmurray@debian.org>
# Copyright © 2005-2008 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;
use Sbuild::Sysconfig;
use strict;
use warnings;
use POSIX;
use FileHandle;
use Filesys::Df qw();
use Time::Local;
use IO::Zlib;
use MIME::Base64;
use Dpkg::Control;
use Dpkg::Checksums;
use POSIX qw(locale_h);
BEGIN {
use Exporter ();
our (@ISA, @EXPORT);
@ISA = qw(Exporter);
@EXPORT = qw($debug_level $devnull binNMU_version parse_date isin
copy dump_file check_packages help_text version_text
usage_error send_mail debug debug2 df
check_group_membership dsc_files dsc_pkgver shellescape strftime_c);
}
our $devnull;
our $debug_level = 0;
BEGIN {
# A file representing /dev/null
if (!open($devnull, '+<', '/dev/null')) {
die "Cannot open /dev/null: $!\n";;
}
}
sub binNMU_version ($$$);
sub parse_date ($);
sub isin ($@);
sub copy ($);
sub dump_file ($);
sub check_packages ($$);
sub help_text ($$);
sub version_text ($);
sub usage_error ($$);
sub debug (@);
sub debug2 (@);
sub check_group_membership();
sub dsc_files ($);
sub shellescape ($);
sub strftime_c ($@);
sub binNMU_version ($$$) {
my $v = shift;
my $binNMUver = shift;
my $append_to_version = shift;
my $ver = $v;
if (defined($append_to_version) && $append_to_version) {
$ver .= $append_to_version;
}
if (defined($binNMUver) && $binNMUver) {
$ver .= "+b$binNMUver";
}
return $ver;
}
my %monname = ('jan', 0, 'feb', 1, 'mar', 2, 'apr', 3, 'may', 4, 'jun', 5,
'jul', 6, 'aug', 7, 'sep', 8, 'oct', 9, 'nov', 10, 'dec', 11 );
sub parse_date ($) {
my $text = shift;
return 0 if !$text;
die "Cannot parse date: $text\n"
if $text !~ /^(\d{4}) (\w{3}) (\d+) (\d{2}):(\d{2}):(\d{2})$/;
my ($year, $mon, $day, $hour, $min, $sec) = ($1, $2, $3, $4, $5, $6);
$mon =~ y/A-Z/a-z/;
die "Invalid month name $mon" if !exists $monname{$mon};
$mon = $monname{$mon};
return timegm($sec, $min, $hour, $day, $mon, $year);
}
sub isin ($@) {
my $val = shift;
return grep( $_ eq $val, @_ );
}
sub copy ($) {
my $r = shift;
my $new;
if (ref($r) eq "HASH") {
$new = { };
foreach (keys %$r) {
$new->{$_} = copy($r->{$_});
}
}
elsif (ref($r) eq "ARRAY") {
my $i;
$new = [ ];
for( $i = 0; $i < @$r; ++$i ) {
$new->[$i] = copy($r->[$i]);
}
}
elsif (!ref($r)) {
$new = $r;
}
else {
die "unknown ref type in copy\n";
}
return $new;
}
sub dump_file ($) {
my $file = shift;
if (-r "$file" &&
open(SOURCES, "<$file")) {
print " +------------------------------------------------------------------------\n";
while (<SOURCES>) {
chomp;
print " |$_\n";
}
print " +------------------------------------------------------------------------\n";
close(SOURCES) or print "Failed to close $file\n";
} else {
print "W: Failed to open $file\n";
}
}
# set and list saved package list (used by sbuild-checkpackages)
sub check_packages ($$) {
my $session = shift;
my $mode = shift;
my $package_checklist = $session->get_conf('PACKAGE_CHECKLIST');
my $chroot_dir = $session->get('Location');
my (@status, @ref, @install, @remove);
my $pipe = $session->pipe_command({
COMMAND => ['dpkg-query', '--show', '--showformat=${Package} ${db:Status-Status}\n']
});
while (<$pipe>) {
chomp;
my @token = split / /, $_;
my $pkgname = shift @token;
my $state = shift @token;
next if $state ne "installed";
push @status, $pkgname;
}
if (! close $pipe) {
print STDERR "Error reading dpkg status file in chroot: $!\n";
return 1;
}
@status = sort @status;
if (!@status) {
print STDERR "dpkg status file is empty\n";
return 1;
}
if ($mode eq "set") {
if (! open WREF, "> $chroot_dir/$package_checklist") {
print STDERR "Can't write reference status file $chroot_dir/$package_checklist: $!\n";
return 1;
}
foreach (@status) {
print WREF "$_\n";
}
if (! close WREF) {
print STDERR "Error writing reference status file: $!\n";
return 1;
}
} else { # "list"
if (! open REF, "< $chroot_dir/$package_checklist") {
print STDERR "Can't read reference status file $chroot_dir/$package_checklist: $!\n";
return 1;
}
while (<REF>) {
chomp;
push @ref, $_;
}
if (! close REF) {
print STDERR "Error reading reference status file: $!\n";
return 1;
}
@ref = sort @ref;
if (!@ref) {
print STDERR "Reference status file is empty\n";
return 1;
}
print "DELETE ADD\n";
print "--------------------------------------\n";
my $i = 0;
my $j = 0;
while ($i < scalar @status && $j < scalar @ref) {
my $c = $status[$i] cmp $ref[$j];
if ($c < 0) {
# In status, not reference; remove.
print "$status[$i]\n";
$i++;
} elsif ($c > 0) {
# In reference, not status; install.
print " $ref[$j]\n";
$j++;
} else {
# Identical; skip.
$i++; $j++;
}
}
# Print any remaining elements
while ($i < scalar @status) {
print "$status[$i]\n";
$i++;
}
while ($j < scalar @ref) {
print " $ref[$j]\n";
$j++;
}
}
}
sub help_text ($$) {
my $section = shift;
my $page = shift;
system('man', '--', $section, $page);
exit 0;
}
sub version_text ($) {
my $program = shift;
print <<"EOF";
$program (Debian sbuild) $Sbuild::Sysconfig::version ($Sbuild::Sysconfig::release_date)
Written by Roman Hodek, James Troup, Ben Collins, Ryan Murray, Rick
Younie, Francesco Paolo Lovergine, Michael Banck, Roger Leigh and
Andres Mejia.
Copyright © 1998-2000 Roman Hodek <roman\@hodek.net>
© 1998-1999 James Troup <troup\@debian.org>
© 2003-2006 Ryan Murray <rmurray\@debian.org>
© 2001-2003 Rick Younie <younie\@debian.org>
© 2003-2004 Francesco Paolo Lovergine <frankie\@debian.org>
© 2005 Michael Banck <mbanck\@debian.org>
© 2005-2010 Roger Leigh <rleigh\@debian.org>
© 2009-2010 Andres Mejia <mcitadel\@gmail.com>
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
EOF
exit 0;
}
# Print an error message about incorrect command-line options
sub usage_error ($$) {
my $program = shift;
my $message = shift;
print STDERR "E: $message\n";
print STDERR "I: Run '$program --help' to list usage example and all available options\n";
exit 1;
}
sub send_mail ($$$$) {
my $conf = shift;
my $to = shift;
my $subject = shift;
my $file = shift;
local( *MAIL, *F );
if (!open( F, "<$file" )) {
warn "Cannot open $file for mailing: $!\n";
return 0;
}
local $SIG{'PIPE'} = 'IGNORE';
if (!open( MAIL, "|" . $conf->get('MAILPROG') . " -oem $to" )) {
warn "Could not open pipe to " . $conf->get('MAILPROG') . ": $!\n";
close( F );
return 0;
}
print MAIL "From: " . $conf->get('MAILFROM') . "\n";
print MAIL "To: $to\n";
print MAIL "Subject: $subject\n";
print MAIL "Content-Type: text/plain; charset=UTF-8\n";
print MAIL "Content-Transfer-Encoding: 8bit\n";
print MAIL "\n";
while( <F> ) {
print MAIL "." if $_ eq ".\n";
print MAIL $_;
}
close( F );
if (!close( MAIL )) {
warn $conf->get('MAILPROG') . " failed (exit status $?)\n";
return 0;
}
return 1;
}
# Note: split to stderr
sub debug (@) {
# TODO: Add debug level checking.
if ($debug_level) {
print STDERR "D: ", @_;
}
}
sub debug2 (@) {
# TODO: Add debug level checking.
if ($debug_level && $debug_level >= 2) {
print STDERR "D2: ", @_;
}
}
sub df {
my $dir = shift;
my $stat = Filesys::Df::df($dir);
return $stat->{bfree} if (defined($stat));
# This only happens if $dir was not a valid file or directory.
return 0;
}
sub check_group_membership () {
# Skip for root
return if ($< == 0);
my $user = getpwuid($<);
my ($name,$passwd,$gid,$members) = getgrnam("sbuild");
if (!$gid) {
die "Group sbuild does not exist";
}
my $in_group = 0;
my @groups = getgroups();
push @groups, getgid();
foreach (@groups) {
($name, $passwd, $gid, $members) = getgrgid($_);
$in_group = 1 if defined($name) && $name eq 'sbuild';
}
if (!$in_group) {
print STDERR "User $user is not currently an effective member of group sbuild. Please run:\n";
print STDERR " sudo sbuild-adduser $user\n";
print STDERR "And then either log out and log in again or use `newgrp sbuild` to gain sbuild group privileges\n";
exit(1);
}
return;
}
sub dsc_files ($) {
my $dsc = shift;
debug("Parsing $dsc\n");
my $pdsc = Dpkg::Control->new(type => CTRL_PKG_SRC);
$pdsc->set_options(allow_pgp => 1);
if (!$pdsc->load($dsc)) {
print STDERR "Could not parse $dsc\n";
return undef;
}
my $csums = Dpkg::Checksums->new();
$csums->add_from_control($pdsc, use_files_for_md5 => 1);
return $csums->get_files();
}
sub dsc_pkgver ($) {
my $dsc = shift;
debug("Parsing $dsc\n");
my $pdsc = Dpkg::Control->new(type => CTRL_PKG_SRC);
$pdsc->set_options(allow_pgp => 1);
if (!$pdsc->load($dsc)) {
print STDERR "Could not parse $dsc\n";
return undef;
}
return ($pdsc->{'Source'}, $pdsc->{'Version'});
}
# avoid dependency on String::ShellQuote by implementing the mechanism
# from python's shlex.quote function
sub shellescape ($) {
my $string = shift;
if (length $string == 0) {
return "''";
}
# search for occurrences of characters that are not safe
# the 'a' regex modifier makes sure that \w only matches ASCII
if ($string !~ m/[^\w@\%+=:,.\/-]/a) {
return $string;
}
# wrap the string in single quotes and handle existing single quotes by
# putting them outside of the single-quoted string
$string =~ s/'/'"'"'/g;
return "'$string'";
};
# this function uses strftime to format a timestamp as a string but makes sure
# to use the C locale to do so instead of the system locale
sub strftime_c ($@) {
my $format = shift;
my @time = @_;
my $old_locale = setlocale(LC_TIME);
setlocale(LC_TIME, "C.UTF-8");
my $ret = strftime $format, @time;
setlocale(LC_TIME, $old_locale);
return $ret;
}
1;
|