Skip to content

Commit ea18b1c

Browse files
authored
Merge pull request #2680 from ganmacs/remove-unnecessary-condition
Delete unnecessary condition
2 parents 64a7b27 + b30e297 commit ea18b1c

8 files changed

+80
-38
lines changed

lib/fluent/plugin/buf_file.rb

+2-6
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,8 @@ def resume
175175

176176
def generate_chunk(metadata)
177177
# FileChunk generates real path with unique_id
178-
if @file_permission
179-
chunk = Fluent::Plugin::Buffer::FileChunk.new(metadata, @path, :create, perm: @file_permission, compress: @compress)
180-
else
181-
chunk = Fluent::Plugin::Buffer::FileChunk.new(metadata, @path, :create, compress: @compress)
182-
end
183-
178+
perm = @file_permission || system_config.file_permission
179+
chunk = Fluent::Plugin::Buffer::FileChunk.new(metadata, @path, :create, perm: perm, compress: @compress)
184180
log.debug "Created new chunk", chunk_id: dump_unique_id_hex(chunk.unique_id), metadata: metadata
185181

186182
return chunk

lib/fluent/plugin/buf_file_single.rb

+2-5
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,8 @@ def resume
195195

196196
def generate_chunk(metadata)
197197
# FileChunk generates real path with unique_id
198-
if @file_permission
199-
chunk = Fluent::Plugin::Buffer::FileSingleChunk.new(metadata, @path, :create, @key_in_path, perm: @file_permission, compress: @compress)
200-
else
201-
chunk = Fluent::Plugin::Buffer::FileSingleChunk.new(metadata, @path, :create, @key_in_path, compress: @compress)
202-
end
198+
perm = @file_permission || system_config.file_permission
199+
chunk = Fluent::Plugin::Buffer::FileSingleChunk.new(metadata, @path, :create, @key_in_path, perm: perm, compress: @compress)
203200

204201
log.debug "Created new chunk", chunk_id: dump_unique_id_hex(chunk.unique_id), metadata: metadata
205202

lib/fluent/plugin/buffer/file_chunk.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,13 @@ class FileChunkError < StandardError; end
3737
# path_prefix: path prefix string, ended with '.'
3838
# path_suffix: path suffix string, like '.log' (or any other user specified)
3939

40-
include SystemConfig::Mixin
41-
4240
FILE_PERMISSION = 0644
4341

4442
attr_reader :path, :permission
4543

46-
def initialize(metadata, path, mode, perm: system_config.file_permission || FILE_PERMISSION, compress: :text)
44+
def initialize(metadata, path, mode, perm: nil, compress: :text)
4745
super(metadata, compress: compress)
46+
perm ||= FILE_PERMISSION
4847
@permission = perm.is_a?(String) ? perm.to_i(8) : perm
4948
@bytesize = @size = @adding_bytes = @adding_size = 0
5049
@meta = nil

lib/fluent/plugin/buffer/file_single_chunk.rb

+2-3
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,17 @@ class FileChunkError < StandardError; end
2929
## buffer chunk path : /path/to/directory/fsb.key.b513b61c9791029c2513b61c9791029c2.buf
3030
## state: b/q - 'b'(on stage), 'q'(enqueued)
3131

32-
include SystemConfig::Mixin
33-
3432
PATH_EXT = 'buf'
3533
PATH_SUFFIX = ".#{PATH_EXT}"
3634
PATH_REGEXP = /\.(b|q)([0-9a-f]+)\.#{PATH_EXT}*\Z/n # //n switch means explicit 'ASCII-8BIT' pattern
3735
FILE_PERMISSION = 0644
3836

3937
attr_reader :path, :permission
4038

41-
def initialize(metadata, path, mode, key, perm: system_config.file_permission || FILE_PERMISSION, compress: :text)
39+
def initialize(metadata, path, mode, key, perm: FILE_PERMISSION, compress: :text)
4240
super(metadata, compress: compress)
4341
@key = key
42+
perm ||= FILE_PERMISSION
4443
@permission = perm.is_a?(String) ? perm.to_i(8) : perm
4544
@bytesize = @size = @adding_bytes = @adding_size = 0
4645

test/plugin/test_buf_file.rb

