update_responses 25.1 KB
Newer Older
1
2
3
4
5
6
7
8
#!/usr/bin/perl -w

use strict;
use feature "state";
use English;
use FindBin;
use YAML qw(LoadFile);
use File::Slurp;
9
use File::Path qw(make_path);
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
use Digest::SHA qw(sha256_hex);
use XML::Writer;
use Cwd;
use File::Copy;
use File::Temp;
use File::Find;
use POSIX qw(setlocale LC_ALL);
use IO::CaptureOutput qw(capture_exec);
use Parallel::ForkManager;
use File::Basename;
use XML::LibXML '1.70';
use LWP::Simple;
use JSON;

# Set umask and locale to provide a consistent environment for MAR file
# generation, etc.
umask(0022);
$ENV{"LC_ALL"} = "C";
setlocale(LC_ALL, "C");

my $htdocsdir = "$FindBin::Bin/htdocs";
my $config = LoadFile("$FindBin::Bin/config.yml");
my %htdocsfiles;
my $releases_dir = $config->{releases_dir};
$releases_dir = "$FindBin::Bin/$releases_dir" unless $releases_dir =~ m/^\//;
my @check_errors;
my $initPATH = $ENV{PATH};
my $initLD_LIBRARY_PATH = $ENV{LD_LIBRARY_PATH};

sub exit_error {
    print STDERR "Error: ", $_[0], "\n";
    chdir '/';
    exit (exists $_[1] ? $_[1] : 1);
}

45
46
47
48
49
50
51
sub get_tmpdir {
    my ($config) = @_;
    return File::Temp->newdir($config->{tmp_dir} ?
                                (DIR => $config->{tmp_dir})
                                : ());
}

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
sub build_targets_by_os {
    return ($_[0]) unless $config->{build_targets}{$_[0]};
    my $r = $config->{build_targets}{$_[0]};
    return ref $r eq 'ARRAY' ? @$r : ($r);
}

sub get_nbprocs {
    return $ENV{NUM_PROCS} if defined $ENV{NUM_PROCS};
    if (-f '/proc/cpuinfo') {
        return scalar grep { m/^processor\s+:\s/ } read_file '/proc/cpuinfo';
    }
    return 4;
}

sub write_htdocs {
    my ($channel, $file, $content) = @_;
    mkdir $htdocsdir unless -d $htdocsdir;
    mkdir "$htdocsdir/$channel" unless -d "$htdocsdir/$channel";
    write_file("$htdocsdir/$channel/$file", $content);
    $htdocsfiles{$channel}->{$file} = 1;
}

sub clean_htdocs {
    my (@channels) = @_;
    foreach my $channel (@channels) {
        opendir(my $d, "$htdocsdir/$channel");
        my @files = grep { ! $htdocsfiles{$channel}->{$_} } readdir $d;
        closedir $d;
        unlink map { "$htdocsdir/$channel/$_" } @files;
    }
}

sub get_sha512_hex_of_file {
    my ($file) = @_;
    my $sha = Digest::SHA->new("512");
    $sha->addfile($file);
    return $sha->hexdigest;
}

sub get_version_files {
    my ($config, $version) = @_;
    return if $config->{versions}{$version}{files};
    my $appname = $config->{appname_marfile};
    my $files = {};
96
    my $vdir = version_dir($config, $version);
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
    my $download_url = "$config->{download}{mars_url}/$version";
    opendir(my $d, $vdir) or exit_error "Error opening directory $vdir";
    foreach my $file (readdir $d) {
        next unless -f "$vdir/$file";
        if ($file =~ m/^$appname-([^-]+)-${version}_(.+)\.mar$/) {
            my ($os, $lang) = ($1, $2);
            $files->{$os}{$lang}{complete} = {
                type => 'complete',
                URL => "$download_url/$file",
                size => -s "$vdir/$file",
                hashFunction => 'SHA512',
                hashValue => get_sha512_hex_of_file("$vdir/$file"),
            };
            next;
        }
        if ($file =~ m/^$appname-([^-]+)-(.+)-${version}_(.+)\.incremental\.mar$/) {
            my ($os, $from_version, $lang) = ($1, $2, $3);
            $files->{$os}{$lang}{partial}{$from_version} = {
                type => 'partial',
                URL => "$download_url/$file",
                size => -s "$vdir/$file",
                hashFunction => 'SHA512',
                hashValue => get_sha512_hex_of_file("$vdir/$file"),
            }
        }
    }
    closedir $d;
    $config->{versions}{$version}{files} = $files;
}

