File Coverage

blib/lib/File/LibMagic/FFI.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1             package File::LibMagic::FFI;
2              
3 3     3   23121 use strict;
  3         7  
  3         138  
4 3     3   15 use warnings;
  3         5  
  3         105  
5 3     3   64 use 5.008001;
  3         8  
  3         126  
6 3     3   1705 use FFI::Platypus;
  0            
  0            
7             use FFI::Platypus::Buffer ();
8             use FFI::CheckLib 0.06 ();
9             use constant {
10             _MAGIC_NONE => 0x000000,
11             _MAGIC_MIME => 0x000410, #MIME_TYPE | MIME_ENCODING,
12             };
13              
14             # ABSTRACT: Determine MIME types of data or files using libmagic
15             our $VERSION = '0.04'; # VERSION
16              
17              
18             our $ffi = FFI::Platypus->new;
19             $ffi->lib(FFI::CheckLib::find_lib_or_die( lib => "magic" ));
20             $ffi->type(opaque => 'magic_t');
21              
22             $ffi->attach( [magic_open => '_open'] => ['int'] => 'magic_t' );
23             $ffi->attach( [magic_load => '_load'] => ['magic_t','string'] => 'int' );
24             $ffi->attach( [magic_file => '_file'] => ['magic_t','string'] => 'string' );
25             $ffi->attach( [magic_buffer => '_buffer'] => ['magic_t','opaque','size_t'] => 'string' );
26             $ffi->attach( [magic_close => '_close'] => ['magic_t'] => 'void' );
27              
28              
29             sub new
30             {
31             my($class, $magic_file) = @_;
32             return bless { magic_file => $magic_file }, $class;
33             }
34              
35             sub _mime_handle
36             {
37             my($self) = @_;
38             return $self->{mime_handle} ||= do {
39             my $handle = _open(_MAGIC_MIME);
40             _load($handle, $self->{magic_file});
41             $handle;
42             };
43             }
44              
45             sub _describe_handle
46             {
47             my($self) = @_;
48             return $self->{describe_handle} ||= do {
49             my $handle = _open(_MAGIC_NONE);
50             _load($handle, $self->{magic_file});
51             $handle;
52             };
53             }
54              
55             sub DESTROY
56             {
57             my($self) = @_;
58             _close($self->{magic_handle}) if defined $self->{magic_handle};
59             _close($self->{mime_handle}) if defined $self->{mime_handle};
60             }
61              
62              
63             sub checktype_contents
64             {
65             _buffer($_[0]->_mime_handle, FFI::Platypus::Buffer::scalar_to_buffer(ref $_[1] ? ${$_[1]} : $_[1]));
66             }
67              
68              
69             sub checktype_filename
70             {
71             _file($_[0]->_mime_handle, $_[1]);
72             }
73              
74              
75             sub describe_contents
76             {
77             _buffer($_[0]->_describe_handle, FFI::Platypus::Buffer::scalar_to_buffer(ref $_[1] ? ${$_[1]} : $_[1]));
78             }
79              
80              
81             sub describe_filename
82             {
83             _file($_[0]->_describe_handle, $_[1]);
84             }
85              
86              
87             1;
88              
89             __END__