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