sub get_version_downloads {
    my ($config, $version) = @_;
    my $downloads = {};
130
    my $vdir = version_dir($config, $version);
131
132
133
134
135
136
137
138
139
    my $download_url = "$config->{download}{bundles_url}/$version";
    opendir(my $d, $vdir) or exit_error "Error opening directory $vdir";
    foreach my $file (readdir $d) {
        next unless -f "$vdir/$file";
        my ($os, $lang);
        if ($file =~ m/^$config->{appname_bundle_osx}-$version-osx64_(.+).dmg$/) {
            ($os, $lang) = ('osx64', $1);
        } elsif ($file =~ m/^$config->{appname_bundle_linux}-(linux32|linux64)-${version}_(.+).tar.xz$/) {
            ($os, $lang) = ($1, $2);
140
141
142
        } elsif ($file =~ m/^$config->{appname_bundle_win64}-${version}_(.+).exe$/) {
            ($os, $lang) = ('win64', $1);
        } elsif ($file =~ m/^$config->{appname_bundle_win32}-${version}_(.+).exe$/) {
143
144
145
146
147
148
149
150
151
152
153
154
155
156
            ($os, $lang) = ('win32', $1);
        } else {
            next;
        }
        $downloads->{$os}{$lang} = {
            binary => "$download_url/$file",
            sig => "$download_url/$file.asc",
        };
    }
    closedir $d;
    $config->{versions}{$version}{downloads} = $downloads;
}

sub extract_mar {
157
    my ($mar_file, $dest_dir, $compression) = @_;
158
159
160
161
162
    my $old_cwd = getcwd;
    mkdir $dest_dir;
    chdir $dest_dir or exit_error "Cannot enter $dest_dir";
    my $res = system('mar', '-x', $mar_file);
    exit_error "Error extracting $mar_file" if $res;
163
164
165
166
167
168
    if ($compression ne 'bzip2' && $compression ne 'xz') {
        exit_error "Unknown compression format $compression";
    }
    my $compr_ext = $compression eq 'bzip2' ? 'bz2' : 'xz';
    my $compr_cmd = $compression eq 'bzip2' ? 'bunzip2' : 'unxz';
    my $uncompress_file = sub {
169
        return unless -f $File::Find::name;
170
171
        rename $File::Find::name, "$File::Find::name.$compr_ext";
        system($compr_cmd, "$File::Find::name.$compr_ext") == 0
172
173
                || exit_error "Error decompressing $File::Find::name";
    };
174
    find($uncompress_file, $dest_dir);
175
176
177
178
179
180
181
182
183
184
185
186
187
    my $manifest = -f 'updatev3.manifest' ? 'updatev3.manifest'
                        : 'updatev2.manifest';
    my @lines = read_file($manifest) if -f $manifest;
    foreach my $line (@lines) {
        if ($line =~ m/^addsymlink "(.+)" "(.+)"$/) {
            exit_error "$mar_file: Could not create symlink $1 -> $2"
                unless symlink $2, $1;
        }
    }
    chdir $old_cwd;
}

sub mar_filename {
188
189
    my ($config, $appname, $version, $os, $lang) = @_;
    version_dir($config, $version) .  "/$appname-$os-${version}_$lang.mar";
190
191
192
}

