Skip to content

Commit

Permalink
workaround to make chdir threadsafe until an alternative is found
Browse files Browse the repository at this point in the history
Signed-off-by: Steven ONeill <oneill.steven@gmail.com>
  • Loading branch information
stevenoneill committed Aug 2, 2018
1 parent 3728e27 commit 93c4467
Showing 1 changed file with 21 additions and 9 deletions.
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

0 comments on commit 93c4467

Please sign in to comment.