File: install.pl

package info (click to toggle)
dpkg 1.23.4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 59,120 kB
  • sloc: ansic: 40,205; perl: 30,746; sh: 18,907; cpp: 6,054; makefile: 5,226; sed: 127
file content (325 lines) | stat: -rwxr-xr-x 8,527 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/perl
#
# Copyright © 1995-1998 Ian Jackson <ijackson@chiark.greenend.org.uk>
# Copyright © 1998 Heiko Schlittermann <hs@schlittermann.de>
# Copyright © 2009 Raphaël Hertzog <hertzog@debian.org>
# Copyright © 2009-2025 Guillem Jover <guillem@debian.org>
#
# This 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 <https://www.gnu.org/licenses/>.

use v5.36;

use Dselect::Method::Config;
use Dselect::Method::Media;

eval q{
    use Dpkg::Version;
};
if ($@) {
    warn "Missing Dpkg modules required by the Media access method.\n\n";
    exit 1;
}

my $vardir = $ARGV[0];
my $method = $ARGV[1];
my $option = $ARGV[2];

chdir "$vardir/methods/$method";

my $conf = Dselect::Method::Config->new();

$conf->load("./shvar.$option");

my $p_blockdev = $conf->get('p_blockdev');
my $p_mountpoint = $conf->get('p_mountpoint');
my $p_hierbase = $conf->get('p_hierbase');

my $umount;

my $exit = 1;

sub do_umount {
    if (length $umount) {
        system 'umount', $umount;
    }
}

sub do_mount {
    my $opts = 'nosuid,nodev';
    if (! -b $p_blockdev) {
        $opts .= ',loop';
    }

    if (length $p_blockdev) {
        $umount = $p_mountpoint;
        system qw(mount -rt iso9660 -o), $opts, $p_blockdev, $p_mountpoint;
    }
}

do_mount();

my $predep = "$vardir/predep-package";
my $binaryprefix = "$p_mountpoint$p_hierbase";

while (1) {
    my $package;
    my $version;
    my @filename;
    my $medium;

    my $thisdisk = get_disk_label($p_mountpoint, $p_hierbase);

    system "dpkg --admindir '$vardir' --predep-package >'$predep'";
    my $rc = $? >> 8;
    last if $rc == 1;
    die if $rc != 0;

    open my $predep_fh, '<', $predep
        or die "cannot open $predep: $!\n";
    while (<$predep_fh>) {
        s/\s*\n$//;
        $package = $_ if s/^Package: //i;
        $version = $_ if s{^Version: }{}i;
        if (m{^X-Medium:\s+(.*)\s*}) {
            $medium = $1;
        }
        @filename = split / / if s/^Filename: //i;
    }
    close $predep_fh;
    die 'internal error - no package' if length($package) == 0;
    die 'internal error - no filename' if not @filename;
    if ($medium && ($medium ne $thisdisk)) {
        print <<"INFO";

This is
    $thisdisk
However, $package is expected on disc:
    $medium
Please change the discs and press <RETURN>.

INFO
        exit(1);
    }

    my @invoke = ();
    $| = 1;
    foreach my $i (0 .. $#filename) {
        my $ppart = $i + 1;
        my $print;
        my $invoke;
        my $base;

        print "Looking for part $ppart of $package ... ";
        if (-f "$binaryprefix$filename[$i]") {
            $print = $filename[$i];
            $invoke = "$binaryprefix$filename[$i]";
        } else {
            $base = $filename[$i];
            $base =~ s{.*/}{};
            my $c = open my $find_fh, '-|';
            if (not defined $c) {
                die "failed to fork for find: $!\n";
            }
            if (! $c) {
                exec('find', '-L',
                     length($binaryprefix) ? $binaryprefix : '.',
                     '-name', $base);
                die "failed to exec find: $!\n";
            }
            while (chop($invoke = <$find_fh>)) {
                last if -f $invoke;
            }
            close $find_fh;
            $print = $invoke;
            my $printprefix = substr $print, 0, length $binaryprefix + 1;
            if ($printprefix eq "$binaryprefix/") {
                $print = substr $print, length $binaryprefix;
            }
        }
        if (! length($invoke)) {
            warn <<"WARN";

Cannot find the appropriate file(s) anywhere needed to install or upgrade
package $package. Expecting version $version or later, as listed in the
Packages file.

Perhaps the package was downloaded with an unexpected name? In any case,
you must find the file(s) and then either place it with the correct
filename(s) (as listed in the Packages file or in $vardir/available)
and rerun the installation, or upgrade the package by using
"dpkg --install --auto-deconfigure" by hand.

WARN
            exit(1);
        }

        print "$print\n";
        push(@invoke, $invoke);
    }

    print "Running dpkg -iB for $package ...\n";
    exec('dpkg', '-iB', '--', @invoke);
    die "failed to exec dpkg: $!\n";
}