sub create_incremental_mar {
193
    my ($config, $pm, $from_version, $new_version, $os, $lang, $channel) = @_;
194
195
    my $appname = $config->{appname_marfile};
    my $mar_file = "$appname-$os-${from_version}-${new_version}_$lang.incremental.mar";
196
    my $mar_file_path = version_dir($config, $new_version) . '/' . $mar_file;
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
    if ($ENV{MAR_SKIP_EXISTING} && -f $mar_file_path) {
        print "Skipping $mar_file\n";
        return;
    }
    print "Starting $mar_file\n";
    my $download_url = "$config->{download}{mars_url}/$new_version";
    my $finished_file = sub {
        exit_error "Error creating $mar_file" unless $_[1] == 0;
        print "Finished $mar_file\n";
        $config->{versions}{$new_version}{files}{$os}{$lang}{partial}{$from_version} = {
            type => 'partial',
            URL => "$download_url/$mar_file",
            size => -s $mar_file_path,
            hashFunction => 'SHA512',
            hashValue => get_sha512_hex_of_file($mar_file_path),
        };
    };
    return if $pm->start($finished_file);
215
    my $tmpdir = get_tmpdir($config);
216
217
218
219
220
221
    my $mar_c_from = get_config($config, $from_version, $os, 'mar_compression');
    my $mar_c_new = get_config($config, $new_version, $os, 'mar_compression');
    extract_mar(mar_filename($config, $appname, $from_version, $os, $lang),
                "$tmpdir/A", $mar_c_from);
    extract_mar(mar_filename($config, $appname, $new_version, $os, $lang),
                "$tmpdir/B", $mar_c_new);
222
    # bug 26054: make sure previous macOS version is code signed
223
224
    if (!$ENV{NO_CODESIGNATURE} && ($os eq 'osx64')
                && ! -f "$tmpdir/A/Contents/_CodeSignature/CodeResources") {
225
226
        exit_error "Missing code signature in $from_version while creating $mar_file";
    }
227
228
229
230
231
232
    if ($ENV{CHECK_CODESIGNATURE_EXISTS}) {
        unless (-f "$tmpdir/A/Contents/_CodeSignature/CodeResources"
            && -f "$tmpdir/B/Contents/_CodeSignature/CodeResources") {
            exit_error "Missing code signature while creating $mar_file";
        }
    }
233
234
    local $ENV{MOZ_PRODUCT_VERSION} = $new_version;
    local $ENV{MAR_CHANNEL_ID} = "torbrowser-torproject-$channel";
235
236
237
238
239
240
241
242
243
244
    my ($out, $err, $success) = capture_exec('make_incremental_update.sh',
                                   $mar_file_path, "$tmpdir/A", "$tmpdir/B");
    if (!$success) {
        unlink $mar_file_path if -f $mar_file_path;
        exit_error "making incremental mar:\n" . $err;
    }
    $pm->finish;
}

sub create_incremental_mars_for_version {
245
    my ($config, $version, $channel) = @_;
246
247
248
249
250
251
252
253
254
255
    my $pm = Parallel::ForkManager->new(get_nbprocs);
    $pm->run_on_finish(sub { $_[2]->(@_) });
    my $v = $config->{versions}{$version};
    foreach my $from_version (@{$v->{incremental_from}}) {
        $config->{versions}{$from_version} //= {};
        get_version_files($config, $from_version);
        my $from_v = $config->{versions}{$from_version};
        foreach my $os (keys %{$v->{files}}) {
            foreach my $lang (keys %{$v->{files}{$os}}) {
                next unless defined $from_v->{files}{$os}{$lang}{complete};
256
                create_incremental_mar($config, $pm, $from_version, $version, $os, $lang, $channel);
257
258
259
260
261
262
263
264
265
266
267
268
269
            }
        }
    }
    $pm->wait_all_children;
}

sub get_config {
    my ($config, $version, $os, $name) = @_;
    return $config->{versions}{$version}{$os}{$name}
        // $config->{versions}{$version}{$name}
        // $config->{$name};
}

270
271
272
273
274
sub version_dir {
    my ($config, $version) = @_;
    return get_config($config, $version, 'any', 'releases_dir') . "/$version";
}

275
276
277
278
279
280
281
282
283
284
285
286
287
sub channel_to_version {
    my ($config, @channels) = @_;
    return values %{$config->{channels}} unless @channels;
    foreach my $channel (@channels) {
        exit_error "Unknown channel $channel"
                unless $config->{channels}{$channel};
    }
    return map { $config->{channels}{$_} } @channels;
}

