Skip to content

Commit

Permalink
Merge pull request #1995 from fluent/out_file-avoid-broken-file-with-…
Browse files Browse the repository at this point in the history
…gzip-append

out_file: Temporal fix for broken gzipped files with gzip and append. ref #1903
  • Loading branch information
repeatedly authored May 22, 2018
2 parents 9830b27 + 0140a4c commit a93e3e7
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions lib/fluent/plugin/out_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
require 'fileutils'
require 'zlib'
require 'time'
require 'tempfile'

require 'fluent/plugin/output'
require 'fluent/config/error'
Expand Down Expand Up @@ -213,10 +214,28 @@ def write_without_compression(path, chunk)
end

def write_gzip_with_compression(path, chunk)
File.open(path, "ab", @file_perm) do |f|
gz = Zlib::GzipWriter.new(f)
chunk.write_to(gz, compressed: :text)
gz.close
if @append
# This code will be removed after zlib/multithread bug is fixed.
# Use Tempfile to avoid broken gzip files: https://github.com/fluent/fluentd/issues/1903
Tempfile.create('out_file-gzip-append') { |temp|
begin
writer = Zlib::GzipWriter.new(temp)
chunk.write_to(writer, compressed: :text)
ensure
writer.finish # avoid zlib finalizer warning
end
temp.rewind

File.open(path, "ab", @file_perm) do |f|
IO.copy_stream(temp, f)
end
}
else
File.open(path, "ab", @file_perm) do |f|
gz = Zlib::GzipWriter.new(f)
chunk.write_to(gz, compressed: :text)
gz.close
end
end
end

Expand Down

0 comments on commit a93e3e7

Please sign in to comment.