mahoro 0.3 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,16 +2,35 @@
2
2
 
3
3
  require 'test/unit'
4
4
  require 'mahoro'
5
+ require 'mahoro/thread_safe'
6
+ require 'pathname'
5
7
 
6
8
  class MahoroTestCase < Test::Unit::TestCase
9
+ C_FILE = "#{File.dirname(__FILE__)}/../ext/mahoro/mahoro.c"
7
10
 
8
11
  def setup
9
12
  @m = Mahoro.new
10
13
  end
11
14
 
15
+ # Different versions of libmagic may generate different values.
16
+ # So far, we have seen:
17
+ # - ASCII C program text
18
+ # - C source, ASCII text
19
+ def assert_c_text(buf)
20
+ assert_match(/\bC\b/, buf, "contains the letter C")
21
+ assert_match(/\s/, buf, "contains spaces")
22
+ assert_match(/(?:source|text)/, buf, "is source or text")
23
+ end
24
+
25
+ def test_pathname
26
+ @m.flags = Mahoro::NONE
27
+ pn = Pathname.new(C_FILE)
28
+ assert_c_text(@m.file(pn))
29
+ end
30
+
12
31
  def test_file
13
32
  @m.flags = Mahoro::NONE
14
- assert_equal('ASCII C program text', @m.file('mahoro.c'))
33
+ assert_c_text(@m.file(C_FILE))
15
34
  end
16
35
 
17
36
  def test_mime_file
@@ -19,23 +38,22 @@ class MahoroTestCase < Test::Unit::TestCase
19
38
  assert({
20
39
  'text/x-c; charset=us-ascii' => true,
21
40
  'text/x-c charset=us-ascii' => true
22
- }.include?(@m.file('mahoro.c')))
41
+ }.include?(@m.file(C_FILE)))
23
42
  end
24
43
 
25
44
  def test_null_byte_in_path
26
- assert_raises(ArgumentError) { @m.file("mahoro.c\0foo") }
45
+ assert_raises(ArgumentError) { @m.file("#{C_FILE}\0foo") }
27
46
  end
28
47
 
29
48
  def test_buffer
30
49
  @m.flags = Mahoro::NONE
31
- assert_equal('ASCII C program text',
32
- @m.buffer(File.read('mahoro.c')))
50
+ assert_c_text(@m.buffer(File.read(C_FILE)))
33
51
  end
34
52
 
35
53
  def test_buffer_string_convert
36
- tmp = File.read('mahoro.c')
54
+ tmp = File.read(C_FILE)
37
55
  buf = Struct.new(:to_str).new(tmp)
38
- assert_equal('ASCII C program text', @m.buffer(buf))
56
+ assert_c_text(@m.buffer(buf))
39
57
  end
40
58
 
41
59
  def test_buffer_invalid
@@ -48,7 +66,7 @@ class MahoroTestCase < Test::Unit::TestCase
48
66
  assert({
49
67
  'text/x-c; charset=us-ascii' => true,
50
68
  'text/x-c charset=us-ascii' => true
51
- }.include?(@m.buffer(File.read('mahoro.c'))))
69
+ }.include?(@m.buffer(File.read(C_FILE))))
52
70
  end
53
71
 
54
72
  def test_valid
@@ -56,7 +74,7 @@ class MahoroTestCase < Test::Unit::TestCase
56
74
  end
57
75
 
58
76
  def test_valid_with_null
59
- assert_raises(ArgumentError) { @m.valid? "mahoro.c\0" }
77
+ assert_raises(ArgumentError) { @m.valid? "#{C_FILE}\0" }
60
78
  end
61
79
 
62
80
  def test_compile
@@ -74,6 +92,14 @@ class MahoroTestCase < Test::Unit::TestCase
74
92
  Mahoro.compile "magic.sample\0"
75
93
  end
76
94
  end
95
+
96
+ def test_thread_safe
97
+ before = @m.method(:file)
98
+ @m.extend(Mahoro::ThreadSafe)
99
+ @m.flags = Mahoro::NONE
100
+ assert_c_text(@m.file(C_FILE))
101
+ assert_not_equal(before.object_id, @m.method(:file).object_id)
102
+ end
77
103
  end
78
104
 
79
105
  # arch-tag: test
metadata CHANGED
@@ -1,69 +1,71 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: mahoro
3
- version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: "0.3"
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.4'
6
5
  platform: ruby
7
- authors:
6
+ authors:
8
7
  - Shu-yu Guo
9
8
  - Eric Wong
10
9
  autorequire:
11
10
  bindir: bin
12
11
  cert_chain: []
13
-
14
- date: 2011-03-15 00:00:00 +00:00
15
- default_executable:
12
+ date: 2013-04-05 00:00:00.000000000 Z
16
13
  dependencies: []
17
-
18
14
  description: |-