sub get_buildinfos {
    my ($config, $version) = @_;
    return if exists $config->{versions}{$version}{buildID};
288
    extract_martools($config, $version);
289
290
291
292
    my $files = $config->{versions}{$version}{files};
    foreach my $os (keys %$files) {
        foreach my $lang (keys %{$files->{$os}}) {
            next unless $files->{$os}{$lang}{complete};
293
            my $tmpdir = get_tmpdir($config);
294
            my $mar_compression = get_config($config, $version, $os, 'mar_compression');
295
            extract_mar(
296
                mar_filename($config, $config->{appname_marfile}, $version, $os, $lang),
297
298
                "$tmpdir",
                $mar_compression);
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
            my $appfile = "$tmpdir/application.ini" if -f "$tmpdir/application.ini";
            $appfile = "$tmpdir/Contents/Resources/application.ini"
                                if -f "$tmpdir/Contents/Resources/application.ini";
            exit_error "Could not find application.ini" unless $appfile;
            foreach my $line (read_file($appfile)) {
                if ($line =~ m/^BuildID=(.*)$/) {
                    $config->{versions}{$version}{buildID} = $1;
                    return;
                }
            }
            exit_error "Could not extract buildID from application.ini";
        }
    }
}

sub get_response {
    my ($config, $version, $os, @patches) = @_;
    my $res;
    my $writer = XML::Writer->new(OUTPUT => \$res, ENCODING => 'UTF-8');
    $writer->xmlDecl;
    $writer->startTag('updates');
    if (get_config($config, $version, $os, 'unsupported')) {
        $writer->startTag('update',
            unsupported => 'true',
            detailsURL => get_config($config, $version, $os, 'detailsURL'),
        );
        goto CLOSETAGS;
    }
    my $minversion = get_config($config, $version, $os, 'minSupportedOSVersion');
    my $mininstruc = get_config($config, $version, $os, 'minSupportedInstructionSet');
    $writer->startTag('update',
        type => 'minor',
        displayVersion => $version,
        appVersion => $version,
        platformVersion => get_config($config, $version, $os, 'platformVersion'),
        buildID => get_config($config, $version, $os, 'buildID'),
        detailsURL => get_config($config, $version, $os, 'detailsURL'),
        actions => 'showURL',
        openURL => get_config($config, $version, $os, 'detailsURL'),
        defined $minversion ? ( minSupportedOSVersion => $minversion ) : (),
        defined $mininstruc ? ( minSupportedInstructionSet => $mininstruc ) : (),
    );
    foreach my $patch (@patches) {
        my @sorted_patch = map { $_ => $patch->{$_} } sort keys %$patch;
        $writer->startTag('patch', @sorted_patch);
        $writer->endTag('patch');
    }
    CLOSETAGS:
    $writer->endTag('update');
    $writer->endTag('updates');
    $writer->end;
    return $res;
}

sub write_responses {
    my ($config, @channels) = @_;
    @channels = keys %{$config->{channels}} unless @channels;
    foreach my $channel (@channels) {
        my $version = $config->{channels}{$channel};
        get_version_files($config, $version);
        get_buildinfos($config, $version);
        my $files = $config->{versions}{$version}{files};
        my $migrate_archs = $config->{versions}{$version}{migrate_archs} // {};
        foreach my $old_os (keys %$migrate_archs) {
            my $new_os = $migrate_archs->{$old_os};
            foreach my $lang (keys %{$files->{$new_os}}) {
                $files->{$old_os}{$lang}{complete} =
                        $files->{$new_os}{$lang}{complete};
            }
        }
        foreach my $os (keys %$files) {
            foreach my $lang (keys %{$files->{$os}}) {
                my $resp = get_response($config, $version, $os,
                                $files->{$os}{$lang}{complete});
                write_htdocs($channel, "$version-$os-$lang.xml", $resp);
                foreach my $from_version (keys %{$files->{$os}{$lang}{partial}}) {
                    $resp = get_response($config, $version, $os,
                                $files->{$os}{$lang}{complete},
                                $files->{$os}{$lang}{partial}{$from_version});
                    write_htdocs($channel, "$from_version-$version-$os-$lang.xml", $resp);
                }
            }
        }
        write_htdocs($channel, 'no-update.xml',
            '<?xml version="1.0" encoding="UTF-8"?>'
            . "\n<updates></updates>\n");
    }
}

