line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# $Id$ |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# file::type Brik |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
package Metabrik::File::Type; |
7
|
1
|
|
|
1
|
|
833
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
30
|
|
8
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
25
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
4
|
use base qw(Metabrik::System::Package); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1018
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
sub brik_properties { |
13
|
|
|
|
|
|
|
return { |
14
|
0
|
|
|
0
|
1
|
|
revision => '$Revision$', |
15
|
|
|
|
|
|
|
tags => [ qw(unstable) ], |
16
|
|
|
|
|
|
|
author => 'GomoR ', |
17
|
|
|
|
|
|
|
license => 'http://opensource.org/licenses/BSD-3-Clause', |
18
|
|
|
|
|
|
|
commands => { |
19
|
|
|
|
|
|
|
install => [ ], # Inherited |
20
|
|
|
|
|
|
|
get_mime_type => [ qw(file|file_list) ], |
21
|
|
|
|
|
|
|
get_magic_type => [ qw(file|file_list) ], |
22
|
|
|
|
|
|
|
is_mime_type => [ qw(file|file_list mime_type) ], |
23
|
|
|
|
|
|
|
is_magic_type => [ qw(file|file_list mime_type) ], |
24
|
|
|
|
|
|
|
get_types => [ qw(file|file_list) ], |
25
|
|
|
|
|
|
|
}, |
26
|
|
|
|
|
|
|
require_modules => { |
27
|
|
|
|
|
|
|
'File::LibMagic' => [ ], |
28
|
|
|
|
|
|
|
}, |
29
|
|
|
|
|
|
|
need_packages => { |
30
|
|
|
|
|
|
|
ubuntu => [ qw(libmagic-dev) ], |
31
|
|
|
|
|
|
|
debian => [ qw(libmagic-dev) ], |
32
|
|
|
|
|
|
|
kali => [ qw(libmagic-dev) ], |
33
|
|
|
|
|
|
|
}, |
34
|
|
|
|
|
|
|
}; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub get_mime_type { |
38
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
39
|
0
|
|
|
|
|
|
my ($files) = @_; |
40
|
|
|
|
|
|
|
|
41
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('get_mime_type', $files) or return; |
42
|
0
|
0
|
|
|
|
|
my $ref = $self->brik_help_run_invalid_arg('get_mime_type', $files, 'ARRAY', 'SCALAR') |
43
|
|
|
|
|
|
|
or return; |
44
|
|
|
|
|
|
|
|
45
|
0
|
|
|
|
|
|
my $magic = File::LibMagic->new; |
46
|
|
|
|
|
|
|
|
47
|
0
|
0
|
|
|
|
|
if ($ref eq 'ARRAY') { |
48
|
0
|
|
|
|
|
|
my $types = {}; |
49
|
0
|
|
|
|
|
|
for my $file (@$files) { |
50
|
0
|
0
|
|
|
|
|
my $type = $self->get_mime_type($file) or next; |
51
|
0
|
|
|
|
|
|
$types->{$file} = $type; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
|
|
|
|
return $types; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
else { |
57
|
0
|
0
|
|
|
|
|
$self->brik_help_run_file_not_found('get_mime_type', $files) or return; |
58
|
0
|
|
|
|
|
|
my $info = $magic->info_from_filename($files); |
59
|
|
|
|
|
|
|
|
60
|
0
|
|
|
|
|
|
return $info->{mime_type}; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# Error |
64
|
0
|
|
|
|
|
|
return; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub get_magic_type { |
68
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
69
|
0
|
|
|
|
|
|
my ($files) = @_; |
70
|
|
|
|
|
|
|
|
71
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('get_magic_type', $files) or return; |
72
|
0
|
0
|
|
|
|
|
my $ref = $self->brik_help_run_invalid_arg('get_magic_type', $files, 'ARRAY', 'SCALAR') |
73
|
|
|
|
|
|
|
or return; |
74
|
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
my $magic = File::LibMagic->new; |
76
|
|
|
|
|
|
|
|
77
|
0
|
0
|
|
|
|
|
if ($ref eq 'ARRAY') { |
78
|
0
|
|
|
|
|
|
my $types = {}; |
79
|
0
|
|
|
|
|
|
for my $file (@$files) { |
80
|
0
|
0
|
|
|
|
|
my $type = $self->get_magic_type($file) or next; |
81
|
0
|
|
|
|
|
|
$types->{$file} = $type; |
82
|
|
|
|
|
|
|
} |
83
|
0
|
|
|
|
|
|
return $types; |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
else { |
86
|
0
|
0
|
|
|
|
|
$self->brik_help_run_file_not_found('get_magic_type', $files) or return; |
87
|
0
|
|
|
|
|
|
my $info = $magic->info_from_filename($files); |
88
|
0
|
|
|
|
|
|
return $info->{description}; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
# Error |
92
|
0
|
|
|
|
|
|
return; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub is_mime_type { |
96
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
97
|
0
|
|
|
|
|
|
my ($files, $mime_type) = @_; |
98
|
|
|
|
|
|
|
|
99
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('is_mime_type', $files) or return; |
100
|
0
|
0
|
|
|
|
|
my $ref = $self->brik_help_run_invalid_arg('is_mime_type', $files, 'ARRAY', 'SCALAR') |
101
|
|
|
|
|
|
|
or return; |
102
|
|
|
|
|
|
|
|
103
|
0
|
|
|
|
|
|
my $types = {}; |
104
|
0
|
0
|
|
|
|
|
if ($ref eq 'ARRAY') { |
105
|
0
|
0
|
|
|
|
|
$self->brik_help_run_empty_array_arg('is_mime_type', $files) or return; |
106
|
0
|
|
|
|
|
|
for my $file (@$files) { |
107
|
0
|
0
|
|
|
|
|
my $res = $self->is_mime_type($file, $mime_type) or next; |
108
|
0
|
|
|
|
|
|
$types->{$files} = $res; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
else { |
112
|
0
|
0
|
|
|
|
|
my $type = $self->get_mime_type($files, $mime_type) or return; |
113
|
0
|
0
|
|
|
|
|
if ($type eq $mime_type) { |
114
|
0
|
|
|
|
|
|
$types->{$files} = 1; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
else { |
117
|
0
|
|
|
|
|
|
$types->{$files} = 0; |
118
|
|
|
|
|
|
|
} |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
|
121
|
0
|
0
|
|
|
|
|
return $ref eq 'ARRAY' ? $types : $types->{$files}; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub is_magic_type { |
125
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
126
|
0
|
|
|
|
|
|
my ($files, $magic_type) = @_; |
127
|
|
|
|
|
|
|
|
128
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('is_magic_type', $files) or return; |
129
|
0
|
0
|
|
|
|
|
my $ref = $self->brik_help_run_invalid_arg('is_magic_type', $files, 'ARRAY', 'SCALAR') |
130
|
|
|
|
|
|
|
or return; |
131
|
|
|
|
|
|
|
|
132
|
0
|
|
|
|
|
|
my $types = {}; |
133
|
0
|
0
|
|
|
|
|
if ($ref eq 'ARRAY') { |
134
|
0
|
0
|
|
|
|
|
$self->brik_help_run_empty_array_arg('is_magic_type', $files) or return; |
135
|
0
|
|
|
|
|
|
for my $file (@$files) { |
136
|
0
|
0
|
|
|
|
|
my $res = $self->is_magic_type($file, $magic_type) or next; |
137
|
0
|
|
|
|
|
|
$types->{$files} = $res; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
else { |
141
|
0
|
0
|
|
|
|
|
my $type = $self->get_magic_type($files, $magic_type) or return; |
142
|
0
|
0
|
|
|
|
|
if ($type eq $magic_type) { |
143
|
0
|
|
|
|
|
|
$types->{$files} = 1; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
else { |
146
|
0
|
|
|
|
|
|
$types->{$files} = 0; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
0
|
0
|
|
|
|
|
return $ref eq 'ARRAY' ? $types : $types->{$files}; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
sub get_types { |
154
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
155
|
0
|
|
|
|
|
|
my ($files) = @_; |
156
|
|
|
|
|
|
|
|
157
|
0
|
0
|
|
|
|
|
$self->brik_help_run_undef_arg('get_types', $files) or return; |
158
|
0
|
0
|
|
|
|
|
my $ref = $self->brik_help_run_invalid_arg('get_types', $files, 'ARRAY', 'SCALAR') |
159
|
|
|
|
|
|
|
or return; |
160
|
|
|
|
|
|
|
|
161
|
0
|
0
|
|
|
|
|
my $mime = $self->get_mime_type($files) or return; |
162
|
0
|
0
|
|
|
|
|
my $magic = $self->get_magic_type($files) or return; |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
return { |
165
|
0
|
|
|
|
|
|
mime => $mime, |
166
|
|
|
|
|
|
|
magic => $magic, |
167
|
|
|
|
|
|
|
}; |
168
|
|
|
|
|
|
|
} |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
1; |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
__END__ |