Skip to content

Commit

Permalink
Drop dependency on FileUtils
Browse files Browse the repository at this point in the history
Fix: #404

But also it's annoying to have delayed `require` like this one.
  • Loading branch information
byroot committed Feb 22, 2022
1 parent cb1adae commit 843c101
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Unreleased

* Drop dependency on `fileutils`.

* Better respect `Kernel#require` duck typing. While it almost never comes up in practice, `Kernel#require`
follow a fairly intricate duck-typing protocol on its argument implemented as `rb_get_path(VALUE)` in MRI.
So when applicable we bind `rb_get_path` and use it for improved compatibility. See #396 and #406.
Expand Down
23 changes: 18 additions & 5 deletions lib/bootsnap/load_path_cache/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,17 @@ def load_data
end

def dump_data
require "fileutils" unless defined? FileUtils

# Change contents atomically so other processes can't get invalid
# caches if they read at an inopportune time.
tmp = "#{@store_path}.#{Process.pid}.#{(rand * 100_000).to_i}.tmp"
FileUtils.mkpath(File.dirname(tmp))
mkdir_p(File.dirname(tmp))
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.open(tmp, mode: exclusive_write, encoding: Encoding::BINARY) do |io|
MessagePack.dump(@data, io, freeze: true)
MessagePack.dump(@data, io)
end
FileUtils.mv(tmp, @store_path)
File.rename(tmp, @store_path)
rescue Errno::EEXIST
retry
rescue SystemCallError
Expand All @@ -110,6 +108,21 @@ def dump_data
def default_data
{VERSION_KEY => CURRENT_VERSION}
end

def mkdir_p(path)
stack = []
until File.directory?(path)
stack.push path
path = File.dirname(path)
end
stack.reverse_each do |dir|
begin
Dir.mkdir(dir)
rescue SystemCallError
raise unless File.directory?(dir)
end
end
end
end
end
end
2 changes: 1 addition & 1 deletion test/load_path_cache/store_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def test_retry_on_collision

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)
File.expects(:rename).in_sequence(retries)

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

0 comments on commit 843c101

Please sign in to comment.