ruby-magic 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: be72439dd976c929316ed38bd3cb1bdfd31436120e4d62db55858eac2c6a7645
4
- data.tar.gz: 976cd79869df839352f50f05fbd80117db27a52800812013266df50bde6fe342
3
+ metadata.gz: 1a066f36addb7d1c8cbe0858a67fa6a8e2103b3d365ab2d0ccda3027ea43a8ce
4
+ data.tar.gz: 3828bda59c9df6377991481d7f4fa9a8526e981804ff80dbb84aeba7eb316578
5
5
  SHA512:
6
- metadata.gz: b8c1a58140d2a7d7fe473d6310ab478f798ce1088b524a653b0f3535af1a368a52da887d3fd8102b9ec47c96a3c62ab883bc493d949278cb450b91a3c6b249b7
7
- data.tar.gz: 22715a80bc847c7cc4fc7ce58c098e36a0b32514f56ad39b0fadb4f8f7f62b2ac7ccd15101cd33b5b9ec8448494833a9199504b21b95d85b0cbfd92a0c1d8592
6
+ metadata.gz: 1ea5942f3c0f33f438f684437a3def3db633f18a279c84574f8a9ee0022d60e8852954b99ee32cc5ad7aa0fa5e10e1b1673e97575d72dfe28c862ac18476d551
7
+ data.tar.gz: 7ad49df412d72dd3d2b29e07509916e7aef46bf3058191019a290d07fed8792877fa623b9d327e4fa94fa21f49ef2fc8ad242777d3f398c53322a08413dd6a58
data/CONTRIBUTORS.md ADDED
@@ -0,0 +1,15 @@
1
+ # Ruby Magic contributors
2
+
3
+ We thank all of our [contributors][1]!
4
+
5
+ The following is a list of people who have contributed at some point to the code
6
+ of Ruby Magic. They are hereby acknowledged and collectively identified as "The
7
+ Ruby Magic Authors". Copyright is held by the original contributor according to
8
+ the versioning history; see [LICENSE](LICENSE) file.
9
+
10
+
11
+ - John Hawthorn ([@jhawthorn](https://github.com/jhawthorn))
12
+ - Shinya Maeda ([@dosuken123](https://github.com/dosuken123))
13
+ - Stan Hu ([@stanhu](https://github.com/stanhu))
14
+
15
+ [1]: https://github.com/kwilczynski/ruby-magic/graphs/contributors "Contributors"
data/NOTICE CHANGED
@@ -464,3 +464,42 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
464
464
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
465
465
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
466
466
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
467
+
468
+ * file
469
+
470
+ file(1) Command and the libmagic(3) library.
471
+
472
+ LICENSE:
473
+ BSD, CDDL
474
+
475
+ HOMEPAGE:
476
+ http://darwinsys.com/file
477
+
478
+ Copyright (c) Ian F. Darwin 1986, 1987, 1989, 1990, 1991, 1992, 1994, 1995.
479
+ Software written by Ian F. Darwin and others;
480
+ maintained 1994- Christos Zoulas.
481
+
482
+ This software is not subject to any export provision of the United States
483
+ Department of Commerce, and may be exported to any country or planet.
484
+
485
+ Redistribution and use in source and binary forms, with or without
486
+ modification, are permitted provided that the following conditions
487
+ are met:
488
+ 1. Redistributions of source code must retain the above copyright
489
+ notice immediately at the beginning of the file, without modification,
490
+ this list of conditions, and the following disclaimer.
491
+ 2. Redistributions in binary form must reproduce the above copyright
492
+ notice, this list of conditions and the following disclaimer in the
493
+ documentation and/or other materials provided with the distribution.
494
+
495
+ THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
496
+ ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
497
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
498
+ ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
499
+ ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
500
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
501
+ OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
502
+ HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
503
+ LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
504
+ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
505
+ SUCH DAMAGE.
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # File Magic in Ruby
2
2
 
3
- [![Build Status](https://travis-ci.org/kwilczynski/ruby-magic.svg)](https://travis-ci.org/kwilczynski/ruby-magic)
3
+ [![Build Status](https://travis-ci.com/kwilczynski/ruby-magic.svg)](https://travis-ci.com/kwilczynski/ruby-magic)
4
4
  [![Documentation Status](https://inch-ci.org/github/kwilczynski/ruby-magic.svg)](https://inch-ci.org/github/kwilczynski/ruby-magic)
5
5
  [![Gem Version](https://badge.fury.io/rb/ruby-magic.svg)](http://badge.fury.io/rb/ruby-magic)
6
6
  [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
@@ -14,13 +14,14 @@ Provides simple interface to [libmagic][1] for Ruby Programming Language.
14
14
  1. [Contributing](#contributing)
15
15
  2. [Versioning](#versioning)
16
16
  3. [Author](#author)
17
+ 4. [Contributors](#contributors)
17
18
  4. [Copyright](#copyright)
18
19
  5. [License](#license)
19
20
 
20
21
  ## Contributing
21
22
 
22
- See [CONTRIBUTING.md](CONTRIBUTING.md) for best practices and instructions on
23
- setting up your development environment.
23
+ See the [CONTRIBUTING.md](CONTRIBUTING.md) for best practices and instructions
24
+ on setting up your development environment.
24
25
 
25
26
  ## Versioning
26
27
 
@@ -31,6 +32,11 @@ For the versions available, see the tags on this repository.
31
32
 
32
33
  Krzysztof Wilczyński (<kw@linux.com>)
33
34
 
35
+ ## Contributors
36
+
37
+ See the [CONTRIBUTORS.md](CONTRIBUTORS.md) file for a list of contributors to
38
+ the project.
39
+
34
40
  ## Copyright
35
41
 
36
42
  Copyright 2013-2021 Krzysztof Wilczyński
data/ext/magic/common.h CHANGED
@@ -26,8 +26,11 @@ extern "C" {
26
26
  #include <errno.h>
27
27
  #include <assert.h>
28
28
  #include <sys/stat.h>
29
+
29
30
  #include <magic.h>
31
+
30
32
  #include <ruby.h>
33
+ #include <ruby/version.h>
31
34
 
32
35
  #if defined(HAVE_RUBY_IO_H)
33
36
  # include <ruby/io.h>
@@ -58,6 +61,12 @@ extern "C" {
58
61
  #define MAGIC_EXTENSION_SEPARATOR "/"
59
62
  #define MAGIC_CONTINUE_SEPARATOR "\n- "
60
63
 
64
+ #if !defined(MAGIC_SYSTEM_LIBRARIES)
65
+ # define MAGIC_STATUS_CHECK(x) x
66
+ #else
67
+ # define MAGIC_STATUS_CHECK(x) 1
68
+ #endif
69
+
61
70
  #if !defined(MAGIC_NO_CHECK_CSV)
62
71
  # define MAGIC_NO_CHECK_CSV 0
63
72
  #endif
@@ -82,6 +91,12 @@ extern "C" {
82
91
  #define RARRAY_EMPTY_P(a) (RARRAY_LEN(a) == 0)
83
92
  #define RARRAY_FIRST(a) (RARRAY_EMPTY_P(a) ? Qnil : rb_ary_entry((a), 0))
84
93
 
94
+ #if RUBY_API_VERSION_CODE > 20700
95
+ # define RVALUE_TYPE enum ruby_value_type
96
+ #else
97
+ # define RVALUE_TYPE int
98
+ #endif
99
+
85
100
  #define CLASS_NAME(o) (NIL_P((o)) ? "nil" : rb_obj_classname((o)))
86
101
 
87
102
  #if !defined(T_INTEGER)
data/ext/magic/extconf.rb CHANGED
@@ -1,42 +1,152 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'find'
3
4
  require 'mkmf'
4
- require 'digest'
5
- require 'open-uri'
6
-
7
- LIBMAGIC_TAG = '5.39'.freeze
8
-
9
- workdir = Dir.pwd
10
- libdir = File.join(workdir, 'file-' + LIBMAGIC_TAG)
11
- gemdir = File.expand_path(File.join(__dir__, '../..'))
12
- gem_ext_dir = File.join(gemdir, 'lib', 'ext')
13
- gem_include_dir = File.join(gem_ext_dir, 'include')
14
- gem_lib_dir = File.join(gem_ext_dir, 'lib')
15
- build_lib_dir = File.join(libdir, 'src', '.libs')
16
-
17
- expected_sha256 = 'f05d286a76d9556243d0cb05814929c2ecf3a5ba07963f8f70bfaaa70517fad1'
18
- filename = "#{workdir}/file.tar.gz"
19
-
20
- unless File.exist?(filename)
21
- File.open(filename, 'wb') do |target_file|
22
- URI.open("https://fossies.org/linux/misc/file-#{LIBMAGIC_TAG}.tar.gz", "User-Agent" => "RubyMagic/#{RUBY_DESCRIPTION}") do |read_file|
23
- target_file.write(read_file.read)
5
+ require 'pathname'
6
+
7
+ LIBMAGIC_TAG = '5.39'
8
+ LIBIMAGE_SHA256 = 'f05d286a76d9556243d0cb05814929c2ecf3a5ba07963f8f70bfaaa70517fad1'
9
+
10
+ # helpful constants
11
+ PACKAGE_ROOT_DIR = File.expand_path(File.join(File.dirname(__FILE__), '..', '..'))
12
+
13
+ # The gem version constraint in the Rakefile is not respected at install time.
14
+ # Keep this version in sync with the one in the Rakefile !
15
+ REQUIRED_MINI_PORTILE_VERSION = "~> 2.5.0"
16
+
17
+ MAGIC_HELP_MESSAGE = <<~HELP
18
+ USAGE: ruby #{$0} [options]
19
+
20
+ Flags that are always valid:
21
+
22
+ --use-system-libraries
23
+ --enable-system-libraries
24
+ Use system libraries instead of building and using the packaged libraries.
25
+
26
+ --disable-system-libraries
27
+ Use the packaged libraries, and ignore the system libraries. This is the default on most
28
+ platforms, and overrides `--use-system-libraries` and the environment variable
29
+ `MAGIC_USE_SYSTEM_LIBRARIES`.
30
+
31
+ --disable-clean
32
+ Do not clean out intermediate files after successful build.
33
+
34
+ Flags only used when building and using the packaged libraries:
35
+
36
+ --disable-static
37
+ Do not statically link packaged libraries, instead use shared libraries.
38
+
39
+ --enable-cross-build
40
+ Enable cross-build mode. (You probably do not want to set this manually.)
41
+
42
+ Flags only used when using system libraries:
43
+
44
+ Related to libmagic:
45
+
46
+ --with-magic-dir=DIRECTORY
47
+ Look for libmagic headers and library in DIRECTORY.
48
+
49
+ --with-magic-lib=DIRECTORY
50
+ Look for libmagic library in DIRECTORY.
51
+
52
+ --with-magic-include=DIRECTORY
53
+ Look for libmagic headers in DIRECTORY.
54
+
55
+ Environment variables used:
56
+
57
+ CC
58
+ Use this path to invoke the compiler instead of `RbConfig::CONFIG['CC']`
59
+
60
+ CPPFLAGS
61
+ If this string is accepted by the C preprocessor, add it to the flags passed to the C preprocessor
62
+
63
+ CFLAGS
64
+ If this string is accepted by the compiler, add it to the flags passed to the compiler
65
+
66
+ LDFLAGS
67
+ If this string is accepted by the linker, add it to the flags passed to the linker
68
+
69
+ LIBS
70
+ Add this string to the flags passed to the linker
71
+ HELP
72
+
73
+ def process_recipe(name, version, static_p, cross_p)
74
+ require 'rubygems'
75
+ gem('mini_portile2', REQUIRED_MINI_PORTILE_VERSION)
76
+ require 'mini_portile2'
77
+ message("Using mini_portile version #{MiniPortile::VERSION}\n")
78
+
79
+ MiniPortile.new(name, version).tap do |recipe|
80
+ # Prefer host_alias over host in order to use i586-mingw32msvc as
81
+ # correct compiler prefix for cross build, but use host if not set.
82
+ recipe.host = RbConfig::CONFIG["host_alias"].empty? ? RbConfig::CONFIG["host"] : RbConfig::CONFIG["host_alias"]
83
+ recipe.target = File.join(PACKAGE_ROOT_DIR, "ports")
84
+ recipe.configure_options << "--libdir=#{File.join(recipe.path, 'lib')}"
85
+
86
+ yield recipe
87
+
88
+ env = Hash.new do |hash, key|
89
+ hash[key] = (ENV[key]).to_s
90
+ end
91
+
92
+ recipe.configure_options.flatten!
93
+
94
+ recipe.configure_options = [
95
+ "--disable-silent-rules",
96
+ "--disable-dependency-tracking",
97
+ "--enable-fsect-man5"
98
+ ]
99
+
100
+ if static_p
101
+ recipe.configure_options += [
102
+ "--disable-shared",
103
+ "--enable-static",
104
+ ]
105
+ env["CFLAGS"] = concat_flags(env["CFLAGS"], "-fPIC")
106
+ else
107
+ recipe.configure_options += [
108
+ "--enable-shared",
109
+ "--disable-static",
110
+ ]
111
+ end
112
+
113
+ if cross_p
114
+ recipe.configure_options += [
115
+ "--target=#{recipe.host}",
116
+ "--host=#{recipe.host}",
117
+ ]
24
118
  end
25
- end
26
119
 
27
- checksum = Digest::SHA256.hexdigest(File.read(filename))
120
+ recipe.configure_options += env.map do |key, value|
121
+ "#{key}=#{value.strip}"
122
+ end
28
123
 
29
- if checksum != expected_sha256
30
- raise "SHA256 of #{filename} does not match: got #{checksum}, expected #{expected_sha256}"
124
+ recipe.cook
125
+ recipe.activate
31
126
  end
32
127
  end
33
128
 
34
- system("tar -xzf #{filename}") || raise('ERROR')
35
- system("cd #{libdir} && ./configure --prefix=#{gem_ext_dir} && make install") || raise('ERROR')
129
+ #
130
+ # utility functions
131
+ #
132
+ def config_clean?
133
+ enable_config('clean', true)
134
+ end
135
+
136
+ def config_static?
137
+ default_static = !truffle?
138
+ enable_config("static", default_static)
139
+ end
140
+
141
+ def config_cross_build?
142
+ enable_config("cross-build")
143
+ end
36
144
 
37
- $LOCAL_LIBS << '-lmagic'
38
- $LIBPATH << gem_lib_dir
39
- $CFLAGS << " -I #{libdir}/src"
145
+ def config_system_libraries?
146
+ enable_config("system-libraries", ENV.key?("MAGIC_USE_SYSTEM_LIBRARIES")) do |_, default|
147
+ arg_config('--use-system-libraries', default)
148
+ end
149
+ end
40
150
 
41
151
  def darwin?
42
152
  RbConfig::CONFIG['target_os'] =~ /darwin/
@@ -46,38 +156,129 @@ def windows?
46
156
  RbConfig::CONFIG['target_os'] =~ /mswin|mingw32|windows/
47
157
  end
48
158
 
159
+ def truffle?
160
+ ::RUBY_ENGINE == 'truffleruby'
161
+ end
162
+
163
+ def concat_flags(*args)
164
+ args.compact.join(" ")
165
+ end
166
+
167
+ def do_help
168
+ print(MAGIC_HELP_MESSAGE)
169
+ exit!(0)
170
+ end
171
+
172
+ def do_clean
173
+ root = Pathname(PACKAGE_ROOT_DIR)
174
+ pwd = Pathname(Dir.pwd)
175
+
176
+ # Skip if this is a development work tree
177
+ unless (root + '.git').exist?
178
+ message("Cleaning files only used during build.\n")
179
+
180
+ # (root + 'tmp') cannot be removed at this stage because
181
+ # libmagic.so is yet to be copied to lib.
182
+
183
+ # clean the ports build directory
184
+ Pathname.glob(pwd.join('tmp', '*', 'ports')) do |dir|
185
+ FileUtils.rm_rf(dir, verbose: true)
186
+ end
187
+
188
+ FileUtils.rm_rf(root + 'ports' + 'archives', verbose: true)
189
+
190
+ # Remove everything but share/ directory
191
+ remove_paths = %w[bin include]
192
+ remove_paths << 'lib' if config_static?
193
+
194
+ Pathname.glob(File.join(root, 'ports', '*', 'libmagic', '*')) do |dir|
195
+ remove_paths.each do |path|
196
+ remove_dir = File.join(dir, path)
197
+ FileUtils.rm_rf(remove_dir, verbose: true)
198
+ end
199
+ end
200
+ end
201
+
202
+ exit!(0)
203
+ end
204
+
205
+ #
206
+ # main
207
+ #
208
+ do_help if arg_config('--help')
209
+ do_clean if arg_config('--clean')
210
+
49
211
  if ENV['CC']
50
212
  RbConfig::CONFIG['CC'] = RbConfig::MAKEFILE_CONFIG['CC'] = ENV['CC']
51
213
  end
52
214
 
53
215
  ENV['CC'] = RbConfig::CONFIG['CC']
54
216
 
55
- $CFLAGS += ' -std=c99 -fPIC'
56
- $CFLAGS += ' -Wall -Wextra -pedantic'
217
+ append_cflags(ENV["CFLAGS"].split) if ENV["CFLAGS"]
218
+ append_cppflags(ENV["CPPFLAGS"].split) if ENV["CPPFLAGS"]
219
+ append_ldflags(ENV["LDFLAGS"].split) if ENV["LDFLAGS"]
220
+
221
+ $LIBS = concat_flags($LIBS, ENV["LIBS"]) if ENV["LIBS"]
222
+
223
+ if config_system_libraries?
224
+ message "Building ruby-magic using system libraries.\n"
225
+
226
+ $CPPFLAGS += " -DMAGIC_SYSTEM_LIBRARIES"
227
+
228
+ dir_config('magic')
229
+ else
230
+ message "Building ruby-magic using packaged libraries.\n"
231
+
232
+ static_p = config_static?
233
+ message "Static linking is #{static_p ? 'enabled' : 'disabled'}.\n"
234
+ cross_build_p = config_cross_build?
235
+ message "Cross build is #{cross_build_p ? 'enabled' : 'disabled'}.\n"
236
+
237
+ libmagic_recipe = process_recipe('libmagic', LIBMAGIC_TAG, static_p, cross_build_p) do |recipe|
238
+ recipe.files = [{
239
+ url: "https://ruby-magic.s3.eu-central-1.amazonaws.com/file-#{recipe.version}.tar.gz",
240
+ sha256: LIBIMAGE_SHA256
241
+ }]
242
+ end
243
+
244
+ $LIBPATH = [File.join(libmagic_recipe.path, 'lib')]
245
+ $CFLAGS << " -I#{File.join(libmagic_recipe.path, 'include')} "
246
+ $LDFLAGS += " -Wl,-rpath,#{libmagic_recipe.path}/lib"
247
+
248
+ if static_p
249
+ ENV['PKG_CONFIG_PATH'] = "#{libmagic_recipe.path}/lib/pkgconfig"
250
+ # mkmf appends -- to the first option
251
+ $LIBS += " " + pkg_config('libmagic', 'libs --static')
252
+ $LIBS += " " + File.join(libmagic_recipe.path, 'lib', "libmagic.#{$LIBEXT}")
253
+ end
254
+ end
255
+
256
+ $CFLAGS += ' -std=c99'
57
257
 
58
258
  if RbConfig::CONFIG['CC'] =~ /gcc/
59
259
  $CFLAGS += ' -O3' unless $CFLAGS =~ /-O\d/
60
- $CFLAGS += ' -Wcast-qual -Wwrite-strings -Wconversion -Wmissing-noreturn -Winline'
260
+ end
261
+
262
+ %w[
263
+ -Wcast-qual
264
+ -Wwrite-strings
265
+ -Wconversion
266
+ -Wmissing-noreturn
267
+ -Winline
268
+ ].select do |flag|
269
+ try_link('int main(void) { return 0; }', flag)
270
+ end.each do |flag|
271
+ $CFLAGS += " " + flag
61
272
  end
62
273
 
63
274
  unless darwin?
64
- $LDFLAGS += ' -Wl,--as-needed -Wl,--no-undefined'
275
+ $LDFLAGS += ' -Wl,--as-needed -Wl,--exclude-libs,ALL'
65
276
  end
66
277
 
67
278
  if windows?
68
279
  $LDFLAGS += ' -static-libgcc'
69
280
  end
70
281
 
71
- %w[
72
- CFLAGS
73
- CXXFLAGS
74
- CPPFLAGS
75
- ].each do |variable|
76
- $CFLAGS += format(' %s', ENV[variable]) if ENV[variable]
77
- end
78
-
79
- $LDFLAGS += format(' %s', ENV['LDFLAGS']) if ENV['LDFLAGS']
80
-
81
282
  unless have_header('ruby.h')
82
283
  abort "\n" + (<<-EOS).gsub(/^[ ]{,3}/, '') + "\n"
83
284
  You appear to be missing Ruby development libraries and/or header
@@ -188,7 +389,17 @@ end
188
389
  have_func(f)
189
390
  end
190
391
 
191
- dir_config('magic', [gem_include_dir], [gem_lib_dir])
192
-
193
392
  create_header
194
393
  create_makefile('magic/magic')
394
+
395
+ if config_clean?
396
+ # Do not clean if run in a development work tree.
397
+ File.open('Makefile', 'at') do |mk|
398
+ mk.print(<<~EOF)
399
+
400
+ all: clean-ports
401
+ clean-ports: $(DLLIB)
402
+ \t-$(Q)$(RUBY) $(srcdir)/extconf.rb --clean --#{static_p ? 'enable' : 'disable'}-static
403
+ EOF
404
+ end
405
+ end
@@ -208,6 +208,14 @@ magic_errno_wrapper(magic_t magic)
208
208
  inline const char*
209
209
  magic_getpath_wrapper(void)
210
210
  {
211
+ /*
212
+ * The second argument translates to same value as the
213
+ * FILE_LOAD constant, which when used results in this
214
+ * function calling the get_default_magic() internally.
215
+ *
216
+ * N.B. magic_getpath() also honors the "MAGIC"
217
+ * environment variable."
218
+ */
211
219
  return magic_getpath(NULL, 0);
212
220
  }
213
221
 
@@ -8,17 +8,17 @@ static int rb_mgc_do_not_auto_load;
8
8
  static int rb_mgc_do_not_stop_on_error;
9
9
  static int rb_mgc_warning;
10
10
 
11
- ID id_at_flags;
12
- ID id_at_paths;
11
+ static ID id_at_flags;
12
+ static ID id_at_paths;
13
13
 
14
- VALUE rb_cMagic = Qundef;
14
+ static VALUE rb_cMagic = Qundef;
15
15
 
16
- VALUE rb_mgc_eError = Qundef;
17
- VALUE rb_mgc_eMagicError = Qundef;
18
- VALUE rb_mgc_eLibraryError = Qundef;
19
- VALUE rb_mgc_eParameterError = Qundef;
20
- VALUE rb_mgc_eFlagsError = Qundef;
21
- VALUE rb_mgc_eNotImplementedError = Qundef;
16
+ static VALUE rb_mgc_eError = Qundef;
17
+ static VALUE rb_mgc_eMagicError = Qundef;
18
+ static VALUE rb_mgc_eLibraryError = Qundef;
19
+ static VALUE rb_mgc_eParameterError = Qundef;
20
+ static VALUE rb_mgc_eFlagsError = Qundef;
21
+ static VALUE rb_mgc_eNotImplementedError = Qundef;
22
22
 
23
23
  void Init_magic(void);
24
24
 
@@ -742,6 +742,8 @@ rb_mgc_file(VALUE object, VALUE value)
742
742
  magic_arguments_t ma;
743
743
  const char *empty = "(null)";
744
744
 
745
+ UNUSED(empty);
746
+
745
747
  if (NIL_P(value))
746
748
  goto error;
747
749
 
@@ -761,7 +763,7 @@ rb_mgc_file(VALUE object, VALUE value)
761
763
  ma.type.file.path = RVAL2CSTR(value);
762
764
 
763
765
  MAGIC_SYNCHRONIZED(magic_file_internal, &ma);
764
- if (!ma.result) {
766
+ if (ma.status < 0 && !ma.result) {
765
767
  /*
766
768
  * Handle the case when the "ERROR" flag is set regardless of the
767
769
  * current version of the underlying Magic library.
@@ -777,8 +779,8 @@ rb_mgc_file(VALUE object, VALUE value)
777
779
  */
778
780
  if (mo->stop_on_errors || (ma.flags & MAGIC_ERROR))
779
781
  MAGIC_LIBRARY_ERROR(ma.cookie);
780
- else
781
- ma.result = magic_error_wrapper(ma.cookie);
782
+
783
+ ma.result = magic_error_wrapper(ma.cookie);
782
784
  }
783
785
  if (!ma.result)
784
786
  MAGIC_GENERIC_ERROR(rb_mgc_eMagicError, EINVAL, E_UNKNOWN);
@@ -824,7 +826,7 @@ rb_mgc_buffer(VALUE object, VALUE value)
824
826
  ma.type.buffers.sizes = (size_t *)RSTRING_LEN(value);
825
827
 
826
828
  MAGIC_SYNCHRONIZED(magic_buffer_internal, &ma);
827
- if (!ma.result)
829
+ if (ma.status < 0)
828
830
  MAGIC_LIBRARY_ERROR(ma.cookie);
829
831
 
830
832
  assert(ma.result != NULL && \
@@ -861,7 +863,7 @@ rb_mgc_descriptor(VALUE object, VALUE value)
861
863
  MAGIC_SYNCHRONIZED(magic_descriptor_internal, &ma);
862
864
  local_errno = errno;
863
865
 
864
- if (!ma.result) {
866
+ if (ma.status < 0) {
865
867
  if (local_errno == EBADF)
866
868
  rb_raise(rb_eIOError, "Bad file descriptor");
867
869
 
@@ -931,6 +933,8 @@ nogvl_magic_file(void *data)
931
933
  ma->result = magic_file_wrapper(ma->cookie,
932
934
  ma->type.file.path,
933
935
  ma->flags);
936
+
937
+ ma->status = !ma->result ? -1 : 0;
934
938
  return NULL;
935
939
  }
936
940
 
@@ -942,6 +946,8 @@ nogvl_magic_descriptor(void *data)
942
946
  ma->result = magic_descriptor_wrapper(ma->cookie,
943
947
  ma->type.file.fd,
944
948
  ma->flags);
949
+
950
+ ma->status = !ma->result ? -1 : 0;
945
951
  return NULL;
946
952
  }
947
953
 
@@ -999,7 +1005,14 @@ magic_close_internal(void *data)
999
1005
  static inline VALUE
1000
1006
  magic_load_internal(void *data)
1001
1007
  {
1002
- return (VALUE)NOGVL(nogvl_magic_load, data);
1008
+ magic_arguments_t *ma = data;
1009
+ int old_flags = ma->flags;
1010
+
1011
+ NOGVL(nogvl_magic_load, ma);
1012
+ if (MAGIC_STATUS_CHECK(ma->status < 0))
1013
+ magic_setflags_wrapper(ma->cookie, old_flags);
1014
+
1015
+ return (VALUE)NULL;
1003
1016
  }
1004
1017
 
1005
1018
  static inline VALUE
@@ -1018,34 +1031,46 @@ magic_load_buffers_internal(void *data)
1018
1031
  static inline VALUE
1019
1032
  magic_compile_internal(void *data)
1020
1033
  {
1021
- return (VALUE)NOGVL(nogvl_magic_compile, data);
1034
+ magic_arguments_t *ma = data;
1035
+ int old_flags = ma->flags;
1036
+
1037
+ NOGVL(nogvl_magic_compile, ma);
1038
+ if (MAGIC_STATUS_CHECK(ma->status < 0))
1039
+ magic_setflags_wrapper(ma->cookie, old_flags);
1040
+
1041
+ return (VALUE)NULL;
1022
1042
  }
1023
1043
 
1024
1044
  static inline VALUE
1025
1045
  magic_check_internal(void *data)
1026
1046
  {
1027
- return (VALUE)NOGVL(nogvl_magic_check, data);
1047
+ magic_arguments_t *ma = data;
1048
+ int old_flags = ma->flags;
1049
+
1050
+ NOGVL(nogvl_magic_check, ma);
1051
+ if (MAGIC_STATUS_CHECK(ma->status < 0))
1052
+ magic_setflags_wrapper(ma->cookie, old_flags);
1053
+
1054
+ return (VALUE)NULL;
1028
1055
  }
1029
1056
 
1030
1057
  static VALUE
1031
1058
  magic_file_internal(void *data)
1032
1059
  {
1033
- int local_errno;
1034
- int old_flags = 0;
1035
- int restore_flags = 0;
1036
1060
  magic_arguments_t *ma = data;
1061
+ int old_flags = ma->flags;
1062
+ int restore_flags;
1063
+ int local_errno;
1037
1064
 
1038
- if (ma->stop_on_errors) {
1039
- old_flags = ma->flags;
1065
+ if (ma->stop_on_errors)
1040
1066
  ma->flags |= MAGIC_ERROR;
1041
- restore_flags = 1;
1042
- }
1043
- if (ma->flags & MAGIC_CONTINUE) {
1044
- old_flags = ma->flags;
1067
+
1068
+ if (ma->flags & MAGIC_CONTINUE)
1045
1069
  ma->flags |= MAGIC_RAW;
1046
- restore_flags = 1;
1047
- }
1048
- if (restore_flags && ma->flags)
1070
+
1071
+ restore_flags = old_flags != ma->flags;
1072
+
1073
+ if (restore_flags)
1049
1074
  magic_setflags_wrapper(ma->cookie, ma->flags);
1050
1075
 
1051
1076
  NOGVL(nogvl_magic_file, ma);
@@ -1057,12 +1082,10 @@ magic_file_internal(void *data)
1057
1082
  * Magic library itself, and if that does not work, then from
1058
1083
  * the saved errno value.
1059
1084
  */
1060
- if (magic_errno_wrapper(ma->cookie))
1061
- ma->status = -1;
1062
- else if (local_errno)
1085
+ if (magic_errno_wrapper(ma->cookie) || local_errno)
1063
1086
  ma->status = -1;
1064
1087
 
1065
- if (restore_flags && old_flags)
1088
+ if (restore_flags)
1066
1089
  magic_setflags_wrapper(ma->cookie, old_flags);
1067
1090
 
1068
1091
  return (VALUE)NULL;
@@ -1071,23 +1094,26 @@ magic_file_internal(void *data)
1071
1094
  static VALUE
1072
1095
  magic_buffer_internal(void *data)
1073
1096
  {
1074
- int old_flags = 0;
1075
- int restore_flags = 0;
1076
1097
  magic_arguments_t *ma = data;
1098
+ int old_flags = ma->flags;
1099
+ int restore_flags;
1077
1100
 
1078
- if (ma->flags & MAGIC_CONTINUE) {
1079
- old_flags = ma->flags;
1101
+ if (ma->flags & MAGIC_CONTINUE)
1080
1102
  ma->flags |= MAGIC_RAW;
1081
- restore_flags = 1;
1082
- }
1083
- if (restore_flags && ma->flags)
1103
+
1104
+ restore_flags = old_flags != ma->flags;
1105
+
1106
+ if (restore_flags)
1084
1107
  magic_setflags_wrapper(ma->cookie, ma->flags);
1085
1108
 
1086
1109
  ma->result = magic_buffer_wrapper(ma->cookie,
1087
1110
  (const void *)ma->type.buffers.pointers,
1088
1111
  (size_t)ma->type.buffers.sizes,
1089
1112
  ma->flags);
1090
- if (restore_flags && old_flags)
1113
+
1114
+ ma->status = !ma->result ? -1 : 0;
1115
+
1116
+ if (restore_flags)
1091
1117
  magic_setflags_wrapper(ma->cookie, old_flags);
1092
1118
 
1093
1119
  return (VALUE)NULL;
@@ -1096,21 +1122,21 @@ magic_buffer_internal(void *data)
1096
1122
  static VALUE
1097
1123
  magic_descriptor_internal(void *data)
1098
1124
  {
1099
- int old_flags = 0;
1100
- int restore_flags = 0;
1101
1125
  magic_arguments_t *ma = data;
1126
+ int old_flags = ma->flags;
1127
+ int restore_flags;
1102
1128
 
1103
- if (ma->flags & MAGIC_CONTINUE) {
1104
- old_flags = ma->flags;
1129
+ if (ma->flags & MAGIC_CONTINUE)
1105
1130
  ma->flags |= MAGIC_RAW;
1106
- restore_flags = 1;
1107
- }
1108
- if (restore_flags && ma->flags)
1131
+
1132
+ restore_flags = old_flags != ma->flags;
1133
+
1134
+ if (restore_flags)
1109
1135
  magic_setflags_wrapper(ma->cookie, ma->flags);
1110
1136
 
1111
1137
  NOGVL(nogvl_magic_descriptor, ma);
1112
1138
 
1113
- if (restore_flags && old_flags)
1139
+ if (restore_flags)
1114
1140
  magic_setflags_wrapper(ma->cookie, old_flags);
1115
1141
 
1116
1142
  return (VALUE)NULL;
@@ -1243,6 +1269,8 @@ magic_library_error(VALUE klass, void *data)
1243
1269
  const char *empty = "(null)";
1244
1270
  magic_t cookie = data;
1245
1271
 
1272
+ UNUSED(empty);
1273
+
1246
1274
  assert(cookie != NULL && \
1247
1275
  "Must be a valid pointer to `magic_t' type");
1248
1276
 
@@ -1268,7 +1296,7 @@ magic_lock(VALUE object, VALUE(*function)(ANYARGS), void *data)
1268
1296
  magic_object_t *mo;
1269
1297
 
1270
1298
  MAGIC_OBJECT(mo);
1271
- rb_funcall(mo->mutex, rb_intern("lock"), 0, Qundef);
1299
+ rb_funcall(mo->mutex, rb_intern("lock"), 0);
1272
1300
 
1273
1301
  return rb_ensure(function, (VALUE)data, magic_unlock, object);
1274
1302
  }
@@ -1279,7 +1307,7 @@ magic_unlock(VALUE object)
1279
1307
  magic_object_t *mo;
1280
1308
 
1281
1309
  MAGIC_OBJECT(mo);
1282
- rb_funcall(mo->mutex, rb_intern("unlock"), 0, Qundef);
1310
+ rb_funcall(mo->mutex, rb_intern("unlock"), 0);
1283
1311
 
1284
1312
  return Qnil;
1285
1313
  }
@@ -156,8 +156,8 @@ static const char *errors[] = {
156
156
  static inline VALUE
157
157
  magic_shift(VALUE v)
158
158
  {
159
- return ARRAY_P(v) ? \
160
- rb_funcall(v, rb_intern("shift"), 0, Qundef) : \
159
+ return ARRAY_P(v) ? \
160
+ rb_funcall(v, rb_intern("shift"), 0) : \
161
161
  Qnil;
162
162
  }
163
163
 
@@ -180,8 +180,8 @@ magic_join(VALUE a, VALUE b)
180
180
  static inline VALUE
181
181
  magic_flatten(VALUE v)
182
182
  {
183
- return ARRAY_P(v) ? \
184
- rb_funcall(v, rb_intern("flatten"), 0, Qundef) : \
183
+ return ARRAY_P(v) ? \
184
+ rb_funcall(v, rb_intern("flatten"), 0) : \
185
185
  Qnil;
186
186
  }
187
187
 
@@ -192,7 +192,7 @@ magic_fileno(VALUE object)
192
192
  rb_io_t *io;
193
193
 
194
194
  if (rb_respond_to(object, rb_intern("fileno"))) {
195
- object = rb_funcall(object, rb_intern("fileno"), 0, Qundef);
195
+ object = rb_funcall(object, rb_intern("fileno"), 0);
196
196
  return NUM2INT(object);
197
197
  }
198
198
 
@@ -213,19 +213,19 @@ magic_path(VALUE object)
213
213
  return object;
214
214
 
215
215
  if (rb_respond_to(object, rb_intern("to_path")))
216
- return rb_funcall(object, rb_intern("to_path"), 0, Qundef);
216
+ return rb_funcall(object, rb_intern("to_path"), 0);
217
217
 
218
218
  if (rb_respond_to(object, rb_intern("path")))
219
- return rb_funcall(object, rb_intern("path"), 0, Qundef);
219
+ return rb_funcall(object, rb_intern("path"), 0);
220
220
 
221
221
  if (rb_respond_to(object, rb_intern("to_s")))
222
- return rb_funcall(object, rb_intern("to_s"), 0, Qundef);
222
+ return rb_funcall(object, rb_intern("to_s"), 0);
223
223
 
224
224
  return Qnil;
225
225
  }
226
226
 
227
227
  static inline void
228
- magic_check_type(VALUE object, int type)
228
+ magic_check_type(VALUE object, RVALUE_TYPE type)
229
229
  {
230
230
  VALUE boolean = Qundef;
231
231
 
@@ -250,55 +250,9 @@ magic_check_type_array_of_strings(VALUE object)
250
250
  }
251
251
  }
252
252
 
253
- RUBY_EXTERN ID id_at_flags;
254
- RUBY_EXTERN ID id_at_paths;
255
-
256
- RUBY_EXTERN VALUE rb_cMagic;
257
-
258
- RUBY_EXTERN VALUE rb_mgc_eError;
259
- RUBY_EXTERN VALUE rb_mgc_eMagicError;
260
- RUBY_EXTERN VALUE rb_mgc_eLibraryError;
261
- RUBY_EXTERN VALUE rb_mgc_eParameterError;
262
- RUBY_EXTERN VALUE rb_mgc_eFlagsError;
263
- RUBY_EXTERN VALUE rb_mgc_eNotImplementedError;
264
-
265
- RUBY_EXTERN VALUE rb_mgc_get_do_not_auto_load_global(VALUE object);
266
- RUBY_EXTERN VALUE rb_mgc_set_do_not_auto_load_global(VALUE object,
267
- VALUE value);
268
-
269
- RUBY_EXTERN VALUE rb_mgc_get_do_not_stop_on_error_global(VALUE object);
270
- RUBY_EXTERN VALUE rb_mgc_set_do_not_stop_on_error_global(VALUE object,
271
- VALUE value);
272
-
273
- RUBY_EXTERN VALUE rb_mgc_initialize(VALUE object, VALUE arguments);
274
-
275
- RUBY_EXTERN VALUE rb_mgc_get_do_not_stop_on_error(VALUE object);
276
- RUBY_EXTERN VALUE rb_mgc_set_do_not_stop_on_error(VALUE object, VALUE value);
277
-
278
- RUBY_EXTERN VALUE rb_mgc_open_p(VALUE object);
279
- RUBY_EXTERN VALUE rb_mgc_close(VALUE object);
280
- RUBY_EXTERN VALUE rb_mgc_close_p(VALUE object);
281
-
282
- RUBY_EXTERN VALUE rb_mgc_get_paths(VALUE object);
283
-
284
- RUBY_EXTERN VALUE rb_mgc_get_parameter(VALUE object, VALUE tag);
285
- RUBY_EXTERN VALUE rb_mgc_set_parameter(VALUE object, VALUE tag, VALUE value);
286
-
287
- RUBY_EXTERN VALUE rb_mgc_get_flags(VALUE object);
288
- RUBY_EXTERN VALUE rb_mgc_set_flags(VALUE object, VALUE value);
289
-
290
- RUBY_EXTERN VALUE rb_mgc_load(VALUE object, VALUE arguments);
291
- RUBY_EXTERN VALUE rb_mgc_load_buffers(VALUE object, VALUE arguments);
292
- RUBY_EXTERN VALUE rb_mgc_load_p(VALUE object);
293
-
294
- RUBY_EXTERN VALUE rb_mgc_compile(VALUE object, VALUE arguments);
295
- RUBY_EXTERN VALUE rb_mgc_check(VALUE object, VALUE arguments);
296
-
297
- RUBY_EXTERN VALUE rb_mgc_file(VALUE object, VALUE value);
298
- RUBY_EXTERN VALUE rb_mgc_buffer(VALUE object, VALUE value);
299
- RUBY_EXTERN VALUE rb_mgc_descriptor(VALUE object, VALUE value);
300
-
301
- RUBY_EXTERN VALUE rb_mgc_version(VALUE object);
253
+ VALUE rb_mgc_close_p(VALUE object);
254
+ VALUE rb_mgc_load(VALUE object, VALUE arguments);
255
+ VALUE rb_mgc_descriptor(VALUE object, VALUE value);
302
256
 
303
257
  #if defined(__cplusplus)
304
258
  }
data/lib/magic/version.rb CHANGED
@@ -4,7 +4,7 @@ class Magic
4
4
  #
5
5
  # Current version of _Magic_.
6
6
  #
7
- VERSION = '0.3.1'.freeze
7
+ VERSION = '0.3.2'.freeze
8
8
 
9
9
  class << self
10
10
  #
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-magic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Krzysztof Wilczyński
@@ -9,8 +9,22 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain:
11
11
  - kwilczynski-public.pem
12
- date: 2021-03-25 00:00:00.000000000 Z
13
- dependencies: []
12
+ date: 2021-04-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mini_portile2
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: 2.5.0
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: 2.5.0
14
28
  description: |
15
29
  File Magic in Ruby.
16
30
 
@@ -23,11 +37,11 @@ extra_rdoc_files: []
23
37
  files:
24
38
  - AUTHORS
25
39
  - CHANGELOG.md
40
+ - CONTRIBUTORS.md
26
41
  - COPYRIGHT
27
42
  - LICENSE
28
43
  - NOTICE
29
44
  - README.md
30
- - VERSION
31
45
  - ext/magic/common.h
32
46
  - ext/magic/extconf.rb
33
47
  - ext/magic/functions.c
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 0.3.1