malmal 0.4.0-aarch64-linux-gnu → 0.4.1-aarch64-linux-gnu

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0ce5dad9419233f8e2d313567a621d1d43954b10428da3d67977cca978bb31b1
4
- data.tar.gz: 1e772d86126daabfbb42f3d850d26091da12f5f98b3431acb4a2cf2783068982
3
+ metadata.gz: cb7cf26574f00ef024d601848c2b684e99e7975c63334791a81d7c7d44207d38
4
+ data.tar.gz: 2de9fb26986e08310b7151df12b91fd7579f5bd22e067aec5c68f01e76c6ad6d
5
5
  SHA512:
6
- metadata.gz: 4ac250fadbf035b3851dad95ef80367de39cfc0941f738ab99ec5d1137ba80d2356240b6b304cde90bfb038f44c5e42e77944f701c972b033b91524efc8275d8
7
- data.tar.gz: 77c6067c887dfb4aed02114b04b89fe58f578b13eafdba819f49555003eb5114afbf58db76249b5c1d7fb904df9978dbfe4150cd030da35fe1d380d978c1f234
6
+ metadata.gz: 6187070edb78ee68737e7d181c729ff901dc94c23eba35b9e6e7712f04c9fad6cc82f6e51dc3c14eea4a1973bb808b70f7ff6d0e7ed4c3841276b5786f479518
7
+ data.tar.gz: 34b12c044e3e58870bc8617837e772eb9332d05a78c8eb9b92e53085fc66a4f7384f971d4c9eccdcf860c8f7c2c208e74c08fe7805bb72b3f40583b5948f59f3
Binary file
@@ -62,6 +62,8 @@ module Malmal
62
62
  @options.merge!(limits)
63
63
  @handle = build(JSON.generate(@options))
64
64
  @mutex = Mutex.new
65
+ @in_use = 0
66
+ @closed = false
65
67
  end
66
68
 
67
69
  # Scan +data+: a String of bytes, or an IO read up to the size limit and
@@ -69,14 +71,18 @@ module Malmal
69
71
  # claimed. Returns a Report; raises ScanError if the file was not scanned.
70
72
  def scan(data, filename: nil, content_type: nil)
71
73
  bytes = read(data)
72
- with_input(bytes) do |pointer|
73
- report = FFI::MemoryPointer.new(:pointer)
74
- verdict = Native.malmal_scan(handle, pointer, bytes.bytesize, claim(filename), claim(content_type), report)
75
- json = take_string(report.read_pointer)
76
- raise ScanError, "the scanner failed; the file was not scanned" if verdict == Native::ERROR || json.nil?
77
-
78
- Report.from_h(JSON.parse(json))
74
+ filename = claim(filename)
75
+ content_type = claim(content_type)
76
+ verdict, json = native_call do |scanner|
77
+ with_input(bytes) do |pointer|
78
+ report = FFI::MemoryPointer.new(:pointer)
79
+ verdict = Native.malmal_scan(scanner, pointer, bytes.bytesize, filename, content_type, report)
80
+ [verdict, take_string(report.read_pointer)]
81
+ end
79
82
  end
83
+ raise ScanError, "the scanner failed; the file was not scanned" if verdict == Native::ERROR || json.nil?
84
+
85
+ Report.from_h(JSON.parse(json))
80
86
  end
81
87
 
82
88
  # Scan +data+ and, if it is clean and of a type that can be, write it
@@ -93,22 +99,32 @@ module Malmal
93
99
  options[:types] = Array(options[:types]).map(&:to_s) if options.key?(:types)
94
100
  options[:jpeg_quality] = jpeg_quality if jpeg_quality
95
101
  bytes = read(data)
