FFI-C-Stat

 view release on metacpan or  search on metacpan

lib/FFI/C/Stat.pm  view on Meta::CPAN

package FFI::C::Stat;

use strict;
use warnings;
use 5.008004;
use Ref::Util qw( is_ref is_globref is_blessed_ref );
use Carp ();
use FFI::Platypus 1.00;

# ABSTRACT: Object-oriented FFI interface to native stat and lstat
our $VERSION = '0.03'; # VERSION


my $ffi = FFI::Platypus->new( api => 1 );
$ffi->bundle;      # for accessors / constructor / destructor

$ffi->type('object(FFI::C::Stat)', 'stat');
if($^O eq 'MSWin32')
{
  $ffi->type('uint' => $_) for qw( uid_t gid_t nlink_t blksize_t blkcnt_t );
}

$ffi->mangler(sub { "stat__$_[0]" });

$ffi->attach( '_stat'  => ['string'] => 'opaque' );
$ffi->attach( '_fstat' => ['int',  ] => 'opaque' );
$ffi->attach( '_lstat' => ['string'] => 'opaque' );
$ffi->attach( '_new'   => []         => 'opaque' );


sub new
{
  my($class, $file, %options) = @_;

  my $ptr;
  if(is_globref $file)
  { $ptr = _fstat(fileno($file)) }
  elsif(!is_ref($file) && defined $file)
  { $ptr = $options{symlink} ? _lstat($file) : _stat($file) }
  elsif(!defined $file)
  { $ptr = _new() }
  else
  { Carp::croak("Tried to stat something whch is neither a glob reference nor a plain string") }

  bless \$ptr, $class;
}


$ffi->attach( clone => ['opaque'] => 'opaque' => sub {
  my($xsub, $class, $other) = @_;

  my $ptr;
  if(is_blessed_ref $other)
  {
    if($other->isa('FFI::C::Stat'))
    {
      $ptr = $xsub->($$other);
    }
    else
    {
      Carp::croak("Not a FFI::C::Struct instance");
    }
  }
  elsif(!is_ref $other)
  {
    $ptr = $xsub->($other);



( run in 1.839 second using v1.01-cache-2.11-cpan-d8267643d1d )