#!/usr/local/bin/perl -w ##################################################### # vit -- # vi a file in a tarball # Mike Schilli, 2004 (m@perlmeister.com) ##################################################### use strict; use Archive::Tar; use File::Temp qw(tempdir); use Pod::Usage; use File::Basename; use Sysadm::Install qw(:all); use Getopt::Std; use Fcntl ':mode'; our $CVSVERSION = '$Revision: 1.2 $'; our ($VERSION) = ($CVSVERSION =~ /(\d+\.\d+)/); getopts("i", \my %opts); my $tar = Archive::Tar->new; my($tarball, $path) = @ARGV; $tar->read($tarball, 1) or die "Cannot open $tarball ($!)"; if($opts{i}) { my @files = # Delete leading slashes map { s#^/##; $_ } # Build full path map { "$_->{prefix}/$_->{name}" } # Pick only plain files grep { S_ISREG($_->{mode}) } $tar->list_files(['name', 'mode', 'prefix']); $path = pick("Select file", [@files], 1); } else { pod2usage "argument error" unless defined $path; } my $tmpdir = tempdir("vitXXXX", DIR => "/tmp", CLEANUP => 1); $tar->extract_file($path, $tmpdir) or die "Cannot extract ($!)"; my $tmpfile = "$tmpdir/" . basename $tmpdir; my $editor = $ENV{EDITOR}; $editor = "vi" unless defined $editor; system("$editor $tmpfile"); open FILE, "<$tmpfile" or die "Cannot open $tmpfile"; my $data = join '', ; close FILE; $tar->replace_content($path, $data); my $compressed = $tarball =~ /gz$/ ? 1 : 0; $tar->write($tarball, $compressed) or die "Cannot write $tarball ($!)"; rmf $tmpdir; __END__ =pod =head1 NAME vit - Edit a file in a tarball =head1 DOWNLOAD _SRC_HERE_ =head1 SYNOPSIS vit tarball path/to/file vit -i tarball =head1 DESCRIPTION vit allows editing files within a tarball without actually unpacking it. The relative path of the file to be edited can be specified on the command line. Or, by giving the C<-i> option, the user gets to pick the file interactively: $ ./vit -i Module-Build-0.2607.tar.gz [1] Module-Build-0.2607/Build.PL [2] Module-Build-0.2607/Changes ... [63] Module-Build-0.2607/t/XSTest/test.pl Select file [1]> =head1 EXAMPLES # Edit perl's readme file $ vit stable.tar.gz perl-5.8.5/README # Pick from all the files contained in the module distro $ vit -i Module-Build-0.2607.tar.gz =head1 LEGALESE Copyright 2004 by Mike Schilli, all rights reserved. This program is free software, you can redistribute it and/or modify it under the same terms as Perl itself. =head1 AUTHOR 2004, Mike Schilli