Skip to content

Commit a36cf9a

Browse files
committed
out_file: add support for relative symlink paths in configuration
1 parent 515e351 commit a36cf9a

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

lib/fluent/plugin/out_file.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ class FileOutput < Output
5555
config_param :recompress, :bool, default: false
5656
desc "Create symlink to temporary buffered file when buffer_type is file (disabled on Windows)."
5757
config_param :symlink_path, :string, default: nil
58+
desc "Use relative path for symlink target (default: false)"
59+
config_param :symlink_path_use_relative, :bool, default: false
5860

5961
config_section :format do
6062
config_set_default :@type, 'out_file'
@@ -98,8 +100,12 @@ def generate_chunk(metadata)
98100
if chunk.metadata == @latest_metadata
99101
sym_path = @_output_plugin_for_symlink.extract_placeholders(@_symlink_path, chunk)
100102
FileUtils.mkdir_p(File.dirname(sym_path), mode: @_output_plugin_for_symlink.dir_perm)
101-
relative_path = Pathname.new(chunk.path).relative_path_from(Pathname.new(File.dirname(sym_path)))
102-
FileUtils.ln_sf(relative_path, sym_path)
103+
if @_output_plugin_for_symlink.symlink_path_use_relative
104+
relative_path = Pathname.new(chunk.path).relative_path_from(Pathname.new(File.dirname(sym_path)))
105+
FileUtils.ln_sf(relative_path, sym_path)
106+
else
107+
FileUtils.ln_sf(chunk.path, sym_path)
108+
end
103109
end
104110
chunk
105111
end

test/plugin/test_out_file.rb

+26-2
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,24 @@ def parse_system(text)
779779
end
780780
end
781781

782-
def run_and_check(d, symlink_path)
782+
test 'relative symlink' do
783+
omit "Windows doesn't support symlinks" if Fluent.windows?
784+
785+
conf = CONFIG + %[
786+
symlink_path #{SYMLINK_PATH}
787+
symlink_path_use_relative true
788+
]
789+
symlink_path = "#{SYMLINK_PATH}"
790+
791+
d = create_driver(conf)
792+
begin
793+
run_and_check(d, symlink_path, relative_symlink=true)
794+
ensure
795+
FileUtils.rm_rf(symlink_path)
796+
end
797+
end
798+
799+
def run_and_check(d, symlink_path, relative_symlink=false)
783800
d.run(default_tag: 'tag') do
784801
es = Fluent::OneEventStream.new(event_time("2011-01-02 13:14:15 UTC"), {"a"=>1})
785802
d.feed(es)
@@ -794,7 +811,14 @@ def run_and_check(d, symlink_path)
794811
assert File.exist?(symlink_path)
795812

796813
meta = d.instance.metadata('tag', event_time("2011-01-03 14:15:16 UTC"), {})
797-
assert_equal d.instance.buffer.instance_eval{ @stage[meta].path }, File.readlink(symlink_path)
814+
if relative_symlink
815+
target_path = d.instance.buffer.instance_eval{ @stage[meta].path }
816+
link_target = File.readlink(symlink_path)
817+
expected_path = Pathname.new(target_path).relative_path_from(Pathname.new(File.dirname(symlink_path))).to_s
818+
assert_equal expected_path, link_target
819+
else
820+
assert_equal d.instance.buffer.instance_eval{ @stage[meta].path }, File.readlink(symlink_path)
821+
end
798822
end
799823
end
800824
end

0 commit comments

Comments
 (0)