• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

const/H29-Aug-2020-420288

dev-bin/H29-Aug-2020-2114

inc/H29-Aug-2020-203130

lib/File/H29-Aug-2020-834231

t/H29-Aug-2020-1,069844

xt/H29-Aug-2020-359267

CODE_OF_CONDUCT.mdH A D29-Aug-20203.2 KiB7656

CONTRIBUTING.mdH A D29-Aug-20203.6 KiB10570

ChangesH A D29-Aug-20209.1 KiB311189

INSTALL.mdH A D29-Aug-20201.6 KiB6738

LICENSEH A D29-Aug-202018.1 KiB380292

LibMagic.xsH A D29-Aug-202010.5 KiB390344

MANIFESTH A D29-Aug-20201.1 KiB6059

META.jsonH A D29-Aug-202036.9 KiB1,1011,099

META.ymlH A D29-Aug-202023.5 KiB813812

Makefile.PLH A D29-Aug-20203.5 KiB142115

README.mdH A D29-Aug-202012.7 KiB398249

azure-pipelines.ymlH A D29-Aug-2020664 3026

cpanfileH A D29-Aug-20201.9 KiB6458

dist.iniH A D29-Aug-20201.2 KiB4841

perlcriticrcH A D29-Aug-20201.8 KiB6847

perltidyrcH A D29-Aug-2020254 2120

ppport.hH A D29-Aug-2020504.5 KiB16,9545,051

tidyall.iniH A D29-Aug-2020681 3228

typemapH A D29-Aug-202028 32

README.md

