Skip to content

Commit

Permalink
Use msgpack IO interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
byroot committed Feb 4, 2021
1 parent c1d2493 commit 4869281
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
22 changes: 15 additions & 7 deletions lib/bootsnap/load_path_cache/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,18 @@ def commit_transaction

def load_data
@data = begin
MessagePack.load(File.binread(@store_path))
# handle malformed data due to upgrade incompatibility
rescue Errno::ENOENT, MessagePack::MalformedFormatError, MessagePack::UnknownExtTypeError, EOFError
{}
rescue ArgumentError => e
e.message =~ /negative array size/ ? {} : raise
File.open(@store_path, encoding: Encoding::BINARY) do |io|
MessagePack.load(io)
end
# handle malformed data due to upgrade incompatibility
rescue Errno::ENOENT, MessagePack::MalformedFormatError, MessagePack::UnknownExtTypeError, EOFError
{}
rescue ArgumentError => error
if error.message =~ /negative array size/
{}
else
raise
end
end
end

Expand All @@ -79,7 +85,9 @@ def dump_data
exclusive_write = File::Constants::CREAT | File::Constants::EXCL | File::Constants::WRONLY
# `encoding:` looks redundant wrt `binwrite`, but necessary on windows
# because binary is part of mode.
File.binwrite(tmp, MessagePack.dump(@data), mode: exclusive_write, encoding: Encoding::BINARY)
File.open(tmp, mode: exclusive_write, encoding: Encoding::BINARY) do |io|
MessagePack.dump(@data, io)
end
FileUtils.mv(tmp, @store_path)
rescue Errno::EEXIST
retry
Expand Down
4 changes: 2 additions & 2 deletions test/load_path_cache/store_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ def test_no_commit_unless_dirty
def test_retry_on_collision
retries = sequence('retries')

File.expects(:binwrite).in_sequence(retries).raises(Errno::EEXIST.new("File exists @ rb_sysopen"))
File.expects(:binwrite).in_sequence(retries).returns(1)
MessagePack.expects(:dump).in_sequence(retries).raises(Errno::EEXIST.new("File exists @ rb_sysopen"))
MessagePack.expects(:dump).in_sequence(retries).returns(1)
FileUtils.expects(:mv).in_sequence(retries)

store.transaction { store.set('a', 1) }
Expand Down

0 comments on commit 4869281

Please sign in to comment.