Class: MagikkuFFI

Inherits:
Object
  • Object
show all
Extended by:
MagikkuHelpers
Defined in:
lib/magikku_ffi.rb

Overview

Note the implementation of this class may either be via FFI or via native C bindings depending on your installation and what version of ruby you are using.

Defined Under Namespace

Modules: Flags Classes: ClosedError, CompileError, DbLoadError, FlagError, InitFatal, MagikkuError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(param = nil) ⇒ MagikkuFFI

Initializes a new libmagic data scanner

Parameters:

  • params (Hash, nil)

    A hash of parameters or nil for defaults.

Raises:

  • (TypeError)

See Also:

  • MagikkuFFI::Flags and libmagic(3) manpage
  • dbload()


133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/magikku_ffi.rb', line 133

def initialize(param = nil)
  param ||= {}
  raise(TypeError, "Invalid Type for params") if not param.is_a?(Hash)

  flags = param[:flags] || Flags::NONE
  raise(TypeError, "flags must be a Fixnum") if not flags.is_a?(Fixnum)

  db = param[:db]
  raise(TypeError, "db must be nil or a String") if db and not db.is_a?(String)

  @_cookie = FFI::Libmagic.magic_open(flags)
  if @_cookie.null?
    raise(InitFatal, "magic_open(#{flags}) returned a null pointer")
  end

  if FFI::Libmagic.magic_load(_cookie, db) != 0
    err = lasterror()
    FFI::Libmagic.magic_close(_cookie)
    raise(DbLoadError, "Error loading db: #{db.inspect} " << err)
  end
end

Class Method Details

.pathObject

Returns the default magic database path.



89
90
91
# File 'lib/magikku_ffi.rb', line 89

def self.path
  FFI::Libmagic.magic_getpath(nil, 0)
end

Instance Method Details

#check_syntax(filenames = nil) ⇒ true, false

Can be used to check the validity of magic files before compiling them. This is basically a dry-run that can be used before compiling magicfile databases.

Note: Errors and warnings may be displayed on stderr.

Parameters:

  • filename (String, nil)

    A colon seperated list of filenames or a single file. nil checks the default database.

Returns:

  • (true, false)

    Indicates whether the check was successful.



257
258
259
# File 'lib/magikku_ffi.rb', line 257

def check_syntax(filenames=nil)
  return (FFI::Libmagic.magic_check(_cookie, filenames) == 0)
end

#closeObject

Close the libmagic data scanner handle when you are finished with it



156
157
158
159
160
161
# File 'lib/magikku_ffi.rb', line 156

def close
  FFI::Libmagic.magic_close(_cookie) unless @closed

  @closed = true
  return nil
end

#closed?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/magikku_ffi.rb', line 163

def closed?
  (@closed == true)
end

#compile(filenames) ⇒ true

Can be used to compile magic files. This does not load files, however. You must use dbload for that.

Note: Errors and warnings may be displayed on stderr.

Parameters:

  • filename (String, nil)

    A colon seperated list of filenames or a single filename. The compiled files created are generated in the current directory using the basename(1) of each file argument with “.mgc” appended to it. Directory names can be compiled, in which case the contents of the directory will be compiled together as a single .mgc file. nil compiles the default database.

Returns:

  • (true)

    if everything went well.

Raises:



238
239
240
241
242
243
244
# File 'lib/magikku_ffi.rb', line 238

def compile(filenames)
  if FFI::Libmagic.magic_compile(_cookie, filenames) != 0
    raise(CompileError, "Error compiling #{filenames.inspect}: " << lasterror())
  else
    return true
  end
end

#dbload(magicfiles) ⇒ Object

Used to load one or more magic databases.

Parameters:

  • magicfiles

    One or more filenames seperated by colons. If nil, the default database is loaded. If uncompiled magic files are specified, they are compiled on the fly but they do not generate new .mgc files as with the compile method. Multiple files be specified by seperating them with colons.

Raises:

  • (DbLoadError)

    if an error occurred loading the database(s)



202
203
204
205
206
207
208
# File 'lib/magikku_ffi.rb', line 202

def dbload(magicfiles)
  if FFI::Libmagic.magic_load(_cookie, magicfiles) != 0
    raise(DbLoadError, "Error loading db: #{magicfiles.inspect} " << lasterror())
  else
    return true
  end
end

#file(filename) ⇒ String

Analyzes file contents against the magicfile database

Parameters:

  • filename

    The path to a file to inspect

Returns:

  • (String)

    A textual description of the contents of the file

Raises:

  • (TypeError)


173
174
175
176
177
# File 'lib/magikku_ffi.rb', line 173

def file(filename)
  File.stat(filename)
  raise(TypeError, "filename must not be nil") if filename.nil?
  FFI::Libmagic.magic_file(_cookie, filename)
end

#flags=(flags) ⇒ Object

Sets flags for the magic analyzer handle.

Parameters:

  • flags

    Flags to to set for magic. See MagikkuFFI::Flags and libmagic(3) manpage. The flag values should be ‘or’ed together with ‘|’. Using 0 will clear all flags.



216
217
218
219
220
# File 'lib/magikku_ffi.rb', line 216

def flags=(flags)
  if FFI::Libmagic.magic_setflags(_cookie, flags) < 0
    raise(FlagError, lasterror())
  end
end

#string(str) ⇒ String

Analyzes a string buffer against the magicfile database

Parameters:

  • filename

    The string buffer to inspect

Returns:

  • (String)

    A textual description of the contents the string

Raises:

  • (TypeError)


185
186
187
188
189
190
# File 'lib/magikku_ffi.rb', line 185

def string(str)
  raise(TypeError, "wrong argument type #{str.class} (expected String)") unless str.is_a?(String)
  p = FFI::MemoryPointer.new(str.size)
  p.write_string_length(str, str.size)
  FFI::Libmagic.magic_buffer(_cookie, p, str.size)
end