1# NAME
2
3File::LibMagic - Determine MIME types of data or files using libmagic
4
5# VERSION
6
7version 1.23
8
9# SYNOPSIS
10
11    use File::LibMagic;
12
13    my $magic = File::LibMagic->new;
14
15    my $info = $magic->info_from_filename('path/to/file');
16    # Prints a description like "ASCII text"
17    print $info->{description};
18    # Prints a MIME type like "text/plain"
19    print $info->{mime_type};
20    # Prints a character encoding like "us-ascii"
21    print $info->{encoding};
22    # Prints a MIME type with encoding like "text/plain; charset=us-ascii"
23    print $info->{mime_with_encoding};
24
25    my $file_content = read_file('path/to/file');
26    $info = $magic->info_from_string($file_content);
27
28    open my $fh, '<', 'path/to/file' or die $!;
29    $info = $magic->info_from_handle($fh);
30
31# DESCRIPTION
32
33The `File::LibMagic` module is a simple perl interface to libmagic from the
34file package (version 4.x or 5.x). You will need both the library
35(`libmagic.so`) and the header file (`magic.h`) to build this Perl module.
36
37## Installing libmagic
38
39On Debian/Ubuntu run:
40
41    sudo apt-get install libmagic-dev
42
43on Red Hat run:
44
45    sudo yum install file-devel
46
47On Mac you can use homebrew (https://brew.sh/):
48
49    brew install libmagic
50
51## Specifying lib and/or include directories
52
53On some systems, you may need to pass additional lib and include directories
54to the Makefile.PL. You can do this with the \`--lib\` and \`--include\`
55parameters:
56
57    perl Makefile.PL --lib /usr/local/lib --include /usr/local/include
58
59You can pass these parameters multiple times to specify more than one
60location.
61
62# API
63
64This module provides an object-oriented API with the following methods:
65
66## File::LibMagic->new
67
68Creates a new File::LibMagic object.
69
70Using the object oriented interface only opens the magic database once, which
71is probably most efficient for repeated uses.
72
73Each `File::LibMagic` object loads the magic database independently of other
74`File::LibMagic` objects, so you may want to share a single object across
75many modules.
76
77This method takes the following named parameters:
78
79- `magic_file`
80
81    This should be a string or an arrayref containing one or more magic files.
82
83    If a file you provide doesn't exist the constructor will throw an exception,
84    but only with libmagic 4.17+.
85
86    If you don't set this parameter, the constructor will throw an exception if it
87    can't find any magic files at all.
88
89    Note that even if you're using a custom file, you probably _also_ want to use
90    the standard file (`/usr/share/misc/magic` on my system, yours may vary).
91
92- `follow_symlinks`
93
94    If this is true, then calls to `$magic->info_from_filename` will follow
95    symlinks to the real file.
96
97- `uncompress`
98
99    If this is true, then compressed files (such as gzip files) will be
100    uncompressed, and the various `info_from_*` methods will return info
101    about the uncompressed file.
102
103- Processing limits
104
105    Newer versions of the libmagic library have a number of limits order to
106    prevent malformed or malicious files from causing resource exhaustion or other
107    errors.
108
109    If your libmagic support it, you can set the following limits through
110    constructor parameters. If your version does not support setting these limits,
111    passing these options will cause the constructor to croak. In addition, the
112    specific limits were introduced over a number of libmagic releases, and your
113    version of libmagic may not support every parameter. Using a parameter that is
114    not supported by your libmagic will also cause the constructor to cloak.
115
116    - `max_indir`
117
118        This limits recursion for indirection when processing entries in the
119        magic file.
120
121    - `max_name`
122
123        This limits the maximum number of levels of name/use magic that will be
124        processed in the magic file.
125
126    - `max_elf_notes`
127
128        This limits the maximum number of ELF notes that will be processed when
129        determining a file's mime type.
130
131    - `max_elf_phnum`
132
133        This limits the maximum number of ELF program sections that will be processed
134        when determining a file's mime type.
135
136    - `max_elf_shnum`
137
138        This limits the maximum number of ELF sections that will be processed when
139        determining a file's mime type.
140
141    - `max_regex`
142
143        This limits the maximum size of regexes when processing entries in the magic
144        file.
145
146    - `max_bytes`
147
148        This limits the maximum number of bytes read from a file when determining a
149        file's mime type.
150
151    The values of these parameters should be integer limits.
152
153- `max_future_compat`
154
155    For compatibility with future additions to the libmagic processing limit
156    parameters, you can pass a `max_future_compat` parameter. This is a hash
157    reference where the keys are constant values (integers defined by libmagic,
158    not names) and the values are the limit you want to set.
159
160## $magic->info\_from\_filename('path/to/file')
161
162This method returns info about the given file. The return value is a hash
163reference with four keys:
164
165- `description`
166
167    A textual description of the file content like "ASCII C program text".
168
169- `mime_type`
170
171    The MIME type without a character encoding, like "text/x-c".
172
173- `encoding`
174
175    Just the character encoding, like "us-ascii".
176
177- `mime_with_encoding`
178
179    The MIME type with a character encoding, like "text/x-c;
180    charset=us-ascii". Note that if no encoding was found, this will be the same
181    as the `mime_type` key.
182
183## $magic->info\_from\_string($string)
184
185This method returns info about the contents of the given string. The string
186can be passed as a reference to save memory.
187
188The return value is the same as that of `$mime->info_from_filename`.
189
190## $magic->info\_from\_handle($fh)
191
192This method returns info about the contents read from the given filehandle. It
193will read data starting from the handle's current position, and leave the
194handle at that same position after reading.
195
196## File::LibMagic->max\_param\_constant
197
198This method returns the maximum value that can be passed as a processing limit
199parameter to the constructor. You can use this to determine if passing a
200particular value in the `max_future_compat` constructor parameter will work.
201
202This may include constant values that do not have corresponding `max_X`
203constructor keys if your version of libmagic is newer than the one used to
204build this distribution.
205
206Conversely, if your version is older than it's possible that not all of the
207defined keys will be supported.
208
209## File::LibMagic->limit\_key\_is\_supported($key)
210
211This method takes a processing limit key like `max_indir` or `max_name` and
212returns a boolean indicating whether the linked version of libmagic supports
213that processing limit.
214
215# DISCOURAGED APIS
216
217This module offers two different procedural APIs based on optional exports,
218the "easy" and "complete" interfaces. There is also an older OO API still
219available. All of these APIs are discouraged, but will not be removed in the
220near future, nor will using them cause any warnings.
221
222I strongly recommend you use the new OO API. It's simpler than the complete
223interface, more efficient than the easy interface, and more featureful than
224the old OO API.
225
226## The Old OO API
227
228This API uses the same constructor as the current API.
229
230- $magic->checktype\_contents($data)
231
232    Returns the MIME type of the data given as the first argument. The data can be
233    passed as a plain scalar or as a reference to a scalar.
234
235    This is the same value as would be returned by the `file` command with the
236    `-i` switch.
237
238- $magic->checktype\_filename($filename)
239
240    Returns the MIME type of the given file.
241
242    This is the same value as would be returned by the `file` command with the
243    `-i` switch.
244
245- $magic->describe\_contents($data)
246
247    Returns a description (as a string) of the data given as the first argument.
248    The data can be passed as a plain scalar or as a reference to a scalar.
249
250    This is the same value as would be returned by the `file` command with no
251    switches.
252
253- $magic->describe\_filename($filename)
254
255    Returns a description (as a string) of the given file.
256
257    This is the same value as would be returned by the `file` command with no
258    switches.
259
260## The "easy" interface
261
262This interface is exported by:
263
264    use File::LibMagic ':easy';
265
266This interface exports two subroutines:
267
268- MagicBuffer($data)
269
270    Returns the description of a chunk of data, just like the `describe_contents`
271    method.
272
273- MagicFile($filename)
274
275    Returns the description of a file, just like the `describe_filename` method.
276
277## The "complete" interface
278
279This interface is exported by:
280
281    use File::LibMagic ':complete';
282
283This interface exports several subroutines:
284
285- magic\_open($flags)
286
287    This subroutine opens creates a magic handle. See the libmagic man page for a
288    description of all the flags. These are exported by the `:complete` import.
289
290        my $handle = magic_open(MAGIC_MIME);
291
292- magic\_load($handle, $filename)
293
294    This subroutine actually loads the magic file. The `$filename` argument is
295    optional. There should be a sane default compiled into your `libmagic`
296    library.
297
298- magic\_buffer($handle, $data)
299
300    This returns information about a chunk of data as a string. What it returns
301    depends on the flags you passed to `magic_open`, a description, a MIME type,
302    etc.
303
304- magic\_file($handle, $filename)
305
306    This returns information about a file as a string. What it returns depends on
307    the flags you passed to `magic_open`, a description, a MIME type, etc.
308
309- magic\_close($handle)
310
311    Closes the magic handle.
312
313# EXCEPTIONS
314
315This module can throw an exception if your system runs out of memory when
316trying to call `magic_open` internally.
317
318# BUGS
319
320This module is totally dependent on the version of file on your system. It's
321possible that the tests will fail because of this. Please report these
322failures so I can make the tests smarter. Please make sure to report the
323version of file on your system as well!
324
325# DEPENDENCIES/PREREQUISITES
326
327This module requires file 4.x or file 5x and the associated libmagic library
328and headers (https://darwinsys.com/file/).
329
330# RELATED MODULES
331
332Andreas created File::LibMagic because he wanted to use libmagic (from
333file 4.x) [File::MMagic](https://metacpan.org/pod/File%3A%3AMMagic) only worked with file 3.x.
334
335[File::MimeInfo::Magic](https://metacpan.org/pod/File%3A%3AMimeInfo%3A%3AMagic) uses the magic file from freedesktop.org which is
336encoded in XML, and is thus not the fastest approach. See
337[https://mail.gnome.org/archives/nautilus-list/2003-December/msg00260.html](https://mail.gnome.org/archives/nautilus-list/2003-December/msg00260.html)
338for a discussion of this issue.
339
340[File::Type](https://metacpan.org/pod/File%3A%3AType) uses a relatively small magic file, which is directly hacked
341into the module code. It is quite fast but the database is quite small
342relative to the file package.
343
344# SUPPORT
345
346Please submit bugs to the CPAN RT system at
347https://rt.cpan.org/Public/Dist/Display.html?Name=File-LibMagic or via email at
348bug-file-libmagic@rt.cpan.org.
349
350Bugs may be submitted at [https://github.com/houseabsolute/File-LibMagic/issues](https://github.com/houseabsolute/File-LibMagic/issues).
351
352I am also usually active on IRC as 'autarch' on `irc://irc.perl.org`.
353
354# SOURCE
355
356The source code repository for File-LibMagic can be found at [https://github.com/houseabsolute/File-LibMagic](https://github.com/houseabsolute/File-LibMagic).
357
358# DONATIONS
359
360If you'd like to thank me for the work I've done on this module, please
361consider making a "donation" to me via PayPal. I spend a lot of free time
362creating free software, and would appreciate any support you'd care to offer.
363
364Please note that **I am not suggesting that you must do this** in order for me
365to continue working on this particular software. I will continue to do so,
366inasmuch as I have in the past, for as long as it interests me.
367
368Similarly, a donation made in this way will probably not make me work on this
369software much more, unless I get so many donations that I can consider working
370on free software full time (let's all have a chuckle at that together).
371
372To donate, log into PayPal and send money to autarch@urth.org, or use the
373button at [https://www.urth.org/fs-donation.html](https://www.urth.org/fs-donation.html).
374
375# AUTHORS
376
377- Andreas Fitzner
378- Michael Hendricks <michael@ndrix.org>
379- Dave Rolsky <autarch@urth.org>
380
381# CONTRIBUTORS
382
383- E. Choroba <choroba@matfyz.cz>
384- Mithun Ayachit <mayachit@amfam.com>
385- Olaf Alders <olaf@wundersolutions.com>
386- Paul Wise <pabs3@bonedaddy.net>
387- Tom Wyant <wyant@cpan.org>
388
389# COPYRIGHT AND LICENSE
390
391This software is copyright (c) 2020 by Andreas Fitzner, Michael Hendricks, Dave Rolsky, and Paul Wise.
392
393This is free software; you can redistribute it and/or modify it under
394the same terms as the Perl 5 programming language system itself.
395
396The full text of the license can be found in the
397`LICENSE` file included with this distribution.
398