Skip to content

mah0x211/lua-libmagic

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

54 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

lua-libmagic

test codecov

libmagic binding for lua.
see man libmagic for more details.


NOTE: this module is tested with libmagic v5.41.


Dependencies

you should be installed libmagic-dev or equivalent package before install this module.

Installation

luarocks install libmagic

Usage

local libmagic = require('libmagic')
local m = libmagic.open( libmagic.MIME_TYPE, libmagic.NO_CHECK_COMPRESS )
assert(m:load()) -- load the default database

print(m:file('README.md'))  -- output "text/plain"

Constants

libmagic module has the following constants.

  • libmagic.VERSION
  • libmagic.NONE
  • libmagic.DEBUG
  • libmagic.SYMLINK
  • libmagic.COMPRESS
  • libmagic.DEVICES
  • libmagic.MIME_TYPE
  • libmagic.CONTINUE
  • libmagic.CHECK
  • libmagic.PRESERVE_ATIME
  • libmagic.RAW
  • libmagic.ERROR
  • libmagic.MIME_ENCODING
  • libmagic.MIME
  • libmagic.APPLE
  • libmagic.EXTENSION
  • libmagic.COMPRESS_TRANSP
  • libmagic.NODESC
  • libmagic.NO_CHECK_COMPRESS
  • libmagic.NO_CHECK_TAR
  • libmagic.NO_CHECK_SOFT
  • libmagic.NO_CHECK_APPTYPE
  • libmagic.NO_CHECK_ELF
  • libmagic.NO_CHECK_TEXT
  • libmagic.NO_CHECK_CDF
  • libmagic.NO_CHECK_TOKENS
  • libmagic.NO_CHECK_ENCODING
  • libmagic.NO_CHECK_JSON
  • libmagic.NO_CHECK_ASCII
  • libmagic.NO_CHECK_FORTRAN
  • libmagic.NO_CHECK_TROFF

Create an instance of libmagic

local libmagic = require('libmagic')
local m = libmagic.open(libmagic.MIME_TYPE)

str, err = m( arg )

helper function to get a textual description of the contents of the file.

Parameters

  • arg:string|integer|file*: pathname or file descriptor or file handle of the file.
    • arg:string: equivalent to m:file( pathname ).
    • arg:integer: equivalent to m:descriptor( fd ).
    • arg:file*: equivalent to m:filehandle( fh ).

Returns

  • str:string: a textual description.
  • err:string: error string.

str, err = m:file( pathname )

returns a textual description of the contents of the file.

Parameters

  • pathname:string: pathname of the file.

Returns

  • str:string: a textual description.
  • err:string: error string.

str, err = m:descriptor( fd )

returns a textual description of the contents of the file descriptor.

Parameters

  • fd:integer: file descriptor.

Returns

  • str:string: a textual description.
  • err:string: error string.

str, err = m:filehandle( fh )

returns a textual description of the contents of the filehandle.

Parameters

  • fh:filehandle: lua filehandle.

Returns

  • str:string: a textual description.
  • err:string: error string.

str, err= m:buffer( buf:string )

returns a textual description of the contents of the buffer.

Parameters

  • buf:string: buffer string.

Returns

  • str:string: a textual description.
  • err:string: error string.

err = m:error()

returns a textual explanation of the last error.

Returns

  • err:string: error string.

ok = m:setflags( [flag, ...] )

set flags.

Returns

  • ok:boolean: true on success.

ok, err = m:load( [pathnames:string] )

loads the database files from the colon separated list of database files.

Parameters

  • pathnames:string: the colon separated list of database files.

Returns

  • ok:boolean: true on success.
  • err:string: error string.

ok, err = m:compile( pathnames:string )

compile the colon separated list of database files.

Parameters

  • pathnames:string: the colon separated list of database files.

Returns

  • ok:boolean: true on success.
  • err:string: error string.

ok, err = m:check( [pathnames:string] )

check the validity of entries in the colon separated database files.

Parameters

  • pathnames:string: the colon separated list of database files.

Returns

  • ok:boolean: true on success.
  • err:string: error string.

ok, err = m:list( [pathnames:string] )

dumps all magic entries in a human readable format

Returns

  • ok:boolean: true on success.
  • err:string: error string.

errno = m:errno()

returns the last operating system error number.

Returns

  • errno:integer: system error number (errno).