Class: MagikkuFFI
- Inherits:
-
Object
- Object
- MagikkuFFI
- 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
-
.path ⇒ Object
Returns the default magic database path.
Instance Method Summary collapse
-
#check_syntax(filenames = nil) ⇒ true, false
Can be used to check the validity of magic files before compiling them.
-
#close ⇒ Object
Close the libmagic data scanner handle when you are finished with it.
- #closed? ⇒ Boolean
-
#compile(filenames) ⇒ true
Can be used to compile magic files.
-
#dbload(magicfiles) ⇒ Object
Used to load one or more magic databases.
-
#file(filename) ⇒ String
Analyzes file contents against the magicfile database.
-
#flags=(flags) ⇒ Object
Sets flags for the magic analyzer handle.
-
#initialize(param = nil) ⇒ MagikkuFFI
constructor
Initializes a new libmagic data scanner.
-
#string(str) ⇒ String
Analyzes a string buffer against the magicfile database.
Constructor Details
#initialize(param = nil) ⇒ MagikkuFFI
Initializes a new libmagic data scanner
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(, db) != 0 err = lasterror() FFI::Libmagic.magic_close() raise(DbLoadError, "Error loading db: #{db.inspect} " << err) end end |
Class Method Details
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.
257 258 259 |
# File 'lib/magikku_ffi.rb', line 257 def check_syntax(filenames=nil) return (FFI::Libmagic.magic_check(, filenames) == 0) end |
#close ⇒ Object
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() unless @closed @closed = true return nil end |
#closed? ⇒ 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.
238 239 240 241 242 243 244 |
# File 'lib/magikku_ffi.rb', line 238 def compile(filenames) if FFI::Libmagic.magic_compile(, 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.
202 203 204 205 206 207 208 |
# File 'lib/magikku_ffi.rb', line 202 def dbload(magicfiles) if FFI::Libmagic.magic_load(, 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
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(, filename) end |
#flags=(flags) ⇒ Object
Sets flags for the magic analyzer handle.
216 217 218 219 220 |
# File 'lib/magikku_ffi.rb', line 216 def flags=(flags) if FFI::Libmagic.magic_setflags(, flags) < 0 raise(FlagError, lasterror()) end end |
#string(str) ⇒ String
Analyzes a string buffer against the magicfile database
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(, p, str.size) end |