19
15
  An interface to libmagic to determine file types using "magic" numbers.
20
16
  This can be used in place of calling the file(1) command in Ruby scripts.
21
17
  Shu-yu Guo is the original author but all maintenance is handled by
22
18
  Eric Wong nowadays.
23
-
19
+
24
20
  source: http://bogomips.org/mahoro.git/
21
+ documentation: http://bogomips.org/mahoro/
25
22
  email: normalperson@yhbt.net
26
23
  executables: []
27
-
28
- extensions:
29
- - extconf.rb
30
- extra_rdoc_files: []
31
-
32
- files:
24
+ extensions:
25
+ - ext/mahoro/extconf.rb
26
+ extra_rdoc_files:
27
+ - ext/mahoro/mahoro.c
28
+ files:
29
+ - .gitignore
30
+ - GNUmakefile
33
31
  - INSTALL
34
- - extconf.rb
32
+ - README
33
+ - ext/mahoro/extconf.rb
34
+ - ext/mahoro/mahoro.c
35
+ - ext/mahoro/nogvl_compat.h
36
+ - lib/mahoro/thread_safe.rb
35
37
  - magic.sample
36
- - mahoro.c
37
38
  - mahoro.gemspec
38
39
  - maintainership-transfer.mbox.gz
39
- - test.rb
40
- has_rdoc: true
41
- homepage: http://mahoro.rubyforge.org/
42
- licenses:
40
+ - setup.rb
41
+ - test/test_mahoro.rb
42
+ - www/.gitignore
43
+ homepage: http://bogomips.org/mahoro/README
44
+ licenses:
43
45
  - Public Domain
46
+ metadata: {}
44
47
  post_install_message:
45
- rdoc_options: []
46
-
47
- require_paths:
48
+ rdoc_options:
49
+ - --exclude
50
+ - \.o
51
+ - --exclude
52
+ - \.so
53
+ require_paths:
48
54
  - lib
49
- required_ruby_version: !ruby/object:Gem::Requirement
50
- none: false
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: "0"
55
- required_rubygems_version: !ruby/object:Gem::Requirement
56
- none: false
57
- requirements:
58
- - - ">="
59
- - !ruby/object:Gem::Version
60
- version: "0"
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
61
65
  requirements: []
62
-
63
66
  rubyforge_project: mahoro
64
- rubygems_version: 1.6.2
67
+ rubygems_version: 2.0.2
65
68
  signing_key:
66
- specification_version: 3
69
+ specification_version: 4
67
70
  summary: An interface to libmagic
