Skip to content

Commit

Permalink
Merge pull request #259 from swipely/dont-change-dir-to-build-tar
Browse files Browse the repository at this point in the history
Use rubygems to build tar without changing directory
  • Loading branch information
tlunter committed Mar 2, 2015
2 parents 40a5af6 + 0e6c1be commit 7b9cbc0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
1 change: 0 additions & 1 deletion docker-api.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ Gem::Specification.new do |gem|
gem.version = Docker::VERSION
gem.add_dependency 'excon', '>= 0.38.0'
gem.add_dependency 'json'
gem.add_dependency 'archive-tar-minitar'
gem.add_development_dependency 'rake'
gem.add_development_dependency 'rspec', '~> 3.0'
gem.add_development_dependency 'rspec-its'
Expand Down
2 changes: 1 addition & 1 deletion lib/docker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
require 'excon'
require 'tempfile'
require 'base64'
require 'find'
require 'rubygems/package'
require 'archive/tar/minitar'
require 'uri'
require 'open-uri'

Expand Down
33 changes: 26 additions & 7 deletions lib/docker/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,33 @@ def create_tar(hash = {})
end

def create_dir_tar(directory)
cwd = FileUtils.pwd
tempfile_name = Dir::Tmpname.create('out') {}
tempfile = File.open(tempfile_name, 'wb+')
FileUtils.cd(directory)
Archive::Tar::Minitar.pack('.', tempfile)
tempfile = create_temp_file
directory += '/' unless directory.end_with?('/')

create_relative_dir_tar(directory, tempfile)

File.new(tempfile.path, 'r')
ensure
FileUtils.cd(cwd)
end

def create_relative_dir_tar(directory, output)
Gem::Package::TarWriter.new(output) do |tar|
Find.find(directory) do |prefixed_file_name|
stat = File.stat(prefixed_file_name)
next unless stat.file?

unprefixed_file_name = prefixed_file_name[directory.length..-1]
tar.add_file_simple(
unprefixed_file_name, stat.mode, stat.size
) do |tar_file|
IO.copy_stream(File.open(prefixed_file_name, 'rb'), tar_file)
end
end
end
end

def create_temp_file
tempfile_name = Dir::Tmpname.create('out') {}
File.open(tempfile_name, 'wb+')
end

def extract_id(body)
Expand Down

0 comments on commit 7b9cbc0

Please sign in to comment.