96
- with_input(bytes) do |pointer|
97
- image = FFI::MemoryPointer.new(:pointer)
98
- length = FFI::MemoryPointer.new(:size_t)
99
- info = FFI::MemoryPointer.new(:pointer)
100
- status = Native.malmal_sanitize_json(handle, pointer, bytes.bytesize, claim(filename), claim(content_type),
101
- JSON.generate(options), image, length, info)
102
- rebuilt = take_bytes(image.read_pointer, length.read(:size_t))
103
- json = take_string(info.read_pointer)
104
- sanitized(status, rebuilt, json ? JSON.parse(json) : {})
102
+ filename = claim(filename)
103
+ content_type = claim(content_type)
104
+ options = JSON.generate(options)
105
+ status, rebuilt, json = native_call do |scanner|
106
+ with_input(bytes) do |pointer|
107
+ image = FFI::MemoryPointer.new(:pointer)
108
+ length = FFI::MemoryPointer.new(:size_t)
109
+ info = FFI::MemoryPointer.new(:pointer)
110
+ status = Native.malmal_sanitize_json(scanner, pointer, bytes.bytesize, filename, content_type, options,
111
+ image, length, info)
112
+ # Both are taken, and freed, whichever of them fails.
113
+ begin
114
+ rebuilt = take_bytes(image.read_pointer, length.read(:size_t))
115
+ ensure
116
+ json = take_string(info.read_pointer)
117
+ end
118
+ [status, rebuilt, json]
119
+ end
105
120
  end
121
+ sanitized(status, rebuilt, json ? JSON.parse(json) : {})
106
122
  end
107
123
 
108
124
  # Scans that +wait+ stopped waiting for and that are still running.
109
125
  # Above 0, restart the process when it can be.
110
126
  def overdue
111
- Native.malmal_scanner_overdue(handle)
127
+ native_call { |scanner| Native.malmal_scanner_overdue(scanner) }
112
128
  end
113
129
 
114
130
  # The version of the loaded libmalmal_ffi.
@@ -116,16 +132,19 @@ module Malmal
116
132
  Native.version
117
133
  end
118
134
 
119
- # Free the scanner now rather than when it is collected.
135
+ # Free the scanner now rather than when it is collected. Scans already
136
+ # running finish first, and the scanner is freed as the last of them
137
+ # returns; new ones raise ScanError.
120
138
  def close
121
139
  @mutex.synchronize do
122
- @handle&.free
123
- @handle = nil
140
+ @closed = true
141
+ free_if_unused
124
142
  end
143
+ nil
125
144
  end
126
145
 
127
146
  def closed?
128
- @handle.nil?
147
+ @closed
129
148
  end
130
149
 
131
150
  def inspect
@@ -144,8 +163,43 @@ module Malmal
144
163
  Handle.new(pointer)
145
164
  end
146
165
 
147
- def handle
148
- @handle or raise ScanError, "the scanner is closed"
166
+ # Call into the library with the handle, which #close leaves alone until
167
+ # every call holding it has returned. Interrupts, such as Timeout's, wait
168
+ # until the call's results are taken and freed: raised as the native
169
+ # function returns, which is when Ruby raises them, they would leave
170
+ # what it allocated for the caller to free with no one to free it.
171
+ def native_call
172
+ Thread.handle_interrupt(Object => :never) do
173
+ scanner = acquire
174
+ begin
175
+ yield scanner
176
+ ensure
177
+ release
178
+ end
179
+ end
180
+ end
181
+
182
+ def acquire
183
+ @mutex.synchronize do
184
+ raise ScanError, "the scanner is closed" if @closed
185
+
186
+ @in_use += 1
187
+ @handle
188
+ end
189
+ end
190
+
191
+ def release
192
+ @mutex.synchronize do
193
+ @in_use -= 1
194
+ free_if_unused
195
+ end
196
+ end
197
+
198
+ def free_if_unused
199
+ return unless @closed && @in_use.zero? && @handle
200
+
201
+ @handle.free
202
+ @handle = nil
149
203
  end
150
204
 
151
205
  def password_json(password)
@@ -2,5 +2,5 @@
2
2
 
3
3
  module Malmal
4
4
  # The version of the gem, which is also the version of the library it loads.
5
- VERSION = "0.4.0"
5
+ VERSION = "0.4.1"
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: malmal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  platform: aarch64-linux-gnu
6
6
  authors:
7
7
  - nguyenthuan155632