class Magic::TypeChecker

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.cr

Constant Summary

DEFAULT_OPTIONS = LibMagic::RAW | LibMagic::ERROR
Integer = ((((((Int8 | Int16) | Int32) | Int64) | UInt8) | UInt16) | UInt32) | UInt64

Constructors

Instance Method Summary

Constructor Detail

def self.new(options = DEFAULT_OPTIONS, database_files : IterableOfStrings? = nil, limit_settings : Hash(Limit, Int32)? = nil) #

[View source]

Instance Method Detail

def all_types #

Return all matches, not just the first.


[View source]
def all_types=(setting : Bool) #

[View source]
def all_types? #

[View source]
def apple #

Return the Apple creator and type.


[View source]
def apple=(setting : Bool) #

[View source]
def apple? #

[View source]
def check_db #

Check the magic database for consistency and printwarnings to stderr, while checking a file.


[View source]
def check_db=(setting : Bool) #

[View source]
def check_db? #

[View source]
def check_device #

If the file is a block or character special device, thenopen the device and try to look in its contents.


[View source]
def check_device=(setting : Bool) #

[View source]
def check_device? #

[View source]
def checker=(chkr : LibMagic::MagicT) #

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.


[View source]
def db_files : Set(String)? #

database files to read from


[View source]
def db_files=(files : IterableOfStrings) #

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

[View source]
def debug_output #

Have libmagic(2) print debugging messages to stderr.


[View source]
def debug_output=(setting : Bool) #

[View source]
def debug_output? #

[View source]
def default_options? #

[View source]
def error(msg) #

[View source]
def 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. (this differs from the libmagic default).


[View source]
def escape_unprintable=(setting : Bool) #

[View source]
def escape_unprintable? #

[View source]
def extensions(this) #

like #of() and #for() but returns a Set of valid extensions, rather than a single string.


[View source]
def follow_symlinks #

If the file queried is a symlink, follow it.


[View source]
def follow_symlinks=(setting : Bool) #

[View source]
def follow_symlinks? #

[View source]
def for(this) #

same as #of()


[View source]
def get_extensions #

Makes #of() return a slash-separated list of extensions for this file type.


[View source]
def get_extensions=(setting : Bool) #

[View source]
def get_extensions? #

[View source]
def get_mime #

[View source]
def get_mime=(setting : Bool) #

[View source]
def get_mime? #

[View source]
def get_mime_encoding #

Return a MIME encoding, instead of a textual description.


[View source]
def get_mime_encoding=(setting : Bool) #

[View source]
def get_mime_encoding? #

[View source]
def get_mime_type #

Return a MIME type string, instead of a textual description.


[View source]
def get_mime_type=(setting : Bool) #

[View source]
def get_mime_type? #

[View source]
def libmagic_defaults #

use the MAGIC_NONE option (the default for libmagic) instead of the default for magic.cr


[View source]
def libmagic_defaults? #

[View source]
def look_into_compressed_files #

If the file is compressed, unpack it and look at the contents.


[View source]
def look_into_compressed_files=(setting : Bool) #

[View source]
def look_into_compressed_files? #

[View source]
def 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. Example:

# doubles the PARAM_BYTES_MAX limit
ft_checker = Magic::TypeChecker.new
ft_checker.max_bytes do |current|
  current * 2
end

[View source]
def max_bytes #

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).


[View source]
def max_bytes=(value : Int32) #

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).


[View source]
def max_elf_notes #

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.


[View source]
def 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. 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

[View source]
def max_elf_notes=(value : Int32) #

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.


[View source]
def max_elf_phnum #

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.


[View source]
def 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. 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

[View source]
def max_elf_phnum=(value : Int32) #

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.


[View source]
def max_elf_shnum #

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.


[View source]
def 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. 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

[View source]
def max_elf_shnum=(value : Int32) #

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.


[View source]
def 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. 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

[View source]
def max_indir #

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.


[View source]
def max_indir=(value : Int32) #

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.


[View source]
def max_name #

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.


[View source]
def 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. 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

[View source]
def max_name=(value : Int32) #

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.


[View source]
def no_compression_info #

Don't report on compression, only report about the uncompressed data.


[View source]
def no_compression_info=(setting : Bool) #

[View source]
def no_compression_info? #

[View source]
def of(file : File) #

Get the filetype "of" the given file. Raises Errno if there's an error from libmagic instead of returning nil.


[View source]
def of(this : Int32) #

get the filetype "of" the open file at the given file descriptor integer.


[View source]
def of(this : IO) #

Get the filetype "of" the given bytes. Raises Errno if there's an error from libmagic instead of returning nil.


[View source]
def of(filepath) #

Get the filetype "of" the given file. Raises Errno if there's an error from libmagic instead of returning nil.


[View source]
def of?(file_descriptor : Int32) #

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).


[View source]
def of?(filepath, opts) #

Get the filetype "of" the given file, passing opts to libmagic(2).


[View source]
def of?(file : File) #

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).


[View source]
def of?(bytes : IO) #

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).


[View source]
def of?(filepath) #

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).


[View source]
def options #

The current value of the magic flags. Equivalent to calling C's magic_getflags() or LibMagic.flags on this instance.


[View source]
def options=(options) #

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.


[View source]
def preserve_atime #

[View source]
def preserve_atime=(setting : Bool) #

[View source]
def preserve_atime? #

[View source]
def reset_options #

reset any options set on this instance back to the magic.cr default.


[View source]
def return_error_as_text #

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.


[View source]
def return_error_as_text=(setting : Bool) #

[View source]
def return_error_as_text? #

[View source]
def try_to_preserve_access_time #

On systems that support utime(3) or utimes(2), attempt topreserve the access time of files analysed.


[View source]
def try_to_preserve_access_time=(setting : Bool) #

[View source]
def try_to_preserve_access_time? #

[View source]