sub write_htaccess {
    my ($config, @channels) = @_;
    @channels = keys %{$config->{channels}} unless @channels;
    my $flags = "[last]";
    foreach my $channel (@channels) {
        my $htaccess = "RewriteEngine On\n";
394
        $htaccess .= $config->{htaccess_rewrite_rules}{$channel} // '';
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
        my $version = $config->{channels}{$channel};
        my $migrate_langs = $config->{versions}{$version}{migrate_langs} // {};
        my $files = $config->{versions}{$version}{files};
        $htaccess .= "RewriteRule ^[^\/]+/$version/ no-update.xml $flags\n";
        foreach my $os (sort keys %$files) {
            foreach my $bt (build_targets_by_os($os)) {
                foreach my $lang (sort keys %{$files->{$os}}) {
                    foreach my $from_version (sort keys %{$files->{$os}{$lang}{partial}}) {
                        $htaccess .= "RewriteRule ^$bt/$from_version/$lang "
                                  .  "$from_version-$version-$os-$lang.xml $flags\n";
                    }
                    $htaccess .= "RewriteRule ^$bt/[^\/]+/$lang "
                              .  "$version-$os-$lang.xml $flags\n";
                }
                foreach my $lang (sort keys %$migrate_langs) {
                    $htaccess .= "RewriteRule ^$bt/[^\/]+/$lang "
                              .  "$version-$os-$migrate_langs->{$lang}.xml $flags\n";
                }
                $htaccess .= "RewriteRule ^$bt/ $version-$os-en-US.xml $flags\n";
            }
        }
        write_htdocs($channel, '.htaccess', $htaccess);
    }
}

sub write_downloads_json {
    my ($config, @channels) = @_;
422
    return unless $config->{create_downloads_json};
423
424
425
426
427
428
429
430
431
432
433
434
    @channels = keys %{$config->{channels}} unless @channels;
    foreach my $channel (@channels) {
        my $version = $config->{channels}{$channel};
        my $data = {
            version => $version,
            downloads => get_version_downloads($config, $version),
        };
        write_htdocs($channel, 'downloads.json',
            JSON->new->utf8->canonical->encode($data));
    }
}

435
436
437
438
439
sub marzip_path {
    my ($config, $version) = @_;
    for my $osname (qw/linux64 linux32 mac64 win64 win32/) {
        my $marzip = version_dir($config, $version) . "/mar-tools-$osname.zip";
        return $marzip if -f $marzip;
440
    }
441
    exit_error 'Could not find mar-tools';
442
443
444
445
}

my $martools_tmpdir;
sub extract_martools {
446
    my ($config, $version) = @_;
447
    my $marzip = marzip_path($config, $version);
448
    $martools_tmpdir = get_tmpdir($config);
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
    my $old_cwd = getcwd;
    chdir $martools_tmpdir;
    my (undef, undef, $success) = capture_exec('unzip', $marzip);
    chdir $old_cwd;
    exit_error "Error extracting $marzip" unless $success;
    $ENV{PATH} = "$martools_tmpdir/mar-tools:$initPATH";
    if ($initLD_LIBRARY_PATH) {
        $ENV{LD_LIBRARY_PATH} = "$initLD_LIBRARY_PATH:$martools_tmpdir/mar-tools";
    } else {
        $ENV{LD_LIBRARY_PATH} = "$martools_tmpdir/mar-tools";
    }
}

sub log_step {
    my ($url, $step, $status, $details) = @_;
    state $u;
    if (!defined $u || $url ne $u) {
        print "\n" if $u;
        print "$url\n";
        $u = $url;
    }
    print '  ', $step, $status ? ': OK' : ': ERROR',
          $details ? " - $details\n" : "\n";
    return if $status;
    push @check_errors, { url => $url, step => $step, details => $details };
}

