Class: Magic
- Inherits:
-
Object
- Object
- Magic
- Defined in:
- lib/magic.rb,
lib/magic/version.rb,
ext/magic/ruby-magic.c
Overview
:startdoc:
Defined Under Namespace
Classes: Error, FlagsError, LibraryError, MagicError, NotImplementedError
Constant Summary collapse
- VERSION =
Current version of Magic.
'0.0.1'- NONE =
No special handling and/or flags specified. Default behaviour.
INT2NUM(MAGIC_NONE)
- DEBUG =
Print debugging messages to standard error output.
INT2NUM(MAGIC_DEBUG)
- SYMLINK =
If the file queried is a symbolic link, follow it.
INT2NUM(MAGIC_SYMLINK)
- COMPRESS =
If the file is compressed, unpack it and look at the contents.
INT2NUM(MAGIC_COMPRESS)
- DEVICES =
If the file is a block or character special device, then open the device and try to look at the contents.
INT2NUM(MAGIC_DEVICES)
- MIME_TYPE =
Return a MIME type string, instead of a textual description.
INT2NUM(MAGIC_MIME_TYPE)
- CONTINUE =
Return all matches, not just the first.
INT2NUM(MAGIC_CONTINUE)
- CHECK =
Check the Magic database for consistency and print warnings to standard error output.
INT2NUM(MAGIC_CHECK)
- PRESERVE_ATIME =
Attempt to preserve access time (atime, utime or utimes) of the file queried on systems that support such system calls.
INT2NUM(MAGIC_PRESERVE_ATIME)
- RAW =
Do not translate unprintable characters to an octal representation.
INT2NUM(MAGIC_RAW)
- ERROR =
Treat operating system errors while trying to open files and follow symbolic links as first class errors, instead of storing them in the Magic library error buffer for retrieval later.
INT2NUM(MAGIC_ERROR)
- MIME_ENCODING =
Return a MIME encoding, instead of a textual description.
INT2NUM(MAGIC_MIME_ENCODING)
- MIME =
A shorthand for using MIME_TYPE and MIME_ENCODING flags together.
INT2NUM(MAGIC_MIME)
- APPLE =
Return the Apple creator and type.
INT2NUM(MAGIC_APPLE)
- NO_CHECK_COMPRESS =
Do not look for, or inside compressed files.
INT2NUM(MAGIC_NO_CHECK_COMPRESS)
- NO_CHECK_TAR =
Do not look for, or inside tar archive files.
INT2NUM(MAGIC_NO_CHECK_TAR)
- NO_CHECK_SOFT =
Do not consult Magic files.
INT2NUM(MAGIC_NO_CHECK_SOFT)
- NO_CHECK_APPTYPE =
Check for EMX application type (only supported on EMX).
INT2NUM(MAGIC_NO_CHECK_APPTYPE)
- NO_CHECK_ELF =
Do not check for ELF files (do not examine ELF file details).
INT2NUM(MAGIC_NO_CHECK_ELF)
- NO_CHECK_TEXT =
Do not check for various types of text files.
INT2NUM(MAGIC_NO_CHECK_TEXT)
- NO_CHECK_CDF =
Do not check for CDF files.
INT2NUM(MAGIC_NO_CHECK_CDF)
- NO_CHECK_TOKENS =
Do not look for known tokens inside ASCII files.
INT2NUM(MAGIC_NO_CHECK_TOKENS)
- NO_CHECK_ENCODING =
Return a MIME encoding, instead of a textual description.
INT2NUM(MAGIC_NO_CHECK_ENCODING)
- NO_CHECK_BUILTIN =
Do not use built-in tests; only consult the Magic file.
INT2NUM(MAGIC_NO_CHECK_BUILTIN)
- NO_CHECK_ASCII =
Do not check for various types of text files, same as NO_CHECK_TEXT.
INT2NUM(MAGIC_NO_CHECK_ASCII)
- NO_CHECK_FORTRAN =
Do not look for Fortran sequences inside ASCII files.
INT2NUM(MAGIC_NO_CHECK_FORTRAN)
- NO_CHECK_TROFF =
Do not look for troff sequences inside ASCII files.
INT2NUM(MAGIC_NO_CHECK_TROFF)
Class Method Summary collapse
-
.check(path) ⇒ Object
call-seq: Magic.check( path, … ) -> true or false Magic.check( array ) -> true or false.
-
.compile(path) ⇒ Object
call-seq: Magic.compile( path, … ) -> true Magic.compile( array ) -> true.
-
.encoding(&block) ⇒ Object
call-seq: Magic.encoding -> self Magic.encoding {|magic| block } -> string or array.
-
.mime(&block) ⇒ Object
call-seq: Magic.mime -> self Magic.mime {|magic| block } -> string or array.
-
.open(flags = Magic::NONE) ⇒ Object
call-seq: Magic.open( flags ) -> self Magic.open( flags ) {|magic| block } -> string or array.
-
.type(&block) ⇒ Object
call-seq: Magic.type -> self Magic.type {|magic| block } -> string or array.
-
.version ⇒ Integer
Returns.
-
.version_to_a ⇒ Object
(also: version_array)
call-seq: Magic.version_to_a -> array.
-
.version_to_s ⇒ Object
(also: version_string)
call-seq: Magic.version_to_s -> string.
Instance Method Summary collapse
-
#buffer(string) ⇒ String, Array
Returns.
-
#check(arguments) ⇒ Object
Example:.
-
#close ⇒ nil
Closes the underlying Magic database.
-
#closed? ⇒ Boolean
Returns
trueif the underlying Magic database is open, orfalseotherwise. -
#compile(arguments) ⇒ Object
Example:.
-
#descriptor(integer) ⇒ String, Array
Returns.
-
#file(path) ⇒ String, Array
Returns.
-
#flags ⇒ Integer
Returns.
-
#flags=(integer) ⇒ Integer
Example:.
-
#flags_to_a(names = false) ⇒ Object
(also: #flags_array)
call-seq: magic.flags_to_a( names ) -> array.
-
#initialize(arguments) ⇒ Object
constructor
Opens the underlying Magic database and returns a new Magic.
-
#inspect ⇒ Object
call-seq: magic.inspect -> string.
-
#load(arguments) ⇒ Object
Example:.
-
#path ⇒ Array
Returns an
array.
Constructor Details
#new ⇒ self #new(path, ...) ⇒ self #new(array) ⇒ self
Opens the underlying Magic database and returns a new Magic.
Example:
magic = Magic.new #=> #<Magic:0x007f8fdc012e58>
Will raise Magic::LibraryError exception if, or Magic::MagicError exception if, or
See also: Magic::open, Magic::mime, Magic::type, Magic::encoding, Magic::compile and Magic::check
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 |
# File 'ext/magic/ruby-magic.c', line 83
VALUE
rb_mgc_initialize(VALUE object, VALUE arguments)
{
VALUE mutex = Qnil;
magic_arguments_t ma;
const char *klass = NULL;
if (rb_block_given_p()) {
klass = "Magic";
if (!NIL_P(object)) {
klass = rb_class2name(CLASS_OF(object));
}
rb_warn("%s::new() does not take block; use %s::open() instead",
klass, klass);
}
mutex = rb_class_new_instance(0, 0, rb_const_get(rb_cObject,
rb_intern("Mutex")));
rb_ivar_set(object, id_at_mutex, mutex);
MAGIC_COOKIE(ma.cookie);
ma.flags = MAGIC_NONE;
ma.data.file.path = NULL;
rb_ivar_set(object, id_at_flags, INT2NUM(ma.flags));
rb_mgc_load(object, arguments);
return object;
}
|
Class Method Details
.check(path) ⇒ Object
call-seq:
Magic.check( path, ... ) -> true or false
Magic.check( array ) -> true or false
Returns
Example:
See also: Magic::open, Magic::mime, Magic::type, Magic::encoding and Magic::compile
181 182 183 |
# File 'lib/magic.rb', line 181 def check(path) open {|m| m.check(path) } end |
.compile(path) ⇒ Object
call-seq:
Magic.compile( path, ... ) -> true
Magic.compile( array ) -> true
Returns
Example:
See also: Magic::open, Magic::mime, Magic::type, Magic::encoding, and Magic::check
166 167 168 |
# File 'lib/magic.rb', line 166 def compile(path) open {|m| m.compile(path) } end |
.encoding(&block) ⇒ Object
call-seq:
Magic.encoding -> self
Magic.encoding {|magic| block } -> string or array
Returns
Example:
See also: Magic::open, Magic::mime, Magic::type, Magic::compile and Magic::check
151 152 153 |
# File 'lib/magic.rb', line 151 def encoding(&block) open(Magic::MIME_ENCODING, &block) end |
.mime(&block) ⇒ Object
call-seq:
Magic.mime -> self
Magic.mime {|magic| block } -> string or array
Returns
Example:
See also: Magic::open, Magic::type, Magic::encoding, Magic::compile and Magic::check
121 122 123 |
# File 'lib/magic.rb', line 121 def mime(&block) open(Magic::MIME, &block) end |
.open(flags = Magic::NONE) ⇒ Object
call-seq:
Magic.open( flags ) -> self
Magic.open( flags ) {|magic| block } -> string or array
Returns
Example:
See also: Magic::mime, Magic::type, Magic::encoding, Magic::compile and Magic::check
95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/magic.rb', line 95 def open(flags = Magic::NONE) magic = Magic.new magic.flags = flags if block_given? begin yield magic ensure magic.close end else magic end end |
.type(&block) ⇒ Object
call-seq:
Magic.type -> self
Magic.type {|magic| block } -> string or array
Returns
Example:
See also: Magic::open, Magic::mime, Magic::encoding, Magic::compile and Magic::check
136 137 138 |
# File 'lib/magic.rb', line 136 def type(&block) open(Magic::MIME_TYPE, &block) end |
.version ⇒ Integer
Returns
Example:
Magic.version #=> 517
Will raise Magic::NotImplementedError exception if, or
See also: Magic::version_to_a and Magic::version_to_s
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 |
# File 'ext/magic/ruby-magic.c', line 540
VALUE
rb_mgc_version(VALUE object)
{
int rv;
int local_errno;
UNUSED(object);
rv = magic_version_wrapper();
local_errno = errno;
if (rv < 0 && local_errno == ENOSYS) {
MAGIC_GENERIC_ERROR(rb_mgc_eNotImplementedError, ENOSYS,
error(E_NOT_IMPLEMENTED));
}
return INT2NUM(rv);
}
|
.version_to_a ⇒ Object Also known as: version_array
call-seq:
Magic.version_to_a -> array
Returns
Example:
Magic.version_to_a #=> [5, 17]
Will raise Magic::NotImplementedError exception if, or
See also: Magic::version_to_s
46 47 48 |
# File 'lib/magic/version.rb', line 46 def version_to_a [self.version / 100, self.version % 100] end |
.version_to_s ⇒ Object Also known as: version_string
call-seq:
Magic.version_to_s -> string
Returns
Example:
Magic.version_to_s #=> "5.17"
Will raise Magic::NotImplementedError exception if, or
See also: Magic::version_to_a
64 65 66 |
# File 'lib/magic/version.rb', line 64 def version_to_s '%d.%02d' % self.version_to_a end |
Instance Method Details
#buffer(string) ⇒ String, Array
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 |
# File 'ext/magic/ruby-magic.c', line 466
VALUE
rb_mgc_buffer(VALUE object, VALUE value)
{
magic_arguments_t ma;
const char *cstring = NULL;
Check_Type(value, T_STRING);
CHECK_MAGIC_OPEN(object);
MAGIC_COOKIE(ma.cookie);
ma.flags = NUM2INT(rb_mgc_get_flags(object));
ma.data.buffer.size = RSTRING_LEN(value);
ma.data.buffer.buffer = RVAL2CSTR(value);
cstring = (const char *)MAGIC_SYNCHRONIZED(magic_buffer_internal, &ma);
if (!cstring) {
MAGIC_LIBRARY_ERROR(ma.cookie);
}
return magic_return(CSTR2RVAL(cstring), &ma);
}
|
#check ⇒ Boolean #check(path, ...) ⇒ Boolean #check(array) ⇒ Boolean
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'ext/magic/ruby-magic.c', line 376
VALUE
rb_mgc_check(VALUE object, VALUE arguments)
{
magic_arguments_t ma;
VALUE value = Qnil;
CHECK_MAGIC_OPEN(object);
MAGIC_COOKIE(ma.cookie);
if (!RARRAY_EMPTY_P(arguments)) {
value = magic_join(arguments, CSTR2RVAL(":"));
}
else {
value = magic_join(rb_mgc_get_path(object), CSTR2RVAL(":"));
}
ma.flags = NUM2INT(rb_mgc_get_flags(object));
ma.data.file.path = RVAL2CSTR(value);
if (!MAGIC_SYNCHRONIZED(magic_check_internal, &ma)) {
return Qfalse;
}
return Qtrue;
}
|
#close ⇒ nil
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'ext/magic/ruby-magic.c', line 131
VALUE
rb_mgc_close(VALUE object)
{
magic_t cookie;
MAGIC_COOKIE(cookie);
if (cookie) {
MAGIC_SYNCHRONIZED(magic_close_internal, cookie);
if (DATA_P(object)) {
DATA_PTR(object) = NULL;
}
}
return Qnil;
}
|
#closed? ⇒ Boolean
165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'ext/magic/ruby-magic.c', line 165
VALUE
rb_mgc_closed(VALUE object)
{
magic_t cookie;
MAGIC_COOKIE(cookie);
if (DATA_P(object) && DATA_PTR(object) && cookie) {
return Qfalse;
}
return Qtrue;
}
|
#compile ⇒ true #compile(path, ...) ⇒ true #compile(array) ⇒ true
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
# File 'ext/magic/ruby-magic.c', line 336
VALUE
rb_mgc_compile(VALUE object, VALUE arguments)
{
magic_arguments_t ma;
VALUE value = Qnil;
CHECK_MAGIC_OPEN(object);
MAGIC_COOKIE(ma.cookie);
if (!RARRAY_EMPTY_P(arguments)) {
value = magic_join(arguments, CSTR2RVAL(":"));
}
else {
value = magic_join(rb_mgc_get_path(object), CSTR2RVAL(":"));
}
ma.flags = NUM2INT(rb_mgc_get_flags(object));
ma.data.file.path = RVAL2CSTR(value);
if (!MAGIC_SYNCHRONIZED(magic_compile_internal, &ma)) {
MAGIC_LIBRARY_ERROR(ma.cookie);
}
return Qtrue;
}
|
#descriptor(integer) ⇒ String, Array
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 |
# File 'ext/magic/ruby-magic.c', line 504
VALUE
rb_mgc_descriptor(VALUE object, VALUE value)
{
magic_arguments_t ma;
const char *cstring = NULL;
Check_Type(value, T_FIXNUM);
CHECK_MAGIC_OPEN(object);
MAGIC_COOKIE(ma.cookie);
ma.flags = NUM2INT(rb_mgc_get_flags(object));
ma.data.file.fd = NUM2INT(value);
cstring = (const char *)MAGIC_SYNCHRONIZED(magic_descriptor_internal, &ma);
if (!cstring) {
MAGIC_LIBRARY_ERROR(ma.cookie);
}
return magic_return(CSTR2RVAL(cstring), &ma);
}
|
#file(path) ⇒ String, Array
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
# File 'ext/magic/ruby-magic.c', line 416
VALUE
rb_mgc_file(VALUE object, VALUE value)
{
int rv;
magic_arguments_t ma;
const char *cstring = NULL;
const char *empty = "(null)";
Check_Type(value, T_STRING);
CHECK_MAGIC_OPEN(object);
MAGIC_COOKIE(ma.cookie);
ma.flags = NUM2INT(rb_mgc_get_flags(object));
ma.data.file.path = RVAL2CSTR(value);
cstring = (const char *)MAGIC_SYNCHRONIZED(magic_file_internal, &ma);
if (!cstring) {
rv = magic_version_wrapper();
if (ma.flags & MAGIC_ERROR) {
MAGIC_LIBRARY_ERROR(ma.cookie);
}
else if (rv < 515) {
(void)magic_errno(ma.cookie);
cstring = magic_error(ma.cookie);
}
}
assert(cstring != NULL && "Must be a valid pointer to `const char' type");
assert(strncmp(cstring, empty, strlen(empty)) != 0 && \
"Empty or invalid result");
return magic_return(CSTR2RVAL(cstring), &ma);
}
|
#flags ⇒ Integer
226 227 228 229 230 231 |
# File 'ext/magic/ruby-magic.c', line 226
VALUE
rb_mgc_get_flags(VALUE object)
{
CHECK_MAGIC_OPEN(object);
return rb_ivar_get(object, id_at_flags);
}
|
#flags=(integer) ⇒ Integer
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'ext/magic/ruby-magic.c', line 247
VALUE
rb_mgc_set_flags(VALUE object, VALUE value)
{
int local_errno;
magic_t cookie;
Check_Type(value, T_FIXNUM);
CHECK_MAGIC_OPEN(object);
MAGIC_COOKIE(cookie);
if (magic_setflags_wrapper(cookie, NUM2INT(value)) < 0) {
local_errno = errno;
switch (local_errno) {
case EINVAL:
MAGIC_GENERIC_ERROR(rb_mgc_eFlagsError, EINVAL,
error(E_FLAG_INVALID_VALUE));
break;
case ENOSYS:
MAGIC_GENERIC_ERROR(rb_mgc_eNotImplementedError, ENOSYS,
error(E_FLAG_NOT_IMPLEMENTED));
break;
default:
MAGIC_LIBRARY_ERROR(cookie);
break;
}
}
return rb_ivar_set(object, id_at_flags, value);
}
|
#flags_to_a(names = false) ⇒ Object Also known as: flags_array
call-seq:
magic.flags_to_a( names ) -> array
Returns an array
Example:
Will raise Magic::LibraryError exception if, or
See also: Magic#flags
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/magic.rb', line 63 def flags_to_a(names = false) raise LibraryError, "Magic library is not open" if closed? n, i = 0, @flags flags = [] return [names ? 'NONE' : 0] if @flags.zero? @@flags_map ||= flags_as_map if names while i > 0 n = 2 ** (Math.log(i) / Math.log(2)).to_i i = i - n flags.insert(0, names ? @@flags_map[n] : n) end flags end |
#inspect ⇒ Object
47 48 49 |
# File 'lib/magic.rb', line 47 def inspect super.insert(-2, self.closed? ? ' (closed)' : '') end |
#load ⇒ Array #load(path, ...) ⇒ Array #load(array) ⇒ Array
Example:
magic = Magic.new #=> #<Magic:0x007f8fdc012e58>
magic.load #=> ["/etc/magic", "/usr/share/misc/magic"]
magic.load("/usr/share/misc/magic", "/etc/magic") #=> ["/usr/share/misc/magic", "/etc/magic"]
magic.load #=> ["/etc/magic", "/usr/share/misc/magic"]
Will raise Magic::LibraryError exception if, or
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 |
# File 'ext/magic/ruby-magic.c', line 294
VALUE
rb_mgc_load(VALUE object, VALUE arguments)
{
magic_arguments_t ma;
VALUE value = Qnil;
CHECK_MAGIC_OPEN(object);
MAGIC_COOKIE(ma.cookie);
if (!RARRAY_EMPTY_P(arguments) && !NIL_P(RARRAY_FIRST(arguments))) {
value = magic_join(arguments, CSTR2RVAL(":"));
ma.data.file.path = RVAL2CSTR(value);
}
else {
ma.data.file.path = magic_getpath_wrapper();
}
ma.flags = NUM2INT(rb_mgc_get_flags(object));
if (!MAGIC_SYNCHRONIZED(magic_load_internal, &ma)) {
MAGIC_LIBRARY_ERROR(ma.cookie);
}
value = magic_split(CSTR2RVAL(ma.data.file.path), CSTR2RVAL(":"));
return rb_ivar_set(object, id_at_path, value);
}
|
#path ⇒ Array
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'ext/magic/ruby-magic.c', line 192
VALUE
rb_mgc_get_path(VALUE object)
{
VALUE value = Qnil;
const char *cstring = NULL;
CHECK_MAGIC_OPEN(object);
value = rb_ivar_get(object, id_at_path);
if (!NIL_P(value) && !RARRAY_EMPTY_P(value) && !getenv("MAGIC")) {
return value;
}
cstring = magic_getpath_wrapper();
value = magic_split(CSTR2RVAL(cstring), CSTR2RVAL(":"));
return rb_ivar_set(object, id_at_path, value);
}
|