App-Options

 view release on metacpan or  search on metacpan

bin/prefixadmin  view on Meta::CPAN

{
    my (@op);
    if ($App::options{op}) {
        @op = split(/,/,$App::options{op});
    }
    elsif ($#ARGV > -1) {
        @op = @ARGV;
    }
    else {
        @op = ("fix");
    }

    if ($#op > -1) {
        my $admin = App::Options::PrefixAdmin->new();
        
        foreach my $op (@op) {
            if ($op eq "fix") {
                $admin->fix(\%App::options);
            }
            else {
                print "Unknown operation [$op]\n";
            }
        }
    }
    else {
        print "No operations specified\n";
    }
}

package App::Options::PrefixAdmin;

use File::Find;
use Date::Format;
use Fcntl ':mode';

sub new {
    my ($this) = @_;
    my $class = ref($this) || $this;
    my $self = {};
    bless $self, $class;
    return($self);
}

sub fix {
    my ($self, $options) = @_;
    my ($path, $file, $cwd);
    my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks);

    my $verbose = $options->{verbose} || 0;
    my $prefix  = $options->{prefix} || die "prefix not specified";
    die "$prefix is not a directory" if (! -d $prefix);
    chdir($prefix) || die "Could not change directory to $prefix";

    $path = ".";
    ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat($path);
    if ($verbose >= 2) {
        printf("%3d %8d %10s %2d %5d %5d %6d %15d [%17s] %s\n",
            $dev, $ino, $self->format_mode($mode), $nlink, $uid, $gid, $rdev, $size, time2str("%Y-%m-%d %H:%M:%S", $mtime), $path);
    }

    my ($u_name, $u_pass, $u_uid, $u_gid, $u_quota, $u_comment, $u_gcos, $u_dir, $u_shell, $u_expire) = getpwuid($uid);
    print "Uname: $u_name UID: $u_uid\n" if ($verbose >= 2);

    my ($grp_name, $grp_passwd, $grp_gid, $grp_members) = getgrgid($gid);
    print "Gname: $grp_name GID: $grp_gid Members: $grp_members\n" if ($verbose >= 2);

    my ($shgrp_name,$shgrp_passwd,$shgrp_gid,$shgrp_members);
    my $shared_group = $options->{group};
    if ($shared_group) {
        ($shgrp_name,$shgrp_passwd,$shgrp_gid,$shgrp_members) = getgrnam($shared_group);
        print "Shared Gname: $shgrp_name GID: $shgrp_gid Members: $shgrp_members\n" if ($verbose >= 2);
    }
    else {  # if --group is not given on the command line, use the GID of the top level directory
        ($shgrp_name,$shgrp_passwd,$shgrp_gid,$shgrp_members) = getgrgid($gid);
        print "Shared Gname: $shgrp_name GID: $shgrp_gid Members: $shgrp_members\n" if ($verbose >= 2);
    }

    #print STDERR "   searching $prefix\n" if ($verbose >= 2);
    find(
       sub {
           $file = $_;
           $path = $File::Find::name;
           $path =~ s!^\.\/!!;
           $cwd  = $File::Find::dir;
           $cwd  =~ s!^\.\/!!;
           my ($err_msg);

           ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks) = stat($file);

           if (!defined $mode) {
               print ">>> $file\n" if ($verbose);
           }
           else {
               printf("%3d %8d %07o:%10s %2d %5d %5d %6d %15d [%17s] %s\n",
                   $dev, $ino, $mode, $self->format_mode($mode), $nlink, $uid, $gid, $rdev, $size, time2str("%Y-%m-%d %H:%M:%S", $mtime), $path) if ($verbose);
               if ($shgrp_gid) {
                   $err_msg = $self->_share_file($file, $options, $shgrp_gid, $mode, $uid, $gid);
               }
           }
           return(0);
       },
       "."
    );
}

# S_IRWXU S_IRUSR S_IWUSR S_IXUSR
# S_IRWXG S_IRGRP S_IWGRP S_IXGRP
# S_IRWXO S_IROTH S_IWOTH S_IXOTH
#
# # Setuid/Setgid/Stickiness/SaveText.
# # Note that the exact meaning of these is system dependent.
#
# S_ISUID S_ISGID S_ISVTX S_ISTXT

sub format_mode {
    my ($self, $mode) = @_;
    my $fmt_mode = ($mode & S_IFREG) ? "-" : (($mode & S_IFDIR) ? "d" : (($mode & S_IFLNK) ? "l" : "?"));
    $fmt_mode   .= ($mode & S_IRUSR) ? "r" : "-";
    $fmt_mode   .= ($mode & S_IWUSR) ? "w" : "-";
    $fmt_mode   .= ($mode & S_IXUSR) ? (($mode & S_ISUID) ? "s" : "x") : (($mode & S_ISUID) ? "S" : "-");
    $fmt_mode   .= ($mode & S_IRGRP) ? "r" : "-";



( run in 1.724 second using v1.01-cache-2.11-cpan-39bf76dae61 )