Class: Magikku

Inherits:
Object
  • Object
show all
Defined in:
ext/magikku_native/magikku_native.c

Defined Under Namespace

Modules: Flags Classes: ClosedError, CompileError, DbLoadError, FlagError, InitFatal, MagikkuError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(params = {}) ⇒ Object

Instantiates a new Magikku cookie which we can use to load magic databases, compile new databases, and identify files and string buffers.

Parameters:

  • Hash

    params A hash of optional parameters to pass to the initializer.

Raises:

  • Magikku::DbLoadError An error is raised if the database cannot be loaded.

  • Magikku::InitFatal An error is raised if an unknown error is encountered initializing the cookie with libmagic.

See Also:



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
# File 'ext/magikku_native/magikku_native.c', line 68

VALUE 
rb_magic_initialize(int argc, VALUE *argv, VALUE klass) {
  VALUE params = Qnil;
  VALUE flags_val, db_val;
  magic_t cookie; 
  int flags = 0;
  char *magicf = NULL;
  const char *error=NULL;

  rb_scan_args(argc, argv, "01", &params);

  if (params != Qnil) {
    Check_Type(params, T_HASH);

    flags_val=rb_hash_aref(params, ID2SYM(rb_intern("flags")));
    if (flags_val != Qnil) {
      Check_Type(flags_val, T_FIXNUM);
      flags = NUM2INT(flags_val);
    }

    db_val=rb_hash_aref(params, ID2SYM(rb_intern("db")));
    if (db_val != Qnil) {
      Check_Type(db_val, T_STRING);
      magicf = RSTRING_PTR(db_val);
    }
  }

  if ((cookie=magic_open(flags))==NULL)
    rb_raise(e_InitFatal, "magic_open(%i) returned a null pointer", flags);

  if (magic_load(cookie, magicf) != 0) {
    error = magic_error(cookie);
    magic_close(cookie);
    rb_raise(e_DbLoadError, "Error loading db \"%s\": %s", magicf, error);
  }

  return Data_Wrap_Struct(klass, NULL, NULL, cookie);
}

.pathObject

Returns String Returns the default magic database path.

Returns:

  • String Returns the default magic database path.



39
40
41
42
# File 'ext/magikku_native/magikku_native.c', line 39

VALUE 
rb_magic_s_path(VALUE klass) {
  return rb_str_new2(magic_getpath(NULL, 0));
}

Instance Method Details

#check_syntax("some_magic_db") ⇒ Object

Can be used to check the validity of magic files before compiling them. This is basically a dry-run that can be used before compiling magicfile databases.

Note: Errors and warnings may be displayed on stderr.

Parameters:

  • String,nil

    filename A colon seperated list of filenames or a single file. nil checks the default database.

Returns:

  • true,false Indicates whether the check was successful.



277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'ext/magikku_native/magikku_native.c', line 277

VALUE rb_magic_check_syntax(VALUE self, VALUE rb_magicf) {
  magic_t cookie = _check_closed(self);
  char * magicf = NULL;

  if (!cookie) rb_raise(e_ClosedError, CLOSED_ERR_MSG);
  if (rb_magicf != Qnil) {
    Check_Type(rb_magicf, T_STRING);
    magicf = RSTRING_PTR(rb_magicf);
  }

  if (magic_check(cookie, magicf) == 0) return Qtrue;
  else return Qfalse;
}

#closenil

Close the libmagic data scanner handle when you are finished with it

Note that magic handles are never closed automatically, even when garbage collection occurs.

Returns:

  • (nil)


150
151
152
153
154
155
156
157
158
# File 'ext/magikku_native/magikku_native.c', line 150

VALUE 
rb_magic_close(VALUE self) {
  magic_t cookie = _check_closed(self);

  if(cookie) magic_close(cookie);
  rb_iv_set(self, "@closed", Qtrue);

  return Qnil;
}

#closed?Boolean

Indicates whether the magic cookie has been closed

Returns:

  • (Boolean)

    true,false



166
167
168
169
170
171
# File 'ext/magikku_native/magikku_native.c', line 166

VALUE 
rb_magic_is_closed(VALUE self) {
  VALUE ret = rb_iv_get(self, "@closed");
  if (ret == Qnil) ret = Qfalse;
  return ret;
}

#compile(filename = nil) ⇒ Object

Can be used to compile magic files. This does not load files, however. You must use dbload for that.

Note: Errors and warnings may be displayed on stderr.

