Class: LibmagicRb
- Inherits:
-
Object
- Object
- LibmagicRb
- Defined in:
- lib/libmagic_rb/version.rb,
ext/libmagic/magic.c
Defined Under Namespace
Classes: FileClosedError, FileNotFound, FileUnreadable, InvalidDBError, IsDirError
Constant Summary collapse
- VERSION =
"0.2.0"
- MAGIC_VERSION =
:MAGIC_VERSION returns the magic version of the library. For older libmagic version, this can be undefined, so this method will return “0” instead.
rb_str_new_cstr("0")
Instance Attribute Summary collapse
-
#closed ⇒ Object
(also: #closed?)
readonly
Attributes.
- #db ⇒ Object
- #file ⇒ Object
- #mode ⇒ Object readonly
Class Method Summary collapse
-
.check(args) ⇒ Object
Directly check a file with LibmagicRb, without creating any sort of cookies:.
- .lsmodes ⇒ Object
- .lsparams ⇒ Object
Instance Method Summary collapse
-
#check ⇒ Object
Check for file mimetype.
-
#close ⇒ Object
Close database.
-
#getparam ⇒ Object
Get and set params.
-
#initialize(args) ⇒ Object
constructor
Intializes a magic cookie that can be used multiple times.
-
#load ⇒ Object
Load database.
-
#magic_buffer ⇒ Object
Miscellaneous.
- #magic_list ⇒ Object
- #mode ⇒ Object readonly
-
#setflags ⇒ Object
Set modes dynamically.
- #setparam ⇒ Object
Constructor Details
#initialize(args) ⇒ Object
Intializes a magic cookie that can be used multiple times. The benefit of this is to assgign various cookies and change flags of each cookie.
For example:
> cookie = LibmagicRb.new(file: ‘/tmp’) # => #<LibmagicRb:0x000055810139f738 @closed=false, @db=nil, @file=“/tmp”, @mode=1106>
> cookie.check # => “inode/directory; charset=binary”
> cookie.setflags(LibmagicRb::MAGIC_RAW) # => 256
> cookie.check # => “sticky, directory”
> cookie2 = LibmagicRb.new(file: ‘/usr/share/dict/words’) # => #<LibmagicRb:0x000055810190a060 @closed=false, @db=nil, @file=“/usr/share/dict/words”, @mode=1106>
> cookie2.check # => “text/plain; charset=utf-8”
> cookie.close # => #<LibmagicRb:0x000055810139f738 @closed=true, @db=nil, @file=“/tmp”, @mode=256>
> cookie.closed? # => true
> cookie2.closed? # => false
Here in this example, we can’t use cookie, but cookie2 is a different magic_t wrapper, so we can continue using that. Flags/modes applied to cookie, will not affect cookie2 as well. Think of them as totally different containers. Of course, you must close cookies when you don’t need them. Otherwise it can use memories unless GC is triggered.
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'ext/libmagic/magic.c', line 170
VALUE rb_libmagicRb_initialize(VALUE self, VALUE args) {
// Database Path
if(!RB_TYPE_P(args, T_HASH)) {
rb_raise(rb_eArgError, "Expected hash as argument.");
}
VALUE argDBPath = rb_hash_aref(args, ID2SYM(rb_intern("db")));
if (RB_TYPE_P(argDBPath, T_NIL)) {
rb_ivar_set(self, rb_intern("@db"), Qnil);
} else if (!RB_TYPE_P(argDBPath, T_STRING)) {
rb_raise(rb_eArgError, "Database name must be an instance of String.");
} else {
rb_ivar_set(self, rb_intern("@db"), argDBPath);
}
// File path
VALUE argFilePath = rb_hash_aref(args, ID2SYM(rb_intern("file")));
if (RB_TYPE_P(argFilePath, T_NIL)) {
rb_raise(rb_eArgError, "Expected `file:\" key as a string, got nil instead");
} else if (!RB_TYPE_P(argFilePath, T_STRING)) {
rb_raise(rb_eArgError, "Filename must be an instance of String.");
}
rb_ivar_set(self, rb_intern("@file"), argFilePath);
// Modes
VALUE argModes = rb_hash_aref(args, ID2SYM(rb_intern("mode")));
unsigned int modes;
if(RB_TYPE_P(argModes, T_NIL)) {
modes = MAGIC_MIME | MAGIC_CHECK | MAGIC_SYMLINK;
} else if (!RB_TYPE_P(argModes, T_FIXNUM)) {
rb_raise(rb_eArgError, "Modes must be an instance of Integer. Check LibmagicRb.constants() or LibmagicRb.lsmodes().");
} else {
modes = FIX2UINT(argModes);
}
rb_ivar_set(self, rb_intern("@mode"), UINT2NUM(modes));
rb_ivar_set(self, rb_intern("@closed"), Qfalse);
RB_UNWRAP(cookie);
magic_setflags(*cookie, modes);
return self;
}
|
Instance Attribute Details
#closed ⇒ Object (readonly) Also known as: closed?
Attributes
#db ⇒ Object
#file ⇒ Object
#mode ⇒ Object (readonly)
Class Method Details
.check(args) ⇒ Object
Directly check a file with LibmagicRb, without creating any sort of cookies:
LibmagicRb.check(file: ‘/tmp/’, db: ‘/usr/share/file/misc/magic.mgc’, mode: LibmagicRb::MAGIC_CHECK) # => “sticky, directory”
- file
-
The key ‘file:` is the filename to check. Should be a string
- db
-
The key ‘db:` can be left as nil. Or you can give it the path of the current magic database.
- mode
-
The key ‘mode` can be any of the LibmagicRb.lsmodes().
To combine modes you can use ‘|`. For example: `mode: LibmagicRb::MAGIC_CHECK | LibmagicRb::MAGIC_SYMLINK | Libmagic_MAGIC_MIME` If `mode` key is nil, it will default to `MAGIC_MIME | MAGIC_CHECK | MAGIC_SYMLINK`
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'ext/libmagic/magic.c', line 68
static VALUE _check_(VALUE obj, VALUE args) {
if(!RB_TYPE_P(args, T_HASH)) {
rb_raise(rb_eArgError, "Expected hash as argument.");
}
// Database Path
VALUE argDBPath = rb_hash_aref(args, ID2SYM(rb_intern("db")));
char *databasePath = NULL;
if (!NIL_P(argDBPath)) {
if (!RB_TYPE_P(argDBPath, T_STRING)) {
rb_raise(rb_eArgError, "Database name must be an instance of String.");
}
databasePath = StringValuePtr(argDBPath);
}
// File path
VALUE argFilePath = rb_hash_aref(args, ID2SYM(rb_intern("file")));
if (RB_TYPE_P(argFilePath, T_NIL)) {
rb_raise(rb_eArgError, "Expected `file:\" key as a string, got nil instead");
} else if (!RB_TYPE_P(argFilePath, T_STRING)) {
rb_raise(rb_eArgError, "Filename must be an instance of String.");
}
char *checkPath = StringValuePtr(argFilePath);
// Modes
VALUE argModes = rb_hash_aref(args, ID2SYM(rb_intern("mode")));
unsigned int modes;
if(RB_TYPE_P(argModes, T_NIL)) {
modes = MAGIC_MIME | MAGIC_CHECK | MAGIC_SYMLINK;
} else if (!RB_TYPE_P(argModes, T_FIXNUM)) {
rb_raise(rb_eArgError, "Modes must be an instance of Integer. Check LibmagicRb.constants() or LibmagicRb.lsmodes().");
} else {
modes = FIX2UINT(argModes);
}
// Checks
struct magic_set *magic = magic_open(modes);
if (!magic) {
rb_raise(rb_eRuntimeError, "Failed to initialize magic cookie.");
}
// Check if the database is a valid file or not
// Raises ruby error which will return.
fileReadable(checkPath);
if(databasePath) {
magic_validate_db(magic, databasePath);
}
if (magic_load(magic, databasePath) == -1) {
magic_close(magic);
rb_raise(rb_eInvalidDBError, "Failed to load magic database: %s", magic_error(magic));
}
const char *mt = magic_file(magic, checkPath);
VALUE retVal = mt ? rb_str_new_cstr(mt) : Qnil;
magic_close(magic);
return retVal;
}
|
.lsmodes ⇒ Object
.lsparams ⇒ Object
Instance Method Details
#check ⇒ Object
Check for file mimetype
#close ⇒ Object
Close database
#getparam ⇒ Object
Get and set params
#load ⇒ Object
Load database
#magic_buffer ⇒ Object
Miscellaneous
#magic_list ⇒ Object
#mode= ⇒ Object (readonly)
#setflags ⇒ Object
Set modes dynamically