1#!/usr/bin/perl -w
2# This Source Code Form is subject to the terms of the Mozilla Public
3# License, v. 2.0. If a copy of the MPL was not distributed with this
4# file, You can obtain one at http://mozilla.org/MPL/2.0/.
5
6#
7# This tool unpacks a full update package generated by make_full_update.sh
8# Author: Benjamin Smedberg
9#
10
11# -----------------------------------------------------------------------------
12# By default just assume that these tools exist on our path
13
14use Getopt::Std;
15
16my ($MAR, $XZ, $BZIP2, $MAR_OLD_FORMAT, $archive, @marentries, @marfiles);
17
18if (defined($ENV{"MAR"})) {
19    $MAR = $ENV{"MAR"};
20}
21else {
22    $MAR = "mar";
23}
24
25if (defined($ENV{"BZIP2"})) {
26    $BZIP2 = $ENV{"BZIP2"};
27}
28else {
29    $BZIP2 = "bzip2";
30}
31
32if (defined($ENV{"XZ"})) {
33    $XZ = $ENV{"XZ"};
34}
35else {
36    if (system("xz --version > /dev/null 2>&1") != 0) {
37        # Some of the Windows build systems have xz.exe in topsrcdir/xz/.
38        my $xzwinpath = __FILE__;
39        $xzwinpath = substr($xzwinpath, 0, rindex($xzwinpath, '/'));
40        $xzwinpath = substr($xzwinpath, 0, rindex($xzwinpath, '/'));
41        $xzwinpath = substr($xzwinpath, 0, rindex($xzwinpath, '/'));
42        my $xzwin = $xzwinpath . "/xz/xz.exe";
43        if (-e $xzwin) {
44            $XZ = $xzwin;
45        }
46        else {
47            $xzwinpath = substr($xzwinpath, 0, rindex($xzwinpath, '/'));
48            $xzwin = $xzwinpath . "/xz/xz.exe";
49            if (-e $xzwin) {
50                $XZ = $xzwin;
51            }
52            else {
53                # If the xz executable was not found fallback to trying to execute
54                # xz and follow the normal failure path if it isn't found.
55                $XZ = "xz";
56            }
57        }
58    }
59    else {
60        $XZ = "xz";
61    }
62}
63
64sub print_usage
65{
66    print "Usage: unwrap_full_update.pl [OPTIONS] ARCHIVE\n\n";
67    print "The contents of ARCHIVE will be unpacked into the current directory.\n\n";
68    print "Options:\n";
69    print "  -h show this help text\n";
70}
71
72my %opts;
73getopts("h", \%opts);
74
75if (defined($opts{'h'}) || scalar(@ARGV) != 1) {
76    print_usage();
77    exit 1;
78}
79
80$archive = $ARGV[0];
81@marentries = `"$MAR" -t "$archive"`;
82$? && die("Couldn't run \"$MAR\" -t");
83
84system($MAR, "-x", $archive) == 0 ||
85  die "Couldn't run $MAR -x";
86
87# Try to determine if the mar file contains bzip2 compressed files and if not
88# assume that the mar file contains lzma compressed files. The updatev3.manifest
89# file is checked since a valid mar file must have this file in the root path.
90open(my $testfilename, "updatev3.manifest") or die $!;
91binmode($testfilename);
92read($testfilename, my $bytes, 3);
93if ($bytes eq "BZh") {
94    $MAR_OLD_FORMAT = 1;
95} else {
96    undef $MAR_OLD_FORMAT;
97}
98close $testfilename;
99
100shift @marentries;
101
102foreach (@marentries) {
103    tr/\n\r//d;
104    my @splits = split(/\t/,$_);
105    my $file = $splits[2];
106
107    print "Decompressing: " . $file . "\n";
108    if ($MAR_OLD_FORMAT) {
109      system("mv", $file, "$file.bz2") == 0 ||
110        die "Couldn't mv \"$file\"";
111      system($BZIP2, "-d", "$file.bz2") == 0 ||
112        die "Couldn't decompress \"$file\"";
113    }
114    else {
115      system("mv", $file, "$file.xz") == 0 ||
116        die "Couldn't mv \"$file\"";
117      system($XZ, "-d", "$file.xz") == 0 ||
118        die "Couldn't decompress \"$file\"";
119    }
120}
121
122print "Finished\n";
123
124