ricardochimal-ruby-filemagic 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/AUTHORS +1 -0
- data/CHANGELOG +10 -0
- data/README +35 -0
- data/TODO +9 -0
- data/extconf.rb +8 -0
- data/filemagic.c +137 -0
- data/filemagic.rd +36 -0
- data/ruby-filemagic.gemspec +37 -0
- data/test/leaktest.rb +15 -0
- data/test/perl +19 -0
- data/test/pyfile +1 -0
- data/test/pyfile-compressed.gz +0 -0
- data/test/pylink +1 -0
- data/test/regress.rb +48 -0
- metadata +68 -0
data/AUTHORS
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
Travis Whitton <whitton@atlantic.net>
|
data/CHANGELOG
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
--------------------------------------------------------------------------------
|
2
|
+
7/28/2003 version 0.1.0
|
3
|
+
|
4
|
+
o Initial release.
|
5
|
+
|
6
|
+
--------------------------------------------------------------------------------
|
7
|
+
7/30/2003 version 0.1.1
|
8
|
+
|
9
|
+
o Added manual close method
|
10
|
+
o Added unit test suite
|
data/README
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
FileMagic Library Binding
|
2
|
+
|
3
|
+
June 27, 2008 Ricardo Chimal, Jr. <ricardo@heroku.com>
|
4
|
+
|
5
|
+
I needed a packaged gem version of this ruby binding so I stuck it in github and created a gemspec. Enjoy.
|
6
|
+
|
7
|
+
|
8
|
+
July 28, 2003 Travis Whitton <whitton@atlantic.net>
|
9
|
+
|
10
|
+
This is one of my first ruby bindings, so suggestions are welcome. See
|
11
|
+
filemagic.rd and test.rb for usage.
|
12
|
+
|
13
|
+
to install, run these commands:
|
14
|
+
|
15
|
+
ruby extconf.rb
|
16
|
+
make
|
17
|
+
su
|
18
|
+
make install
|
19
|
+
|
20
|
+
the file(1) library and headers are required.
|
21
|
+
I have file 0.4.3.
|
22
|
+
|
23
|
+
Enjoy!
|
24
|
+
Travis
|
25
|
+
|
26
|
+
[Copying]
|
27
|
+
The filemagic extension library is copywrited free software by Travis Whitton
|
28
|
+
<whitton@atlantic.net>. You can redistribute it under the terms specified in
|
29
|
+
the COPYING file of the Ruby distribution.
|
30
|
+
|
31
|
+
[WARRANTY]
|
32
|
+
THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
|
33
|
+
IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
34
|
+
WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
|
35
|
+
PURPOSE.
|
data/TODO
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
o Find a better way to handle magic_load than just defaulting to NULL
|
2
|
+
o Find a way to make magic_check work on NULL
|
3
|
+
o Find a way to make magic_setflags work
|
4
|
+
o Refactor code into initialize instead of new
|
5
|
+
o Enable blocks for automatic cleanup:
|
6
|
+
|
7
|
+
FileMagic.open("somefile.txt") do |file|
|
8
|
+
# close is called at the end of this block
|
9
|
+
end
|
data/extconf.rb
ADDED
data/filemagic.c
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
/*********************************************************
|
2
|
+
|
3
|
+
filemagic.c
|
4
|
+
|
5
|
+
a Ruby extension to bind Ruby to the libmagic library
|
6
|
+
(ftp://ftp.astron.com/pub/file/)
|
7
|
+
|
8
|
+
patterned very closely off of audiofile.c by jaredj which
|
9
|
+
is patterned very very closely off of gdbm.c by matz ;-)
|
10
|
+
|
11
|
+
$Author: twhitton $
|
12
|
+
$Date: 2003/07/30 13:25:10 $
|
13
|
+
|
14
|
+
*********************************************************/
|
15
|
+
|
16
|
+
#include "ruby.h"
|
17
|
+
#include <magic.h>
|
18
|
+
|
19
|
+
static VALUE cFileMagic, rb_FileMagicError;
|
20
|
+
|
21
|
+
#define GetMagicCookie(obj, cookie) {\
|
22
|
+
Data_Get_Struct(obj, struct magic_set, cookie);\
|
23
|
+
}
|
24
|
+
|
25
|
+
/*
|
26
|
+
GC never seems to happen until the program terminates, but this is called
|
27
|
+
on any unclosed objects
|
28
|
+
*/
|
29
|
+
static void magick_free(magic_t cookie)
|
30
|
+
{
|
31
|
+
magic_close(cookie);
|
32
|
+
}
|
33
|
+
|
34
|
+
/* This is mainly for subclassing */
|
35
|
+
static VALUE magick_init(VALUE self, VALUE flags)
|
36
|
+
{
|
37
|
+
/* This isn't used internally, but might as well store it for children */
|
38
|
+
rb_iv_set(self, "@flags", flags);
|
39
|
+
return self;
|
40
|
+
}
|
41
|
+
|
42
|
+
/* Frees resources allocated */
|
43
|
+
static VALUE magick_close(VALUE class)
|
44
|
+
{
|
45
|
+
magic_t cookie;
|
46
|
+
GetMagicCookie(class, cookie);
|
47
|
+
magic_close(cookie);
|
48
|
+
/* This keeps magick_free from trying to free closed objects */
|
49
|
+
RDATA(class)->data = NULL;
|
50
|
+
return Qnil;
|
51
|
+
}
|
52
|
+
|
53
|
+
/* FileMagic.new */
|
54
|
+
static VALUE magick_new(VALUE class, VALUE flags)
|
55
|
+
{
|
56
|
+
VALUE obj;
|
57
|
+
magic_t cookie;
|
58
|
+
cookie = magic_open(NUM2INT(flags));
|
59
|
+
if (cookie == NULL) {
|
60
|
+
rb_fatal("out of memory");
|
61
|
+
}
|
62
|
+
if (magic_load(cookie, NULL) == -1) {
|
63
|
+
rb_fatal("%s", magic_error(cookie));
|
64
|
+
}
|
65
|
+
rb_FileMagicError = rb_define_class("FileMagicError", rb_eStandardError);
|
66
|
+
obj = Data_Wrap_Struct(class, 0, magick_free, cookie);
|
67
|
+
rb_obj_call_init(obj, 1, &flags);
|
68
|
+
return obj;
|
69
|
+
}
|
70
|
+
|
71
|
+
/* Return a string describing file */
|
72
|
+
static VALUE magick_file(VALUE class, VALUE file)
|
73
|
+
{
|
74
|
+
const char *m;
|
75
|
+
magic_t cookie;
|
76
|
+
m = STR2CSTR(file);
|
77
|
+
GetMagicCookie(class, cookie);
|
78
|
+
if ((m = magic_file(cookie, m)) == NULL)
|
79
|
+
rb_raise(rb_FileMagicError, magic_error(cookie));
|
80
|
+
return rb_str_new2(m);
|
81
|
+
}
|
82
|
+
|
83
|
+
/* Return a string describing the string buffer */
|
84
|
+
static VALUE magick_buffer(VALUE class, VALUE buffer)
|
85
|
+
{
|
86
|
+
int i = RSTRING(buffer)->len;
|
87
|
+
const char *m;
|
88
|
+
magic_t cookie;
|
89
|
+
m = STR2CSTR(buffer);
|
90
|
+
GetMagicCookie(class, cookie);
|
91
|
+
if ((m = magic_buffer(cookie, m, i)) == NULL)
|
92
|
+
rb_raise(rb_FileMagicError, magic_error(cookie));
|
93
|
+
return rb_str_new2(m);
|
94
|
+
}
|
95
|
+
|
96
|
+
/* Checks validity of a magic database file */
|
97
|
+
static VALUE magick_check(VALUE class, VALUE file)
|
98
|
+
{
|
99
|
+
int retval;
|
100
|
+
const char *m;
|
101
|
+
magic_t cookie;
|
102
|
+
GetMagicCookie(class, cookie);
|
103
|
+
m = STR2CSTR(file);
|
104
|
+
retval = magic_check(cookie, m);
|
105
|
+
return INT2FIX(retval);
|
106
|
+
}
|
107
|
+
|
108
|
+
/* Compiles a magic database file */
|
109
|
+
static VALUE magick_compile(VALUE class, VALUE file)
|
110
|
+
{
|
111
|
+
int retval;
|
112
|
+
const char *m;
|
113
|
+
magic_t cookie;
|
114
|
+
GetMagicCookie(class, cookie);
|
115
|
+
m = STR2CSTR(file);
|
116
|
+
retval = magic_compile(cookie, m);
|
117
|
+
return INT2FIX(retval);
|
118
|
+
}
|
119
|
+
|
120
|
+
void Init_filemagic() {
|
121
|
+
cFileMagic = rb_define_class("FileMagic", rb_cObject);
|
122
|
+
rb_define_singleton_method(cFileMagic, "new", magick_new, 1);
|
123
|
+
rb_define_method(cFileMagic, "initialize", magick_init, 1);
|
124
|
+
rb_define_method(cFileMagic, "close", magick_close, 0);
|
125
|
+
rb_define_method(cFileMagic, "file", magick_file, 1);
|
126
|
+
rb_define_method(cFileMagic, "buffer", magick_buffer, 1);
|
127
|
+
rb_define_method(cFileMagic, "check", magick_check, 1);
|
128
|
+
rb_define_method(cFileMagic, "compile", magick_compile, 1);
|
129
|
+
rb_define_const(cFileMagic, "MAGIC_NONE", INT2FIX(MAGIC_NONE));
|
130
|
+
rb_define_const(cFileMagic, "MAGIC_DEBUG", INT2FIX(MAGIC_DEBUG));
|
131
|
+
rb_define_const(cFileMagic, "MAGIC_SYMLINK", INT2FIX(MAGIC_SYMLINK));
|
132
|
+
rb_define_const(cFileMagic, "MAGIC_COMPRESS", INT2FIX(MAGIC_COMPRESS));
|
133
|
+
rb_define_const(cFileMagic, "MAGIC_DEVICES", INT2FIX(MAGIC_DEVICES));
|
134
|
+
rb_define_const(cFileMagic, "MAGIC_MIME", INT2FIX(MAGIC_MIME));
|
135
|
+
rb_define_const(cFileMagic, "MAGIC_CONTINUE", INT2FIX(MAGIC_CONTINUE));
|
136
|
+
rb_define_const(cFileMagic, "MAGIC_CHECK", INT2FIX(MAGIC_CHECK));
|
137
|
+
}
|
data/filemagic.rd
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
=begin
|
2
|
+
|
3
|
+
= FileMagic extension module SEE ALSO libmagic(3), file(1) and magic(4)
|
4
|
+
== FileMagic
|
5
|
+
=== Constants
|
6
|
+
: MAGIC_NONE
|
7
|
+
No flags
|
8
|
+
: MAGIC_DEBUG
|
9
|
+
Turn on debuggin
|
10
|
+
: MAGIC_SYMLINK
|
11
|
+
Follow symlinks
|
12
|
+
: MAGIC_COMPRESS
|
13
|
+
Check inside compressed files
|
14
|
+
: MAGIC_DEVICES
|
15
|
+
Look at the contents of devices
|
16
|
+
: MAGIC_MIME
|
17
|
+
Return a mime string
|
18
|
+
: MAGIC_CONTINUE
|
19
|
+
Return all matches, not just the first
|
20
|
+
: MAGIC_CHECK
|
21
|
+
Print warnings to stderr
|
22
|
+
|
23
|
+
=== Methods
|
24
|
+
: file(filename)
|
25
|
+
returns a textual description of the contents of the filename argument
|
26
|
+
: buffer(string)
|
27
|
+
returns a textual description of the contents of the string argument
|
28
|
+
: check(filename)
|
29
|
+
checks the validity of entries in the colon separated database files
|
30
|
+
passed in as filename
|
31
|
+
: compile(filename)
|
32
|
+
compile the the colon separated list of database files passed in as
|
33
|
+
filename
|
34
|
+
: close()
|
35
|
+
closes the magic database and frees any memory allocated. if memory
|
36
|
+
is a concern, use this.
|
@@ -0,0 +1,37 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.required_ruby_version = ">= 1.8.5"
|
3
|
+
s.rubygems_version = "1.1.1"
|
4
|
+
s.platform = "ruby"
|
5
|
+
s.description = "Ruby FileMagic Library Bindings"
|
6
|
+
s.summary = "Ruby FileMagic Library Bindings"
|
7
|
+
s.version = "0.1.1"
|
8
|
+
s.has_rdoc = "false"
|
9
|
+
s.name = "ruby-filemagic"
|
10
|
+
s.authors = ["Travis Whitton", "Ricardo Chimal, Jr."]
|
11
|
+
s.email = "ricardo@heroku.com"
|
12
|
+
s.homepage = "http://github.com/ricardochimal/ruby-filemagic"
|
13
|
+
|
14
|
+
s.extensions = ["extconf.rb"]
|
15
|
+
|
16
|
+
s.files = [
|
17
|
+
"AUTHORS",
|
18
|
+
"CHANGELOG",
|
19
|
+
"extconf.rb",
|
20
|
+
"filemagic.c",
|
21
|
+
"filemagic.rd",
|
22
|
+
"README",
|
23
|
+
"test",
|
24
|
+
"test/perl",
|
25
|
+
"test/pyfile",
|
26
|
+
"test/pylink",
|
27
|
+
"test/regress.rb",
|
28
|
+
"test/leaktest.rb",
|
29
|
+
"test/pyfile-compressed.gz",
|
30
|
+
"TODO",
|
31
|
+
"ruby-filemagic.gemspec",
|
32
|
+
]
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
|
data/test/leaktest.rb
ADDED
data/test/perl
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
|
2
|
+
#------------------------------------------------------------------------------
|
3
|
+
# perl: file(1) magic for Larry Wall's perl language.
|
4
|
+
#
|
5
|
+
# The ``eval'' line recognizes an outrageously clever hack for USG systems.
|
6
|
+
# Keith Waclena <keith@cerberus.uchicago.edu>
|
7
|
+
# Send additions to <perl5-porters@perl.org>
|
8
|
+
0 string/b #!\ /bin/perl perl script text executable
|
9
|
+
0 string eval\ "exec\ /bin/perl perl script text
|
10
|
+
0 string/b #!\ /usr/bin/perl perl script text executable
|
11
|
+
0 string eval\ "exec\ /usr/bin/perl perl script text
|
12
|
+
0 string/b #!\ /usr/local/bin/perl perl script text
|
13
|
+
0 string eval\ "exec\ /usr/local/bin/perl perl script text executable
|
14
|
+
0 string eval\ '(exit\ $?0)'\ &&\ eval\ 'exec perl script text
|
15
|
+
|
16
|
+
# a couple more, by me
|
17
|
+
# XXX: christos matches
|
18
|
+
#0 regex package Perl5 module source text (via regex)
|
19
|
+
0 string package Perl5 module source text
|
data/test/pyfile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
"""
|
Binary file
|
data/test/pylink
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
pyfile
|
data/test/regress.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "../filemagic"
|
3
|
+
|
4
|
+
class TestFileMagic < Test::Unit::TestCase
|
5
|
+
def test_file
|
6
|
+
fm = FileMagic.new(FileMagic::MAGIC_NONE)
|
7
|
+
res = fm.file("pyfile")
|
8
|
+
assert_equal("a python script text executable", res)
|
9
|
+
res = fm.file("pylink")
|
10
|
+
assert_equal("symbolic link to `pyfile'", res)
|
11
|
+
fm.close
|
12
|
+
fm = FileMagic.new(FileMagic::MAGIC_SYMLINK)
|
13
|
+
res = fm.file("pylink")
|
14
|
+
assert_equal("a python script text executable", res)
|
15
|
+
fm.close
|
16
|
+
fm = FileMagic.new(FileMagic::MAGIC_SYMLINK | FileMagic::MAGIC_MIME)
|
17
|
+
res = fm.file("pylink")
|
18
|
+
assert_equal("text/plain; charset=us-ascii", res)
|
19
|
+
fm.close
|
20
|
+
fm = FileMagic.new(FileMagic::MAGIC_COMPRESS)
|
21
|
+
res = fm.file("pyfile-compressed.gz")
|
22
|
+
assert_equal("a python script text executable (gzip compressed data, " +
|
23
|
+
'was "pyfile-compressed", from Unix)', res)
|
24
|
+
fm.close
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_buffer
|
28
|
+
fm = FileMagic.new(FileMagic::MAGIC_NONE)
|
29
|
+
res = fm.buffer("#!/bin/sh\n")
|
30
|
+
fm.close
|
31
|
+
assert_equal("a /bin/sh script text executable", res)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_check
|
35
|
+
fm = FileMagic.new(FileMagic::MAGIC_NONE)
|
36
|
+
res = fm.check("perl")
|
37
|
+
fm.close
|
38
|
+
assert_equal(res, 0)
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_compile
|
42
|
+
fm = FileMagic.new(FileMagic::MAGIC_NONE)
|
43
|
+
res = fm.compile("perl")
|
44
|
+
fm.close
|
45
|
+
assert_equal(res, 0)
|
46
|
+
File.unlink("perl.mgc")
|
47
|
+
end
|
48
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ricardochimal-ruby-filemagic
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Travis Whitton
|
8
|
+
- Ricardo Chimal, Jr.
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2008-06-25 00:00:00 -07:00
|
14
|
+
default_executable:
|
15
|
+
dependencies: []
|
16
|
+
|
17
|
+
description: Ruby FileMagic Library Bindings
|
18
|
+
email: ricardo@heroku.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions:
|
22
|
+
- extconf.rb
|
23
|
+
extra_rdoc_files: []
|
24
|
+
|
25
|
+
files:
|
26
|
+
- AUTHORS
|
27
|
+
- CHANGELOG
|
28
|
+
- extconf.rb
|
29
|
+
- filemagic.c
|
30
|
+
- filemagic.rd
|
31
|
+
- README
|
32
|
+
- test
|
33
|
+
- test/perl
|
34
|
+
- test/pyfile
|
35
|
+
- test/pylink
|
36
|
+
- test/regress.rb
|
37
|
+
- test/leaktest.rb
|
38
|
+
- test/pyfile-compressed.gz
|
39
|
+
- TODO
|
40
|
+
- ruby-filemagic.gemspec
|
41
|
+
has_rdoc: "false"
|
42
|
+
homepage: http://github.com/ricardochimal/ruby-filemagic
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 1.8.5
|
53
|
+
version:
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
version:
|
60
|
+
requirements: []
|
61
|
+
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 1.2.0
|
64
|
+
signing_key:
|
65
|
+
specification_version: 2
|
66
|
+
summary: Ruby FileMagic Library Bindings
|
67
|
+
test_files: []
|
68
|
+
|