# # Copyright (c) 2014 NetApp, Inc., All Rights Reserved # Any use, modification, or distribution is prohibited # without prior written consent from NetApp, Inc. # ## @summary File ComponentState Module ## @author dl-nacl-dev@netapp.com ## @status shared ## @pod here =head1 NAME NACL::CS::Client::File =head1 DESCRIPTION This module does not represent the state of any element, but is an object representation of the output of a Client File. =head1 ATTRIBUTES The individual pieces of data that are part of the state of the File element are the attributes of the File ComponentState. =over =item C<< dev >> (Applicable for Linux and Windows) device number of filesystem. =item C<< ino >> (Applicable for Linux and Windows) inode number. =item C<< mode >> (Applicable for Linux and Windows) file mode (type and permissions) =item C<< nlink >> (Applicable for Linux and Windows) number of (hard) links to the file. =item C<< uid >> (Applicable for Linux and Windows) numeric user ID of file’s owner. =item C<< gid >> (Applicable for Linux and Windows) numeric group ID of file’s owner. =item C<< rdev >> (Applicable for Linux and Windows) the device identifier (special files only) =item C<< size >> (Applicable for Linux and Windows) total size of file, in bytes =item C<< atime >> (Applicable for Linux and Windows) Last access time in seconds since the epoch. =item C<< mtime >> (Applicable for Linux and Windows) Last modify time in seconds since the epoch. =item C<< ctime >> (Applicable for Linux and Windows) Inode change time in seconds since the epoch (*). =item C<< blksize >> (Applicable for Linux and Windows) preferred block size for file system I/O. =item C<< blocks >> (Applicable for Linux and Windows) actual number of blocks allocated. =back =cut package NACL::CS::Client::File; use strict; use warnings; use feature 'state'; use base qw(NACL::CS::ComponentState::Client); use Params::Validate qw(validate_with SCALAR GLOBREF ARRAYREF HASHREF UNDEF); use NATE::Log qw(log_global); my $Log = log_global(); my $may_enter = $Log->may_enter(); my $may_exit = $Log->may_exit(); use File::stat; use NATE::BaseException qw(:try); use NACL::C::Exceptions::Client::File::FileException; use NACL::GeneralUtils qw(_move_common_component_params_with_ci); use Class::MethodMaker [ scalar => 'dev', scalar => 'ino', scalar => 'mode', scalar => 'nlink', scalar => 'uid', scalar => 'gid', scalar => 'rdev', scalar => 'size', scalar => 'atime', scalar => 'mtime', scalar => 'ctime', scalar => 'blksize', scalar => 'blocks', scalar => 'file_name', scalar => 'file_handle', ]; sub fetch { $Log->enter() if $may_enter; my ($pkg, @args) = @_; state $spec = { 'method-timeout' => {type => SCALAR, 'optional' => 1,}, requested_fields => {type => ARRAYREF | UNDEF, default => []}, filter => {type => HASHREF | UNDEF, default => {}}, }; my %opts = validate_with( params => \@args, spec => $spec, allow_extra => 1 ); my $timeout = $opts{'method-timeout'} || 30; my @all_fields = qw(dev ino mode nlink uid gid rdev size atime mtime ctime blksize blocks); my @requested_fields; if (defined $opts{'requested_fields'}) { @requested_fields = @{delete $opts{'requested_fields'}}; } else { @requested_fields = @all_fields; } # Adding timeout loop. local $SIG{ALRM} = sub { NACL::Exceptions::Timeout->throw( "Timed out ($timeout sec) fetch() method\n"); }; my $filter = delete $opts{filter}; my $file = $filter->{'file_name'} || $opts{'file_name'}; my $file_handle = $filter->{'file_handle'} || $opts{'file_handle'}; my ($obj, $row); try { alarm $timeout; my $stat = stat($file); my %rows; foreach (@requested_fields) { $rows{$_} = $stat->$_(); } $row = { 'file_name' => $file, %rows, }; if ($file_handle) { $row->{'file_handle'} = $file_handle; } $obj = $pkg->new(); $obj->_set_fields(row => $row); } catch NACL::Exceptions::Timeout with { $_[0]->throw; } catch NATE::BaseException with { my $exception_obj = shift; my $new_exception_obj = NACL::C::Exceptions::Client::File::FileException->convert( exception => $exception_obj); $new_exception_obj->file_args(%opts); $new_exception_obj->throw(); } finally { alarm 0; }; $Log->exit() if $may_exit; return $obj; } ## end sub fetch 1;