Skip to content

Commit ea80a06

Browse files
committed
fluent-plugin-storage_local: handle reading file with non-latin characters
Signed-off-by: srilumpa <marcandre.doll@gmail.com>
1 parent 7e10002 commit ea80a06

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

lib/fluent/plugin/storage_local.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def configure(conf)
8585
if File.exist?(@path)
8686
raise Fluent::ConfigError, "Plugin storage path '#{@path}' is not readable/writable" unless File.readable?(@path) && File.writable?(@path)
8787
begin
88-
data = File.open(@path, 'r:utf-8') { |io| io.read }
88+
data = File.open(@path, 'r:utf-8::utf-8') { |io| io.read }
8989
if data.empty?
9090
log.warn "detect empty plugin storage file during startup. Ignored: #{@path}"
9191
return
@@ -113,7 +113,7 @@ def load
113113
return if @on_memory
114114
return unless File.exist?(@path)
115115
begin
116-
json_string = File.open(@path, 'r:utf-8'){ |io| io.read }
116+
json_string = File.open(@path, 'r:utf-8::utf-8'){ |io| io.read }
117117
json = JSON.parse(json_string)
118118
unless json.is_a?(Hash)
119119
log.error "broken content for plugin storage (Hash required: ignored)", type: json.class

test/plugin/test_storage_local.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,35 @@ class MyInput < Fluent::Plugin::Input
136136
@d.configure(conf)
137137
end
138138
end
139+
140+
test 'start properly if stored value contains non-latin chracters (issue #5378)' do
141+
storage_path = File.join(TMP_DIR, 'my_store.json')
142+
conf = config_element('ROOT', '', {}, [config_element('storage', '', {'path' => storage_path})])
143+
@d.configure(conf)
144+
@d.start
145+
@p = @d.storage_create()
146+
assert_equal storage_path, @p.path
147+
assert @p.store.empty?
148+
149+
@p.put('key', 'Bonne Année François !')
150+
assert_equal 'Bonne Année François !', @p.get('key')
151+
152+
@p.save
153+
assert File.exist?(storage_path)
154+
155+
@d.stop; @d.before_shutdown; @d.shutdown; @d.after_shutdown; @d.close; @d.terminate
156+
assert_equal({'key' => 'Bonne Année François !'}, File.open(storage_path){|f| JSON.parse(f.read) })
157+
158+
# re-create to reload storage contents
159+
@d = MyInput.new
160+
@d.configure(conf)
161+
@d.start
162+
@p = @d.storage_create()
163+
164+
assert_false @p.store.empty?
165+
assert_equal 'Bonne Année François !', @p.get('key')
166+
167+
end
139168
end
140169

141170
sub_test_case 'configured with root-dir and plugin id' do

0 commit comments

Comments
 (0)