+40
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,46 @@ def write_metadata(path, chunk_id, metadata, size, ctime, mtime)
327327
plugin.stop; plugin.before_shutdown; plugin.shutdown; plugin.after_shutdown; plugin.close; plugin.terminate
328328
FileUtils.rm_r bufdir
329329
end
330+
331+
test '#generate_chunk generates blank file chunk with specified permission with system_config' do
332+
omit "NTFS doesn't support UNIX like permissions" if Fluent.windows?
333+
334+
begin
335+
plugin = Fluent::Plugin::FileBuffer.new
336+
plugin.owner = @d
337+
rand_num = rand(0..100)
338+
bufpath = File.join(File.expand_path("../../tmp/buffer_file_#{rand_num}", __FILE__), 'testbuf.*.log')
339+
bufdir = File.dirname(bufpath)
340+
341+
FileUtils.rm_r bufdir if File.exist?(bufdir)
342+
assert !File.exist?(bufdir)
343+
344+
plugin.configure(config_element('buffer', '', { 'path' => bufpath }))
345+
346+
assert !File.exist?(bufdir)
347+
plugin.start
348+
349+
m = metadata()
350+
c = nil
351+
Fluent::SystemConfig.overwrite_system_config("file_permission" => "700") do
352+
c = plugin.generate_chunk(m)
353+
end
354+
355+
assert c.is_a? Fluent::Plugin::Buffer::FileChunk
356+
assert_equal m, c.metadata
357+
assert c.empty?
358+
assert_equal :unstaged, c.state
359+
assert_equal 0700, c.permission
360+
assert_equal bufpath.gsub('.*.', ".b#{Fluent::UniqueId.hex(c.unique_id)}."), c.path
361+
assert{ File.stat(c.path).mode.to_s(8).end_with?('700') }
362+
363+
c.purge
364+
365+
plugin.stop; plugin.before_shutdown; plugin.shutdown; plugin.after_shutdown; plugin.close; plugin.terminate
366+
ensure
367+
FileUtils.rm_r bufdir
368+
end
369+
end
330370
end
331371

332372
sub_test_case 'configured with system root directory and plugin @id' do

test/plugin/test_buf_file_single.rb

+32
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,38 @@ def create_driver(conf = TAG_CONF, klass = FluentPluginFileSingleBufferTest::Dum
293293

294294
c.purge
295295
end
296+
297+
test '#generate_chunk generates blank file chunk with specified permission with system_config' do
298+
omit "NTFS doesn't support UNIX like permissions" if Fluent.windows?
299+
300+
@d = create_driver(%[
301+
<buffer tag>
302+
@type file_single
303+
path #{PATH}
304+
</buffer>
305+
])
306+
@p = @d.instance.buffer
307+
308+
FileUtils.rm_r @bufdir if File.exist?(@bufdir)
309+
assert !File.exist?(@bufdir)
310+
311+
@p.start
312+
313+
m = metadata()
314+
c = nil
315+
Fluent::SystemConfig.overwrite_system_config("file_permission" => "700") do
316+
c = @p.generate_chunk(m)
317+
end
318+
assert c.is_a? Fluent::Plugin::Buffer::FileSingleChunk
319+
assert_equal m, c.metadata
320+
assert c.empty?
321+
assert_equal :unstaged, c.state
322+
assert_equal 0700, c.permission
323+
assert_equal File.join(@bufdir, "fsb.testing.b#{Fluent::UniqueId.hex(c.unique_id)}.buf"), c.path
324+
assert{ File.stat(c.path).mode.to_s(8).end_with?('700') }
325+
326+
c.purge
327+
end
296328
end
297329

298330
sub_test_case 'configured with system root directory and plugin @id' do

test/plugin/test_buffer_file_chunk.rb

-11
Original file line numberDiff line numberDiff line change
@@ -416,17 +416,6 @@ def gen_chunk_path(prefix, unique_id)
416416
assert_equal d4.to_json + "\n", lines[3]
417417
end
418418

419-
test 'can refer system config for file permission' do
420-
omit "NTFS doesn't support UNIX like permissions" if Fluent.windows?
421-
422-
chunk_path = File.join(@chunkdir, 'testperm.*.log')
423-
Fluent::SystemConfig.overwrite_system_config("file_permission" => "600") do
424-
c = Fluent::Plugin::Buffer::FileChunk.new(gen_metadata, chunk_path, :create)
425-
assert{ File.stat(c.path).mode.to_s(8).end_with?('600') }
426-
assert{ File.stat(c.path + '.meta').mode.to_s(8).end_with?('600') }
427-
end
428-
end
429-
430419
test '#write_metadata tries to store metadata on file' do
431420
d1 = {"f1" => 'v1', "f2" => 'v2', "f3" => 'v3'}
432421
d2 = {"f1" => 'vv1', "f2" => 'vv2', "f3" => 'vv3'}

test/plugin/test_buffer_file_single_chunk.rb

-10
Original file line numberDiff line numberDiff line change
@@ -353,16 +353,6 @@ def gen_chunk_path(prefix, unique_id)
353353
assert_equal d3.to_json + "\n", lines[2]
354354
assert_equal d4.to_json + "\n", lines[3]
355355
end
356-
357-
test 'can refer system config for file permission' do
358-
omit "NTFS doesn't support UNIX like permissions" if Fluent.windows?
359-
360-
chunk_path = File.join(@chunkdir, 'fsb.*.buf')
361-
Fluent::SystemConfig.overwrite_system_config("file_permission" => "600") do
362-
c = Fluent::Plugin::Buffer::FileSingleChunk.new(gen_metadata, chunk_path, :create, nil)
363-
assert{ File.stat(c.path).mode.to_s(8).end_with?('600') }
364-
end
365-
end
366356
end
367357

368358
sub_test_case 'chunk with file for staged chunk' do

0 commit comments

Comments
 (0)