#!/bin/perl # Author: Kevin P. Inscoe . # File: resthdr.pl # Date of creation: June 21, 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). # # Restore the file owner for the file specified by the second parameter for # by reading the generated output file from savehdr.pl. This allows ASCII user and # group names to be duplicated between unix hosts even if the numerical uids and gids # do not agree. This script is intended to be run from restfiles shell script. # # Note: Unlike savehdr.pl which only works on a single file at a time this script # will process all files found in the output file from savehdr.pl. (via find . -exec ) # It is imperative then that relative paths be fed to savehdr.pl and that before # invoking this script the current directory be at the base of the restored # directory tree relative to where savefiles and savehdr.pl was run from. # # http://kevininscoe.com/pub/scripts/savehdr/ # # Requirements require 5.004; use strict; if (@ARGV < 1) { die "Usage: resthdr.pl outputfile-from-savehdr.pl\n"; } my ($file, $user, $group, $uid, $gid, @orec, $dev, $ino, $mode, $nlink, $fuid, $fgid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks, $login, $pass, $puid, $pgid, $gname, $passwd, $ggid, $members); my $input=$ARGV[0]; open(IN,"<$input"); while () { @orec = split(/:/, $_); $file = $orec[0]; $user = $orec[1]; $group = $orec[2]; $uid = $orec[3]; $gid = $orec[4]; $puid = -1; $ggid = -1; ($dev,$ino,$mode,$nlink,$fuid,$fgid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat($file); # print "file: $file, current uid: $fuid, current gid: $fgid - setting owner as $user:group\n"; if ( $fuid eq "" ) { print "FILE NOT FOUND: $file\n" } else { # Convert ascii name to uid ($login,$pass,$puid,$pgid) = getpwnam($user) or die "$user not in passwd file\n"; ($gname, $passwd, $ggid, $members) = getgrnam($group) or die "$group not in group file\n"; if ( $puid == -1 ) { chown($fuid,$ggid,$file); } elsif ( $ggid == -1 ) { chown($fuid,$fgid,$file); } else { chown($puid,$ggid,$file); } } } close(IN);