sub get_remote_xml {
    my ($url) = @_;
    my $content = get $url;
    log_step($url, 'get', defined $content);
    return undef unless defined $content;
    my $dom = eval { XML::LibXML->load_xml(string => $content) };
    log_step($url, 'parse_xml', defined $dom, $@);
    return $dom;
}

sub check_get_version {
    my ($dom) = @_;
    my @updates = $dom->documentElement()->getChildrenByLocalName('update');
    return undef unless @updates;
    return $updates[0]->getAttribute('appVersion');
}

sub check_no_update {
    my ($dom) = @_;
    my @updates = $dom->documentElement()->getChildrenByLocalName('update');
    return @updates == 0;
}

sub check_has_incremental {
    my ($dom) = @_;
    my @updates = $dom->documentElement()->getChildrenByLocalName('update');
    return undef unless @updates;
    my @patches = $updates[0]->getChildrenByLocalName('patch');
    foreach my $patch (@patches) {
        return 1 if $patch->getAttribute('type') eq 'partial';
    }
    return undef;
}

sub build_targets_list {
    map { ref $_ eq 'ARRAY' ? @$_ : $_ } values %{$config->{build_targets}};
}

sub check_update_responses_channel {
    my ($config, $base_url, $channel) = @_;
    my $channel_version = $config->{channels}{$channel};
    foreach my $build_target (build_targets_list()) {
        foreach my $lang (qw(en-US de)) {
            my $url = "$base_url/$channel/$build_target/1.0/$lang";
            my $dom = get_remote_xml($url);
            if ($dom) {
                my $version = check_get_version($dom);
                log_step($url, 'version', $version eq $channel_version,
                         "expected: $channel_version received: $version");
            }
            $url = "$base_url/$channel/$build_target/$channel_version/$lang";
            $dom = get_remote_xml($url);
            log_step($url, 'no_update', check_no_update($dom)) if $dom;
            my @inc = @{$config->{versions}{$channel_version}{incremental_from}}
                      if $config->{versions}{$channel_version}{incremental_from};
            foreach my $inc_from (@inc) {
                my $url = "$base_url/$channel/$build_target/$inc_from/$lang";
                $dom = get_remote_xml($url);
                next unless $dom;
                my $version = check_get_version($dom);
                log_step($url, 'version', $version eq $channel_version,
                         "expected: $channel_version received: $version");
                log_step($url, 'has_incremental', check_has_incremental($dom));
            }
        }
    }
}

sub download_version {
    my ($config, $version) = @_;
546
    my $tmpdir = get_tmpdir($config);
547
    my $destdir = version_dir($config, $version);
548
549
    my $urldir = "$config->{download}{archive_url}/$version";
    print "Downloading version $version\n";
550
    foreach my $file (qw(sha256sums-signed-build.txt sha256sums-signed-build.txt.asc)) {
551
552
553
554
555
556
        if (getstore("$urldir/$file", "$tmpdir/$file") != 200) {
            exit_error "Error downloading $urldir/$file";
        }
    }
    if (system('gpg', '--no-default-keyring', '--keyring',
            "$FindBin::Bin/$config->{download}{gpg_keyring}", '--verify',
557
558
            "$tmpdir/sha256sums-signed-build.txt.asc",
            "$tmpdir/sha256sums-signed-build.txt")) {
559
560
        exit_error "Error checking gpg signature for version $version";
    }
561
    make_path $destdir;
562
563
    move "$tmpdir/sha256sums-signed-build.txt.asc", "$destdir/sha256sums-signed-build.txt.asc";
    move "$tmpdir/sha256sums-signed-build.txt", "$destdir/sha256sums-signed-build.txt";
564
    my %sums = map { chomp; reverse split '  ', $_ }
565
                 read_file "$destdir/sha256sums-signed-build.txt";
566

567
    my $martools = 'mar-tools-linux64.zip';
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
    exit_error "Error downloading $urldir/$martools\n"
        unless getstore("$urldir/$martools", "$tmpdir/$martools") == 200;
    exit_error "Error downloading $urldir/$martools.asc\n"
        unless getstore("$urldir/$martools.asc", "$tmpdir/$martools.asc") == 200;
    if (system('gpg', '--no-default-keyring', '--keyring',
            "$FindBin::Bin/$config->{download}{gpg_keyring}", '--verify',
            "$tmpdir/$martools.asc", "$tmpdir/$martools")) {
        exit_error "Error checking gpg signature for $version/$martools";
    }
    exit_error "Wrong checksum for $version/$martools"
        unless $sums{$martools} eq sha256_hex(read_file("$tmpdir/$martools"));
    move "$tmpdir/$martools", "$destdir/$martools";
    move "$tmpdir/$martools.asc", "$destdir/$martools.asc";

    foreach my $file (sort grep { $_ =~ m/\.mar$/ } keys %sums) {
        print "Downloading $file\n";
        exit_error "Error downloading $urldir/$file\n"
                unless getstore("$urldir/$file", "$tmpdir/$file") == 200;
586
587
        exit_error "Wrong checksum for $file"
                unless $sums{$file} eq sha256_hex(read_file("$tmpdir/$file"));
588
589
590
591
592
593
594
595
596
597
598
599
        move "$tmpdir/$file", "$destdir/$file";
    }
}

