class FileMagic
Constants
- DEFAULT_MAGIC
- FLAGS_BY_INT
Map flag values to their names (Integer => :name).
- FLAGS_BY_SYM
Map flag names to their values (:name => Integer).
- MAGIC_APPLE
- MAGIC_CHECK
- MAGIC_COMPRESS
- MAGIC_COMPRESS_TRANSP
- MAGIC_CONTINUE
- MAGIC_DEBUG
- MAGIC_DEVICES
- MAGIC_ERROR
- MAGIC_EXTENSION
- MAGIC_MIME
- MAGIC_MIME_ENCODING
- MAGIC_MIME_TYPE
- MAGIC_NODESC
- MAGIC_NONE
- MAGIC_NO_CHECK_APPTYPE
- MAGIC_NO_CHECK_ASCII
- MAGIC_NO_CHECK_BUILTIN
defined in b5be901 (2010-01-28, 5.05), but broken until 38e0136 (2013-08-15, 5.15)
- MAGIC_NO_CHECK_CDF
- MAGIC_NO_CHECK_COMPRESS
- MAGIC_NO_CHECK_ELF
- MAGIC_NO_CHECK_ENCODING
- MAGIC_NO_CHECK_FORTRAN
- MAGIC_NO_CHECK_SOFT
- MAGIC_NO_CHECK_TAR
- MAGIC_NO_CHECK_TEXT
- MAGIC_NO_CHECK_TOKENS
- MAGIC_NO_CHECK_TROFF
- MAGIC_PRESERVE_ATIME
- MAGIC_RAW
- MAGIC_SYMLINK
- MAGIC_VERSION
- SIMPLE_RE
Extract “simple” MIME type.
- VERSION
Attributes
simplified[W]
Public Class Methods
clear!()
click to toggle source
Clear our instance cache.
# File lib/filemagic.rb 84 def clear! 85 @fm.each_value(&:close).clear 86 end
flags(p1)
click to toggle source
Converts flags to integer
static VALUE
rb_magic_flags(VALUE klass, VALUE flags) {
VALUE map = rb_const_get(cFileMagic, rb_intern("FLAGS_BY_SYM")), f, g;
int i = MAGIC_NONE, j;
if (TYPE(flags) != T_ARRAY) {
rb_raise(rb_eTypeError,
"wrong argument type %s (expected Array)",
rb_obj_classname(flags));
}
for (j = 0; j < RARRAY_LEN(flags); j++) {
f = rb_ary_entry(flags, j);
switch (TYPE(f)) {
case T_SYMBOL:
if (RTEST(g = rb_hash_aref(map, f))) {
f = g;
/* fall through */
}
else {
f = rb_funcall(f, rb_intern("inspect"), 0);
rb_raise(rb_eArgError, "%s: %s",
NIL_P(g) ? "no such flag" : "flag not available",
StringValueCStr(f));
break;
}
case T_FIXNUM:
i |= NUM2INT(f);
break;
default:
rb_raise(rb_eTypeError,
"wrong argument type %s (expected Fixnum or Symbol)",
rb_obj_classname(f));
}
}
return INT2FIX(i);
}
fm(*flags)
click to toggle source
Provide a “magic singleton”.
# File lib/filemagic.rb 73 def fm(*flags) 74 options = flags.last.is_a?(Hash) ? flags.pop : {} 75 76 if fm = @fm[key = [flags = flags(flags), options]] 77 return fm unless fm.closed? 78 end 79 80 @fm[key] = new(flags, options) 81 end
library_version()
click to toggle source
Returns the magic version
static VALUE
rb_magic_version(VALUE klass) {
char version[8] = "0";
#ifdef HAVE_MAGIC_VERSION
RB_MAGIC_SET_VERSION(magic_version() / 100, magic_version() % 100)
#endif
return rb_str_new2(version);
}
magic_version(default = MAGIC_VERSION)
click to toggle source
# File lib/filemagic.rb 109 def magic_version(default = MAGIC_VERSION) 110 default != '0' ? default : 111 user_magic_version || 112 auto_magic_version || 113 [default, 'unknown'] 114 end
mime(*flags, &block)
click to toggle source
Just a short-cut to open with the mime flag set.
# File lib/filemagic.rb 105 def mime(*flags, &block) 106 open(:mime, *flags, &block) 107 end
new(*args)
click to toggle source
static VALUE
rb_magic_new(int argc, VALUE *argv, VALUE klass) {
VALUE obj, args[2];
magic_t ms;
if (rb_block_given_p()) {
rb_warn(
"FileMagic.new() does not take a block; use FileMagic.open() instead");
}
if (argc > 0 && TYPE(args[1] = argv[argc - 1]) == T_HASH) {
argc--;
}
else {
args[1] = rb_hash_new();
}
args[0] = rb_magic_flags(klass, rb_ary_new4(argc, argv));
if ((ms = magic_open(NUM2INT(args[0]))) == NULL) {
rb_raise(rb_FileMagicError,
"failed to initialize magic cookie (%d)", errno || -1);
}
if (magic_load(ms, NULL) == -1) {
rb_raise(rb_FileMagicError,
"failed to load database: %s", magic_error(ms));
}
obj = Data_Wrap_Struct(klass, 0, rb_magic_free, ms);
rb_obj_call_init(obj, 2, args);
return obj;
}
open(*flags) { |fm| ... }
click to toggle source
Just like new, but takes an optional block, in which case close is called at the end and the value of the block is returned.
# File lib/filemagic.rb 90 def open(*flags) 91 fm = new(*flags) 92 93 if block_given? 94 begin 95 yield fm 96 ensure 97 fm.close 98 end 99 else 100 fm 101 end 102 end
path()
click to toggle source
Returns the magic path
static VALUE
rb_magic_getpath(VALUE klass) {
const char *path = magic_getpath(NULL, 0);
return path != NULL ? rb_str_new2(path) : Qnil;
}
Private Class Methods
auto_magic_version()
click to toggle source
# File lib/filemagic.rb 122 def auto_magic_version 123 require 'nuggets/file/which' 124 125 if cmd = File.which_command([ 126 'dpkg-query -f \'${Version}\' -W libmagic-dev', 127 'file -v' 128 ]) 129 [%x{#{cmd}}[/\d+\.\d+/], 'auto-detected'] 130 end 131 rescue LoadError 132 end
user_magic_version(key = 'MAGIC_VERSION')
click to toggle source
# File lib/filemagic.rb 118 def user_magic_version(key = 'MAGIC_VERSION') 119 [ENV[key], 'user-specified'] if ENV[key] 120 end
Public Instance Methods
close()
click to toggle source
Frees resources allocated
static VALUE
rb_magic_close(VALUE self) {
magic_t ms;
if (RTEST(rb_magic_closed_p(self))) {
return Qnil;
}
GetMagicSet(self, ms);
rb_magic_free(ms);
/* This keeps rb_magic_free from trying to free closed objects */
DATA_PTR(self) = NULL;
rb_iv_set(self, "closed", Qtrue);
return Qnil;
}
closed?()
click to toggle source
static VALUE
rb_magic_closed_p(VALUE self) {
return rb_attr_get(self, rb_intern("closed"));
}
fd(fd)
click to toggle source
# File lib/filemagic.rb 149 def fd(fd) 150 descriptor(fd.respond_to?(:fileno) ? fd.fileno : fd) 151 end
flags()
click to toggle source
Get the flags as array of symbols
static VALUE
rb_magic_getflags(VALUE self) {
VALUE ary = rb_ary_new();
VALUE map = rb_const_get(cFileMagic, rb_intern("FLAGS_BY_INT"));
int i = NUM2INT(rb_attr_get(self, rb_intern("iflags"))), j = 0;
while ((i -= j) > 0) {
j = pow(2, (int)(log(i) / log(2)));
rb_ary_unshift(ary, rb_hash_aref(map, INT2FIX(j)));
}
return ary;
}
flags=(p1)
click to toggle source
Set flags on the ms object
static VALUE
rb_magic_setflags(VALUE self, VALUE flags) {
magic_t ms;
GetMagicSet(self, ms);
rb_iv_set(self, "iflags",
flags = rb_magic_flags(CLASS_OF(self), rb_Array(flags)));
return INT2FIX(magic_setflags(ms, NUM2INT(flags)));
}
inspect()
click to toggle source
Calls superclass method
# File lib/filemagic.rb 153 def inspect 154 super.insert(-2, closed? ? ' (closed)' : '') 155 end
io(io, length = 8, rewind = false)
click to toggle source
# File lib/filemagic.rb 142 def io(io, length = 8, rewind = false) 143 pos = io.pos if rewind 144 buffer(io.read(length)) 145 ensure 146 io.pos = pos if pos 147 end
simplified?()
click to toggle source
# File lib/filemagic.rb 138 def simplified? 139 @simplified 140 end