###
###  Copyright 2000-2007 University of Illinois Board of Trustees
###  All rights reserved. 
###
###  PSGConf::Action::ChOwn - chown file action type for psgconf
###
###  Campus Information Technologies and Educational Services
###  University of Illinois at Urbana-Champaign
###


package PSGConf::Action::ChOwn;

use strict;

use File::stat;

use PSGConf::Action;

our @ISA = qw(PSGConf::Action);


###############################################################################
###  constructor
###############################################################################

sub new
{
	my ($class, %opts) = @_;
	my ($self) = \%opts;

	$self->{uid} = 0
		if (!exists($self->{uid}));

	return PSGConf::Action::new($class, %$self);
}


###############################################################################
###  check method
###############################################################################

sub check
{
	my ($self, $psgconf) = @_;

	return if ($self->{uid} == -1);

	$self->{stat} = stat($self->{name});

     ### check ownership
	$self->{changed} = 1
     	if (!defined $self->{stat} || $self->{stat}->uid != $self->{uid});

	return $self->{changed};
}


###############################################################################
###  diff method
###############################################################################

sub diff
{
	my ($self, $psgconf) = @_;

	return if ($self->{uid} == -1);

	$self->{stat} = stat($self->{name});

     if (!defined $self->{stat} || $self->{stat}->uid != $self->{uid})
	{
		print "###############################################################################\n";
		print "### DIFF FOR $self->{name}\n";
		print "###############################################################################\n";
		print '+ UID CHANGE: '
			. ((!defined $self->{stat})? -1: $self->{stat}->uid)
			. ' -> ' . $self->{uid} . "\n\n";
	}
}


###############################################################################
###  do() method
###############################################################################

sub do
{
	my ($self, $psgconf) = @_;

	return if ($self->{uid} == -1);

	$self->{stat} = stat($self->{name});

	### check ownership
     if (!defined $self->{stat} || $self->{stat}->uid != $self->{uid})
	{
		if (!chown($self->{uid}, -1, $self->{name}))
		{
			warn "\t!!! chown(\"$self->{name}\"): $!\n";
			return -1;
		}
	}

	return 1;
}

###############################################################################
###  documentation
###############################################################################

1;

__END__

=head1 NAME

PSGConf::Action::ChOwn - chown file action type for psgconf

=head1 SYNOPSIS

  use PSGConf::Action::ChOwn;

  $psgconf->register_actions(
		PSGConf::Action::ChOwn->new(
			'name'		=> '/path/to/file',
			'uid'		=> 'Owner of the file',
			...
		),
		...
	);

=head1 DESCRIPTION

The B<PSGConf::Action::ChOwn> module provides a B<PSGConf> action
class for changing a file's ownership.  The default uid is C<0> and
C<-1> means ignore who owns the file.

The B<PSGConf::Action::ChOwn> class is derived from the
B<PSGConf::Action> class, but it defines/overrides the
following methods:

=over 4

=item check()

If the file ownership is correct, returns 0.  Otherwise, returns 1.

=item diff()

Displays a message indicating that the file needs to be chown()ed.

=item do()

Actually calls chown().

=back

=head1 SEE ALSO

L<perl>

C<CE<lt>chown(1)E<gt>>

L<PSGConf>

L<PSGConf::Action>

=cut



syntax highlighted by Code2HTML, v. 0.9.1