Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix to make dir.chdir threadsafe #29

Merged
merged 1 commit into from
Aug 3, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions lib/mixlib/archive/lib_archive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ class LibArchive
attr_reader :options
attr_reader :archive

class << self
attr_accessor :mutex_chdir
end

Mixlib::Archive::LibArchive.mutex_chdir = Mutex.new

def initialize(archive, options = {})
@archive = archive
@options = options
Expand All @@ -20,18 +26,24 @@ def extract(destination, perms: true, ignore: [])
ignore_re = Regexp.union(ignore)
flags = perms ? ::Archive::EXTRACT_PERM : nil
FileUtils.mkdir_p(destination)
Dir.chdir(destination) do
reader = ::Archive::Reader.open_filename(@archive)

reader.each_entry do |entry|
if entry.pathname =~ ignore_re
Mixlib::Archive::Log.warn "ignoring entry #{entry.pathname}"
next
end
# @note Dir.chdir is applied to the process, thus it is not thread-safe
# and must be synchronized.
# TODO: figure out a better alternative to chdir
Mixlib::Archive::LibArchive.mutex_chdir.synchronize do
Dir.chdir(destination) do
reader = ::Archive::Reader.open_filename(@archive)

reader.extract(entry, flags.to_i)
reader.each_entry do |entry|
if entry.pathname =~ ignore_re
Mixlib::Archive::Log.warn "ignoring entry #{entry.pathname}"
next
end

reader.extract(entry, flags.to_i)
end
reader.close
end
reader.close
end
end

Expand Down