components/rpm2cpio/src/rpm2cpio.pl
author Lukas Rovensky <Lukas.Rovensky@oracle.com>
Thu, 16 Jun 2011 02:27:24 -0700
changeset 307 c12c36cb5349
child 2020 b8615970ab39
permissions -rw-r--r--
7054536 move rpm2cpio to userland
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
307
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
     1
#!/usr/perl5/bin/perl
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
     2
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
     3
# Copyright (C) 1997,1998,1999, Roger Espel Llima
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
     4
# 
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
     5
# Permission is hereby granted, free of charge, to any person obtaining a copy
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
     6
# of this software and any associated documentation files (the "Software"), to 
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
     7
# deal in the Software without restriction, including without limitation the 
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
     8
# rights to use, copy, modify, merge, publish, distribute, sublicense, 
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
     9
# and/or sell copies of the Software, and to permit persons to whom the 
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    10
# Software is furnished to do so, subject to the following conditions:
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    11
# 
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    12
# The above copyright notice and this permission notice shall be included in
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    13
# all copies or substantial portions of the Software.
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    14
# 
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    15
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    16
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    17
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    18
# SOFTWARE'S COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    19
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    20
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    21
# THE SOFTWARE
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    22
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    23
# (whew, that's done!)
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    24
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    25
# why does the world need another rpm2cpio?  because the existing one
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    26
# won't build unless you have half a ton of things that aren't really
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    27
# required for it, since it uses the same library used to extract RPM's.
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    28
# in particular, it won't build on the HPsUX box i'm on.
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    29
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    30
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    31
# add a path if desired
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    32
$gzip = "gzip";
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    33
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    34
sub printhelp {
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    35
  print <<HERE;
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    36
rpm2cpio, perl version by orabidoo <odar\@pobox.com>
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    37
dumps the contents to stdout as a cpio archive
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    38
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    39
use: rpm2cpio [file.rpm] > file.cpio
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    40
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    41
Here's how to use cpio:
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    42
     list of contents:   cpio -t -i < /file/name
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    43
        extract files:   cpio -d -i < /file/name
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    44
HERE
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    45
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    46
  exit 0;
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    47
}
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    48
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    49
if ($#ARGV == -1) {
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    50
  printhelp if -t STDIN;
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    51
  $f = "STDIN";
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    52
} elsif ($#ARGV == 0) {
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    53
  open(F, "< $ARGV[0]") or die "Can't read file $ARGV[0]\n";
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    54
  $f = 'F';
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    55
} else {
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    56
  printhelp;
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    57
}
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    58
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    59
printhelp if -t STDOUT;
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    60
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    61
# gobble the file up
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    62
undef $/;
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    63
$|=1;
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    64
$rpm = <$f>;
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    65
close ($f);
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    66
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    67
($magic, $major, $minor, $crap) = unpack("NCC C90", $rpm);
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    68
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    69
die "Not an RPM\n" if $magic != 0xedabeedb;
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    70
die "Not a version 3 or 4 RPM\n" if $major != 3 && $major != 4;
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    71
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    72
$rpm = substr($rpm, 96);
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    73
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    74
while ($rpm ne '') {
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    75
  $rpm =~ s/^\[email protected]*//s;
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    76
  ($magic, $crap, $sections, $bytes) = unpack("N4", $rpm);
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    77
  $smagic = unpack("n", $rpm);
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    78
  last if $smagic eq 0x1f8b;
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    79
  die "Error: header not recognized\n" if $magic != 0x8eade801;
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    80
  $rpm = substr($rpm, 16*(1+$sections) + $bytes);
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    81
}
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    82
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    83
die "bogus RPM\n" if $rpm eq '';
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    84
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    85
open(ZCAT, "|gzip -cd") || die "can't pipe to gzip\n";
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    86
print STDERR "CPIO archive found!\n";
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    87
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    88
print ZCAT $rpm;
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    89
close ZCAT;
c12c36cb5349 7054536 move rpm2cpio to userland
Lukas Rovensky <Lukas.Rovensky@oracle.com>
parents:
diff changeset
    90