Class: FileMagic
- Inherits:
-
Object
- Object
- FileMagic
- Defined in:
- lib/filemagic.rb,
lib/filemagic/version.rb,
ext/filemagic/filemagic.c
Defined Under Namespace
Modules: Ext, Version Classes: FileMagicError
Constant Summary collapse
- DEFAULT_MAGIC =
__FILE__.sub(/\.rb\z/, '/magic.mgc')
- FLAGS_BY_SYM =
Map flag names to their values (:name => Integer).
[ :none, # No flags :debug, # Turn on debugging :symlink, # Follow symlinks :compress, # Check inside compressed files :devices, # Look at the contents of devices :mime_type, # Return the MIME type :continue, # Return all matches :check, # Print warnings to stderr :preserve_atime, # Restore access time on exit :raw, # Don't convert unprintable chars :error, # Handle ENOENT etc as real errors :mime_encoding, # Return the MIME encoding :mime, # MAGIC_MIME_TYPE | MAGIC_MIME_ENCODING :apple, # Return the Apple creator/type :extension, # Return a /-separated list of extensions :compress_transp, # Check inside compressed files but not report compression :nodesc, # MAGIC_EXTENSION | MAGIC_MIME | MAGIC_APPLE :no_check_compress, # Don't check for compressed files :no_check_tar, # Don't check for tar files :no_check_soft, # Don't check magic entries :no_check_apptype, # Don't check application type :no_check_elf, # Don't check for elf details :no_check_text, # Don't check for text files :no_check_cdf, # Don't check for cdf files :no_check_tokens, # Don't check tokens :no_check_encoding, # Don't check text encodings :no_check_builtin, # No built-in tests; only consult the magic file # Defined for backwards compatibility (renamed) :no_check_ascii, # MAGIC_NO_CHECK_TEXT # Defined for backwards compatibility; do nothing :no_check_fortran, # Don't check ascii/fortran :no_check_troff # Don't check ascii/troff ].inject({}) { |flags, flag| const = "MAGIC_#{flag.to_s.upcase}" flags.update(flag => const_defined?(const) && const_get(const)) }
- FLAGS_BY_INT =
Map flag values to their names (Integer => :name).
FLAGS_BY_SYM.invert.update(0 => :none)
- SIMPLE_RE =
Extract “simple” MIME type.
%r{([.\w\/-]+)}
- VERSION =
Version.to_s
- MAGIC_VERSION =
rb_str_new2(version)
- MAGIC_NONE =
INT2FIX(MAGIC_NONE)
- MAGIC_DEBUG =
INT2FIX(MAGIC_DEBUG)
- MAGIC_SYMLINK =
INT2FIX(MAGIC_SYMLINK)
- MAGIC_COMPRESS =
INT2FIX(MAGIC_COMPRESS)
- MAGIC_DEVICES =
INT2FIX(MAGIC_DEVICES)
- MAGIC_MIME_TYPE =
INT2FIX(MAGIC_MIME_TYPE)
- MAGIC_CONTINUE =
INT2FIX(MAGIC_CONTINUE)
- MAGIC_CHECK =
INT2FIX(MAGIC_CHECK)
- MAGIC_PRESERVE_ATIME =
INT2FIX(MAGIC_PRESERVE_ATIME)
- MAGIC_RAW =
INT2FIX(MAGIC_RAW)
- MAGIC_ERROR =
INT2FIX(MAGIC_ERROR)
- MAGIC_MIME_ENCODING =
INT2FIX(MAGIC_MIME_ENCODING)
- MAGIC_MIME =
INT2FIX(MAGIC_MIME)
- MAGIC_APPLE =
INT2FIX(MAGIC_APPLE)
- MAGIC_EXTENSION =
INT2FIX(MAGIC_EXTENSION)
- MAGIC_COMPRESS_TRANSP =
INT2FIX(MAGIC_COMPRESS_TRANSP)
- MAGIC_NODESC =
INT2FIX(MAGIC_NODESC)
- MAGIC_NO_CHECK_COMPRESS =
INT2FIX(MAGIC_NO_CHECK_COMPRESS)
- MAGIC_NO_CHECK_TAR =
INT2FIX(MAGIC_NO_CHECK_TAR)
- MAGIC_NO_CHECK_SOFT =
INT2FIX(MAGIC_NO_CHECK_SOFT)
- MAGIC_NO_CHECK_APPTYPE =
INT2FIX(MAGIC_NO_CHECK_APPTYPE)
- MAGIC_NO_CHECK_ELF =
INT2FIX(MAGIC_NO_CHECK_ELF)
- MAGIC_NO_CHECK_TEXT =
INT2FIX(MAGIC_NO_CHECK_TEXT)
- MAGIC_NO_CHECK_CDF =
INT2FIX(MAGIC_NO_CHECK_CDF)
- MAGIC_NO_CHECK_TOKENS =
INT2FIX(MAGIC_NO_CHECK_TOKENS)
- MAGIC_NO_CHECK_ENCODING =
INT2FIX(MAGIC_NO_CHECK_ENCODING)
- MAGIC_NO_CHECK_BUILTIN =
defined in b5be901 (2010-01-28, 5.05), but broken until 38e0136 (2013-08-15, 5.15)
INT2FIX(MAGIC_NO_CHECK_BUILTIN)
- MAGIC_NO_CHECK_ASCII =
INT2FIX(MAGIC_NO_CHECK_ASCII)
- MAGIC_NO_CHECK_FORTRAN =
INT2FIX(MAGIC_NO_CHECK_FORTRAN)
- MAGIC_NO_CHECK_TROFF =
INT2FIX(MAGIC_NO_CHECK_TROFF)
Instance Attribute Summary collapse
-
#simplified ⇒ Object
writeonly
Sets the attribute simplified.
Class Method Summary collapse
-
.clear! ⇒ Object
Clear our instance cache.
-
.flags(flags) ⇒ Object
Converts flags to integer.
-
.fm(*flags) ⇒ Object
Provide a “magic singleton”.
-
.library_version ⇒ Object
Returns the magic version.
- .magic_version(default = MAGIC_VERSION) ⇒ Object
-
.mime(*flags, &block) ⇒ Object
Just a short-cut to #open with the
mime
flag set. -
.new(*args) ⇒ Object
FileMagic.new.
-
.open(*flags) ⇒ Object
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.
-
.path ⇒ Object
Returns the magic path.
Instance Method Summary collapse
- #buffer ⇒ Object
- #check ⇒ Object
-
#close ⇒ Object
Frees resources allocated.
- #closed? ⇒ Boolean
- #compile ⇒ Object
- #descriptor ⇒ Object
- #fd(fd) ⇒ Object
- #file ⇒ Object
- #flags ⇒ Object
-
#flags=(flags) ⇒ Object
Set flags on the ms object.
- #initialize(*args) ⇒ Object constructor
- #inspect ⇒ Object
- #io(io, length = 8, rewind = false) ⇒ Object
- #list ⇒ Object
- #load ⇒ Object
- #simplified? ⇒ Boolean
Constructor Details
#initialize(*args) ⇒ Object
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 132 133 |
# File 'ext/filemagic/filemagic.c', line 103
static VALUE
rb_magic_init(int argc, VALUE *argv, VALUE self) {
VALUE flags, options, keys, k, m;
ID mid;
int i;
if (rb_scan_args(argc, argv, "11", &flags, &options) == 1) {
options = rb_hash_new();
}
rb_iv_set(self, "iflags", flags);
rb_iv_set(self, "closed", Qfalse);
rb_iv_set(self, "@simplified", Qfalse);
keys = rb_funcall(options, rb_intern("keys"), 0);
for (i = 0; i < RARRAY_LEN(keys); i++) {
k = rb_ary_entry(keys, i);
m = rb_str_plus(rb_obj_as_string(k), rb_str_new2("="));
if (rb_respond_to(self, mid = rb_intern(StringValueCStr(m)))) {
rb_funcall(self, mid, 1, rb_hash_aref(options, k));
}
else {
k = rb_funcall(k, rb_intern("inspect"), 0);
rb_raise(rb_eArgError, "illegal option: %s", StringValueCStr(k));
}
}
return Qnil;
}
|
Instance Attribute Details
#simplified=(value) ⇒ Object (writeonly)
Sets the attribute simplified
136 137 138 |
# File 'lib/filemagic.rb', line 136 def simplified=(value) @simplified = value end |
Class Method Details
.clear! ⇒ Object
Clear our instance cache.
84 85 86 |
# File 'lib/filemagic.rb', line 84 def clear! @fm.each_value(&:close).clear end |
.flags(flags) ⇒ Object
Converts flags to integer
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'ext/filemagic/filemagic.c', line 21
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) ⇒ Object
Provide a “magic singleton”.
73 74 75 76 77 78 79 80 81 |
# File 'lib/filemagic.rb', line 73 def fm(*flags) = flags.last.is_a?(Hash) ? flags.pop : {} if fm = @fm[key = [flags = flags(flags), ]] return fm unless fm.closed? end @fm[key] = new(flags, ) end |
.library_version ⇒ Object
Returns the magic version
4 5 6 7 8 9 10 11 |
# File 'ext/filemagic/filemagic.c', line 4 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) ⇒ Object
109 110 111 112 113 114 |
# File 'lib/filemagic.rb', line 109 def magic_version(default = MAGIC_VERSION) default != '0' ? default : user_magic_version || auto_magic_version || [default, 'unknown'] end |
.mime(*flags, &block) ⇒ Object
Just a short-cut to #open with the mime
flag set.
105 106 107 |
# File 'lib/filemagic.rb', line 105 def mime(*flags, &block) open(:mime, *flags, &block) end |
.new(*args) ⇒ Object
FileMagic.new
63 64 65 66 67 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 |
# File 'ext/filemagic/filemagic.c', line 63
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) ⇒ Object
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.
90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/filemagic.rb', line 90 def open(*flags) fm = new(*flags) if block_given? begin yield fm ensure fm.close end else fm end end |
.path ⇒ Object
Returns the magic path
14 15 16 17 18 |
# File 'ext/filemagic/filemagic.c', line 14 static VALUE rb_magic_getpath(VALUE klass) { const char *path = magic_getpath(NULL, 0); return path != NULL ? rb_str_new2(path) : Qnil; } |
Instance Method Details
#buffer ⇒ Object
#check ⇒ Object
#close ⇒ Object
Frees resources allocated
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'ext/filemagic/filemagic.c', line 136
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? ⇒ Boolean
155 156 157 158 |
# File 'ext/filemagic/filemagic.c', line 155 static VALUE rb_magic_closed_p(VALUE self) { return rb_attr_get(self, rb_intern("closed")); } |
#compile ⇒ Object
#descriptor ⇒ Object
#fd(fd) ⇒ Object
149 150 151 |
# File 'lib/filemagic.rb', line 149 def fd(fd) descriptor(fd.respond_to?(:fileno) ? fd.fileno : fd) end |
#file ⇒ Object
#flags ⇒ Object
#flags=(flags) ⇒ Object
Set flags on the ms object
185 186 187 188 189 190 191 192 193 194 195 |
# File 'ext/filemagic/filemagic.c', line 185
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 ⇒ Object
153 154 155 |
# File 'lib/filemagic.rb', line 153 def inspect super.insert(-2, closed? ? ' (closed)' : '') end |
#io(io, length = 8, rewind = false) ⇒ Object
142 143 144 145 146 147 |
# File 'lib/filemagic.rb', line 142 def io(io, length = 8, rewind = false) pos = io.pos if rewind buffer(io.read(length)) ensure io.pos = pos if pos end |
#list ⇒ Object
#load ⇒ Object
#simplified? ⇒ Boolean
138 139 140 |
# File 'lib/filemagic.rb', line 138 def simplified? @simplified end |