Skip to content

Commit b2dd262

Browse files
authored
handle EOF when checking archive validity (#321)
1 parent 58b8fb7 commit b2dd262

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-3
lines changed

Diff for: CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## 4.4.5
2+
- Handle EOF when checking archive validity [#321](https://github.com/logstash-plugins/logstash-input-file/pull/321)
3+
14
## 4.4.4
25
- Fixes gzip file handling in read mode when run on JDK12+, including JDK17 that is bundled with Logstash 8.4+ [#312](https://github.com/logstash-plugins/logstash-input-file/pull/312)
36

Diff for: lib/filewatch/read_mode/handlers/read_zip_file.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@ def close_and_ignore_ioexception(closeable)
7171

7272
def corrupted?(watched_file)
7373
begin
74+
start = Time.new
7475
file_stream = FileInputStream.new(watched_file.path)
7576
gzip_stream = GZIPInputStream.new(file_stream)
7677
buffer = Java::byte[8192].new
77-
start = Time.new
7878
until gzip_stream.read(buffer) == -1
7979
end
8080
return false
81-
rescue ZipException => e
81+
rescue ZipException, Java::JavaIo::EOFException => e
8282
duration = Time.now - start
8383
logger.warn("Detected corrupted archive #{watched_file.path} file won't be processed", :message => e.message,
8484
:duration => duration.round(3))

Diff for: logstash-input-file.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Gem::Specification.new do |s|
22

33
s.name = 'logstash-input-file'
4-
s.version = '4.4.4'
4+
s.version = '4.4.5'
55
s.licenses = ['Apache-2.0']
66
s.summary = "Streams events from files"
77
s.description = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program"

Diff for: spec/helpers/spec_helper.rb

+6
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ def self.corrupt_gzip(file_path)
2424
f.close()
2525
end
2626

27+
def self.truncate_gzip(file_path)
28+
f = File.open(file_path, "ab")
29+
f.truncate(100)
30+
f.close()
31+
end
32+
2733
class TracerBase
2834
def initialize
2935
@tracer = Concurrent::Array.new

Diff for: spec/inputs/file_read_spec.rb

+31
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,37 @@
245245
expect(IO.read(log_completed_path)).to be_empty
246246
end
247247
end
248+
249+
it "the truncated file is untouched" do
250+
directory = Stud::Temporary.directory
251+
file_path = fixture_dir.join('compressed.log.gz')
252+
truncated_file_path = ::File.join(directory, 'truncated.gz')
253+
FileUtils.cp(file_path, truncated_file_path)
254+
255+
FileInput.truncate_gzip(truncated_file_path)
256+
257+
log_completed_path = ::File.join(directory, "C_completed.txt")
258+
f = File.new(log_completed_path, "w")
259+
f.close()
260+
261+
conf = <<-CONFIG
262+
input {
263+
file {
264+
type => "blah"
265+
path => "#{truncated_file_path}"
266+
mode => "read"
267+
file_completed_action => "log_and_delete"
268+
file_completed_log_path => "#{log_completed_path}"
269+
check_archive_validity => true
270+
}
271+
}
272+
CONFIG
273+
274+
events = input(conf) do |pipeline, queue|
275+
wait(1)
276+
expect(IO.read(log_completed_path)).to be_empty
277+
end
278+
end
248279
end
249280
end
250281

0 commit comments

Comments
 (0)