sub download_missing_versions {
    my ($config, @channels) = @_;
    foreach my $channel (@channels) {
        exit_error "Unknown channel $channel"
                unless $config->{channels}{$channel};
        my $cversion = $config->{channels}{$channel};
        next unless $config->{versions}{$cversion}{incremental_from};
        foreach my $version (@{$config->{versions}{$cversion}{incremental_from}}) {
600
            next if -d version_dir($config, $version);
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
            download_version($config, $version);
        }
    }
}

sub check_update_responses {
    my ($config) = @_;
    exit_error "usage: $PROGRAM_NAME <base_url> [channels...]" unless @ARGV;
    my ($base_url, @channels) = @ARGV;
    foreach my $channel (@channels ? @channels : keys %{$config->{channels}}) {
        check_update_responses_channel($config, $base_url, $channel);
    }
    if (!@check_errors) {
        print "\n\nNo errors\n";
        return;
    }
    print "\n\nErrors list:\n";
    my $url = '';
    foreach my $error (@check_errors) {
        if ($url ne $error->{url}) {
            $url = $error->{url};
            print "$url\n";
        }
        print "  $error->{step}",
              $error->{details} ? " - $error->{details}\n" : "\n";
    }
}

my %actions = (
    update_responses => sub {
        my ($config) = @_;
        my @channels = @ARGV ? @ARGV : keys %{$config->{channels}};
        foreach my $channel (@channels) {
            exit_error "Unknown channel $channel"
                unless $config->{channels}{$channel};
            $htdocsfiles{$channel} = { '.' => 1, '..' => 1 };
        }
        write_responses($config, @channels);
        write_htaccess($config, @channels);
        write_downloads_json($config, @channels);
        clean_htdocs(@channels);
    },
    gen_incrementals => sub {
        my ($config) = @_;
645
646
        foreach my $channel (@ARGV) {
            my ($version) = channel_to_version($config, $channel);
647
            extract_martools($config, $version);
648
            get_version_files($config, $version);
649
            create_incremental_mars_for_version($config, $version, $channel);
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
        }
    },
    download_missing_versions => sub {
        my ($config) = @_;
        my @channels = @ARGV ? @ARGV : keys %{$config->{channels}};
        download_missing_versions($config, @channels);
    },
    check_update_responses_deployement => \&check_update_responses,
    get_channel_version => sub {
        my ($config) = @_;
        exit_error "Wrong arguments" unless @ARGV == 1;
        exit_error "Unknown channel" unless $config->{channels}{$ARGV[0]};
        print $config->{channels}{$ARGV[0]}, "\n";
    },
);

my $action = fileparse($PROGRAM_NAME);
exit_error "Unknown action $action" unless $actions{$action};
$actions{$action}->($config);