Skip to content

Use relative path instead of absolute path in symlink_path for the out file plugin #4904

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 9 additions & 1 deletion 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 'pathname'

require 'fluent/plugin/output'
require 'fluent/config/error'
Expand Down Expand Up @@ -54,6 +55,8 @@ class FileOutput < Output
config_param :recompress, :bool, default: false
desc "Create symlink to temporary buffered file when buffer_type is file (disabled on Windows)."
config_param :symlink_path, :string, default: nil
desc "Use relative path for symlink target (default: false)"
config_param :symlink_path_use_relative, :bool, default: false

config_section :format do
config_set_default :@type, 'out_file'
Expand Down Expand Up @@ -97,7 +100,12 @@ def generate_chunk(metadata)
if chunk.metadata == @latest_metadata
sym_path = @_output_plugin_for_symlink.extract_placeholders(@_symlink_path, chunk)
FileUtils.mkdir_p(File.dirname(sym_path), mode: @_output_plugin_for_symlink.dir_perm)
FileUtils.ln_sf(chunk.path, sym_path)
if @_output_plugin_for_symlink.symlink_path_use_relative
relative_path = Pathname.new(chunk.path).relative_path_from(Pathname.new(File.dirname(sym_path)))
FileUtils.ln_sf(relative_path, sym_path)
else
FileUtils.ln_sf(chunk.path, sym_path)
end
end
chunk
end
Expand Down
28 changes: 26 additions & 2 deletions test/plugin/test_out_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,24 @@ def parse_system(text)
end
end

def run_and_check(d, symlink_path)
test 'relative symlink' do
omit "Windows doesn't support symlinks" if Fluent.windows?

conf = CONFIG + %[
symlink_path #{SYMLINK_PATH}
symlink_path_use_relative true
]
symlink_path = "#{SYMLINK_PATH}"

d = create_driver(conf)
begin
run_and_check(d, symlink_path, relative_symlink=true)
ensure
FileUtils.rm_rf(symlink_path)
end
end

def run_and_check(d, symlink_path, relative_symlink=false)
d.run(default_tag: 'tag') do
es = Fluent::OneEventStream.new(event_time("2011-01-02 13:14:15 UTC"), {"a"=>1})
d.feed(es)
Expand All @@ -794,7 +811,14 @@ def run_and_check(d, symlink_path)
assert File.exist?(symlink_path)

meta = d.instance.metadata('tag', event_time("2011-01-03 14:15:16 UTC"), {})
assert_equal d.instance.buffer.instance_eval{ @stage[meta].path }, File.readlink(symlink_path)
if relative_symlink
target_path = d.instance.buffer.instance_eval{ @stage[meta].path }
link_target = File.readlink(symlink_path)
expected_path = Pathname.new(target_path).relative_path_from(Pathname.new(File.dirname(symlink_path))).to_s
assert_equal expected_path, link_target
else
assert_equal d.instance.buffer.instance_eval{ @stage[meta].path }, File.readlink(symlink_path)
end
end
end
end
Expand Down