DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (64af83fa5586)

VCS Links

Line Code
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
#!/usr/bin/perl -w
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

#
# This tool extracts a mar file, changes the compression of the files contained
# by the mar file either from bzip2 to lzma or lzma to bzip2, and then recreates
# the mar file. The script determines whether the files are compressed with
# bzip2 or lzma to determine what compression should be used. The permissions
# of the files will be retained as long as the script is run on a system such
# as Linux or Mac OS X but not on Windows. The mar file will be created with the
# same MAR channel and Product version information as the original mar file.
# If a mar signature is required the converted mar files will need to be
# re-signed afterwards.
# Author: Robert Strong
#

# -----------------------------------------------------------------------------
# By default just assume that these tools exist in our path

use Getopt::Std;
use Cwd 'abs_path';
use File::Basename;

$|++;

my ($MAR, $XZ, $BZIP2, $MAR_OLD_FORMAT, $FILES, $CHANNEL, $VERSION, $REPLACE, $archive, $tmparchive, @marentries, @marfiles);

if (defined($ENV{"MAR"})) {
    $MAR = $ENV{"MAR"};
}
else {
    $MAR = "mar";
}

if (defined($ENV{"BZIP2"})) {
    $BZIP2 = $ENV{"BZIP2"};
}
else {
    $BZIP2 = "bzip2";
}

if (defined($ENV{"XZ"})) {
    $XZ = $ENV{"XZ"};
}
else {
    $XZ = "xz";
}

sub print_usage
{
    print "Usage: change_mar_compression.pl [OPTIONS] ARCHIVE\n";
	print "\n";
    print "The ARCHIVE will be recreated using either bzip2 for mar file contents that\n";
	print "are compressed with lzma or lzma for mar file contents that are compressed\n";
	print "with bzip2. The new mar file that is created will not be signed but all other\n";
	print "attributes that should be retained will be retained.\n";
	print "\n";
    print "Options:\n";
    print "  -h show this help text\n";
    print "  -r replace the original mar file with the new mar file\n";
}

my %opts;
getopts("hr", \%opts);

if (defined($opts{'h'}) || scalar(@ARGV) != 1) {
    print_usage();
    exit 1;
}

if ($opts{'r'}) {
    $REPLACE = 1;
}

$archive = $ARGV[0];
@marentries = `"$MAR" -T "$archive"`;
$? && die("Couldn't run \"$MAR\" -t");

system("$MAR -x \"$archive\"") == 0 ||
  die "Couldn't run $MAR -x";

open(my $testfilename, "updatev3.manifest") or die $!;
binmode($testfilename);
read($testfilename, my $bytes, 3);
if ($bytes eq "BZh") {
    $MAR_OLD_TO_NEW = 1;
    print "Converting mar file from bzip2 to lzma compression\n";
} else {
    undef $MAR_OLD_TO_NEW;
    print "Converting mar file from lzma to bzip2 compression\n";
}
close $testfilename;

print "\n";

# The channel is the 4th line of the output
shift @marentries;
shift @marentries;
shift @marentries;
$CHANNEL = substr($marentries[0], 24, -1);
print "MAR channel name: " . $CHANNEL . "\n";

# The version is the 5th line of the output
shift @marentries;
$VERSION = substr($marentries[0], 23, -1);
print "Product version: " . $VERSION . "\n";

# The file entries start on the 8th line of the output
shift @marentries;
shift @marentries;
shift @marentries;

print "\n";
# Decompress the extracted files
foreach (@marentries) {
    tr/\n\r//d;
    my @splits = split(/\t/,$_);
    my $file = $splits[2];

    print "Decompressing: " . $file . "\n";
    if ($MAR_OLD_TO_NEW) {
        system("mv \"$file\" \"$file.bz2\"") == 0 ||
          print "\n" && die "Couldn't mv \"$file\"";
        system("\"$BZIP2\" -d \"$file.bz2\"") == 0 ||
          print "\n" && die "Couldn't decompress \"$file\"";
    }
    else {
        system("mv \"$file\" \"$file.xz\"") == 0 ||
          print "\n" && die "Couldn't mv \"$file\"";
        system("\"$XZ\" -d \"$file.xz\"") == 0 ||
          print "\n" && die "Couldn't decompress \"$file\"";
    }
}
print "All files decompressed\n";

print "\n";
# Compress the files in the requested format
$FILES = "";
foreach (@marentries) {
    tr/\n\r//d;
    my @splits = split(/\t/,$_);
    my $mod = $splits[1];
    my $file = $splits[2];

    print "Compressing: " . $file . "\n";
    if ($MAR_OLD_TO_NEW) {
        system("\"$XZ\" --compress --x86 --lzma2 --format=xz --check=crc64 --force --stdout \"$file\" > \"$file.xz\"") == 0 ||
          die "Couldn't compress \"$file\"";
        system("mv \"$file.xz\" \"$file\"") == 0 ||
          die "Couldn't mv \"$file.xz\"";
    }
    else {
        system("\"$BZIP2\" -z9 \"$file\"") == 0 ||
          die "Couldn't compress \"$file\"";
        system("mv \"$file.bz2\" \"$file\"") == 0 ||
          die "Couldn't mv \"$file.bz2\"";
    }
    $FILES = $FILES . "\"$file\" ";
    chmod oct($mod), $file;
}
print "All files compressed\n";

my $filesuffix = ".bz";
if ($MAR_OLD_TO_NEW) {
    $filesuffix = ".xz";
}
$tmparchive = $archive . $filesuffix;

system("$MAR -H $CHANNEL -V $VERSION -c \"$tmparchive\" $FILES") == 0 ||
  die "Couldn't run $MAR -c";

if ($REPLACE) {
    print "\n";
    print "Replacing mar file with the converted mar file\n";
    unlink $archive;
    system("mv \"$tmparchive\" \"$archive\"") == 0 ||
      die "Couldn't mv \"$tmparchive\"";
}

print "\n";
print "Removing extracted files\n";
foreach (@marentries) {
    tr/\n\r//d;
    my @splits = split(/\t/,$_);
    my $file = $splits[2];

    unlink $file;
    my $dirpath = $file;
    while (1) {
        if (index($dirpath, '/') < 0) {
            last;
        }
        $dirpath = substr($dirpath, 0, rindex($dirpath, '/'));
        rmdir($dirpath);
        if (-d $dirpath) {
            last;
        }
    }
}

print "\n";
if ($MAR_OLD_TO_NEW) {
    print "Finished converting mar file from bzip2 to lzma compression\n";
} else {
    print "Finished converting mar file from lzma to bzip2 compression\n";
}