| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
use Archive::Cpio; |
| 4 |
use Getopt::Long; |
| 5 |
|
| 6 |
=head1 NAME |
| 7 |
|
| 8 |
cpio-filter - transform a cpio archive |
| 9 |
|
| 10 |
=head1 SYNOPSIS |
| 11 |
|
| 12 |
cpio-filter [--exclude <PATTERN>] [--in-place] [<cpio file>] |
| 13 |
|
| 14 |
=head1 DESCRIPTION |
| 15 |
|
| 16 |
Transform a cpio archive on the fly. Reads on stdin and output on stderr |
| 17 |
|
| 18 |
=head1 AUTHOR |
| 19 |
|
| 20 |
Pascal Rigaux <[email protected]> |
| 21 |
|
| 22 |
=cut |
| 23 |
|
| 24 |
my %options = ( |
| 25 |
'exclude=s' => \ (my $exclude), |
| 26 |
'in-place|i' => \ (my $in_place), |
| 27 |
); |
| 28 |
|
| 29 |
sub usage { |
| 30 |
die "usage: cpio-filter [--exclude <PATTERN>] [--in-place] [<cpio file>]\n"; |
| 31 |
} |
| 32 |
|
| 33 |
GetOptions(%options) or usage(); |
| 34 |
@ARGV <= 1 or usage(); |
| 35 |
|
| 36 |
if ($in_place) { |
| 37 |
@ARGV or die "you can't use --in-place without giving a cpio file\n"; |
| 38 |
} |
| 39 |
|
| 40 |
my ($file) = @ARGV; |
| 41 |
|
| 42 |
sub exclude { |
| 43 |
my ($e) = @_; |
| 44 |
$exclude && $e->name =~ m!(^|/)${exclude}($|/)!; |
| 45 |
} |
| 46 |
|
| 47 |
my $cpio = Archive::Cpio->new; |
| 48 |
if ($in_place) { |
| 49 |
$cpio->read($file); |
| 50 |
$cpio->remove(map { exclude($_) ? $_->name : () } $cpio->get_files); |
| 51 |
$cpio->write($file); |
| 52 |
} else { |
| 53 |
my $IN; |
| 54 |
if ($file) { |
| 55 |
open($IN, '<', $file) or die "can't open $file: $!\n"; |
| 56 |
} else { |
| 57 |
$IN = \*STDIN; |
| 58 |
} |
| 59 |
|
| 60 |
$cpio->read_with_handler($IN, sub { |
| 61 |
my ($e) = @_; |
| 62 |
$cpio->write_one(\*STDOUT, $e) if !exclude($e); |
| 63 |
}); |
| 64 |
$cpio->write_trailer(\*STDOUT); |
| 65 |
} |