ruby-magic 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # File Magic in Ruby
2
+
3
+ [![Build Status](https://travis-ci.org/kwilczynski/ruby-magic.svg)](https://travis-ci.org/kwilczynski/ruby-magic)
4
+ [![Documentation Status](https://inch-ci.org/github/kwilczynski/ruby-magic.svg)](https://inch-ci.org/github/kwilczynski/ruby-magic)
5
+ [![Gem Version](https://badge.fury.io/rb/ruby-magic.svg)](http://badge.fury.io/rb/ruby-magic)
6
+ [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
7
+
8
+ ## Introduction
9
+
10
+ Provides simple interface to [libmagic][1] for Ruby Programming Language.
11
+
12
+ ## Table of Contents
13
+
14
+ 1. [Contributing](#contributing)
15
+ 2. [Versioning](#versioning)
16
+ 3. [Author](#author)
17
+ 4. [Copyright](#copyright)
18
+ 5. [License](#license)
19
+
20
+ ## Contributing
21
+
22
+ See [CONTRIBUTING.md](CONTRIBUTING.md) for best practices and instructions on
23
+ setting up your development environment.
24
+
25
+ ## Versioning
26
+
27
+ This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
28
+ For the versions available, see the tags on this repository.
29
+
30
+ ## Author
31
+
32
+ Krzysztof Wilczyński (<kw@linux.com>)
33
+
34
+ ## Copyright
35
+
36
+ Copyright 2013-2021 Krzysztof Wilczyński
37
+
38
+ ## License
39
+
40
+ This project is licensed under the terms of the Apache License, Version 2.0 license.
41
+ To view the full license see the included [LICENSE](LICENSE) file.
42
+
43
+ [1]: https://en.wikipedia.org/wiki/File_(command)
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.3.0
data/ext/magic/common.h CHANGED
@@ -1,23 +1,3 @@
1
- /* :enddoc: */
2
-
3
- /*
4
- * common.h
5
- *
6
- * Copyright 2013-2015 Krzysztof Wilczynski
7
- *
8
- * Licensed under the Apache License, Version 2.0 (the "License");
9
- * you may not use this file except in compliance with the License.
10
- * You may obtain a copy of the License at
11
- *
12
- * http://www.apache.org/licenses/LICENSE-2.0
13
- *
14
- * Unless required by applicable law or agreed to in writing, software
15
- * distributed under the License is distributed on an "AS IS" BASIS,
16
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
- * See the License for the specific language governing permissions and
18
- * limitations under the License.
19
- */
20
-
21
1
  #if !defined(_COMMON_H)
22
2
  #define _COMMON_H 1
23
3
 
@@ -29,12 +9,17 @@
29
9
  # define _BSD_SOURCE 1
30
10
  #endif
31
11
 
12
+ #if !defined(_DEFAULT_SOURCE)
13
+ # define _DEFAULT_SOURCE 1
14
+ #endif
15
+
32
16
  #if defined(__cplusplus)
33
17
  extern "C" {
34
18
  #endif
35
19
 
36
20
  #include <stdio.h>
37
21
  #include <stdlib.h>
22
+ #include <limits.h>
38
23
  #include <string.h>
39
24
  #include <unistd.h>
40
25
  #include <fcntl.h>
@@ -44,50 +29,100 @@ extern "C" {
44
29
  #include <magic.h>
45
30
  #include <ruby.h>
46
31
 
47
- #if defined(HAVE_LOCALE_H)
48
- # include <locale.h>
32
+ #if defined(HAVE_RUBY_IO_H)
33
+ # include <ruby/io.h>
34
+ #else
35
+ # include <rubyio.h>
49
36
  #endif
50
37
 
51
- #if defined(HAVE_XLOCALE_H)
52
- # include <xlocale.h>
38
+ #if !defined(BIT)
39
+ # define BIT(n) (1 << (n))
53
40
  #endif
54
41
 
55
- #if !defined(ENOMEM)
56
- # define ENOMEM 12
42
+ #if !defined(UNUSED)
43
+ # define UNUSED(x) (void)(x)
57
44
  #endif
58
45
 
59
- #if !defined(EFAULT)
60
- # define EFAULT 14
46
+ #if defined(F_DUPFD_CLOEXEC)
47
+ # define HAVE_F_DUPFD_CLOEXEC 1
61
48
  #endif
62
49
 
63
- #if !defined(EINVAL)
64
- # define EINVAL 22
50
+ #if defined(O_CLOEXEC)
51
+ # define HAVE_O_CLOEXEC 1
65
52
  #endif
66
53
 
67
- #if !defined(ENOSYS)
68
- # define ENOSYS 38
54
+ #if defined(POSIX_CLOSE_RESTART)
55
+ # define HAVE_POSIX_CLOSE_RESTART 1
69
56
  #endif
70
57
 
71
- #if !defined(ANYARGS)
72
- # if defined(__cplusplus)
73
- # define ANYARGS ...
74
- # else
75
- # define ANYARGS
76
- # endif
58
+ #define MAGIC_EXTENSION_SEPARATOR "/"
59
+ #define MAGIC_CONTINUE_SEPARATOR "\n- "
60
+
61
+ #if !defined(MAGIC_NO_CHECK_CSV)
62
+ # define MAGIC_NO_CHECK_CSV 0
77
63
  #endif
78
64
 
79
- #if defined(UNUSED)
80
- # undef(UNUSED)
65
+ #if !defined(MAGIC_NO_CHECK_JSON)
66
+ # define MAGIC_NO_CHECK_JSON 0
67
+ #endif
68
+
69
+ #define DATA_P(x) (RB_TYPE_P((x), T_DATA))
70
+ #define BOOLEAN_P(x) (RB_TYPE_P((x), T_TRUE) || RB_TYPE_P((x), T_FALSE))
71
+ #define STRING_P(x) (RB_TYPE_P((x), T_STRING))
72
+ #define ARRAY_P(x) (RB_TYPE_P((x), T_ARRAY))
73
+ #define FILE_P(x) (RB_TYPE_P((x), T_FILE))
74
+
75
+ #define RVAL2CBOOL(x) (RTEST(x))
76
+ #define CBOOL2RVAL(x) ((x) ? Qtrue : Qfalse)
77
+
78
+ #define RVAL2CSTR(x) (NIL_P(x) ? NULL : StringValueCStr(x))
79
+ #define CSTR2RVAL(x) ((x) == NULL ? Qnil : rb_str_new2(x))
80
+
81
+ #define RARRAY_EMPTY rb_ary_new()
82
+ #define RARRAY_EMPTY_P(a) (RARRAY_LEN(a) == 0)
83
+ #define RARRAY_FIRST(a) (RARRAY_EMPTY_P(a) ? Qnil : rb_ary_entry((a), 0))
84
+
85
+ #define CLASS_NAME(o) (NIL_P((o)) ? "nil" : rb_obj_classname((o)))
86
+
87
+ #if !defined(T_INTEGER)
88
+ # define T_INTEGER rb_cInteger
81
89
  #endif
82
90
 
83
- #define UNUSED(x) (void)(x)
91
+ #if !defined(HAVE_RB_IO_T)
92
+ # define rb_io_t OpenFile
93
+ #endif
84
94
 
85
- #if !defined(HAVE_MAGIC_VERSION) || MAGIC_VERSION < 518
86
- # define HAVE_BROKEN_MAGIC 1
95
+ #if !defined(GetReadFile)
96
+ # define FPTR_TO_FD(p) ((p)->fd)
97
+ #else
98
+ # define FPTR_TO_FD(p) (fileno(GetReadFile(p)))
87
99
  #endif
88
100
 
89
- #if defined(HAVE_NEWLOCALE) && defined(HAVE_USELOCALE) && defined(HAVE_FREELOCALE)
90
- # define HAVE_SAFE_LOCALE 1
101
+ #define NOGVL_FUNCTION (VALUE (*)(void *))
102
+
103
+ #if defined(HAVE_RB_THREAD_CALL_WITHOUT_GVL) && \
104
+ defined(HAVE_RUBY_THREAD_H) && HAVE_RUBY_THREAD_H
105
+ # include <ruby/thread.h>
106
+ # define NOGVL(f, d) \
107
+ rb_thread_call_without_gvl((f), (d), RUBY_UBF_IO, NULL)
108
+ #elif defined(HAVE_RB_THREAD_BLOCKING_REGION)
109
+ # define NOGVL(f, d) \
110
+ rb_thread_blocking_region(NOGVL_FUNCTION(f), (d), RUBY_UBF_IO, NULL)
111
+ #else
112
+ # include <rubysig.h>
113
+ static inline VALUE
114
+ fake_blocking_region(VALUE (*f)(ANYARGS), void *data)
115
+ {
116
+ VALUE rv;
117
+
118
+ TRAP_BEG;
119
+ rv = f(data);
120
+ TRAP_END;
121
+
122
+ return rv;
123
+ }
124
+ # define NOGVL(f, d) \
125
+ fake_blocking_region(NOGVL_FUNCTION(f), (d))
91
126
  #endif
92
127
 
93
128
  #if defined(__cplusplus)
@@ -95,5 +130,3 @@ extern "C" {
95
130
  #endif
96
131
 
97
132
  #endif /* _COMMON_H */
98
-
99
- /* vim: set ts=8 sw=4 sts=2 et : */
data/ext/magic/extconf.rb CHANGED
@@ -1,135 +1,194 @@
1
- # -*- encoding: utf-8 -*-
2
-
3
- # :enddoc:
4
-
5
- #
6
- # extconf.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
- #
1
+ # frozen_string_literal: true
22
2
 
23
3
  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", 'rb') do |read_file|
23
+ target_file.write(read_file.read)
24
+ end
25
+ end
26
+
27
+ checksum = Digest::SHA256.hexdigest(File.read(filename))
28
+
29
+ if checksum != expected_sha256
30
+ raise "SHA256 of #{filename} does not match: got #{checksum}, expected #{expected_sha256}"
31
+ end
32
+ end
33
+
34
+ system("tar -xzf #{filename}") || raise('ERROR')
35
+ system("cd #{libdir} && ./configure --prefix=#{gem_ext_dir} && make install") || raise('ERROR')
36
+
37
+ $LOCAL_LIBS << '-lmagic'
38
+ $LIBPATH << gem_lib_dir
39
+ $CFLAGS << " -I #{libdir}/src"
24
40
 
25
- RbConfig::MAKEFILE_CONFIG['CC'] = ENV['CC'] if ENV['CC']
41
+ def darwin?
42
+ RbConfig::CONFIG['target_os'] =~ /darwin/
43
+ end
26
44
 
27
- $CFLAGS << ' -std=c99 -g -Wall -Wextra -pedantic'
45
+ def windows?
46
+ RbConfig::CONFIG['target_os'] =~ /mswin|mingw32|windows/
47
+ end
28
48
 
29
- unless RbConfig::CONFIG['host_os'][/darwin/]
30
- $LDFLAGS << ' -Wl,--as-needed'
49
+ if ENV['CC']
50
+ RbConfig::CONFIG['CC'] = RbConfig::MAKEFILE_CONFIG['CC'] = ENV['CC']
31
51
  end
32
52
 
33
- $LDFLAGS << " %s" % ENV['LDFLAGS'] if ENV['LDFLAGS']
53
+ ENV['CC'] = RbConfig::CONFIG['CC']
54
+
55
+ $CFLAGS += ' -std=c99 -fPIC'
56
+ $CFLAGS += ' -Wall -Wextra -pedantic'
34
57
 
35
- %w(CFLAGS CXXFLAGS CPPFLAGS).each do |variable|
36
- $CFLAGS << " %s" % ENV[variable] if ENV[variable]
58
+ if RbConfig::CONFIG['CC'] =~ /gcc/
59
+ $CFLAGS += ' -O3' unless $CFLAGS =~ /-O\d/
60
+ $CFLAGS += ' -Wcast-qual -Wwrite-strings -Wconversion -Wmissing-noreturn -Winline'
37
61
  end
38
62
 
39
- have_header('locale.h')
40
- have_header('xlocale.h')
41
- have_header('utime.h')
42
- have_header('sys/types.h')
43
- have_header('sys/time.h')
63
+ unless darwin?
64
+ $LDFLAGS += ' -Wl,--as-needed -Wl,--no-undefined'
65
+ end
66
+
67
+ if windows?
68
+ $LDFLAGS += ' -static-libgcc'
69
+ end
70
+
71
+ %w[
72
+ CFLAGS
73
+ CXXFLAGS
74
+ CPPFLAGS
75
+ ].each do |variable|
76
+ $CFLAGS += format(' %s', ENV[variable]) if ENV[variable]
77
+ end
44
78
 
45
- have_ruby_h = have_header('ruby.h')
46
- have_magic_h = have_header('magic.h')
79
+ $LDFLAGS += format(' %s', ENV['LDFLAGS']) if ENV['LDFLAGS']
47
80
 
48
- have_func('newlocale', ['locale.h', 'xlocale.h'])
49
- have_func('uselocale', ['locale.h', 'xlocale.h'])
50
- have_func('freelocale', ['locale.h', 'xlocale.h'])
81
+ unless have_header('ruby.h')
82
+ abort "\n" + (<<-EOS).gsub(/^[ ]{,3}/, '') + "\n"
83
+ You appear to be missing Ruby development libraries and/or header
84
+ files. You can install missing compile-time dependencies in one of
85
+ the following ways:
51
86
 
52
- have_func('utime', ['utime.h', 'sys/types.h'])
53
- have_func('utimes', 'sys/time.h')
87
+ - Debian / Ubuntu
54
88
 
55
- unless have_ruby_h
56
- abort <<-EOS
89
+ apt-get install ruby-dev
57
90
 
58
- You appear to be missing Ruby development libraries and/or header
59
- files. You can install missing compile-time dependencies in one of
60
- the following ways:
91
+ - Red Hat / CentOS / Fedora
61
92
 
62
- - Debian / Ubuntu
93
+ yum install ruby-devel or dnf install ruby-devel
63
94
 
64
- apt-get install ruby-dev
95
+ - Mac OS X (Darwin)
65
96
 
66
- - Red Hat / CentOS / Fedora
97
+ brew install ruby (for Homebrew, see https://brew.sh)
98
+ port install ruby2.6 (for MacPorts, see https://www.macports.org)
67
99
 
68
- yum install ruby-devel
100
+ - OpenBSD / NetBSD
69
101
 
102
+ pkg_add ruby (for pkgsrc, see https://www.pkgsrc.org)
70
103
 
71
- Alternatively, you can use either of the following Ruby version
72
- managers in order to install Ruby locally (for your user only)
73
- and/or system-wide:
104
+ - FreeBSD
74
105
 
75
- - Ruby Version Manager (for RVM, see http://rvm.io/)
76
- - Ruby Environment (for rbenv, see http://github.com/sstephenson/rbenv)
106
+ pkg install ruby (for FreeBSD Ports, see https://www.freebsd.org/ports)
77
107
 
108
+ Alternatively, you can use either of the following Ruby version
109
+ managers in order to install Ruby locally (for your user only)
110
+ and/or system-wide:
111
+
112
+ - Ruby Version Manager (for RVM, see https://rvm.io)
113
+ - Ruby Environment (for rbenv, see https://github.com/sstephenson/rbenv)
114
+ - Change Ruby (for chruby, see https://github.com/postmodern/chruby)
115
+
116
+ More information about how to install Ruby on various platforms
117
+ available at the following web site:
118
+
119
+ https://www.ruby-lang.org/en/documentation/installation
78
120
  EOS
79
121
  end
80
122
 
81
123
  have_func('rb_thread_call_without_gvl')
82
124
  have_func('rb_thread_blocking_region')
83
125
 
84
- unless have_magic_h
85
- abort <<-EOS
126
+ unless have_header('magic.h')
127
+ abort "\n" + (<<-EOS).gsub(/^[ ]{,3}/, '') + "\n"
128
+ You appear to be missing libmagic(3) library and/or necessary header
129
+ files. You can install missing compile-time dependencies in one of
130
+ the following ways:
86
131
 
87
- You appear to be missing libmagic(3) library and/or necessary header
88
- files. You can install missing compile-time dependencies in one of
89
- the following ways:
132
+ - Debian / Ubuntu
90
133
 
91
- - Debian / Ubuntu
134
+ apt-get install libmagic-dev
92
135
 
93
- apt-get install libmagic-dev
136
+ - Red Hat / CentOS / Fedora
94
137
 
95
- - Red Hat / CentOS / Fedora
138
+ yum install file-devel or dns install file-devel
96
139
 
97
- yum install file-devel
140
+ - Mac OS X (Darwin)
98
141
 
99
- - Mac OS X (Darwin)
142
+ brew install libmagic (for Homebrew, see https://brew.sh)
143
+ port install libmagic (for MacPorts, see https://www.macports.org)
100
144
 
101
- brew install libmagic (for Homebrew, see http://brew.sh/)
102
- port install libmagic (for MacPorts, see http://www.macports.org/)
145
+ - OpenBSD / NetBSD
103
146
 
147
+ pkg_add file (for pkgsrc, see https://www.pkgsrc.org)
104
148
 
105
- Alternatively, you can download recent release of the file(1) package
106
- from the following web site and attempt to compile libmagic(3) manually:
149
+ - FreeBSD
107
150
 
108
- http://www.darwinsys.com/file/
151
+ pkg install file (for FreeBSD Ports, see https://www.freebsd.org/ports)
109
152
 
153
+ Alternatively, you can download recent release of the file(1) package
154
+ from the following web site and attempt to compile libmagic(3) manually:
155
+
156
+ https://www.darwinsys.com/file
110
157
  EOS
111
158
  end
112
159
 
113
- unless have_library('magic', 'magic_getpath')
114
- abort <<-EOS
115
-
116
- Your version of libmagic(3) appears to be too old. Please, consider
117
- upgrading to at least version 5.09 or newer if possible ...
160
+ have_library('magic')
118
161
 
119
- For more information about file(1) command and libmagic(3) please
120
- visit the following web site:
162
+ unless have_func('magic_getpath')
163
+ abort "\n" + (<<-EOS).gsub(/^[ ]{,3}/, '') + "\n"
164
+ Your version of libmagic(3) appears to be too old.
121
165
 
122
- http://www.darwinsys.com/file/
166
+ Please, consider upgrading to at least version 5.29 or newer,
167
+ if possible. For more information about file(1) command and
168
+ libmagic(3) please visit the following web site:
123
169
 
170
+ https://www.darwinsys.com/file
124
171
  EOS
125
172
  end
126
173
 
127
- have_func('magic_version', 'magic.h')
174
+ have_func('magic_getflags')
128
175
 
129
- dir_config('magic')
176
+ %w[
177
+ utime.h
178
+ sys/types.h
179
+ sys/time.h
180
+ ].each do |h|
181
+ have_header(h)
182
+ end
183
+
184
+ %w[
185
+ utime
186
+ utimes
187
+ ].each do |f|
188
+ have_func(f)
189
+ end
190
+
191
+ dir_config('magic', [gem_include_dir], [gem_lib_dir])
130
192
 
131
193
  create_header
132
194
  create_makefile('magic/magic')
133
-
134
- # vim: set ts=2 sw=2 sts=2 et :
135
- # encoding: utf-8