$SIG{INT} = sub {
    chdir $vardir;
    unlink <tmp/*>;
    exit 1;
};
$| = 1;

my $line;
my $AVAIL = "$vardir/methods/media/available";
my $STATUS = "$vardir/status";
my (%installed, %filename, %medium);

print 'Get currently installed package versions...';
open my $status_fh, '<', $STATUS
    or die "cannot open $STATUS: $!\n";
$line = 0;
{
    local $/ = q{};

    while (<$status_fh>) {
        if ($line++ % 20 == 0) {
            print q{.};
        }
        s/\n\s+/ /g;
        my %status = (
            q{},
            split /^(\S*?):\s*/m,
        );
        foreach my $field (keys %status) {
            chomp $status{$field};
            $status{$field} =~ s/^\s*(.*?)\s*$/$1/;
        }
        my @pstat = split(/ /, $status{Status});
        next unless ($pstat[0] eq 'install');
        if ($pstat[2] eq 'config-files' || $pstat[2] eq 'not-installed') {
            $installed{$status{Package}} = '0.0';
        } else {
            $installed{$status{Package}} = $status{Version} || q{};
        }
    }
}
close $status_fh;
print "\nGot ", scalar keys %installed, " installed/pending packages\n";

print 'Scanning available packages...';
$line = 0;
open my $avail_fh, '<', $AVAIL
    or die("Cannot open $AVAIL: $!\n");
{
    local $/ = q{};
    while (<$avail_fh>) {
        my $updated;

        if ($line++ % 20 == 0) {
            print q{.};
        }

        s/\n\s+/ /g;
        my %avail = (
            q{},
            split /^(\S*?):\s*/m,
        );
        foreach my $field (keys %avail) {
            chomp $avail{$field};
            $avail{$field} =~ s/^\s*(.*?)\s*$/$1/;
        }

        next unless defined $installed{$avail{Package}};

        $updated = version_compare($avail{Version},
                                   $installed{$avail{Package}}) > 0;
#       print "$avail{Package}(" . ($updated ? "+" : "=") . ") ";
        next unless $updated;

        $filename{$avail{Package}} = $avail{Filename};

        next unless defined $avail{'X-Medium'};
        $medium{$avail{'X-Medium'}} //= [];
        push @{$medium{$avail{'X-Medium'}}}, $avail{Package};
    }
}
close $avail_fh;
print "\n";

my $ouch;

my @media = sort keys %medium;
if (@media) {
    print "You will need the following distribution disc(s):\n",
          join(q{, }, @media), "\n";
}

foreach my $need (@media) {
    my $disk = get_disk_label($p_mountpoint, $p_hierbase);

    print "Processing disc\n   $need\n";

    while ($disk ne $need) {
        print "Wrong disc.  This is disc\n    $disk\n";
        print "However, the needed disc is\n    $need\n";
        print "Please change the discs and press <RETURN>\n";
        do_umount();
        <STDIN>;
        do_mount();
        if ($?) {
            warn "cannot mount $p_mountpoint\n";
        }
    } continue {
        $disk = get_disk_label($p_mountpoint, $p_hierbase);
    }

    if (! -d 'tmp') {
        mkdir 'tmp', 0o755
            or die("Cannot mkdir tmp: $!\n");
    }
    unlink <tmp/*>;

    print "creating symlinks...\n";
    foreach my $pkgname (@{$medium{$need}}) {
        my $basename;

        ($basename = $filename{$pkgname}) =~ s/.*\///;
        symlink "$p_mountpoint/$p_hierbase/$filename{$pkgname}", "tmp/$basename";
    }
    chdir 'tmp' or die "cannot chdir to tmp: $!\n";
    system 'dpkg', '-iGROEB', q{.};
    unlink <*>;
    chdir q{..};

    if ($?) {
        print "\nThe dpkg run produced errors. Please state whether to\n",
              'continue with the next media disc. [Y/n]: ';
        my $answer = <STDIN>;
        exit 1 if $answer =~ /^n/i;
        $ouch = $?;
    }
}

exit $ouch;


print 'Installation OK. Hit RETURN.';
<STDIN>;

$exit = 0;

END {
    do_umount();
    exit $exit;
}