mahoro 0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/INSTALL +9 -0
- data/extconf.rb +7 -0
- data/mahoro.c +191 -0
- data/mahoro.gemspec +25 -0
- data/maintainership-transfer.mbox.gz +0 -0
- data/test.rb +45 -0
- metadata +78 -0
data/INSTALL
ADDED
data/extconf.rb
ADDED
data/mahoro.c
ADDED
@@ -0,0 +1,191 @@
|
|
1
|
+
/*
|
2
|
+
* This file is Public Domain.
|
3
|
+
*/
|
4
|
+
|
5
|
+
#include <ruby.h>
|
6
|
+
#include <magic.h>
|
7
|
+
|
8
|
+
#ifndef RSTRING_LEN
|
9
|
+
# define RSTRING_LEN(s)->len
|
10
|
+
#endif
|
11
|
+
|
12
|
+
struct MagicCookie
|
13
|
+
{
|
14
|
+
magic_t cookie;
|
15
|
+
};
|
16
|
+
|
17
|
+
static VALUE cMahoro;
|
18
|
+
static VALUE eMahoroError;
|
19
|
+
|
20
|
+
static void
|
21
|
+
mahoro_free(ptr)
|
22
|
+
struct MagicCookie *ptr;
|
23
|
+
{
|
24
|
+
magic_close(ptr->cookie);
|
25
|
+
xfree(ptr);
|
26
|
+
}
|
27
|
+
|
28
|
+
static VALUE
|
29
|
+
mahoro_allocate(klass)
|
30
|
+
VALUE klass;
|
31
|
+
{
|
32
|
+
return Data_Wrap_Struct(klass, 0, mahoro_free, 0);
|
33
|
+
}
|
34
|
+
|
35
|
+
static VALUE
|
36
|
+
mahoro_initialize(argc, argv, self)
|
37
|
+
int argc;
|
38
|
+
VALUE *argv, self;
|
39
|
+
{
|
40
|
+
int flags = MAGIC_NONE;
|
41
|
+
char *path = 0;
|
42
|
+
struct MagicCookie *ptr;
|
43
|
+
magic_t cookie;
|
44
|
+
VALUE vpath, vflags;
|
45
|
+
|
46
|
+
switch(rb_scan_args(argc, argv, "02", &vflags, &vpath)) {
|
47
|
+
case 2:
|
48
|
+
if(!NIL_P(vpath)) {
|
49
|
+
path = StringValuePtr(vpath);
|
50
|
+
}
|
51
|
+
/* fallthrough */
|
52
|
+
case 1:
|
53
|
+
flags = FIX2INT(vflags);
|
54
|
+
break;
|
55
|
+
}
|
56
|
+
|
57
|
+
if(!(cookie = magic_open(flags))) {
|
58
|
+
rb_raise(eMahoroError, "failed to initialize magic cookie");
|
59
|
+
}
|
60
|
+
|
61
|
+
if(magic_load(cookie, path)) {
|
62
|
+
rb_raise(eMahoroError, "failed to load database: %s",
|
63
|
+
magic_error(cookie));
|
64
|
+
}
|
65
|
+
|
66
|
+
ptr = ALLOC(struct MagicCookie);
|
67
|
+
ptr->cookie = cookie;
|
68
|
+
DATA_PTR(self) = ptr;
|
69
|
+
|
70
|
+
return self;
|
71
|
+
}
|
72
|
+
|
73
|
+
static VALUE
|
74
|
+
mahoro_file(self, path)
|
75
|
+
VALUE self, path;
|
76
|
+
{
|
77
|
+
const char *msg;
|
78
|
+
magic_t cookie = ((struct MagicCookie *) DATA_PTR(self))->cookie;
|
79
|
+
|
80
|
+
if(!(msg = magic_file(cookie, StringValuePtr(path)))) {
|
81
|
+
rb_raise(eMahoroError, "failed lookup: %s", magic_error(cookie));
|
82
|
+
}
|
83
|
+
|
84
|
+
return rb_str_new2(msg);
|
85
|
+
}
|
86
|
+
|
87
|
+
static VALUE
|
88
|
+
mahoro_buffer(self, input)
|
89
|
+
VALUE self, input;
|
90
|
+
{
|
91
|
+
const char *msg;
|
92
|
+
magic_t cookie = ((struct MagicCookie *) DATA_PTR(self))->cookie;
|
93
|
+
|
94
|
+
if(!(msg = magic_buffer(cookie, StringValuePtr(input),
|
95
|
+
RSTRING_LEN(StringValue(input))))) {
|
96
|
+
rb_raise(eMahoroError, "failed lookup: %s", magic_error(cookie));
|
97
|
+
}
|
98
|
+
|
99
|
+
return rb_str_new2(msg);
|
100
|
+
}
|
101
|
+
|
102
|
+
static VALUE
|
103
|
+
mahoro_set_flags(self, flags)
|
104
|
+
VALUE self, flags;
|
105
|
+
{
|
106
|
+
magic_t cookie = ((struct MagicCookie *) DATA_PTR(self))->cookie;
|
107
|
+
|
108
|
+
return INT2FIX(magic_setflags(cookie, FIX2INT(flags)));
|
109
|
+
}
|
110
|
+
|
111
|
+
static VALUE
|
112
|
+
mahoro_check(argc, argv, self)
|
113
|
+
int argc;
|
114
|
+
VALUE *argv, self;
|
115
|
+
{
|
116
|
+
char *path = 0;
|
117
|
+
VALUE vpath;
|
118
|
+
magic_t cookie = ((struct MagicCookie *) DATA_PTR(self))->cookie;
|
119
|
+
|
120
|
+
switch(rb_scan_args(argc, argv, "01", &vpath)) {
|
121
|
+
case 1:
|
122
|
+
if(!NIL_P(vpath)) {
|
123
|
+
path = StringValuePtr(vpath);
|
124
|
+
}
|
125
|
+
break;
|
126
|
+
}
|
127
|
+
|
128
|
+
if(!magic_check(cookie, path)) {
|
129
|
+
return Qtrue;
|
130
|
+
} else {
|
131
|
+
return Qfalse;
|
132
|
+
}
|
133
|
+
}
|
134
|
+
|
135
|
+
static VALUE
|
136
|
+
mahoro_compile(klass, path)
|
137
|
+
VALUE klass, path;
|
138
|
+
{
|
139
|
+
magic_t cookie = magic_open(MAGIC_NONE);
|
140
|
+
|
141
|
+
if(magic_compile(cookie, StringValuePtr(path))) {
|
142
|
+
rb_raise(eMahoroError, "failed compile: %s", magic_error(cookie));
|
143
|
+
}
|
144
|
+
|
145
|
+
magic_close(cookie);
|
146
|
+
|
147
|
+
return Qtrue;
|
148
|
+
}
|
149
|
+
|
150
|
+
static VALUE
|
151
|
+
mahoro_load(self, path)
|
152
|
+
VALUE self, path;
|
153
|
+
{
|
154
|
+
magic_t cookie = ((struct MagicCookie *) DATA_PTR(self))->cookie;
|
155
|
+
|
156
|
+
if(magic_load(cookie, StringValuePtr(path))) {
|
157
|
+
rb_raise(eMahoroError, "failed load: %s", magic_error(cookie));
|
158
|
+
}
|
159
|
+
|
160
|
+
return self;
|
161
|
+
}
|
162
|
+
|
163
|
+
void Init_mahoro(void)
|
164
|
+
{
|
165
|
+
cMahoro = rb_define_class("Mahoro", rb_cObject);
|
166
|
+
eMahoroError = rb_define_class_under(cMahoro, "Error", rb_eStandardError);
|
167
|
+
|
168
|
+
rb_const_set(cMahoro, rb_intern("NONE"), INT2FIX(MAGIC_NONE));
|
169
|
+
rb_const_set(cMahoro, rb_intern("DEBUG"), INT2FIX(MAGIC_DEBUG));
|
170
|
+
rb_const_set(cMahoro, rb_intern("SYMLINK"), INT2FIX(MAGIC_SYMLINK));
|
171
|
+
rb_const_set(cMahoro, rb_intern("COMPRESS"), INT2FIX(MAGIC_COMPRESS));
|
172
|
+
rb_const_set(cMahoro, rb_intern("DEVICES"), INT2FIX(MAGIC_DEVICES));
|
173
|
+
rb_const_set(cMahoro, rb_intern("MIME"), INT2FIX(MAGIC_MIME));
|
174
|
+
rb_const_set(cMahoro, rb_intern("CONTINUE"), INT2FIX(MAGIC_CONTINUE));
|
175
|
+
rb_const_set(cMahoro, rb_intern("CHECK"), INT2FIX(MAGIC_CHECK));
|
176
|
+
rb_const_set(cMahoro, rb_intern("PRESERVE_ATIME"),
|
177
|
+
INT2FIX(MAGIC_PRESERVE_ATIME));
|
178
|
+
rb_const_set(cMahoro, rb_intern("RAW"), INT2FIX(MAGIC_RAW));
|
179
|
+
rb_const_set(cMahoro, rb_intern("ERROR"), INT2FIX(MAGIC_ERROR));
|
180
|
+
|
181
|
+
rb_define_alloc_func(cMahoro, mahoro_allocate);
|
182
|
+
rb_define_method(cMahoro, "initialize", mahoro_initialize, -1);
|
183
|
+
rb_define_method(cMahoro, "file", mahoro_file, 1);
|
184
|
+
rb_define_method(cMahoro, "buffer", mahoro_buffer, 1);
|
185
|
+
rb_define_method(cMahoro, "flags=", mahoro_set_flags, 1);
|
186
|
+
rb_define_method(cMahoro, "valid?", mahoro_check, -1);
|
187
|
+
rb_define_singleton_method(cMahoro, "compile", mahoro_compile, 1);
|
188
|
+
rb_define_method(cMahoro, "load", mahoro_load, 1);
|
189
|
+
}
|
190
|
+
|
191
|
+
/* arch-tag: mahoro */
|
data/mahoro.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
ENV["VERSION"] or abort "VERSION= must be specified"
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{mahoro}
|
5
|
+
s.version = ENV["VERSION"].dup
|
6
|
+
s.homepage = "http://mahoro.rubyforge.org/"
|
7
|
+
s.authors = [ "Shu-yu Guo", "Eric Wong" ]
|
8
|
+
s.date = Time.now.utc.strftime('%Y-%m-%d')
|
9
|
+
s.description = %{
|
10
|
+
An interface to libmagic to determine file types using "magic" numbers.
|
11
|
+
This can be used in place of calling the file(1) command in Ruby scripts.
|
12
|
+
Shu-yu Guo is the original author but all maintenance is handled by
|
13
|
+
Eric Wong nowadays.
|
14
|
+
|
15
|
+
source: http://bogomips.org/mahoro.git/
|
16
|
+
}.strip
|
17
|
+
s.email = %q{normalperson@yhbt.net}
|
18
|
+
s.files = `git ls-files`.split(/\n/)
|
19
|
+
s.rubyforge_project = %q{mahoro}
|
20
|
+
s.summary = 'An interface to libmagic'
|
21
|
+
s.test_files = %w(test.rb)
|
22
|
+
s.extensions = %w(extconf.rb)
|
23
|
+
|
24
|
+
s.license = "Public Domain" # disabled for compatibility with older RubyGems
|
25
|
+
end
|
Binary file
|
data/test.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'mahoro'
|
5
|
+
|
6
|
+
class MahoroTestCase < Test::Unit::TestCase
|
7
|
+
|
8
|
+
def setup
|
9
|
+
@m = Mahoro.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_file
|
13
|
+
@m.flags = Mahoro::NONE
|
14
|
+
assert_equal('ASCII C program text', @m.file('mahoro.c'))
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_mime_file
|
18
|
+
@m.flags = Mahoro::MIME
|
19
|
+
assert({
|
20
|
+
'text/x-c; charset=us-ascii' => true,
|
21
|
+
'text/x-c charset=us-ascii' => true
|
22
|
+
}.include?(@m.file('mahoro.c')))
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_buffer
|
26
|
+
@m.flags = Mahoro::NONE
|
27
|
+
assert_equal('ASCII C program text',
|
28
|
+
@m.buffer(File.read('mahoro.c')))
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_mime_buffer
|
32
|
+
@m.flags = Mahoro::MIME
|
33
|
+
assert({
|
34
|
+
'text/x-c; charset=us-ascii' => true,
|
35
|
+
'text/x-c charset=us-ascii' => true
|
36
|
+
}.include?(@m.buffer(File.read('mahoro.c'))))
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_valid
|
40
|
+
assert(@m.valid?, 'Default database was not valid.')
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
# arch-tag: test
|
metadata
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mahoro
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 15
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 2
|
9
|
+
version: "0.2"
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Shu-yu Guo
|
13
|
+
- Eric Wong
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-01-31 00:00:00 +00:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: |-
|
23
|
+
An interface to libmagic to determine file types using "magic" numbers.
|
24
|
+
This can be used in place of calling the file(1) command in Ruby scripts.
|
25
|
+
Shu-yu Guo is the original author but all maintenance is handled by
|
26
|
+
Eric Wong nowadays.
|
27
|
+
|
28
|
+
source: http://bogomips.org/mahoro.git/
|
29
|
+
email: normalperson@yhbt.net
|
30
|
+
executables: []
|
31
|
+
|
32
|
+
extensions:
|
33
|
+
- extconf.rb
|
34
|
+
extra_rdoc_files: []
|
35
|
+
|
36
|
+
files:
|
37
|
+
- INSTALL
|
38
|
+
- extconf.rb
|
39
|
+
- mahoro.c
|
40
|
+
- mahoro.gemspec
|
41
|
+
- maintainership-transfer.mbox.gz
|
42
|
+
- test.rb
|
43
|
+
has_rdoc: true
|
44
|
+
homepage: http://mahoro.rubyforge.org/
|
45
|
+
licenses:
|
46
|
+
- Public Domain
|
47
|
+
post_install_message:
|
48
|
+
rdoc_options: []
|
49
|
+
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
hash: 3
|
67
|
+
segments:
|
68
|
+
- 0
|
69
|
+
version: "0"
|
70
|
+
requirements: []
|
71
|
+
|
72
|
+
rubyforge_project: mahoro
|
73
|
+
rubygems_version: 1.3.7
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: An interface to libmagic
|
77
|
+
test_files:
|
78
|
+
- test.rb
|