Parameters:

  • String,nil

    filename A colon seperated list of filenames or a single filename. The compiled files created are generated in the current directory using the basename(1) of each file argument with “.mgc” appended to it. Directory names can be compiled, in which case the contents of the directory will be compiled together as a single .mgc file. nil compiles the default database.

Returns:

  • true if everything went well.

Raises:

  • CompileError if an error occurred.



249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'ext/magikku_native/magikku_native.c', line 249

VALUE rb_magic_compile(VALUE self, VALUE magicf) {
  char *_magicf;
  magic_t cookie = _check_closed(self);

  if (!cookie)          rb_raise(e_ClosedError, CLOSED_ERR_MSG);
  if (magicf != Qnil)   Check_Type(magicf, T_STRING);

  _magicf = RSTRING_PTR(magicf);

  if (magic_compile(cookie, _magicf) == 0) return Qtrue;
  else rb_raise(e_CompileError, 
        "Error compiling \"%s\": %s", _magicf, magic_error(cookie));
}

#dbload(magicfiles) ⇒ true

Used to load one or more magic databases.

Returns:

  • (true)

Parameters:

  • String

    magicfiles One or more filenames seperated by colons. If nil, the default database is loaded. If uncompiled magic files are specified, they are compiled on the fly but they do not generate new .mgc files as with the compile method. Multiple files be specified by seperating them with colons.

Raises:

  • DbLoadError if an error occurred loading the database(s)

  • Magikku::CloseError Raises an error if the Magikku object has been closed.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'ext/magikku_native/magikku_native.c', line 124

VALUE 
rb_magic_dbload(VALUE self, VALUE magicf_val) {
  magic_t cookie = _check_closed(self);
  char *magicf = NULL;

  if(!cookie)             rb_raise(e_ClosedError, CLOSED_ERR_MSG);
  if(magicf_val != Qnil){
    Check_Type(magicf_val, T_STRING);
    magicf = RSTRING_PTR(magicf_val);
  }

  Data_Get_Struct(self, void, cookie);

  if (magic_load(cookie, magicf) != 0)
    rb_raise(e_DbLoadError, "Error loading db \"%s\": %s", magicf, magic_error(cookie));

  return Qtrue;
}

#file(filename) ⇒ String

Identifies file contents using the magicfile database.

Returns:

  • (String)

Parameters:

  • String

    filename The path to the file to analyze

Returns:

  • String A textual description of the contents of the file

Raises:

  • Magikku::CloseError Raises an error if the Magikku object has been closed.



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'ext/magikku_native/magikku_native.c', line 186

VALUE 
rb_magic_file(VALUE self, VALUE filename) {
  magic_t cookie = _check_closed(self);
  if (cookie) {
    char * fname;
    struct stat st;

    Check_Type(filename, T_STRING);
    fname = RSTRING_PTR(filename);

    if(stat(fname, &st) < 0) 
      rb_sys_fail(fname);

    return rb_str_new2(magic_file(cookie, fname));

  } else {
    rb_raise(e_ClosedError, CLOSED_ERR_MSG);
  }
      
}

#flags=(flags) ⇒ Object

Sets libmagic flags on the object. See Magikku::Flags

Raises:

  • Magikku::CloseError Raises an error if the Magikku object has been closed.



298
299
300
301
302
303
304
305
306
307
# File 'ext/magikku_native/magikku_native.c', line 298

VALUE rb_magic_set_flags(VALUE self, VALUE flags) {
  magic_t cookie = _check_closed(self);

  Check_Type(flags, T_FIXNUM);
  
  if(magic_setflags(cookie, NUM2INT(flags)) < 0)
    rb_raise(e_FlagError, magic_error(cookie));

  return flags;
}

#string(buf) ⇒ String

Identifies string contents using the magicfile database.

Returns:

  • (String)

Parameters:

  • String

    buf The string to analyze.

Returns:

  • String A textual description of the contents of the string

Raises:

  • Magikku::CloseError Raises an error if the Magikku object has been closed.



220
221
222
223
224
225
226
227
228
# File 'ext/magikku_native/magikku_native.c', line 220

VALUE rb_magic_string(VALUE self, VALUE string) {
  const char *ret;
  magic_t cookie = _check_closed(self);
  if (!cookie)          rb_raise(e_ClosedError, CLOSED_ERR_MSG);
  Check_Type(string, T_STRING);

  ret=magic_buffer(cookie, RSTRING_PTR(string), RSTRING_LEN(string));
  return rb_str_new2(ret);
}