NAME

RPM::Payload - simple in-memory access to RPM cpio archive

SYNOPSIS

    use RPM::Payload;
    my $cpio = RPM::Payload->new("rpm-3.0.4-0.48.i386.rpm");
    while (my $entry = $cpio->next) {
	print $entry->filename, "\n";
    }

DESCRIPTION

RPM::Payload provides in-memory access to RPM cpio archive. Cpio headers and file data can be read in a simple loop. RPM::Payload uses rpm2cpio program which comes with RPM.

EXAMPLE

Piece of Bourne shell code:

    rpmfile()
    {
	tmpdir=`mktemp -dt rpmfile.XXXXXXXX`
	rpm2cpio "$1" |(cd "$tmpdir"
	    cpio -idmu --quiet --no-absolute-filenames
	    chmod -Rf u+rwX .
	    find -type f -print0 |xargs -r0 file)
	rm -rf "$tmpdir"
    }

Sample output:

$ rpmfile rss2mail2-2.25-alt1.noarch.rpm 
./usr/share/man/man1/rss2mail2.1.gz: gzip compressed data, from Unix, max compression
./usr/bin/rss2mail2:                 perl script text executable
./etc/rss2mail2rc:                   ASCII text
$

Perl implementation:

    use RPM::Payload;
    use Fcntl qw(S_ISREG);
    use File::LibMagic qw(MagicBuffer);
    sub rpmfile {
	my $f = shift;
	my $cpio = RPM::Payload->new($f);
	while (my $entry = $cpio->next) {
	    next unless S_ISREG($entry->mode);
	    next unless $entry->size > 0;
	    $entry->read(my $buf, 8192) > 0 or die "read error";
	    print $entry->filename, "\t", MagicBuffer($buf), "\n";
	}
    }

CAVEATS

rpm2cpio program (which comes with RPM) must be installed.

It will die on error, so you may need an enclosing eval block. However, they say "when you must fail, fail noisily and as soon as possible".

Entries obtained with $cpio->next are coupled with current position in $cpio stream. Thus, $entry->read and $entry->readlink methods may only be invoked before the next $cpio->next call.

Hradlinks must be handled manually. Alternatively, you may want to skip entries with $entry->size == 0 altogether.

AUTHOR

Written by Alexey Tourbin <at@altlinux.org>.

COPYING

Copyright (c) 2006, 2009 Alexey Tourbin, ALT Linux Team.

This is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

SEE ALSO

rpm2cpio(8).

Edward C. Bailey. Maximum RPM. http://www.rpm.org/max-rpm/index.html (RPM File Format).

Eric S. Raymond. The Art of Unix Programming. http://www.faqs.org/docs/artu/index.html (Rule of Repair).