ruby-magic 0.0.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES +45 -0
- data/CHANGES.rdoc +45 -0
- data/COPYRIGHT +1 -1
- data/README.rdoc +3 -2
- data/Rakefile +2 -1
- data/TODO +10 -7
- data/VERSION +1 -1
- data/bin/magic +1 -1
- data/ext/magic/common.h +15 -6
- data/ext/magic/extconf.rb +31 -11
- data/ext/magic/functions.c +65 -10
- data/ext/magic/functions.h +13 -2
- data/ext/magic/ruby-magic.c +28 -14
- data/ext/magic/ruby-magic.h +1 -1
- data/lib/magic.rb +20 -10
- data/lib/magic/core/file.rb +8 -9
- data/lib/magic/core/string.rb +4 -5
- data/lib/magic/version.rb +2 -2
- data/ruby-magic.gemspec +6 -12
- data/test/helpers/magic_test_helper.rb +35 -0
- data/test/test_constants.rb +53 -4
- data/test/test_magic.rb +270 -69
- metadata +9 -88
data/ext/magic/ruby-magic.c
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
/*
|
4
4
|
* ruby-magic.c
|
5
5
|
*
|
6
|
-
* Copyright 2013-
|
6
|
+
* Copyright 2013-2015 Krzysztof Wilczynski
|
7
7
|
*
|
8
8
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
9
9
|
* you may not use this file except in compliance with the License.
|
@@ -18,6 +18,10 @@
|
|
18
18
|
* limitations under the License.
|
19
19
|
*/
|
20
20
|
|
21
|
+
#if defined(__cplusplus)
|
22
|
+
extern "C" {
|
23
|
+
#endif
|
24
|
+
|
21
25
|
#include "ruby-magic.h"
|
22
26
|
|
23
27
|
ID id_at_flags, id_at_path, id_at_mutex;
|
@@ -76,7 +80,7 @@ static VALUE magic_return(VALUE value, void *data);
|
|
76
80
|
* magic = Magic.new #=> #<Magic:0x007f8fdc012e58>
|
77
81
|
*
|
78
82
|
* Will raise <i>Magic::LibraryError</i> exception if, or
|
79
|
-
* <i>Magic::MagicError</i> exception if
|
83
|
+
* <i>Magic::MagicError</i> exception if
|
80
84
|
*
|
81
85
|
* See also: Magic::open, Magic::mime, Magic::type, Magic::encoding, Magic::compile and Magic::check
|
82
86
|
*/
|
@@ -241,7 +245,7 @@ rb_mgc_get_flags(VALUE object)
|
|
241
245
|
* magic.flags = Magic::MIME_TYPE #=> 16
|
242
246
|
*
|
243
247
|
* Will raise <i>Magic::FlagsError</i> exception if, or
|
244
|
-
* <i>Magic::LibraryError</i> exception if, or
|
248
|
+
* <i>Magic::LibraryError</i> exception if, or
|
245
249
|
* <i>Magic::NotImplementedError</i> exception if, or
|
246
250
|
*/
|
247
251
|
VALUE
|
@@ -289,7 +293,9 @@ rb_mgc_set_flags(VALUE object, VALUE value)
|
|
289
293
|
* magic.load("/usr/share/misc/magic", "/etc/magic") #=> ["/usr/share/misc/magic", "/etc/magic"]
|
290
294
|
* magic.load #=> ["/etc/magic", "/usr/share/misc/magic"]
|
291
295
|
*
|
292
|
-
* Will raise <i>Magic::LibraryError</i> exception if, or
|
296
|
+
* Will raise <i>Magic::LibraryError</i> exception if, or
|
297
|
+
*
|
298
|
+
* See also: Magic#check, Magic#compile, Magic::check and Magic::compile
|
293
299
|
*/
|
294
300
|
VALUE
|
295
301
|
rb_mgc_load(VALUE object, VALUE arguments)
|
@@ -329,9 +335,9 @@ rb_mgc_load(VALUE object, VALUE arguments)
|
|
329
335
|
*
|
330
336
|
* magic = Magic.new #=> #<Magic:0x007f8fdc012e58>
|
331
337
|
*
|
332
|
-
* See also: Magic#check, Magic::check and Magic::compile
|
333
|
-
*
|
334
338
|
* Will raise <i>Magic::LibraryError</i> exception if, or
|
339
|
+
*
|
340
|
+
* See also: Magic#check, Magic::check and Magic::compile
|
335
341
|
*/
|
336
342
|
VALUE
|
337
343
|
rb_mgc_compile(VALUE object, VALUE arguments)
|
@@ -369,9 +375,9 @@ rb_mgc_compile(VALUE object, VALUE arguments)
|
|
369
375
|
*
|
370
376
|
* magic = Magic.new #=> #<Magic:0x007f8fdc012e58>
|
371
377
|
*
|
372
|
-
*
|
378
|
+
* Will raise <i>Magic::LibraryError</i> exception if, or
|
373
379
|
*
|
374
|
-
*
|
380
|
+
* See also: Magic#compile, Magic::compile and Magic::check
|
375
381
|
*/
|
376
382
|
VALUE
|
377
383
|
rb_mgc_check(VALUE object, VALUE arguments)
|
@@ -409,7 +415,7 @@ rb_mgc_check(VALUE object, VALUE arguments)
|
|
409
415
|
*
|
410
416
|
* magic = Magic.new #=> #<Magic:0x007f8fdc012e58>
|
411
417
|
*
|
412
|
-
* Will raise <i>Magic::LibraryError</i> exception if, or
|
418
|
+
* Will raise <i>Magic::LibraryError</i> exception if, or
|
413
419
|
*
|
414
420
|
* See also: Magic#buffer and Magic#descriptor
|
415
421
|
*/
|
@@ -459,7 +465,7 @@ rb_mgc_file(VALUE object, VALUE value)
|
|
459
465
|
*
|
460
466
|
* magic = Magic.new #=> #<Magic:0x007f8fdc012e58>
|
461
467
|
*
|
462
|
-
* Will raise <i>Magic::LibraryError</i> exception if, or
|
468
|
+
* Will raise <i>Magic::LibraryError</i> exception if, or
|
463
469
|
*
|
464
470
|
* See also: Magic#file and Magic#descriptor
|
465
471
|
*/
|
@@ -497,7 +503,7 @@ rb_mgc_buffer(VALUE object, VALUE value)
|
|
497
503
|
*
|
498
504
|
* magic = Magic.new #=> #<Magic:0x007f8fdc012e58>
|
499
505
|
*
|
500
|
-
* Will raise <i>Magic::LibraryError</i> exception if, or
|
506
|
+
* Will raise <i>Magic::LibraryError</i> exception if, or
|
501
507
|
*
|
502
508
|
* See also: Magic#file and Magic#buffer
|
503
509
|
*/
|
@@ -556,7 +562,7 @@ rb_mgc_version(VALUE object)
|
|
556
562
|
return INT2NUM(rv);
|
557
563
|
}
|
558
564
|
|
559
|
-
/* :
|
565
|
+
/* :stopdoc: */
|
560
566
|
|
561
567
|
static inline void*
|
562
568
|
nogvl_magic_load(void *data)
|
@@ -654,7 +660,7 @@ magic_allocate(VALUE klass)
|
|
654
660
|
|
655
661
|
cookie = magic_open(MAGIC_NONE);
|
656
662
|
if (!cookie) {
|
657
|
-
MAGIC_GENERIC_ERROR(rb_mgc_eLibraryError,
|
663
|
+
MAGIC_GENERIC_ERROR(rb_mgc_eLibraryError, ENOMEM,
|
658
664
|
error(E_MAGIC_LIBRARY_INITIALIZE));
|
659
665
|
}
|
660
666
|
|
@@ -761,13 +767,15 @@ magic_return(VALUE value, void *data)
|
|
761
767
|
VALUE array = Qnil;
|
762
768
|
|
763
769
|
if (ma->flags & MAGIC_CONTINUE) {
|
764
|
-
array = magic_split(value, CSTR2RVAL("
|
770
|
+
array = magic_split(value, CSTR2RVAL("\x5c\x30\x31\x32\x2d\x20"));
|
765
771
|
return (NUM2INT(magic_size(array)) > 1) ? array : magic_shift(array);
|
766
772
|
}
|
767
773
|
|
768
774
|
return value;
|
769
775
|
}
|
770
776
|
|
777
|
+
/* :startdoc: */
|
778
|
+
|
771
779
|
void
|
772
780
|
Init_magic(void)
|
773
781
|
{
|
@@ -970,4 +978,10 @@ Init_magic(void)
|
|
970
978
|
rb_define_const(rb_cMagic, "NO_CHECK_TROFF", INT2NUM(MAGIC_NO_CHECK_TROFF));
|
971
979
|
}
|
972
980
|
|
981
|
+
/* :enddoc: */
|
982
|
+
|
983
|
+
#if defined(__cplusplus)
|
984
|
+
}
|
985
|
+
#endif
|
986
|
+
|
973
987
|
/* vim: set ts=8 sw=4 sts=2 et : */
|
data/ext/magic/ruby-magic.h
CHANGED
data/lib/magic.rb
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
#
|
6
6
|
# magic.rb
|
7
7
|
#
|
8
|
-
# Copyright 2013-
|
8
|
+
# Copyright 2013-2015 Krzysztof Wilczynski
|
9
9
|
#
|
10
10
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
11
11
|
# you may not use this file except in compliance with the License.
|
@@ -20,10 +20,10 @@
|
|
20
20
|
# limitations under the License.
|
21
21
|
#
|
22
22
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
23
|
+
require_relative 'magic/magic'
|
24
|
+
require_relative 'magic/version'
|
25
|
+
require_relative 'magic/core/file'
|
26
|
+
require_relative 'magic/core/string'
|
27
27
|
|
28
28
|
# :startdoc:
|
29
29
|
|
@@ -61,14 +61,12 @@ class Magic
|
|
61
61
|
# See also: Magic#flags
|
62
62
|
#
|
63
63
|
def flags_to_a(names = false)
|
64
|
-
raise LibraryError,
|
64
|
+
raise LibraryError, 'Magic library is not open' if closed?
|
65
|
+
return [names ? 'NONE' : 0] if @flags.zero?
|
65
66
|
|
66
67
|
n, i = 0, @flags
|
67
|
-
|
68
68
|
flags = []
|
69
69
|
|
70
|
-
return [names ? 'NONE' : 0] if @flags.zero?
|
71
|
-
|
72
70
|
@@flags_map ||= flags_as_map if names
|
73
71
|
|
74
72
|
while i > 0
|
@@ -90,6 +88,8 @@ class Magic
|
|
90
88
|
#
|
91
89
|
# Example:
|
92
90
|
#
|
91
|
+
# Will raise <i></i>
|
92
|
+
#
|
93
93
|
# See also: Magic::mime, Magic::type, Magic::encoding, Magic::compile and Magic::check
|
94
94
|
#
|
95
95
|
def open(flags = Magic::NONE)
|
@@ -116,6 +116,8 @@ class Magic
|
|
116
116
|
#
|
117
117
|
# Example:
|
118
118
|
#
|
119
|
+
# Will raise <i></i>
|
120
|
+
#
|
119
121
|
# See also: Magic::open, Magic::type, Magic::encoding, Magic::compile and Magic::check
|
120
122
|
#
|
121
123
|
def mime(&block)
|
@@ -131,6 +133,8 @@ class Magic
|
|
131
133
|
#
|
132
134
|
# Example:
|
133
135
|
#
|
136
|
+
# Will raise <i></i>
|
137
|
+
#
|
134
138
|
# See also: Magic::open, Magic::mime, Magic::encoding, Magic::compile and Magic::check
|
135
139
|
#
|
136
140
|
def type(&block)
|
@@ -146,6 +150,8 @@ class Magic
|
|
146
150
|
#
|
147
151
|
# Example:
|
148
152
|
#
|
153
|
+
# Will raise <i></i>
|
154
|
+
#
|
149
155
|
# See also: Magic::open, Magic::mime, Magic::type, Magic::compile and Magic::check
|
150
156
|
#
|
151
157
|
def encoding(&block)
|
@@ -161,6 +167,8 @@ class Magic
|
|
161
167
|
#
|
162
168
|
# Example:
|
163
169
|
#
|
170
|
+
# Will raise <i></i>
|
171
|
+
#
|
164
172
|
# See also: Magic::open, Magic::mime, Magic::type, Magic::encoding, and Magic::check
|
165
173
|
#
|
166
174
|
def compile(path)
|
@@ -176,6 +184,8 @@ class Magic
|
|
176
184
|
#
|
177
185
|
# Example:
|
178
186
|
#
|
187
|
+
# Will raise <i></i>
|
188
|
+
#
|
179
189
|
# See also: Magic::open, Magic::mime, Magic::type, Magic::encoding and Magic::compile
|
180
190
|
#
|
181
191
|
def check(path)
|
@@ -189,7 +199,7 @@ class Magic
|
|
189
199
|
|
190
200
|
def flags_as_map
|
191
201
|
self.class.constants.inject({}) do |flags,constant|
|
192
|
-
value
|
202
|
+
value = self.class.const_get(constant)
|
193
203
|
flags[value] = constant.to_s if value.is_a?(Fixnum)
|
194
204
|
flags
|
195
205
|
end
|
data/lib/magic/core/file.rb
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
#
|
6
6
|
# core/file.rb
|
7
7
|
#
|
8
|
-
# Copyright 2013-
|
8
|
+
# Copyright 2013-2015 Krzysztof Wilczynski
|
9
9
|
#
|
10
10
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
11
11
|
# you may not use this file except in compliance with the License.
|
@@ -29,7 +29,7 @@ class File
|
|
29
29
|
class << self
|
30
30
|
#
|
31
31
|
# call-seq:
|
32
|
-
# File.magic( path ) -> string
|
32
|
+
# File.magic( path ) -> string or array
|
33
33
|
#
|
34
34
|
# Returns
|
35
35
|
#
|
@@ -42,13 +42,12 @@ class File
|
|
42
42
|
path ||= path.to_path if path.respond_to?(:to_path)
|
43
43
|
path ||= path.to_s
|
44
44
|
|
45
|
-
Magic.open(flags) {|mgc| mgc.file(path) }
|
46
|
-
rescue Magic::Error
|
45
|
+
Magic.open(flags | Magic::ERROR) {|mgc| mgc.file(path) }
|
47
46
|
end
|
48
47
|
|
49
48
|
#
|
50
49
|
# call-seq:
|
51
|
-
# File.mime( path ) -> string
|
50
|
+
# File.mime( path ) -> string or array
|
52
51
|
#
|
53
52
|
# Returns
|
54
53
|
#
|
@@ -62,7 +61,7 @@ class File
|
|
62
61
|
|
63
62
|
#
|
64
63
|
# call-seq:
|
65
|
-
# File.type( path ) -> string
|
64
|
+
# File.type( path ) -> string or array
|
66
65
|
#
|
67
66
|
# Returns
|
68
67
|
#
|
@@ -77,7 +76,7 @@ class File
|
|
77
76
|
|
78
77
|
#
|
79
78
|
# call-seq:
|
80
|
-
# File.magic( path ) -> string
|
79
|
+
# File.magic( path ) -> string or array
|
81
80
|
#
|
82
81
|
# Returns
|
83
82
|
#
|
@@ -91,7 +90,7 @@ class File
|
|
91
90
|
|
92
91
|
#
|
93
92
|
# call-seq:
|
94
|
-
# File.mime( path ) -> string
|
93
|
+
# File.mime( path ) -> string or array
|
95
94
|
#
|
96
95
|
# Returns
|
97
96
|
#
|
@@ -105,7 +104,7 @@ class File
|
|
105
104
|
|
106
105
|
#
|
107
106
|
# call-seq:
|
108
|
-
# File.type( path ) -> string
|
107
|
+
# File.type( path ) -> string or array
|
109
108
|
#
|
110
109
|
# Returns
|
111
110
|
#
|
data/lib/magic/core/string.rb
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
#
|
6
6
|
# core/string.rb
|
7
7
|
#
|
8
|
-
# Copyright 2013-
|
8
|
+
# Copyright 2013-2015 Krzysztof Wilczynski
|
9
9
|
#
|
10
10
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
11
11
|
# you may not use this file except in compliance with the License.
|
@@ -28,7 +28,7 @@
|
|
28
28
|
class String
|
29
29
|
#
|
30
30
|
# call-seq:
|
31
|
-
# string.magic -> string
|
31
|
+
# string.magic -> string or array
|
32
32
|
#
|
33
33
|
# Returns
|
34
34
|
#
|
@@ -38,12 +38,11 @@ class String
|
|
38
38
|
#
|
39
39
|
def magic(flags = Magic::NONE)
|
40
40
|
Magic.open(flags) {|mgc| mgc.buffer(self) }
|
41
|
-
rescue Magic::Error
|
42
41
|
end
|
43
42
|
|
44
43
|
#
|
45
44
|
# call-seq:
|
46
|
-
# string.mime -> string
|
45
|
+
# string.mime -> string or array
|
47
46
|
#
|
48
47
|
# Returns
|
49
48
|
#
|
@@ -57,7 +56,7 @@ class String
|
|
57
56
|
|
58
57
|
#
|
59
58
|
# call-seq:
|
60
|
-
# string.type -> string
|
59
|
+
# string.type -> string or array
|
61
60
|
#
|
62
61
|
# Returns
|
63
62
|
#
|
data/lib/magic/version.rb
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
#
|
6
6
|
# version.rb
|
7
7
|
#
|
8
|
-
# Copyright 2013-
|
8
|
+
# Copyright 2013-2015 Krzysztof Wilczynski
|
9
9
|
#
|
10
10
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
11
11
|
# you may not use this file except in compliance with the License.
|
@@ -26,7 +26,7 @@ class Magic
|
|
26
26
|
#
|
27
27
|
# Current version of _Magic_.
|
28
28
|
#
|
29
|
-
VERSION = '0.0.
|
29
|
+
VERSION = '0.2.0'.freeze
|
30
30
|
|
31
31
|
class << self
|
32
32
|
#
|
data/ruby-magic.gemspec
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
#
|
6
6
|
# ruby-magic.gemspec
|
7
7
|
#
|
8
|
-
# Copyright 2013-
|
8
|
+
# Copyright 2013-2015 Krzysztof Wilczynski
|
9
9
|
#
|
10
10
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
11
11
|
# you may not use this file except in compliance with the License.
|
@@ -23,7 +23,8 @@
|
|
23
23
|
signing_key = File.expand_path('~/.gem/kwilczynski-private.pem')
|
24
24
|
|
25
25
|
Gem::Specification.new do |s|
|
26
|
-
s.name
|
26
|
+
s.name = 'ruby-magic'
|
27
|
+
s.summary = 'File Magic in Ruby'
|
27
28
|
|
28
29
|
s.description = <<-EOS
|
29
30
|
File Magic in Ruby.
|
@@ -37,12 +38,10 @@ Simple interface to libmagic for Ruby Programming Language.
|
|
37
38
|
s.author = 'Krzysztof Wilczynski'
|
38
39
|
s.email = 'krzysztof.wilczynski@linux.com'
|
39
40
|
s.homepage = 'http://about.me/kwilczynski'
|
41
|
+
s.has_rdoc = true
|
40
42
|
|
41
|
-
s.
|
42
|
-
s.rubygems_version
|
43
|
-
s.has_rdoc = true
|
44
|
-
|
45
|
-
s.summary = 'File Magic in Ruby'
|
43
|
+
s.required_ruby_version = '>= 1.9.2'
|
44
|
+
s.rubygems_version = '~> 2.2.0'
|
46
45
|
|
47
46
|
s.files = Dir['ext/**/*.{c,h,rb}'] +
|
48
47
|
Dir['lib/**/*.rb'] +
|
@@ -56,11 +55,6 @@ Simple interface to libmagic for Ruby Programming Language.
|
|
56
55
|
s.require_paths << 'lib'
|
57
56
|
s.extensions << 'ext/magic/extconf.rb'
|
58
57
|
|
59
|
-
s.add_development_dependency 'rake', '~> 10.1', '>= 10.1.1'
|
60
|
-
s.add_development_dependency 'rdoc', '~> 4.1', '>= 4.1.1'
|
61
|
-
s.add_development_dependency 'test-unit', '~> 2.5', '>= 2.5.5'
|
62
|
-
s.add_development_dependency 'rake-compiler', '~> 0.9', '>= 0.9.2'
|
63
|
-
|
64
58
|
s.signing_key = signing_key if File.exists?(signing_key)
|
65
59
|
end
|
66
60
|
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
# :enddoc:
|
4
|
+
|
5
|
+
#
|
6
|
+
# helpers.rb
|
7
|
+
#
|
8
|
+
# Copyright 2013-2015 Krzysztof Wilczynski
|
9
|
+
#
|
10
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
11
|
+
# you may not use this file except in compliance with the License.
|
12
|
+
# You may obtain a copy of the License at
|
13
|
+
#
|
14
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
15
|
+
#
|
16
|
+
# Unless required by applicable law or agreed to in writing, software
|
17
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
18
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
19
|
+
# See the License for the specific language governing permissions and
|
20
|
+
# limitations under the License.
|
21
|
+
#
|
22
|
+
|
23
|
+
require 'stringio'
|
24
|
+
|
25
|
+
module MagicTestHelpers
|
26
|
+
def capture_stderr
|
27
|
+
saved_stderr, $stderr = $stderr, StringIO.new
|
28
|
+
[yield, $stderr.string]
|
29
|
+
ensure
|
30
|
+
$stderr = saved_stderr
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# vim: set ts=2 sw=2 sts=2 et :
|
35
|
+
# encoding: utf-8
|