Skip Menu |

This queue is for tickets about the File-LibMagic CPAN distribution.

Report information
The Basics
Id: 103157
Status: resolved
Priority: 0/
Queue: File-LibMagic

People
Owner: Nobody in particular
Requestors: hartzell [...] alerce.com
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: 1.12
Fixed in: 1.16



Subject: oo-api.t fails on OS X 10.9.5 w/ libmagic from homebrew; magic.mgc confusion.
On a Mac running OS X 10.9.5 with libmagic v5.22 installed via homebrew. oo-api.t fails the custom file part of the test, where it tries to load /usr/share/file/magic.mgc. The `$info` returned around line 46 of oo-api.t for that file reports that it is ``` DB<1> x $info 0 HASH(0x7fe03aed5780) 'description' => 'magic binary file for file(1) cmd (version 7) (little endian)' 'encoding' => 'binary' 'mime_type' => 'application/octet-stream' 'mime_with_encoding' => 'application/octet-stream; charset=binary' DB<2> n ``` but when it is used as the standard_file argument to File::LibMagic->new() it generates the following failures: ``` t/samples/magic, 2: Warning: using regular magic file `/usr/share/file/magic.mgc' /usr/share/file/magic.mgc, 1: Warning: offset `�' invalid /usr/share/file/magic.mgc, 1: Warning: type `�' invalid /usr/share/file/magic.mgc, 2: Warning: offset `Firmware v' invalid [...] ``` and then the tests fail. If I replace that magic file with `/usr/local//Cellar/libmagic/5.22/share/misc/magic.mgc` then `$info` is ``` DB<1> x $info 0 HASH(0x7fa75402c950) 'description' => 'magic binary file for file(1) cmd (version 12) (little endian)' 'encoding' => 'binary' 'mime_type' => 'application/octet-stream' 'mime_with_encoding' => 'application/octet-stream; charset=binary' DB<2> n ``` and the tests all pass. It seems that the homebrew installed library does not work with the system magic.mgc file. Given that the path to the magic.mgc file that work contains the releases version number, I'm not sure how to best proceed. My best idea is to work with the homebrew port and have it link its magic.mgc file somewhere underneath /usr/share then have t/oo-api.t use it a) in preference to the one in /usr/share, or b) if it's running on a mac or c) based on an environment variable or.... What can I do to help get this cleaned up? Thanks!
The underlying problem is not, as far as I can tell, related to homebrew. but rather that there appears to be some weirdness in the copy of magic.mgc distributed by Apple. Pull request #7 addresses this in the least-bad way I could come up with. As that request states, it involves scraping the output of 'file -v' to try to get the magic that File::LibMagic will actually be using. Normally the preferred implementation would be to get this information via the API, but the call that does this is undocumented, which makes me fear it is there simply for the convenience of the 'file' command, and subject to change without notice. A .patch file representing the code in the pull request is attached, for the benefit of the curious.
Subject: File-LibMagic-t-ooapi.patch
diff --git a/t/oo-api.t b/t/oo-api.t index 74c5d10..7eb2f71 100644 --- a/t/oo-api.t +++ b/t/oo-api.t @@ -8,6 +8,9 @@ use Test::More 0.96; use File::LibMagic; +local $ENV{MAGIC}; +delete $ENV{MAGIC}; + { my %standard = ( 'foo.foo' => [ @@ -16,7 +19,11 @@ use File::LibMagic; qr/us-ascii/, ], 'foo.c' => [ - [ 'ASCII C program text', 'C source, ASCII text' ], + [ + 'ASCII C program text', + 'C source, ASCII text', + 'c program, ASCII text', # Apple default magic + ], 'text/x-c', qr/us-ascii/, ], @@ -39,7 +46,10 @@ use File::LibMagic; SKIP: { - my $standard_file = '/usr/share/file/magic.mgc'; + my $standard_file = _scrape_file_version(); + + note "Testing with standard magic file '$standard_file'"; + skip "The standard magic file must exist at $standard_file", 1 unless -l $standard_file || -f _; @@ -54,7 +64,11 @@ SKIP: 'us-ascii', ], 'foo.c' => [ - [ 'ASCII C program text', 'C source, ASCII text' ], + [ + 'ASCII C program text', + 'C source, ASCII text', + 'c program, ASCII text', # Apple default magic + ], 'text/x-c', 'us-ascii', ], @@ -75,6 +89,20 @@ SKIP: ); } +sub _scrape_file_version { + foreach ( `file -v` ) { + chomp; + next + unless m/\AXmagic file from (.*)/; + return "$1.mgc" + if -f "$1.mgc"; + return "$1" + if -f $1; + last; + } + return '/usr/share/file/magic.mgc'; +} + sub _test_flm { my $flm = shift; my $tests = shift;