BUILDR-702: Retain unix permissions when merging zip files
Project: http://git-wip-us.apache.org/repos/asf/buildr/repo Commit: http://git-wip-us.apache.org/repos/asf/buildr/commit/5b7933a5 Tree: http://git-wip-us.apache.org/repos/asf/buildr/tree/5b7933a5 Diff: http://git-wip-us.apache.org/repos/asf/buildr/diff/5b7933a5 Branch: refs/heads/master Commit: 5b7933a57bbc53ba80c90538c8681fabe7b82568 Parents: 8f54965 Author: Pepijn Van Eeckhoudt <[email protected]> Authored: Thu Aug 28 17:12:59 2014 +0200 Committer: Pepijn Van Eeckhoudt <[email protected]> Committed: Thu Aug 28 17:12:59 2014 +0200 ---------------------------------------------------------------------- CHANGELOG | 2 ++ lib/buildr/packaging/archive.rb | 16 +++++++++++++++- lib/buildr/packaging/tar.rb | 2 +- lib/buildr/packaging/ziptask.rb | 3 ++- spec/packaging/archive_spec.rb | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 54 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/buildr/blob/5b7933a5/CHANGELOG ---------------------------------------------------------------------- diff --git a/CHANGELOG b/CHANGELOG index d648bd3..b959b9c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,4 +1,6 @@ 1.4.21 (Pending) +* Fixed : BUILDR-702 - Retain Unix permission flags when merging + zip files into another zip or tar archive. Submitted by Pepijn Van Eeckhoudt. 1.4.20 (2014-08-23) * Fixed : BUILDR-688 - Disregard package-info.java files when determining if http://git-wip-us.apache.org/repos/asf/buildr/blob/5b7933a5/lib/buildr/packaging/archive.rb ---------------------------------------------------------------------- diff --git a/lib/buildr/packaging/archive.rb b/lib/buildr/packaging/archive.rb index 0462fee..cc053b7 100644 --- a/lib/buildr/packaging/archive.rb +++ b/lib/buildr/packaging/archive.rb @@ -298,7 +298,7 @@ module Buildr #:nodoc: [email protected]? { |pattern| File.fnmatch(pattern, entry.name) } dest = path =~ /^\/?$/ ? entry.name : Util.relative_path(path + "/" + entry.name) trace "Adding #{dest}" - file_map[dest] = lambda { |output| output.write source.read(entry) } + file_map[dest] = ZipEntryData.new(source, entry) end end end @@ -306,6 +306,20 @@ module Buildr #:nodoc: end + class ZipEntryData + def initialize(zipfile, entry) + @zipfile = zipfile + @entry = entry + end + + def call(output) + output.write @zipfile.read(@entry) + end + + def mode + @entry.unix_perms + end + end def initialize(*args) #:nodoc: super http://git-wip-us.apache.org/repos/asf/buildr/blob/5b7933a5/lib/buildr/packaging/tar.rb ---------------------------------------------------------------------- diff --git a/lib/buildr/packaging/tar.rb b/lib/buildr/packaging/tar.rb index e681821..fe1da78 100644 --- a/lib/buildr/packaging/tar.rb +++ b/lib/buildr/packaging/tar.rb @@ -92,7 +92,7 @@ module Buildr #:nodoc: file_map.each do |path, content| if content.respond_to?(:call) - tar.add_file(path, options) { |os, opts| content.call os } + tar.add_file(path, content.respond_to?(:mode) ? options.merge(:mode => content.mode) : options) { |os, _| content.call os } elsif content.nil? elsif File.directory?(content.to_s) stat = File.stat(content.to_s) http://git-wip-us.apache.org/repos/asf/buildr/blob/5b7933a5/lib/buildr/packaging/ziptask.rb ---------------------------------------------------------------------- diff --git a/lib/buildr/packaging/ziptask.rb b/lib/buildr/packaging/ziptask.rb index f4e7a0d..f57b4da 100644 --- a/lib/buildr/packaging/ziptask.rb +++ b/lib/buildr/packaging/ziptask.rb @@ -67,7 +67,8 @@ module Buildr #:nodoc: warn "Warning: Path in zipfile #{name} contains backslash: #{path}" if path =~ /\\/ mkpath.call File.dirname(path) if content.respond_to?(:call) - zip.put_next_entry(path, compression_level) + entry = zip.put_next_entry(path, compression_level) + entry.unix_perms = content.mode & 07777 if content.respond_to?(:mode) content.call zip elsif content.nil? || File.directory?(content.to_s) mkpath.call path http://git-wip-us.apache.org/repos/asf/buildr/blob/5b7933a5/spec/packaging/archive_spec.rb ---------------------------------------------------------------------- diff --git a/spec/packaging/archive_spec.rb b/spec/packaging/archive_spec.rb index 42d7e47..b911baf 100644 --- a/spec/packaging/archive_spec.rb +++ b/spec/packaging/archive_spec.rb @@ -445,6 +445,23 @@ describe TarTask do unzip('target' => 'foo.tgz').extract (File.stat('target/hello').mode & 0777).should == 0777 end + + it 'should preserve file permissions when merging zip files' do + # with JRuby it's important to use absolute paths with File.chmod() + # http://jira.codehaus.org/browse/JRUBY-3300 + hello = File.expand_path('src/main/bin/hello') + write hello, 'echo hi' + File.chmod(0777, hello) + fail("Failed to set permission on #{hello}") unless (File.stat(hello).mode & 0777) == 0777 + + foo = zip('foo.zip') + foo.include('src/main/bin/*').invoke + bar = tar('bar.tgz') + bar.merge(foo) + bar.invoke + unzip('target' => 'bar.tgz').extract + (File.stat('target/hello').mode & 0777).should == 0777 + end end end @@ -567,6 +584,23 @@ describe "ZipTask" do unzip('target' => 'foo.zip').extract (File.stat('target/hello').mode & 0777).should == 0777 end + + it 'should preserve file permissions when merging zip files' do + # with JRuby it's important to use absolute paths with File.chmod() + # http://jira.codehaus.org/browse/JRUBY-3300 + hello = File.expand_path('src/main/bin/hello') + write hello, 'echo hi' + File.chmod(0777, hello) + fail("Failed to set permission on #{hello}") unless (File.stat(hello).mode & 0777) == 0777 + + foo = zip('foo.zip') + foo.include('src/main/bin/*').invoke + bar = zip('bar.zip') + bar.merge(foo) + bar.invoke + unzip('target' => 'bar.zip').extract + (File.stat('target/hello').mode & 0777).should == 0777 + end end end
