File-Ownership-Unix

 view release on metacpan or  search on metacpan

lib/File/Ownership/Unix.pm  view on Meta::CPAN

package File::Ownership::Unix;

use warnings;
use strict;
use base 'Error::Helper';

=head1 NAME

File::Ownership::Unix - A object oriented system for working with file ownership under unix.

=head1 VERSION

Version 0.0.0

=cut

our $VERSION = '0.0.0';

=head1 SYNOPSIS

    use File::Ownership::Unix;
    
    my $foo=File::Ownership::Unix->new( 1001, 1001 );

    #gets the ownership info for a file
    $foo->setFromFile('/tmp/foo');

    #chowns a file using the current [GU]ID
    $foo->chown('/tmp/bar');

    #copies the ownership info from one file to another
    $foo->setFromFile('/tmp/foo');
    $foo->chown('/tmp/bar');

=head1 METHODS

=head2 new

This initiates the object.

There are two optional arguments taken. The first
one is the UID and the second is the GID.

Both default to zero.

    my $foo=File::Ownership::Unix->new;
    if( $foo->error ){
        warn('error:'.$foo->error.': '.$foo->errorString);
    }

=cut

sub new{
	my $uid=$_[1];
	my $gid=$_[2];

	my $self={
		perror=>undef,
		error=>undef,
		errorString=>'',
		uid=>0,
		gid=>0,
	};
	bless $self;

	if(defined($uid)){
		$self->{uid}=$uid;
	}
	if(defined($gid)){
		$self->{gid}=$gid;
	}

	if( $self->{gid} !~ /[0123456789]*/ ){
		$self->{perror}=1;
		$self->{error}=1;
		$self->{errorString}='"'.$self->{gid}.'" is not a valid value for GID';
		return $self;
	}
	
	if( $self->{uid} !~ /[0123456789]*/ ){
		$self->{perror}=1;
		$self->{error}=1;
		$self->{errorString}='"'.$self->{uid}.'" is not a valid value for the UID';
		return $self;
	}

	return $self;
}

=head2 chown

This chowns the specified file.

    $foo->chown('/tmp/foo');
    if( $foo->error ){
        warn('error:'.$foo->error.': '.$foo->errorString);
    }

=cut

sub chown{
    my $self=$_[0];
	my $file=$_[1];

	$self->errorblank;
	if($self->error){
		return undef;
	}

	if(!defined($file)){
		$self->{error}=2;
		$self->{errorString}='No file specified';
		return undef;
	}

	if (! -e $file){
		$self->{error}=3;
		$self->{errorString}='"'.$file.'" does not exist';
		return undef;
	}

	if(!chown( $self->{uid}, $self->{gid}, $file )){
		$self->{error}=4;
		$self->{errorString}='Failed to chown "'.$file.'" to "'.$self->{uid}.':'.$self->{gid}.'"';
		return undef;
	}

	return 1;
}

=head2 getGID

This returns the currently set GID.

    my $gid=$foo->getGID;

=cut

sub getGID{
	my $self=$_[0];

	$self->errorblank;
	if($self->error){
		return undef;
	}

	return $self->{gid};
}

=head2 getUID

This returns the currently set UID.

    my $gid=$foo->getGID;

=cut

sub getUID{
    my $self=$_[0];

    $self->errorblank;
    if($self->error){
        return undef;
    }

    return $self->{uid};
}

=head2 setGID

This sets the current GID.

    $foo->setGID('1001');
    if( $foo->error ){
        warn('error:'.$foo->error.': '.$foo->errorString);
    }

=cut

sub setGID{
    my $self=$_[0];
	my $gid=$_[1];

    $self->errorblank;



( run in 1.738 second using v1.01-cache-2.11-cpan-71847e10f99 )