| 1 | package File::MimeInfo; | ||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | 9 | ▶ | use Fcntl 'SEEK_SET'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use File::BaseDir qw/data_files/; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | 9 | require Exporter; | |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||
| 11 | 9 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||
| 12 | 9 | our @EXPORT = qw(mimetype); | |||||||||||||||||||||||||||||||||
| 13 | 9 | our @EXPORT_OK = qw(extensions describe globs inodetype mimetype_canon mimetype_isa); | |||||||||||||||||||||||||||||||||
| 14 | 9 | our $VERSION = '0.37'; | |||||||||||||||||||||||||||||||||
| 15 | 9 | our $DEBUG; | |||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||
| 17 | 9 | our ($_hashed, $_hashed_aliases, $_hashed_subclasses, $_has_mimeinfo_database); | |||||||||||||||||||||||||||||||||
| 18 | 9 | our (@globs, %literal, %extension, %mime2ext, %aliases, %subclasses); | |||||||||||||||||||||||||||||||||
| 19 | 9 | our ($LANG, @DIRS); | |||||||||||||||||||||||||||||||||
| 20 | # @globs = [ [ 'glob', qr//, $mime_string ], ... ] | ||||||||||||||||||||||||||||||||||
| 21 | # %literal contains literal matches | ||||||||||||||||||||||||||||||||||
| 22 | # %extension contains extensions (globs matching /^\*(\.\w)+$/ ) | ||||||||||||||||||||||||||||||||||
| 23 | # %mime2ext is used for looking up extension by mime type | ||||||||||||||||||||||||||||||||||
| 24 | # %aliases contains the aliases table | ||||||||||||||||||||||||||||||||||
| 25 | # %subclasses contains the subclasses table | ||||||||||||||||||||||||||||||||||
| 26 | # $LANG can be used to set a default language for the comments | ||||||||||||||||||||||||||||||||||
| 27 | # @DIRS can be used to specify custom database directories | ||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||
| 29 | 1 | ▶ | sub new { bless \$VERSION, shift } # what else is there to bless ? | ||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||
| 31 | 21 | ▶ | sub mimetype { | ||||||||||||||||||||||||||||||||
|
Subroutine
mimetype: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 32 | 21 | my $file = pop; | |||||||||||||||||||||||||||||||||
| 33 | 21 | ▶ | croak 'subroutine "mimetype" needs a filename as argument' unless defined $file; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 34 | return | ||||||||||||||||||||||||||||||||||
| 35 | 21 | ▶ | inodetype($file) || | ||||||||||||||||||||||||||||||||
|
Condition100%6/6
inodetype($file) || globs($file) || default($file)
inodetype($file) or globs($file)
MC/DC100%3/3
inodetype($file) || globs($file) || default($file)
inodetype($file)
globs($file)
default($file)
Truth table100%4/4
A or B || C
A= inodetype($file)B= globs($file)C= default($file)
| |||||||||||||||||||||||||||||||||||
| 36 | globs($file) || | ||||||||||||||||||||||||||||||||||
| 37 | default($file); | ||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||
| 40 | 29 | ▶ | sub inodetype { | ||||||||||||||||||||||||||||||||
|
Subroutine
inodetype: called CC=14 SCAR=29.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 41 | 29 | my $file = pop; | |||||||||||||||||||||||||||||||||
| 42 | 29 | ▶ | print STDERR "> Checking inode type\n" if $DEBUG; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 43 | 29 | ▶ | lstat $file or return undef; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 44 | 16 | ▶ | return undef if -f _; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 45 | 2 | ▶ | my $t = (-l $file) ? 'inode/symlink' : # Win32 does not like '_' here | ||||||||||||||||||||||||||||||||
|
Branch: 6 branches
| |||||||||||||||||||||||||||||||||||
| 46 | (-d _) ? 'inode/directory' : | ||||||||||||||||||||||||||||||||||
| 47 | (-p _) ? 'inode/fifo' : | ||||||||||||||||||||||||||||||||||
| 48 | (-c _) ? 'inode/chardevice' : | ||||||||||||||||||||||||||||||||||
| 49 | (-b _) ? 'inode/blockdevice' : | ||||||||||||||||||||||||||||||||||
| 50 | (-S _) ? 'inode/socket' : '' ; | ||||||||||||||||||||||||||||||||||
| 51 | 2 | ▶ | if ($t eq 'inode/directory') { # compare devices to detect mount-points | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 52 | 1 | my $dev = (stat _)[0]; # device of the node under investigation | |||||||||||||||||||||||||||||||||
| 53 | 1 | $file = File::Spec->rel2abs($file); # get full path | |||||||||||||||||||||||||||||||||
| 54 | 1 | my @dirs = File::Spec->splitdir($file); | |||||||||||||||||||||||||||||||||
| 55 | 1 | $file = File::Spec->catfile(@dirs); # removes trailing '/' or equivalent | |||||||||||||||||||||||||||||||||
| 56 | 1 | ▶ | return $t if -l $file; # parent can be on other dev for links | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 57 | 1 | pop @dirs; | |||||||||||||||||||||||||||||||||
| 58 | 1 | my $dir = File::Spec->catdir(@dirs); # parent dir | |||||||||||||||||||||||||||||||||
| 59 | 1 | ▶ | $t = 'inode/mount-point' unless (stat $dir)[0] == $dev; # compare devices | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 60 | 1 | return $t; | |||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||
| 62 | 1 | ▶ | else { return $t ? $t : undef } | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||
| 65 | 25 | ▶ | sub globs { | ||||||||||||||||||||||||||||||||
|
Subroutine
globs: called CC=15 SCAR=27.4
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 66 | 25 | my $file = pop; | |||||||||||||||||||||||||||||||||
| 67 | 25 | ▶ | croak 'subroutine "globs" needs a filename as argument' unless defined $file; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 68 | 25 | ▶ | rehash() unless $_hashed; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 69 | 25 | (undef, undef, $file) = File::Spec->splitpath($file); # remove path | |||||||||||||||||||||||||||||||||
| 70 | 25 | ▶ | print STDERR "> Checking globs for basename '$file'\n" if $DEBUG; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||
| 72 | 25 | ▶ | return $literal{$file} if exists $literal{$file}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||
| 74 | 24 | ▶ | if ($file =~ /\.(\w+(\.\w+)*)$/) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 75 | 11 | my @ext = split /\./, $1; | |||||||||||||||||||||||||||||||||
| 76 | 11 | ▶ | while (@ext) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 77 | 16 | my $ext = join('.', @ext); | |||||||||||||||||||||||||||||||||
| 78 | 16 | ▶ | print STDERR "> Checking for extension '.$ext'\n" if $DEBUG; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 79 | 16 | ▶ | carp "WARNING: wantarray behaviour of globs() will change in the future.\n" if wantarray; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 80 | return wantarray | ||||||||||||||||||||||||||||||||||
| 81 | ? ($extension{$ext}, $ext) | ||||||||||||||||||||||||||||||||||
| 82 | : $extension{$ext} | ||||||||||||||||||||||||||||||||||
| 83 | 16 | ▶ | if exists $extension{$ext}; | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 84 | 6 | shift @ext; | |||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||
| 88 | 14 | for (@globs) { | |||||||||||||||||||||||||||||||||
| 89 | 14 | ▶ | next unless $file =~ $_->[1]; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 90 | 2 | ▶ | print STDERR "> This file name matches \"$_->[0]\"\n" if $DEBUG; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 91 | 2 | return $_->[2]; | |||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||
| 94 | 12 | ▶ | return globs(lc $file) if $file =~ /[A-Z]/; # recurs | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 95 | 11 | return undef; | |||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||
| 98 | 8 | ▶ | sub default { | ||||||||||||||||||||||||||||||||
|
Subroutine
default: called CC=19 SCAR=38.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 99 | 8 | my $file = pop; | |||||||||||||||||||||||||||||||||
| 100 | 8 | ▶ | croak 'subroutine "default" needs a filename as argument' unless defined $file; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||
| 102 | 8 | my $line; | |||||||||||||||||||||||||||||||||
| 103 | 8 | ▶ | unless (ref $file) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 104 | 8 | ▶ | return undef unless -f $file; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 105 | 7 | ▶ | print STDERR "> File exists, trying default method\n" if $DEBUG; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 106 | 7 | ▶ | return 'text/plain' if -z $file; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||
| 108 | 6 | ▶ | open FILE, '<', $file or return undef; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 109 | 6 | ▶ | binmode FILE, ':utf8' unless $] < 5.008; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 110 | 6 | read FILE, $line, 32; | |||||||||||||||||||||||||||||||||
| 111 | 6 | close FILE; | |||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||
| 113 | elsif (ref $file eq 'Path::Tiny') { | ||||||||||||||||||||||||||||||||||
| 114 | 0 | ▶ | return undef unless $file->exists; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | print STDERR "> File is Path::Tiny object and exists, " | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 116 | . "trying default method\n" if $DEBUG; | ||||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | open my $fh, '<', $file or return undef; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | binmode $fh, ':utf8' unless $] < 5.008; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 119 | 0 | read $fh, $line, 32; | |||||||||||||||||||||||||||||||||
| 120 | 0 | close $fh; | |||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||
| 122 | else { | ||||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | print STDERR "> Trying default method on object\n" if $DEBUG; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||
| 125 | 0 | $file->seek(0, SEEK_SET); | |||||||||||||||||||||||||||||||||
| 126 | 0 | $file->read($line, 32); | |||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||
| 129 | { | ||||||||||||||||||||||||||||||||||
| 130 | 9 | ▶ | no warnings; # warnings can be thrown when input not ascii | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 131 | 6 | ▶ | if ($] < 5.008 or ! utf8::valid($line)) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$] < "5.008" or not utf8::valid($line)
MC/DC50%1/2
$] < "5.008" or not utf8::valid($line)
$] < "5.008"
not utf8::valid($line)
Truth table66%2/3
A or not(B)
A= $] < "5.008"B= not utf8::valid($line)
| |||||||||||||||||||||||||||||||||||
| 132 | 9 | ▶ | use bytes; # avoid invalid utf8 chars | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 133 | 2 | $line =~ s/\s//g; # \m, \n and \t are also control chars | |||||||||||||||||||||||||||||||||
| 134 | 2 | ▶ | return 'text/plain' unless $line =~ /[\x00-\x1F\x7F]/; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||||||||
| 136 | else { | ||||||||||||||||||||||||||||||||||
| 137 | # use perl to do something intelligent for ascii & utf8 | ||||||||||||||||||||||||||||||||||
| 138 | 4 | ▶ | return 'text/plain' unless $line =~ /[^[:print:]\s]/; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||||||||
| 141 | 3 | ▶ | print STDERR "> First 10 bytes of the file contain control chars\n" if $DEBUG; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 142 | 3 | return 'application/octet-stream'; | |||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||
| 145 | 5 | ▶ | sub rehash { | ||||||||||||||||||||||||||||||||
|
Subroutine
rehash: called CC=6 SCAR=18.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 146 | 5 | (@globs, %literal, %extension, %mime2ext) = (); # clear all data | |||||||||||||||||||||||||||||||||
| 147 | 5 | local $_; # limit scope of $_ ... :S | |||||||||||||||||||||||||||||||||
| 148 | my @globfiles = @DIRS | ||||||||||||||||||||||||||||||||||
| 149 | 5 | ▶ | ? ( grep {-e $_ && -r $_} map "$_/globs", @DIRS ) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
-e $_ && -r $_
MC/DC0%0/2
-e $_ && -r $_
-e $_
-r $_
Truth table33%1/3
A && B
A= -e $_B= -r $_
| |||||||||||||||||||||||||||||||||||
| 150 | : ( reverse data_files('mime/globs') ); | ||||||||||||||||||||||||||||||||||
| 151 | 5 | ▶ | if (@globfiles) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 152 | 3 | $_has_mimeinfo_database = 1; | |||||||||||||||||||||||||||||||||
| 153 | } else { | ||||||||||||||||||||||||||||||||||
| 154 | 2 | carp "WARNING: You don't seem to have a mime-info database. " . | |||||||||||||||||||||||||||||||||
| 155 | "The shared-mime-info package is available from http://freedesktop.org/"; | ||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||
| 157 | 5 | my @done; | |||||||||||||||||||||||||||||||||
| 158 | 5 | for my $file (@globfiles) { | |||||||||||||||||||||||||||||||||
| 159 | 5 | ▶ | next if grep {$file eq $_} @done; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 160 | 3 | _hash_globs($file); | |||||||||||||||||||||||||||||||||
| 161 | 3 | push @done, $file; | |||||||||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||||||||
| 163 | 5 | $_hashed = 1; | |||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||
| 166 | 3 | ▶ | sub _hash_globs { | ||||||||||||||||||||||||||||||||
|
Subroutine
_hash_globs: called CC=16 SCAR=32.2
| |||||||||||||||||||||||||||||||||||
| 167 | 3 | my $file = shift; | |||||||||||||||||||||||||||||||||
| 168 | 3 | ▶ | open GLOB, '<', $file or croak "Could not open file '$file' for reading" ; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 169 | 3 | ▶ | binmode GLOB, ':utf8' unless $] < 5.008; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 170 | 3 | my ($string, $glob); | |||||||||||||||||||||||||||||||||
| 171 | 3 | my %seen_extension; | |||||||||||||||||||||||||||||||||
| 172 | 3 | ▶ | while (<GLOB>) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 173 | 33 | ▶ | next if /^\s*#/ or ! /\S/; # skip comments and empty lines | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
/^\s*#/ or not /\S/
MC/DC50%1/2
/^\s*#/ or not /\S/
/^\s*#/
not /\S/
Truth table66%2/3
A or not(B)
A= /^\s*#/B= not /\S/
| |||||||||||||||||||||||||||||||||||
| 174 | 30 | chomp; | |||||||||||||||||||||||||||||||||
| 175 | 30 | ($string, $glob) = split /:/, $_, 2; | |||||||||||||||||||||||||||||||||
| 176 | 30 | ▶ | unless ($glob =~ /[\?\*\[]/) { $literal{$glob} = $string } | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 177 | elsif ($glob =~ /^\*\.(\w+(\.\w+)*)$/) { | ||||||||||||||||||||||||||||||||||
| 178 | 24 | ▶ | next if $seen_extension{$1}++; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 179 | 21 | ▶ | if (exists $extension{$1} && $extension{$1} ne $string) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $File::MimeInfo::extension{$1} and $File::MimeInfo::extension{$1} ne $string
MC/DC0%0/2
exists $File::MimeInfo::extension{$1} and $File::MimeInfo::extension{$1} ne $string
exists $File::MimeInfo::extension{$1}
$File::MimeInfo::extension{$1} ne $string
Truth table33%1/3
A and B
A= exists $File::MimeInfo::extension{$1}B= $File::MimeInfo::extension{$1} ne $string
| |||||||||||||||||||||||||||||||||||
| 180 | 0 | my $old = $extension{$1}; | |||||||||||||||||||||||||||||||||
| 181 | 0 | ▶ | if (defined $mime2ext{$old}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 182 | 0 | @{$mime2ext{$old}} = grep { $_ ne $1 } @{$mime2ext{$old}}; | |||||||||||||||||||||||||||||||||
| 183 | 0 | ▶ | delete $mime2ext{$old} unless @{$mime2ext{$old}}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||
| 186 | 21 | $extension{$1} = $string; | |||||||||||||||||||||||||||||||||
| 187 | 21 | ▶ | $mime2ext{$string} = [] if !defined($mime2ext{$string}); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 188 | 21 | push @{$mime2ext{$string}}, $1 | |||||||||||||||||||||||||||||||||
| 189 | 21 | ▶ | unless grep { $_ eq $1 } @{$mime2ext{$string}}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 190 | 3 | } else { unshift @globs, [$glob, _glob_to_regexp($glob), $string] } | |||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||
| 192 | 3 | ▶ | close GLOB or croak "Could not open file '$file' for reading" ; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||
| 195 | 7 | ▶ | sub _glob_to_regexp { | ||||||||||||||||||||||||||||||||
|
Subroutine
_glob_to_regexp: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 196 | 7 | my $glob = shift; | |||||||||||||||||||||||||||||||||
| 197 | 7 | $glob =~ s/\./\\./g; | |||||||||||||||||||||||||||||||||
| 198 | 7 | $glob =~ s/([?*])/.$1/g; | |||||||||||||||||||||||||||||||||
| 199 | 7 | $glob =~ s/([^\w\/\\\.\?\*\[\]])/\\$1/g; | |||||||||||||||||||||||||||||||||
| 200 | 7 | qr/^$glob$/; | |||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||
| 203 | 2 | ▶ | sub has_mimeinfo_database { | ||||||||||||||||||||||||||||||||
|
Subroutine
has_mimeinfo_database: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 204 | 2 | ▶ | rehash() if (!$_hashed); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 205 | 2 | return $_has_mimeinfo_database; | |||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||
| 208 | 2 | ▶ | sub extensions { | ||||||||||||||||||||||||||||||||
|
Subroutine
extensions: called CC=6 SCAR=18.2
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 209 | 2 | my $mimet = mimetype_canon(pop @_); | |||||||||||||||||||||||||||||||||
| 210 | 2 | ▶ | rehash() unless $_hashed; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 211 | 2 | ▶ | my $ref = $mime2ext{$mimet} if exists $mime2ext{$mimet}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 212 | 2 | ▶ | return $ref ? @{$ref} : undef if wantarray; | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 213 | 1 | ▶ | return $ref ? @{$ref}[0] : ''; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||
| 216 | 2 | ▶ | sub describe { | ||||||||||||||||||||||||||||||||
|
Subroutine
describe: called CC=14 SCAR=29.3
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 217 | 2 | ▶ | shift if ref $_[0]; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 218 | 2 | my ($mt, $lang) = @_; | |||||||||||||||||||||||||||||||||
| 219 | 2 | ▶ | croak 'subroutine "describe" needs a mimetype as argument' unless $mt; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 220 | 2 | $mt = mimetype_canon($mt); | |||||||||||||||||||||||||||||||||
| 221 | 2 | ▶ | $lang = $LANG unless defined $lang; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 222 | 2 | ▶ | my $att = $lang ? qq{xml:lang="$lang"} : ''; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 223 | 2 | my $desc; | |||||||||||||||||||||||||||||||||
| 224 | my @descfiles = @DIRS | ||||||||||||||||||||||||||||||||||
| 225 | 0 | ▶ | ? ( grep {-e $_ && -r $_} map "$_/$mt.xml", @DIRS ) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-e $_ && -r $_
MC/DC0%0/2
-e $_ && -r $_
-e $_
-r $_
Truth table0%0/3
A && B
A= -e $_B= -r $_
| |||||||||||||||||||||||||||||||||||
| 226 | : ( reverse data_files('mime', split '/', "$mt.xml") ) ; | ||||||||||||||||||||||||||||||||||
| 227 | 2 | for my $file (@descfiles) { | |||||||||||||||||||||||||||||||||
| 228 | 2 | $desc = ''; # if a file was found, return at least empty string | |||||||||||||||||||||||||||||||||
| 229 | 2 | ▶ | open XML, '<', $file or croak "Could not open file '$file' for reading"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 230 | 2 | ▶ | binmode XML, ':utf8' unless $] < 5.008; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 231 | 2 | ▶ | while (<XML>) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 232 | 20 | ▶ | next unless m!<comment\s*$att>(.*?)</comment>!; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 233 | 2 | $desc = $1; | |||||||||||||||||||||||||||||||||
| 234 | 2 | last; | |||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||
| 236 | 2 | ▶ | close XML or croak "Could not open file '$file' for reading"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 237 | 2 | ▶ | last if $desc; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||
| 239 | 2 | return $desc; | |||||||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||
| 242 | 16 | ▶ | sub mimetype_canon { | ||||||||||||||||||||||||||||||||
|
Subroutine
mimetype_canon: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 243 | 16 | my $mimet = pop; | |||||||||||||||||||||||||||||||||
| 244 | 16 | ▶ | croak 'mimetype_canon needs argument' unless defined $mimet; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 245 | 16 | ▶ | rehash_aliases() unless $_hashed_aliases; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 246 | 16 | ▶ | return exists($aliases{$mimet}) ? $aliases{$mimet} : $mimet; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||
| 249 | 2 | ▶ | sub rehash_aliases { | ||||||||||||||||||||||||||||||||
|
Subroutine
rehash_aliases: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 250 | 2 | %aliases = _read_map_files('aliases'); | |||||||||||||||||||||||||||||||||
| 251 | 2 | $_hashed_aliases++; | |||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||
| 254 | 3 | ▶ | sub _read_map_files { | ||||||||||||||||||||||||||||||||
|
Subroutine
_read_map_files: called CC=12 SCAR=25.9
| |||||||||||||||||||||||||||||||||||
| 255 | 3 | my ($name, $list) = @_; | |||||||||||||||||||||||||||||||||
| 256 | my @files = @DIRS | ||||||||||||||||||||||||||||||||||
| 257 | 0 | ▶ | ? ( grep {-e $_ && -r $_} map "$_/$name", @DIRS ) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
-e $_ && -r $_
MC/DC0%0/2
-e $_ && -r $_
-e $_
-r $_
Truth table0%0/3
A && B
A= -e $_B= -r $_
| |||||||||||||||||||||||||||||||||||
| 258 | : ( reverse data_files("mime/$name") ); | ||||||||||||||||||||||||||||||||||
| 259 | 3 | my (@done, %map); | |||||||||||||||||||||||||||||||||
| 260 | 3 | for my $file (@files) { | |||||||||||||||||||||||||||||||||
| 261 | 6 | ▶ | next if grep {$_ eq $file} @done; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 262 | 3 | ▶ | open MAP, '<', $file or croak "Could not open file '$file' for reading"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 263 | 3 | ▶ | binmode MAP, ':utf8' unless $] < 5.008; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 264 | 3 | ▶ | while (my $line = <MAP>) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 265 | 6 | ▶ | next unless $line =~ m/\S/; # skip empty lines | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 266 | 6 | ▶ | next if $line =~ m/^\s*#/; # skip comment lines | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 267 | 6 | chomp $line; | |||||||||||||||||||||||||||||||||
| 268 | 6 | my ($k, $v) = split m/\s+/, $line, 2; | |||||||||||||||||||||||||||||||||
| 269 | 6 | ▶ | if ($list) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 270 | 2 | ▶ | $map{$k} = [] unless $map{$k}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 271 | 2 | push @{$map{$k}}, $v; | |||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||
| 273 | 4 | else { $map{$k} = $v } | |||||||||||||||||||||||||||||||||
| 274 | } | ||||||||||||||||||||||||||||||||||
| 275 | 3 | close MAP; | |||||||||||||||||||||||||||||||||
| 276 | 3 | push @done, $file; | |||||||||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||||||||
| 278 | 3 | return %map; | |||||||||||||||||||||||||||||||||
| 279 | } | ||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||
| 281 | 5 | ▶ | sub mimetype_isa { | ||||||||||||||||||||||||||||||||
|
Subroutine
mimetype_isa: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 282 | 5 | ▶ | my $parent = pop || croak 'mimetype_isa needs argument'; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
pop() || croak('mimetype_isa needs argument')
MC/DC0%0/1
pop() || croak('mimetype_isa needs argument')
pop()
Truth table50%1/2
A
A= pop()
| |||||||||||||||||||||||||||||||||||
| 283 | 5 | my $mimet = pop; | |||||||||||||||||||||||||||||||||
| 284 | 5 | ▶ | if (ref $mimet or ! defined $mimet) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $mimet or not defined $mimet
MC/DC50%1/2
ref $mimet or not defined $mimet
ref $mimet
not defined $mimet
Truth table66%2/3
A or not(B)
A= ref $mimetB= not defined $mimet
| |||||||||||||||||||||||||||||||||||
| 285 | 2 | $mimet = mimetype_canon($parent); | |||||||||||||||||||||||||||||||||
| 286 | 2 | undef $parent; | |||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||||
| 288 | else { | ||||||||||||||||||||||||||||||||||
| 289 | 3 | $mimet = mimetype_canon($mimet); | |||||||||||||||||||||||||||||||||
| 290 | 3 | $parent = mimetype_canon($parent); | |||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||
| 292 | 5 | ▶ | rehash_subclasses() unless $_hashed_subclasses; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||
| 294 | 5 | my @subc; | |||||||||||||||||||||||||||||||||
| 295 | 5 | ▶ | push @subc, 'inode/directory' if $mimet eq 'inode/mount-point'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 296 | 5 | ▶ | push @subc, @{$subclasses{$mimet}} if exists $subclasses{$mimet}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 297 | 5 | ▶ | push @subc, 'text/plain' if $mimet =~ m#^text/#; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 298 | 5 | ▶ | push @subc, 'application/octet-stream' unless $mimet =~ m#^inode/#; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||
| 300 | 5 | ▶ | return $parent ? scalar(grep {$_ eq $parent} @subc) : @subc; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||
| 303 | 1 | ▶ | sub rehash_subclasses { | ||||||||||||||||||||||||||||||||
|
Subroutine
rehash_subclasses: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 304 | 1 | %subclasses = _read_map_files('subclasses', 'LIST'); | |||||||||||||||||||||||||||||||||
| 305 | 1 | $_hashed_subclasses++; | |||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||
| 308 | 9 | 1; | |||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||
| 310 | __END__ | ||||||||||||||||||||||||||||||||||