Class: FileMagic

Inherits:
Object
  • Object
show all
Defined in:
lib/filemagic.rb,
lib/filemagic/version.rb,
ext/filemagic/filemagic.c

Defined Under Namespace

Modules: Ext, Version Classes: FileMagicError

Constant Summary

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 only the MIME type
  :continue,           # Return all matches
  :check,              # Print warnings to stderr
  :preserve_atime,     # Restore access time on exit
  :raw,                # Don't translate unprint chars
  :error,              # Handle ENOENT etc as real errors
  :mime_encoding,      # Return only the MIME encoding
  :mime,               # MAGIC_MIME_TYPE | MAGIC_MIME_ENCODING
  :apple,              # Return the Apple creator and type
  :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 ascii/tokens
  :no_check_encoding,  # Don't check text encodings
  :no_check_ascii,     # MAGIC_NO_CHECK_TEXT
  :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)
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_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_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)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Object) initialize



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'ext/filemagic/filemagic.c', line 51

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, "_flags", flags);
  rb_iv_set(self, "closed", Qfalse);

  keys = rb_funcall(options, rb_intern("keys"), 0);

  for (i = 0; i < RARRAY_LEN(keys); i++) {
    k = rb_funcall(keys, rb_intern("[]"), 1, INT2FIX(i));
    m = rb_funcall(rb_funcall(k, rb_intern("to_s"), 0),
      rb_intern("+"), 1, 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

- (Object) simplified=(value) (writeonly)

Sets the attribute simplified

Parameters:

  • value

    the value to set the attribute simplified to.



86
87
88
# File 'lib/filemagic.rb', line 86

def simplified=(value)
  @simplified = value
end

Class Method Details

+ (Object) clear!

Clear our instance cache.



58
59
60
61
# File 'lib/filemagic.rb', line 58

def clear!
  @fm.each_value { |fm| fm.close }
  @fm.clear
end

+ (Object) fm(*flags)

Provide a “magic singleton”.



52
53
54
55
# File 'lib/filemagic.rb', line 52

def fm(*flags)
  @fm.delete(flags.to_s) if @fm[flags].closed?
  @fm[flags]
end

+ (Object) mime(*flags, &block)

Just a short-cut to #open with the mime flag set.



80
81
82
# File 'lib/filemagic.rb', line 80

def mime(*flags, &block)
  open(:mime, *flags, &block)
end

+ (Object) new

FileMagic.new



19
20
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
# File 'ext/filemagic/filemagic.c', line 19

static VALUE
rb_magic_new(int argc, VALUE *argv, VALUE class) {
  VALUE obj, args[2];
  magic_t cookie;

  if (rb_block_given_p()) {
    rb_warn("FileMagic::new() does not take 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_to_int(rb_ary_new4(argc, argv));

  if ((cookie = magic_open(NUM2INT(args[0]))) == NULL) {
    rb_fatal("out of memory");
  }

  if (magic_load(cookie, NULL) == -1) {
    rb_fatal("%s", magic_error(cookie));
  }

  obj = Data_Wrap_Struct(class, 0, rb_magic_free, cookie);
  rb_obj_call_init(obj, 2, args);

  return obj;
}

+ (Object) open(*flags)

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.



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/filemagic.rb', line 65

def open(*flags)
  fm = new(*flags)

  if block_given?
    begin
      yield fm
    ensure
      fm.close
    end
  else
    fm
  end
end

Instance Method Details

- (Object) buffer

Return a string describing the string buffer



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'ext/filemagic/filemagic.c', line 129

static VALUE
rb_magic_buffer(int argc, VALUE *argv, VALUE self) {
  VALUE buffer, s;
  const char *m;
  magic_t cookie;

  rb_scan_args(argc, argv, "11", &buffer, &s);

  m = StringValuePtr(buffer);
  GetMagicCookie(self, cookie);

  if ((m = magic_buffer(cookie, m, RSTRING_LEN(buffer))) == NULL) {
    rb_raise(rb_FileMagicError, "%s", magic_error(cookie));
  }

  return rb_magic_apply_simple(self, m, s);
}

- (Object) check

Checks validity of a magic database file



179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'ext/filemagic/filemagic.c', line 179

static VALUE
rb_magic_check(int argc, VALUE *argv, VALUE self) {
  VALUE s;
  const char *file;
  int retval;
  magic_t cookie;

  file = rb_scan_args(argc, argv, "01", &s) == 1 ? StringValuePtr(s) : NULL;

  GetMagicCookie(self, cookie);
  retval = magic_check(cookie, file);

  return INT2FIX(retval);
}

- (Object) close

Frees resources allocated



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'ext/filemagic/filemagic.c', line 85

static VALUE
rb_magic_close(VALUE self) {
  magic_t cookie;

  if (RTEST(rb_magic_closed_p(self))) {
    return Qnil;
  }

  GetMagicCookie(self, cookie);
  magic_close(cookie);

  /* This keeps rb_magic_free from trying to free closed objects */
  RDATA(self)->data = NULL;

  rb_iv_set(self, "closed", Qtrue);

  return Qnil;
}

- (Boolean) closed?

Returns:

  • (Boolean)


104
105
106
107
# File 'ext/filemagic/filemagic.c', line 104

static VALUE
rb_magic_closed_p(VALUE self) {
  return rb_attr_get(self, rb_intern("closed"));
}

- (Object) compile

Compiles a magic database file



195
196
197
198
199
200
201
202
203
204
205
206
# File 'ext/filemagic/filemagic.c', line 195

static VALUE
rb_magic_compile(VALUE self, VALUE file) {
  int retval;
  const char *m;
  magic_t cookie;

  GetMagicCookie(self, cookie);
  m = StringValuePtr(file);
  retval = magic_compile(cookie, m);

  return INT2FIX(retval);
}

- (Object) file

Return a string describing file



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'ext/filemagic/filemagic.c', line 110

static VALUE
rb_magic_file(int argc, VALUE *argv, VALUE self) {
  VALUE file, s;
  const char *m;
  magic_t cookie;

  rb_scan_args(argc, argv, "11", &file, &s);

  m = StringValuePtr(file);
  GetMagicCookie(self, cookie);

  if ((m = magic_file(cookie, m)) == NULL) {
    rb_raise(rb_FileMagicError, "%s", magic_error(cookie));
  }

  return rb_magic_apply_simple(self, m, s);
}

- (Object) flags

Get the flags as array of symbols



148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'ext/filemagic/filemagic.c', line 148

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("_flags"))), 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;
}

- (Object) flags=

Set flags on the cookie object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'ext/filemagic/filemagic.c', line 163

static VALUE
rb_magic_setflags(VALUE self, VALUE flags) {
  int retval;
  magic_t cookie;

  flags = rb_Array(flags);
  flags = rb_magic_flags_to_int(flags);
  rb_iv_set(self, "_flags", flags);

  GetMagicCookie(self, cookie);
  retval = magic_setflags(cookie, NUM2INT(flags));

  return INT2FIX(retval);
}

- (Object) inspect



96
97
98
# File 'lib/filemagic.rb', line 96

def inspect
  super.insert(-2, closed? ? ' (closed)' : '')
end

- (Object) io(io, length = 8)



92
93
94
# File 'lib/filemagic.rb', line 92

def io(io, length = 8)
  buffer(io.read(length))
end

- (Boolean) simplified?

Returns:

  • (Boolean)


88
89
90
# File 'lib/filemagic.rb', line 88

def simplified?
  @simplified
end