home *** CD-ROM | disk | FTP | other *** search
/ linuxmafia.com 2016 / linuxmafia.com.tar / linuxmafia.com / pub / linux / utilities-general / rpm2cpio < prev    next >
Text File  |  2005-02-10  |  3KB  |  88 lines

  1. #!/usr/bin/perl
  2.  
  3. # Why does the world need another rpm2cpio?  Because the existing one
  4. # won't build unless you have half a ton of things that aren't really
  5. # required for it, since it uses the same library used to extract RPMs.
  6. # In particular, it won't build on the HPsUX box I'm on.
  7.  
  8. #
  9. # Expanded quick-reference help by Rick Moen (not the original author 
  10. # of this script).
  11. #
  12.  
  13. # add a path if desired
  14. $gzip = "gzip";
  15.  
  16. sub printhelp {
  17.   print "\n";
  18.   print "rpm2cpio, perl version by orabidoo <odar\@pobox.com>\n";
  19.   print "\n";
  20.   print "use: rpm2cpio [file.rpm]\n";
  21.   print "dumps the contents to stdout as a GNU cpio archive\n";
  22.   print "\n";
  23.   print "In case it's non-obvious, you'll need to redirect output\n";
  24.   print "from this script, e.g., './rpm2cpio package.rpm > package.cpio'.\n";
  25.   print "Then, unpack in, say, /tmp with a cpio incantation like this one:\n";
  26.   print "'cpio -ivd < package.cpio'\n";
  27.   print "\n";
  28.   print "You can optionally combine both steps:\n";
  29.   print "'rpm2cpio package.rpm | cpio -iduv'\n";
  30.   print "\n";
  31.   print "In either event, you will also need the 'cpio' utility available\n";
  32.   print "on your system.  If you can't find it elsewhere, source code for\n";
  33.   print "GNU cpio is always available at ftp://ftp.gnu.org/gnu/cpio/.)\n";
  34.   print "You'll also, of course, need Perl, and will want this Perl script\n";
  35.   print "set as executable, i.e., by doing 'chmod 775 rpm2cpio'\n";
  36.   print "\n";
  37.   print "Be aware that this script works ONLY on version 3 or 4-format RPM\n";
  38.   print "archives.  You can check an archive's RPM-format version using\n";
  39.   print "the Unix 'file' utility.  Also, be aware that the 'cpio'\n";
  40.   print "incantations above will unpack files at the current directory\n";
  41.   print "level.\n";
  42.   print "\n";
  43.   exit 0;
  44. }
  45.  
  46. if ($#ARGV == -1) {
  47.   printhelp if -t STDIN;
  48.   $f = "STDIN";
  49. } elsif ($#ARGV == 0) {
  50.   open(F, "< $ARGV[0]") or die "Can't read file $ARGV[0]\n";
  51.   $f = 'F';
  52. } else {
  53.   printhelp;
  54. }
  55.  
  56. printhelp if -t STDOUT;
  57.  
  58. # gobble the file up
  59. undef $/;
  60. $|=1;
  61. $rpm = <$f>;
  62. close ($f);
  63.  
  64. ($magic, $major, $minor, $crap) = unpack("NCC C90", $rpm);
  65.  
  66. die "Not an RPM\n" if $magic != 0xedabeedb;
  67. die "Not a version 3 or 4 RPM\n" if $major != 3 and $major != 4;
  68.  
  69. $rpm = substr($rpm, 96);
  70.  
  71. while ($rpm ne '') {
  72.   $rpm =~ s/^\c@*//s;
  73.   ($magic, $crap, $sections, $bytes) = unpack("N4", $rpm);
  74.   $smagic = unpack("n", $rpm);
  75.   last if $smagic eq 0x1f8b;
  76.   die "Error: header not recognized\n" if $magic != 0x8eade801;
  77.   $rpm = substr($rpm, 16*(1+$sections) + $bytes);
  78. }
  79.  
  80. die "bogus RPM\n" if $rpm eq '';
  81.  
  82. open(ZCAT, "|gzip -cd") || die "can't pipe to gzip\n";
  83. print STDERR "CPIO archive found!\n";
  84.  
  85. print ZCAT $rpm;
  86. close ZCAT;
  87.  
  88.