# ----------------------------------------
# From "Win32 Perl Programming: Administrators Handbook" by Dave Roth
# Published by Macmillan Technical Publishing.
# ISBN # 1-57870-215-1
use vars qw( $VERSION $PATH );
use Getopt::Long;
use Win32::Lanman;
$VERSION = 20040219;
%PATH = (
max => 45,
filler => ' ... ',
);
Configure( \%Config );
if( $Config{help} )
{
Syntax();
exit();
}
foreach my $FileID ( @{$Config{ids}} )
{
my( %File );
if( Win32::Lanman::NetFileGetInfo( $Config{machine}, $FileID, \%File ) )
{
undef %Data;
$Data{path} = $File{pathname};
$Data{user} = $File{username};
$Data{locks} = $File{num_locks};
$Data{id} = $File{id};
if( $PATH{max} < length( $Data{path} ) )
{
my $PathLength = int( ( $PATH{max} - length( $PATH{filler} ) ) / 2 );
my $FillerLength = length( $PATH{filler} );
$Data{path} =~ s/^(.{$PathLength})(.*?)(.{$PathLength})$/$1$PATH{filler}$3/;
}
if( ! Win32::Lanman::NetFileClose( $Config{server}, $FileID ) )
{
my( %Temp ) = %Data;
push( @Failed, \%Temp );
}
else
{
my( %Temp ) = %Data;
push( @Succeeded, \%Temp );
}
}
else
{
my %Temp = (
id => $FileID,
pathname => 'File ID is not in use on this server.',
);
push( @Failed, \%Temp );
}
}
if( scalar @Succeeded )
{
print "\nThe following files were forced closed:\n";
$~ = OUTPUT_HEADER;
write;
$~ = OUTPUT;
foreach my $File ( @Succeeded )
{
%Data = %$File;
write;
}
}
if( scalar @Failed )
{
print "\nThe following attempts to close files failed:\n";
$~ = OUTPUT_HEADER;
write;
$~ = OUTPUT;
foreach my $File ( @Failed )
{
%Data = %$File;
write;
}
}
sub Configure
{
my( $Config ) = @_;
my $Result;
my %ServerList;
Getopt::Long::Configure( "prefix_pattern=(-|\/)" );
$Result = GetOptions( $Config,
qw(
servers|s|m=s@
help|?
) );
push( @{$Config->{ids}}, @ARGV );
$Config->{help} = 1 if( !$Result || 0 == scalar @{$Config->{ids}} );
}
sub Syntax
{
my( $Script ) = ( $0 =~ /([^\\\/]*?)$/ );
my( $Line ) = "-" x length( $0 );
print <<EOT;
$0
$Line
Force an open file on a server to close.
Version $VERSION
Syntax:
perl $0 [-m Machine] FileID [ FileID2 [ ... ] ]
-m..........Close files on the specified machine.
FileID......The file ID of the open file.
EOT
}
format OUTPUT_HEADER=
@<<<<<< @<<<<< @<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
'ID', 'Locks', 'User', 'Path'
------- ------ ------------------ ---------------------------------------------
.
format OUTPUT=
@>>>>>> @>>>>> @<<<<<<<<<<<<<<<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$Data{id}, $Data{locks}, $Data{user}, $Data{path}
.
__END__
History:
20000410 roth
-Created.
20040219 roth
-Updated the Configure() subroutine.