#!/bin/perl # Author: Kevin P. Inscoe . # File: savehdr.pl # Date of creation: June 9, 2004. # Warranty: None expressed or implied. # License: The Open Software License. V1.1 http://www.opensource.org/licenses/osl.php # # OSI Certified Open Source Software. http://www.opensource.org/licenses/ # # Prerequisites: Perl 5.004 (minimum). # # Saves the file info for the file specified by the second parameter for # copying files to another server with the same login names but different uid and gid's. # # Use restorehdr.pl to restore from outputfile created by this script. # # Requirements require 5.004; use strict; if (@ARGV < 2) { die "Usage: savehdr.pl file outputfile\n Note: output file will be appended to if it exists.\n"; } my $file=$ARGV[0]; my $output=$ARGV[1]; my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat($file); #print "file=$file uid=$uid gid=$gid\n\n"; open(OUT,">>$output"); my ($user, $group); # Get the ascii owner first $user = &uid_to_name($uid); if ( $user eq "-1" ) { $user = $uid } # Now get the ASCII owner group $group = &gid_to_name($gid); if ( $group eq "-1" ) { $group = $gid } print OUT "$file:$user:$group:$uid:$gid\n"; close(OUT); exit; sub uid_to_name () { my ($uid) = @_; my $passwd = "/etc/passwd"; my ($puid, $puser, @prec); open(PW,"<$passwd"); while () { @prec = split(/:/, $_); $puser = $prec[0]; $puid = $prec[2]; if ( $puid eq $uid ) { # print "UID MATCH: $uid - $puser $puid\n"; return $puser; } } close(PW); return "-1"; } sub gid_to_name () { my ($gid) = @_; my $passwd = "/etc/group"; my ($pgid, $puser, @prec); open(PW,"<$passwd"); while () { @prec = split(/:/, $_); $puser = $prec[0]; $pgid = $prec[2]; if ( $pgid eq $gid ) { # print "GID MATCH: $gid - $puser $pgid\n"; return $puser; } } close(PW); return "-1"; }