class Bundler::CompactIndexClient::CacheFile
if digests are given, the checksums will be verified
write cache files in a way that is robust to concurrent modifications
def self.copy(path, &block)
def self.copy(path, &block) new(path) do |file| file.initialize_digests SharedHelpers.filesystem_access(path, :read) do path.open("rb") do |s| file.open {|f| IO.copy_stream(s, f) } end end yield file end end
def self.write(path, data, digests = nil)
def self.write(path, data, digests = nil) return unless data new(path) do |file| file.digests = digests file.write(data) end end
def append(data)
def append(data) return false unless digests? open("a") {|f| f.write data } verify && commit end
def close
Remove the temp file without replacing the original file.
def close return if @closed FileUtils.remove_file(path) if @path&.file? @closed = true end
def commit
Replace the original file with the temp file without verifying digests.
def commit raise ClosedError, "Cannot commit closed file" if @closed SharedHelpers.filesystem_access(original_path, :write) do FileUtils.mv(path, original_path) end @closed = true end
def commit!
def commit! verify || raise(DigestMismatchError.new(@base64digests, @expected_digests)) commit end
def digests=(expected_digests)
def digests=(expected_digests) @expected_digests = expected_digests if @expected_digests.nil? @digests = nil elsif @digests @digests = @digests.slice(*@expected_digests.keys) else initialize_digests(@expected_digests.keys) end end
def digests?
def digests? @digests&.any? end
def initialize(original_path, &block)
def initialize(original_path, &block) @original_path = original_path @perm = original_path.file? ? original_path.stat.mode : DEFAULT_FILE_MODE @path = original_path.sub(/$/, ".#{$$}.tmp") return unless block_given? begin yield self ensure close end end
def initialize_digests(keys = nil)
def initialize_digests(keys = nil) @digests = keys ? SUPPORTED_DIGESTS.slice(*keys) : SUPPORTED_DIGESTS.dup @digests.transform_values! {|algo_class| SharedHelpers.digest(algo_class).new } end
def md5
def md5 @digests && @digests["md5"] end
def open(write_mode = "wb", perm = @perm, &block)
def open(write_mode = "wb", perm = @perm, &block) raise ClosedError, "Cannot reopen closed file" if @closed SharedHelpers.filesystem_access(path, :write) do path.open(write_mode, perm) do |f| yield digests? ? Gem::Package::DigestIO.new(f, @digests) : f end end end
def reset_digests
def reset_digests @digests&.each_value(&:reset) end
def size
def size path.size end
def verify
def verify return true unless @expected_digests && digests? @base64digests = @digests.transform_values!(&:base64digest) @digests = nil @base64digests.all? {|algo, digest| @expected_digests[algo] == digest } end
def write(data)
def write(data) reset_digests open {|f| f.write data } commit! end