tm : Source for modifying tar file names 
Author Message
 tm : Source for modifying tar file names

Here's a small perl script to modify filenames inside tar files.
"tm" (tar modify) expects a substitution pattern like sed or vi, and applies
it to filenames inside a tar archive. It then recalculates the checksum for each
file header and dumps the modified tar file to STDOUT.

Example :
    tm -s#/usr/home/sriram/#/sriram/# < in.tar > out.tar

    tm -s#^/## < in.tar > out.tar - makes hardcoded pathnames relative by kicking out
                                    the leading slash.

For those who don't follow Perl, the two options you can use are /i (ignore case)
and /g (apply globally) ...
    tm -s#Log#log#i  < in.tar > out.tar

Perl folks may note that substitution pattern is evaluated as a perl
substitution command; it can thus evaluate expressions with the /e option.

-- Sriram

---------------------- Cut ------------------------- Cut --------------------

#!/usr/local/bin/perl

###########################################################################
# tm - A script to modify names inside tar files.                         #
# Usage:                                                                  #
#      tm -s<Perl substiution command>   < input.tar > output.tar         #
# Examples :                                                              #
#      tm -s/.c$/.c.bak/ < x.tar > y.tar (Renames all .c files to .c.bak  #
#      tm -s#^/## (Makes all absolute path names relative, by kicking out #
#                  the leading "/".                                       #
# Note:                                                                   #
#      The first character after -s is used as the delimiter.             #
#      Anything that can be done in a perl substitution expression can be #
#      done (such as ignoring case) etc.                                  #
###########################################################################
#                                                             - S.Sriram  #
###########################################################################

$BLOCKSIZE = 512;


    &Usage;

Quote:
}


    if (/^-h/) {
        &Usage;
    } elsif (/^-s(.*)/) {
        $cmd = $1;
    } else {
        &Usage;
    }

Quote:
}

&Usage if ($cmd eq "");

while (($n = &ProcessFileHeader) != -1){
    for ($i = 0; $i < $n; $i++) {
        &ReadBlock;
        &WriteBlock;
    }

Quote:
}

sub ProcessFileHeader {
    local ($n);
    if (!(&ReadBlock)) {
        return -1;
    }
    ($name, $mode, $uid, $gid, $size, $mtime, $chksum, $linkflag,
     $linkname) =
       unpack ('A100 a8 a8 a8 a12 a12 a8 a1 a100', $data);

    if ($name eq "") {
        &WriteBlock;
        return 1; #One more null block to follow.
    }

    &PrintHeader if $debugging;

    if (eval("\$name =~ s$cmd")) {
        # Name matches substitution pattern
        substr ($data, 0, 100) = pack ("a100", $name); # Replace name
        #recalculate Checksum;
        $s = sprintf ("%lo", &CheckSum);
        substr ($data, 148, 8) = pack ('a7 x', $s);
    }
    $n = int (oct($size) / $BLOCKSIZE);
    (++$n) if (oct($size) % $BLOCKSIZE);
    &WriteBlock;
    return $n;   # The number of data blocks to follow.

Quote:
}

sub ReadBlock {
    return sysread (STDIN, $data, $BLOCKSIZE);

Quote:
}

sub CheckSum {

    substr ($data, 148, 8) = " " x 8; #Blank out checksum

    $sum = 0;

        $sum += $_;
    }
    return $sum;

Quote:
}

sub WriteBlock {
    local ($l);
    $l = length ($data);
    if ($l) {
        die "LENGTH OF BLOCK ($l) != $BLOCKSIZE" if ($l != $BLOCKSIZE) ;
        return syswrite(STDOUT, $data, $BLOCKSIZE);
    }
    return 0;

Quote:
}

sub Usage {
    print STDERR "\n\n Usage:\n\t$0 -s/from pattern/to pattern/ < in.tar > out.tar\n";
    print STDERR "\t(Any character can be used in place of '/')\n\n";
    exit (1);

Quote:
}

sub PrintHeader {
    print STDERR "name     = $name \n";
    print STDERR "mode     = $mode \n";
    print STDERR "uid      = $uid \n";
    print STDERR "gid      = $gid \n";
    print STDERR "size     = $size \n";
    print STDERR "mtime    = $mtime \n";
    print STDERR "chksum   = $chksum \n";
    print STDERR "linkflag = $linkflag \n";
    print STDERR "linkname = $linkname \n";
Quote:
}



Mon, 17 Jun 1996 01:21:24 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. Tar'ing files and changing directory in the tar file using Archive::Tar

2. modify source file

3. How do I modify file name with date?

4. Using Perl to manipulate UNIX(tm) quota files

5. TM:rename a file but keep extension?

6. TM:copying files?

7. TM:Update a Password file

8. TM: Adding to a password file?

9. want better way to extract file name from full path file name

10. Q: Can't connect to SQL data source name via Win32::ODBC API

11. Extracting C function names from C source

12. Data Source Name

 

 
Powered by phpBB® Forum Software