Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle EOF when checking archive validity #321

Merged
merged 3 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/filewatch/read_mode/handlers/read_zip_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ def close_and_ignore_ioexception(closeable)

def corrupted?(watched_file)
begin
start = Time.new
file_stream = FileInputStream.new(watched_file.path)
gzip_stream = GZIPInputStream.new(file_stream)
buffer = Java::byte[8192].new
start = Time.new
until gzip_stream.read(buffer) == -1
end
return false
rescue ZipException => e
rescue ZipException, Java::JavaIo::EOFException => e
duration = Time.now - start
logger.warn("Detected corrupted archive #{watched_file.path} file won't be processed", :message => e.message,
:duration => duration.round(3))
Expand Down
6 changes: 6 additions & 0 deletions spec/helpers/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ def self.corrupt_gzip(file_path)
f.close()
end

def self.truncate_gzip(file_path)
f = File.open(file_path, "ab")
f.truncate(100)
f.close()
end

class TracerBase
def initialize
@tracer = Concurrent::Array.new
Expand Down
31 changes: 31 additions & 0 deletions spec/inputs/file_read_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,37 @@
expect(IO.read(log_completed_path)).to be_empty
end
end

it "the truncated file is untouched" do
directory = Stud::Temporary.directory
file_path = fixture_dir.join('compressed.log.gz')
truncated_file_path = ::File.join(directory, 'truncated.gz')
FileUtils.cp(file_path, truncated_file_path)

FileInput.truncate_gzip(truncated_file_path)

log_completed_path = ::File.join(directory, "C_completed.txt")
f = File.new(log_completed_path, "w")
f.close()

conf = <<-CONFIG
input {
file {
type => "blah"
path => "#{truncated_file_path}"
mode => "read"
file_completed_action => "log_and_delete"
file_completed_log_path => "#{log_completed_path}"
check_archive_validity => true
}
}
CONFIG

events = input(conf) do |pipeline, queue|
wait(1)
expect(IO.read(log_completed_path)).to be_empty
end
end
end
end

Expand Down