aboutsummaryrefslogtreecommitdiffstats
path: root/genhdlist2
blob: 912716ff422567165a12d859662e4780075b70c1 (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
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
#!/usr/bin/perl

(our $VERSION) = q(Id: genhdlist2 20460 2006-11-23 13:19:11Z pixel ) =~ /(\d+\.\d+)/;

use URPM;
use MDV::Packdrakeng;
use Getopt::Long;

main();

sub usage () {
    require Pod::Usage;
    Pod::Usage::pod2usage({ '-verbose' => 1 });
}

sub main() {
    my %options;

    GetOptions(
	'clean' => \$options{no_incremental},
	'no-bad-rpm' => \$options{no_bad_rpm},
	'no-md5sum' => \$options{no_md5sum},
	'nolock' => \$options{nolock},
	'file-deps=s' => \$options{file_deps},
	'h|help' => sub { usage(); exit 0 },
	'q|quiet' => sub { $options{verbose} = -1 },
	'v|verbose' => sub { $options{verbose}++ },
	'version' => sub { warn "$0 version $VERSION\n"; exit 0 },
    );

    @ARGV <= 1 or usage();
    my $rpms_dir = $ARGV[0] || '.';

    do_it($rpms_dir, %options);
}

# global vars
my ($no_bad_rpm, $verbose);
my $tmp_header;

sub do_it {
    my ($rpms_dir, %options) = @_;

    $verbose  = $options{verbose};
    $no_bad_rpm = $options{no_bad_rpm};

    my $media_info_dir = "$rpms_dir/media_info";
    -e $media_info_dir || mkdir $media_info_dir or die "Can't mkdir $media_info_dir: $!n";
    -d $media_info_dir && -w _ && -x _ or die "$media_info_dir isn't a writable directory, bailing out\n";

    my $hdlist = "$media_info_dir/hdlist.cz";
    my $synthesis = "$media_info_dir/synthesis.hdlist.cz";
    my $lock_file = "$media_info_dir/UPDATING";
    $tmp_header = "$media_info_dir/.tmp-header";

    my $lock = !$options{nolock} && lock_file($lock_file);

    $SIG{INT} = sub { 
	unlink "$hdlist.tmp", "$synthesis.tmp", $tmp_header;
	unlink $lock_file if $lock;
	exit 1;
    };
    END { unlink $lock_file if $lock }

    my @rpms = grep { /\.rpm$/ } all($rpms_dir);
    my %rpms_todo = map { /(.*)\.rpm/ => 1 } @rpms;

    my $urpm = new URPM;

    read_file_deps($urpm, $options{file_deps}) if $options{file_deps};

    build_hdlist($urpm, \%rpms_todo, $hdlist, $rpms_dir, $options{no_incremental});
    build_synthesis($urpm, "$synthesis.tmp");

    if (1) {
	print "replacing $hdlist with $hdlist.tmp\n" if $verbose >= 0;
	rename "$hdlist.tmp", $hdlist or die "rename $hdlist failed: $?\n";
	print "replacing $synthesis with $synthesis.tmp\n" if $verbose >= 0;
	rename "$synthesis.tmp", $synthesis or die "ERROR: this should not happen: rename $synthesis failed\n";
	generate_md5sum($media_info_dir, $hdlist, $synthesis) if !$options{no_md5sum};
    }
}

sub lock_file {
    my ($file) = @_;
    #- avoid putting a require on Fcntl ':flock' (which is perl and not perl-base).
    my ($LOCK_EX, $LOCK_NB) = (2, 4);

    print "locking $file\n" if $verbose > 0;
    open(my $lock, '>', $file) or die "lock_file $file failed\n";
    flock $lock, $LOCK_EX|$LOCK_NB or die "another genhdlist2 already running\n";
    $lock;
}

sub read_file_deps {
    my ($urpm, $file_deps) = @_;

    -r $file_deps or die "can't read $file_deps: $?\n";

    foreach (cat_($file_deps)) {
	chomp;
	$urpm->{provides}{$_} = undef;
    }
}

sub build_hdlist {
    my ($urpm, $rpms_todo, $hdlist, $rpms_dir, $b_no_incremental) = @_;

    my $out = MDV::Packdrakeng->new(
	archive => "$hdlist.tmp",
	compress => "gzip",
	uncompress => "gzip -d",
	comp_level => 9,
    ) or die "Can't create archive";

    if (-e $hdlist && !$b_no_incremental) {
	print "filtering $hdlist into $hdlist.tmp\n" if $verbose >= 0;
	filter_existing_hdlist($urpm, $rpms_todo, $hdlist, $out);
    }

    add_new_rpms_to_hdlist($urpm, $rpms_todo, $out, $rpms_dir);
}

sub filter_existing_hdlist {
    my ($urpm, $rpms_todo, $in_hdlist, $out) = @_;

    $urpm->parse_hdlist($in_hdlist, packing => 1, callback => sub {
	my (undef, $pkg) = @_;
	my $fullname = $pkg->fullname;
	if (delete $rpms_todo->{$fullname}) {
	    print "keeping $fullname\n" if $verbose > 1;
	    add_pkg_header($out, $pkg);
	    1; # do keep in memory
	} else {
	    print "removing $fullname\n" if $verbose > 0;
	    0; # don't keep in memory
	}
    });
}

sub add_new_rpms_to_hdlist {
    my ($urpm, $rpms_todo, $out, $rpms_dir) = @_;

    my @rpms = keys %$rpms_todo or return;
    if ($verbose >= 0) {
	if (@rpms > 100 || $verbose == 0) {
	    print "adding ", int(@rpms), " new rpms not available in existing hdlist\n";
	} else {
	    print "adding ", join(' ', @rpms), "\n";
	}
    }

    foreach (@rpms) {
	print "adding $_\n" if $verbose > 1;

	my $rpm = "$rpms_dir/$_.rpm";
	my ($id, undef) = $urpm->parse_rpm($rpm);
	if (!defined $id) {
	    if ($no_bad_rpm) {
		print STDERR "bad rpm $rpm\n";
		next;
	    } else {
		die "bad rpm $rpm\n";
	    }
	}
	my $pkg = $urpm->{depslist}[$id];
	add_pkg_header($out, $pkg);

	$pkg->pack_header; # for synthesis
    }
}

sub add_pkg_header {
    my ($out, $pkg) = @_;
    {
	open(my $fh, ">", $tmp_header);
	$pkg->build_header(fileno $fh);
    }
    {
	open(my $fh, "<", $tmp_header);
	$out->add_virtual('f', scalar($pkg->fullname), $fh);
    }
    unlink $tmp_header;
}

sub build_synthesis {
    my ($urpm, $synthesis) = @_;
    $urpm->build_synthesis(
	start     => 0,
	end       => $#{$urpm->{depslist}},
	synthesis => $synthesis,
    ) or die "build_synthesis failed (disk full?)\n";
}

sub generate_md5sum {
    my ($media_info_dir, $hdlist, $synthesis) = @_;
    print "updating $media_info_dir/MD5SUM\n" if $verbose >= 0;
    my $m = `/usr/bin/md5sum '$hdlist' '$synthesis'`;
    open my $f, '>', "$media_info_dir/MD5SUM" or die "Can't write MD5SUM: $!\n";
    print $f $m;
}


sub cat_ { my @l = map { my $F; open($F, '<', $_) ? <$F> : () } @_; wantarray() ? @l : join '', @l }

sub all {
    my $d = shift;

    local *F;
    opendir F, $d or return;
    my @l = grep { $_ ne '.' && $_ ne '..' } readdir F;
    closedir F;

    @l;
}

__END__

=head1 NAME

genhdlist2 - generates an hdlist and a synthesis file

=head1 SYNOPSIS

    genhdlist2 [options] [dir]

=head1 OPTIONS

=over 4

=item --no-md5sum

Do not generate MD5SUM file.

=item --no-bad-rpm

Do not abort on bad rpms.

=item B<--nolock>

Don't lock the media (can be useful when locks fail, eg NFS)

=item --clean

Do not use existing hdlist.cz, build hdlist from scratch

=item --quiet

Quiet mode.

=back

=head1 DESCRIPTION

F<genhdlist2> is used to generate an hdlist and an associated synthesis file
from a set of RPM packages found in the directories passed on the command-line.
By default it will put the hdlist and synthesis files in the current directory.

=head1 SEE ALSO

gendistrib(1), parsehdlist(1)

=head1 COPYRIGHT

Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005 MandrakeSoft SA

Copyright (C) 2005, 2006 Mandriva SA

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, 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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

=cut