class Magic::TypeChecker
- Magic::TypeChecker
- Reference
- Object
Overview
A TypeChecker checks the Magic database on the system for a given file,
filepath, file descriptor, or byte stream (IO) on its .of
instance method.
It contains methods for changing how its output is given. While the primary
use cases for this module can be performed with the module-level functions,
instances of TypeChecker provide the full flexibility of libmagic in a
syntax that still makes sense in crystal.
Examples:
# get all matching mime types and the encoding from a series of files
mime_types = TypeChecker.new.all_types.get_mime_type.get_mime_encoding
def type_of_each(files : Indexable(File) | Set(File))
files.each do |file|
# and yield the list of mimes to a block
yield mime_types.of(file).split('\n')
end
end
# alternately, return the list of mimes.
def types_of_each(files)
files.each do |file|
mime_types.of(file).split("\n")
end
end
# do the same thing but now follow symbolic links
def types_of_each(files)
mime_types.follow_symlinks
files.each do |file|
yield mime_types.of(file).split("\n")
end
mime_types.follow_symlinks = false
end
Defined in:
type_checker.crConstant Summary
-
DEFAULT_OPTIONS =
LibMagic::RAW | LibMagic::ERROR
-
Integer =
((((((Int8 | Int16) | Int32) | Int64) | UInt8) | UInt16) | UInt32) | UInt64
Constructors
Instance Method Summary
-
#all_types
Return all matches, not just the first.
- #all_types=(setting : Bool)
- #all_types?
-
#apple
Return the Apple creator and type.
- #apple=(setting : Bool)
- #apple?
-
#check_db
Check the magic database for consistency and printwarnings to stderr, while checking a file.
- #check_db=(setting : Bool)
- #check_db?
-
#check_device
If the file is a block or character special device, thenopen the device and try to look in its contents.
- #check_device=(setting : Bool)
- #check_device?
-
#checker=(chkr : LibMagic::MagicT)
Manually set the checker to a given LibMagic::MagicT (libmagic's magic_t).
-
#db_files : Set(String)?
database files to read from
-
#db_files=(files : IterableOfStrings)
Load a new set of database files into the magic database.
-
#debug_output
Have
libmagic(2)
print debugging messages to stderr. - #debug_output=(setting : Bool)
- #debug_output?
- #default_options?
- #error(msg)
-
#escape_unprintable
escapes non-printable bytes as their
0oOOO
octal numeric forms.By default crystal handles this in cases where it's important (inputs
for example), so by default strings contain the raw values forunprintable characters. - #escape_unprintable=(setting : Bool)
- #escape_unprintable?
- #extensions(this)
-
#follow_symlinks
If the file queried is a symlink, follow it.
- #follow_symlinks=(setting : Bool)
- #follow_symlinks?
-
#for(this)
same as
#of()
-
#get_extensions
Makes #of() return a slash-separated list of extensions for this file type.
- #get_extensions=(setting : Bool)
- #get_extensions?
-
#get_mime
sets both
#get_mime_type
and#get_mime_encoding
- #get_mime=(setting : Bool)
- #get_mime?
-
#get_mime_encoding
Return a MIME encoding, instead of a textual description.
- #get_mime_encoding=(setting : Bool)
- #get_mime_encoding?
-
#get_mime_type
Return a MIME type string, instead of a textual description.
- #get_mime_type=(setting : Bool)
- #get_mime_type?
-
#libmagic_defaults
use the MAGIC_NONE option (the default for libmagic) instead of the default for magic.cr
- #libmagic_defaults?
-
#look_into_compressed_files
If the file is compressed, unpack it and look at the contents.
- #look_into_compressed_files=(setting : Bool)
- #look_into_compressed_files?
-
#max_bytes(&block)
Yields the current value of the max_bytes to the block, then sets the value to the result of the block, unless the block returns nil, of course.
-
#max_bytes
Get the current limit of the LibMagic::PARAM_BYTES_MAX libmagic param.
-
#max_bytes=(value : Int32)
Limit LibMagic::PARAM_BYTES_MAX to the given value.
-
#max_elf_notes
Get the current limit of the LibMagic::PARAM_ELF_NOTES_MAX libmagic param.
-
#max_elf_notes(&block)
Yields the current value of the max_elf_notes to the block, then sets the value to the result of the block, unless the block returns nil, of course.
-
#max_elf_notes=(value : Int32)
Limit LibMagic::PARAM_ELF_NOTES_MAX to the given value.
-
#max_elf_phnum
Get the current limit of the LibMagic::PARAM_ELF_PHNUM_MAX libmagic param.
-
#max_elf_phnum(&block)
Yields the current value of the max_elf_phnum to the block, then sets the value to the result of the block, unless the block returns nil, of course.
-
#max_elf_phnum=(value : Int32)
Limit LibMagic::PARAM_ELF_PHNUM_MAX to the given value.
-
#max_elf_shnum
Get the current limit of the LibMagic::PARAM_ELF_SHNUM_MAX libmagic param.
-
#max_elf_shnum(&block)
Yields the current value of the max_elf_shnum to the block, then sets the value to the result of the block, unless the block returns nil, of course.
-
#max_elf_shnum=(value : Int32)
Limit LibMagic::PARAM_ELF_SHNUM_MAX to the given value.
-
#max_indir(&block)
Yields the current value of the max_indir to the block, then sets the value to the result of the block, unless the block returns nil, of course.
-
#max_indir
Get the current limit of the LibMagic::PARAM_INDIR_MAX libmagic param.
-
#max_indir=(value : Int32)
Limit LibMagic::PARAM_INDIR_MAX to the given value.
-
#max_name
Get the current limit of the LibMagic::PARAM_NAME_MAX libmagic param.
-
#max_name(&block)
Yields the current value of the max_name to the block, then sets the value to the result of the block, unless the block returns nil, of course.
-
#max_name=(value : Int32)
Limit LibMagic::PARAM_NAME_MAX to the given value.
-
#no_compression_info
Don't report on compression, only report about the uncompressed data.
- #no_compression_info=(setting : Bool)
- #no_compression_info?
-
#of(file : File)
Get the filetype "of" the given file.
-
#of(this : Int32)
get the filetype "of" the open file at the given file descriptor integer.
-
#of(this : IO)
Get the filetype "of" the given bytes.
-
#of(filepath)
Get the filetype "of" the given file.
-
#of?(file_descriptor : Int32)
Get the filetype "of" the given file.
-
#of?(filepath, opts)
Get the filetype "of" the given file, passing
opts
tolibmagic(2)
. -
#of?(file : File)
Get the filetype "of" the given file.
-
#of?(bytes : IO)
Get the filetype "of" the given file.
-
#of?(filepath)
Get the filetype "of" the given file.
-
#options
The current value of the magic flags.
-
#options=(options)
Directly set the options parameter.
- #preserve_atime
- #preserve_atime=(setting : Bool)
- #preserve_atime?
-
#reset_options
reset any options set on this instance back to the magic.cr default.
-
#return_error_as_text
Return errors while trying to open files and follow symlinks in thefiletype text rather than raising an error.
- #return_error_as_text=(setting : Bool)
- #return_error_as_text?
-
#try_to_preserve_access_time
On systems that support utime(3) or utimes(2), attempt topreserve the access time of files analysed.
- #try_to_preserve_access_time=(setting : Bool)
- #try_to_preserve_access_time?
Constructor Detail
Instance Method Detail
Check the magic database for consistency and printwarnings to stderr, while checking a file.
If the file is a block or character special device, thenopen the device and try to look in its contents.
Manually set the checker to a given LibMagic::MagicT (libmagic's magic_t). This also reloads the datase, so no need to do that manually. If the given MagicT is a null pointer, this will raise Errno.
Load a new set of database files into the magic database. Setting this to
:default sets the database to the value of the MAGIC environment variable,
or, failing that, the default database, usually at
/usr/share/misc/magic
. Example:
check_ft = Magic::TypeChecker.new
check_ft.database = [ "/path/to/file", File.join("other", "path")]
...
check_ft.database = :default
escapes non-printable bytes as their 0oOOO
octal numeric forms.By default crystal handles this in cases where it's important (inputs
for example), so by default strings contain the raw values forunprintable characters. (this differs from the libmagic
default).
use the MAGIC_NONE option (the default for libmagic) instead of the default for magic.cr
Yields the current value of the max_bytes to the block, then sets the value to the result of the block, unless the block returns nil, of course. Example:
# doubles the PARAM_BYTES_MAX limit
ft_checker = Magic::TypeChecker.new
ft_checker.max_bytes do |current|
current * 2
end
Get the current limit of the
LibMagic::PARAM_BYTES_MAX libmagic param.
Defaults to 1048576. This is the same as calling
magic_getparam and passing in the
PARAM_BYTES_MAX constant. See libmagic(2)
.
Limit LibMagic::PARAM_BYTES_MAX to the
given value. This is equivalent to calling magic_setparam
and passing
it the constant PARAM_BYTES_MAX and the
given value. See libmagic(2)
.
Get the current limit of the
LibMagic::PARAM_ELF_NOTES_MAX libmagic param.
Defaults to 256. This is the same as calling
magic_getparam and passing in the
PARAM_ELF_NOTES_MAX constant. See libmagic(2)
.
Controls how many ELF notes will be processed.
Yields the current value of the max_elf_notes to the block, then sets the value to the result of the block, unless the block returns nil, of course. Controls how many ELF notes will be processed. Example:
# doubles the PARAM_ELF_NOTES_MAX limit
ft_checker = Magic::TypeChecker.new
ft_checker.max_elf_notes do |current|
current * 2
end
Limit LibMagic::PARAM_ELF_NOTES_MAX to the
given value. This is equivalent to calling magic_setparam
and passing
it the constant PARAM_ELF_NOTES_MAX and the
given value. See libmagic(2)
. Controls how many ELF notes will be processed.
Get the current limit of the
LibMagic::PARAM_ELF_PHNUM_MAX libmagic param.
Defaults to 128. This is the same as calling
magic_getparam and passing in the
PARAM_ELF_PHNUM_MAX constant. See libmagic(2)
.
Controls how many ELF program sections will be processed.
Yields the current value of the max_elf_phnum to the block, then sets the value to the result of the block, unless the block returns nil, of course. Controls how many ELF program sections will be processed. Example:
# doubles the PARAM_ELF_PHNUM_MAX limit
ft_checker = Magic::TypeChecker.new
ft_checker.max_elf_phnum do |current|
current * 2
end
Limit LibMagic::PARAM_ELF_PHNUM_MAX to the
given value. This is equivalent to calling magic_setparam
and passing
it the constant PARAM_ELF_PHNUM_MAX and the
given value. See libmagic(2)
. Controls how many ELF program sections will be processed.
Get the current limit of the
LibMagic::PARAM_ELF_SHNUM_MAX libmagic param.
Defaults to 32768. This is the same as calling
magic_getparam and passing in the
PARAM_ELF_SHNUM_MAX constant. See libmagic(2)
.
Controls how many ELF sections will be processed.
Yields the current value of the max_elf_shnum to the block, then sets the value to the result of the block, unless the block returns nil, of course. Controls how many ELF sections will be processed. Example:
# doubles the PARAM_ELF_SHNUM_MAX limit
ft_checker = Magic::TypeChecker.new
ft_checker.max_elf_shnum do |current|
current * 2
end
Limit LibMagic::PARAM_ELF_SHNUM_MAX to the
given value. This is equivalent to calling magic_setparam
and passing
it the constant PARAM_ELF_SHNUM_MAX and the
given value. See libmagic(2)
. Controls how many ELF sections will be processed.
Yields the current value of the max_indir to the block, then sets the value to the result of the block, unless the block returns nil, of course. Controls how many levels of recursion will be followed forindirect magic entries. Example:
# doubles the PARAM_INDIR_MAX limit
ft_checker = Magic::TypeChecker.new
ft_checker.max_indir do |current|
current * 2
end
Get the current limit of the
LibMagic::PARAM_INDIR_MAX libmagic param.
Defaults to 15. This is the same as calling
magic_getparam and passing in the
PARAM_INDIR_MAX constant. See libmagic(2)
.
Controls how many levels of recursion will be followed forindirect magic entries.
Limit LibMagic::PARAM_INDIR_MAX to the
given value. This is equivalent to calling magic_setparam
and passing
it the constant PARAM_INDIR_MAX and the
given value. See libmagic(2)
. Controls how many levels of recursion will be followed forindirect magic entries.
Get the current limit of the
LibMagic::PARAM_NAME_MAX libmagic param.
Defaults to 30. This is the same as calling
magic_getparam and passing in the
PARAM_NAME_MAX constant. See libmagic(2)
.
Controls the maximum number of calls for name/use.
Yields the current value of the max_name to the block, then sets the value to the result of the block, unless the block returns nil, of course. Controls the maximum number of calls for name/use. Example:
# doubles the PARAM_NAME_MAX limit
ft_checker = Magic::TypeChecker.new
ft_checker.max_name do |current|
current * 2
end
Limit LibMagic::PARAM_NAME_MAX to the
given value. This is equivalent to calling magic_setparam
and passing
it the constant PARAM_NAME_MAX and the
given value. See libmagic(2)
. Controls the maximum number of calls for name/use.
Get the filetype "of" the given file. Raises Errno if there's an error from libmagic instead of returning nil.
Get the filetype "of" the given bytes. Raises Errno if there's an error from libmagic instead of returning nil.
Get the filetype "of" the given file. Raises Errno if there's an error from libmagic instead of returning nil.
Get the filetype "of" the given file. Returns nil if there's an error
accessing the file or an internal libmagic error. In this case
libmagic(2)
will set errno(3)
(Errno.value
).
Get the filetype "of" the given file. Returns nil if there's an error
accessing the file or an internal libmagic error. In this case
libmagic(2)
will set errno(3)
(Errno.value
).
Get the filetype "of" the given file. Returns nil if there's an error
accessing the file or an internal libmagic error. In this case
libmagic(2)
will set errno(3)
(Errno.value
).
Get the filetype "of" the given file. Returns nil if there's an error
accessing the file or an internal libmagic error. In this case
libmagic(2)
will set errno(3)
(Errno.value
).
The current value of the magic flags. Equivalent to calling C's
magic_getflags()
or LibMagic.flags on this instance.
Directly set the options parameter. This overrides all other options (like
debug_output, follow_symlinks, etc.). Equivalent to calling C's
magic_setflags()
with the given integer. See libmagic(2)
for more
details. Appropriate values can be bitwise-or'd from LibMagic's constants.
Return errors while trying to open files and follow symlinks in thefiletype text rather than raising an error. This differs from the libmagic
default, because it makes more sense to handle the errors in Crystal in most cases than to output them as text.
On systems that support utime(3) or utimes(2), attempt topreserve the access time of files analysed.