68
- test_files:
69
- - test.rb
71
+ test_files: []
data/mahoro.c DELETED
@@ -1,320 +0,0 @@
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
- #ifndef RSTRING_PTR
12
- # define RSTRING_PTR(s)->ptr
13
- #endif
14
-
15
- static VALUE cMahoro;
16
- static VALUE eMahoroError;
17
-
18
- static void
19
- mahoro_free(ptr)
20
- void *ptr;
21
- {
22
- if (ptr)
23
- magic_close((magic_t)ptr);
24
- }
25
-
26
- static VALUE
27
- mahoro_allocate(klass)
28
- VALUE klass;
29
- {
30
- return Data_Wrap_Struct(klass, 0, mahoro_free, 0);
31
- }
32
-
33
- static VALUE
34
- mahoro_initialize(argc, argv, self)
35
- int argc;
36
- VALUE *argv, self;
37
- {
38
- int flags = MAGIC_NONE;
39
- char *path = 0;
40
- magic_t cookie;
41
- VALUE vpath, vflags;
42
-
43
- switch(rb_scan_args(argc, argv, "02", &vflags, &vpath)) {
44
- case 2:
45
- if(!NIL_P(vpath)) {
46
- path = StringValueCStr(vpath);
47
- }
48
- /* fallthrough */
49
- case 1:
50
- flags = FIX2INT(vflags);
51
- break;
52
- }
53
-
54
- if(!(cookie = magic_open(flags))) {
55
- rb_raise(eMahoroError, "failed to initialize magic cookie");
56
- }
57
-
58
- if(magic_load(cookie, path)) {
59
- rb_raise(eMahoroError, "failed to load database: %s",
60
- magic_error(cookie));
61
- }
62
-
63
- DATA_PTR(self) = cookie;
64
-
65
- return self;
66
- }
67
-
68
- static VALUE
69
- mahoro_file(self, path)
70
- VALUE self, path;
71
- {
72
- const char *msg;
73
- magic_t cookie = (magic_t)DATA_PTR(self);
74
-
75
- if(!(msg = magic_file(cookie, StringValueCStr(path)))) {
76
- rb_raise(eMahoroError, "failed lookup: %s", magic_error(cookie));
77
- }
78
-
79
- return rb_str_new2(msg);
80
- }
81
-
82
- static VALUE
83
- mahoro_buffer(self, input)
84
- VALUE self, input;
85
- {
86
- const char *msg;
87
- magic_t cookie = (magic_t)DATA_PTR(self);
88
-
89
- StringValue(input);
90
-
91
- if(!(msg = magic_buffer(cookie, RSTRING_PTR(input),
92
- RSTRING_LEN(input)))) {
93
- rb_raise(eMahoroError, "failed lookup: %s", magic_error(cookie));
94
- }
95
-
96
- return rb_str_new2(msg);
97
- }
98
-
99
- static VALUE
100
- mahoro_set_flags(self, flags)
101
- VALUE self, flags;
102
- {
103
- magic_t cookie = (magic_t)DATA_PTR(self);
104
-
105
- return INT2FIX(magic_setflags(cookie, FIX2INT(flags)));
106
- }
107
-
108
- static VALUE
109
- mahoro_check(argc, argv, self)
110
- int argc;
111
- VALUE *argv, self;
112
- {
113
- char *path = 0;
114
- VALUE vpath;
115
- magic_t cookie = (magic_t)DATA_PTR(self);
116
-
117
- switch(rb_scan_args(argc, argv, "01", &vpath)) {
118
- case 1:
119
- if(!NIL_P(vpath)) {
120
- path = StringValueCStr(vpath);
121
- }
122
- break;
123
- }
124
-
125
- if(!magic_check(cookie, path)) {
126
- return Qtrue;
127
- } else {
128
- return Qfalse;
129
- }
130
- }
131
-
132
- static VALUE
133
- mahoro_compile(self, path)
134
- VALUE self, path;
135
- {
136
- magic_t cookie = (magic_t)DATA_PTR(self);
137
-
138
- if(magic_compile(cookie, StringValueCStr(path))) {
139
- rb_raise(eMahoroError, "failed compile: %s", magic_error(cookie));
140
- }
141
-
142
- return Qtrue;
143
- }
144
-
145
- static VALUE
146
- mahoro_s_compile(klass, path)
147
- VALUE klass, path;
148
- {
149
- VALUE m = rb_funcall(klass, rb_intern("new"), 0, 0);
150
-
151
- return mahoro_compile(m, path);
152
- }
153
-
154
- static VALUE
155
- mahoro_load(self, path)
156
- VALUE self, path;
157
- {
158
- magic_t cookie = (magic_t)DATA_PTR(self);
159
-
160
- if(magic_load(cookie, StringValueCStr(path))) {
161
- rb_raise(eMahoroError, "failed load: %s", magic_error(cookie));
162
- }
163
-
164
- return self;
165
- }
166
-
167
- void Init_mahoro(void)
168
- {
169
- cMahoro = rb_define_class("Mahoro", rb_cObject);
170
- eMahoroError = rb_define_class_under(cMahoro, "Error", rb_eStandardError);
171
-
172
- /* No special handling, the default */
173
- rb_define_const(cMahoro, "NONE", INT2FIX(MAGIC_NONE));
174
-
175
- /* print debugging messages to stderr */
176
- rb_define_const(cMahoro, "DEBUG", INT2FIX(MAGIC_DEBUG));
177
-
178
- /* Follow symlinks */
179
- rb_define_const(cMahoro, "SYMLINK", INT2FIX(MAGIC_SYMLINK));
180
-
181
- /* Check inside compressed files */
182
- rb_define_const(cMahoro, "COMPRESS", INT2FIX(MAGIC_COMPRESS));
183
-
184
- /* Look at the contents of devices */
185
- rb_define_const(cMahoro, "DEVICES", INT2FIX(MAGIC_DEVICES));
186
-
187
- #ifdef MAGIC_MIME_TYPE
188
- /*
189
- * Return only the MIME type
190
- * This constant may not be defined on older systems.
191
- */
192
- rb_define_const(cMahoro, "MIME_TYPE", INT2FIX(MAGIC_MIME_TYPE));
193
- #endif
194
-
195
- /* Return all matches */
196
- rb_define_const(cMahoro, "CONTINUE", INT2FIX(MAGIC_CONTINUE));
197
-
198
- /*
199
- * Check the magic database for consistency and
200
- * print warnings to stderr
201
- */
202
- rb_define_const(cMahoro, "CHECK", INT2FIX(MAGIC_CHECK));
203
-
204
- /* preserve access time of files analyzed */
205
- rb_define_const(cMahoro, "PRESERVE_ATIME",
206
- INT2FIX(MAGIC_PRESERVE_ATIME));
207
-
208
- /*
209
- * Don't translate unprintable characters to a \\ooo octal
210
- * representation
211
- */
212
- rb_define_const(cMahoro, "RAW", INT2FIX(MAGIC_RAW));
213
-
214
- /*
215
- * Treat operating system errors while trying to open files
216
- * and follow symlinks as real errors, instead of printing
217
- * them in the magic buffer.
218
- */
219
- rb_define_const(cMahoro, "ERROR", INT2FIX(MAGIC_ERROR));
220
-
221
- #ifdef MAGIC_MIME_ENCODING
222
- /*
223
- * Return a MIME encoding, instead of a textual description.
224
- * This constant may not be defined on older systems.
225
- */
226
- rb_define_const(cMahoro, "MIME_ENCODING", INT2FIX(MAGIC_MIME_ENCODING));
227
- #endif
228
-
229
- /* return both MIME type and encoding */
230
- rb_define_const(cMahoro, "MIME", INT2FIX(MAGIC_MIME));
231
-
232
- #ifdef MAGIC_APPLE
233
- /*
234
- * Return both Apple creator and type.
235
- * This constant may not be defined on older systems.
236
- */
237
- rb_define_const(cMahoro, "APPLE", INT2FIX(MAGIC_APPLE));
238
- #endif
239
-
240
- #ifdef MAGIC_NO_CHECK_COMPRESS
241
- /*
242
- * Don't check for or inside compressed files.
243
- * This constant may not be defined on older systems.
244
- */
245
- rb_define_const(cMahoro, "NO_CHECK_COMPRESS",
246
- INT2FIX(MAGIC_NO_CHECK_COMPRESS));
247
- #endif
248
-
249
- #ifdef MAGIC_NO_CHECK_TAR
250
- /*
251
- * Don't examine tar files.
252
- * This constant may not be defined on older systems.
253
- */
254
- rb_define_const(cMahoro, "NO_CHECK_TAR", INT2FIX(MAGIC_NO_CHECK_TAR));
255
- #endif
256
-
257
- #ifdef MAGIC_NO_CHECK_SOFT
258
- /*
259
- * Don't consult magic files.
260
- * This constant may not be defined on older systems.
261
- */
262
- rb_define_const(cMahoro, "NO_CHECK_SOFT", INT2FIX(MAGIC_NO_CHECK_SOFT));
263
- #endif
264
-
265
- #ifdef MAGIC_NO_CHECK_APPTYPE
266
- /*
267
- * Don't check application type (EMX only).
268
- * This constant may not be defined on older systems.
269
- */
270
- rb_define_const(cMahoro, "NO_CHECK_APPTYPE",
271
- INT2FIX(MAGIC_NO_CHECK_APPTYPE));
272
- #endif
273
-
274
- #ifdef MAGIC_NO_CHECK_ELF
275
- /*
276
- * Don't check for ELF details.
277
- * This constant may not be defined on older systems.
278
- */
279
- rb_define_const(cMahoro, "NO_CHECK_ELF", INT2FIX(MAGIC_NO_CHECK_ELF));
280
- #endif
281
-
282
- #ifdef MAGIC_NO_CHECK_ASCII
283
- /*
284
- * Don't check for various types of ASCII text files.
285
- * This constant may not be defined on older systems.
286
- */
287
- rb_define_const(cMahoro, "NO_CHECK_TEXT",
288
- INT2FIX(MAGIC_NO_CHECK_ASCII));
289
- #endif
290
-
291
- #ifdef MAGIC_NO_CHECK_TOKENS
292
- /*
293
- * Don't check for known tokens inside ASCII files.
294
- * This constant may not be defined on older systems.
295
- */
296
- rb_define_const(cMahoro, "NO_CHECK_TOKENS",
297
- INT2FIX(MAGIC_NO_CHECK_TOKENS));
298
- #endif
299
-
300
- #ifdef MAGIC_NO_CHECK_ENCODING
301
- /*
302
- * Don't check for text encodings.
303
- * This constant may not be defined on older systems.
304
- */
305
- rb_define_const(cMahoro, "NO_CHECK_ENCODING",
306
- INT2FIX(MAGIC_NO_CHECK_ENCODING));
307
- #endif
308
-
309
- rb_define_alloc_func(cMahoro, mahoro_allocate);
310
- rb_define_method(cMahoro, "initialize", mahoro_initialize, -1);
311
- rb_define_method(cMahoro, "file", mahoro_file, 1);
312
- rb_define_method(cMahoro, "buffer", mahoro_buffer, 1);
313
- rb_define_method(cMahoro, "flags=", mahoro_set_flags, 1);
314
- rb_define_method(cMahoro, "valid?", mahoro_check, -1);
315
- rb_define_singleton_method(cMahoro, "compile", mahoro_s_compile, 1);
316
- rb_define_method(cMahoro, "compile", mahoro_compile, 1);
317
- rb_define_method(cMahoro, "load", mahoro_load, 1);
318
- }
319
-
320
- /* arch-tag: mahoro */