From 642e355b4f042de80a78036f66d5810dc392cfdc Mon Sep 17 00:00:00 2001
From: Markus Reiter <me@reitermark.us>
Date: Fri, 10 Mar 2017 08:24:40 +0100
Subject: [PATCH] Save flags in `with_full_permissions`.

---
 Library/Homebrew/cask/lib/hbc/pkg.rb   | 4 +++-
 Library/Homebrew/test/cask/pkg_spec.rb | 4 ++--
 2 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/Library/Homebrew/cask/lib/hbc/pkg.rb b/Library/Homebrew/cask/lib/hbc/pkg.rb
index 102755c534..902d564494 100644
--- a/Library/Homebrew/cask/lib/hbc/pkg.rb
+++ b/Library/Homebrew/cask/lib/hbc/pkg.rb
@@ -92,12 +92,14 @@ module Hbc
 
     def with_full_permissions(path)
       original_mode = (path.stat.mode % 01000).to_s(8)
-      # TODO: similarly read and restore macOS flags (cf man chflags)
+      original_flags = @command.run!("/usr/bin/stat", args: ["-f", "%Of", "--", path]).stdout.chomp
+
       @command.run!("/bin/chmod", args: ["--", "777", path], sudo: true)
       yield
     ensure
       if path.exist? # block may have removed dir
         @command.run!("/bin/chmod", args: ["--", original_mode, path], sudo: true)
+        @command.run!("/usr/bin/chflags", args: ["--", original_flags, path], sudo: true)
       end
     end
 
diff --git a/Library/Homebrew/test/cask/pkg_spec.rb b/Library/Homebrew/test/cask/pkg_spec.rb
index 9185331fbe..9930cd00ff 100644
--- a/Library/Homebrew/test/cask/pkg_spec.rb
+++ b/Library/Homebrew/test/cask/pkg_spec.rb
@@ -1,7 +1,7 @@
 describe Hbc::Pkg, :cask do
   describe "#uninstall" do
     let(:fake_system_command) { Hbc::NeverSudoSystemCommand }
-    let(:empty_response) { double(stdout: "", plist: {"volume" => "/", "install-location" => "", "paths" => {}}) }
+    let(:empty_response) { double(stdout: "", plist: { "volume" => "/", "install-location" => "", "paths" => {} }) }
     let(:pkg) { described_class.new("my.fake.pkg", fake_system_command) }
 
     it "removes files and dirs referenced by the pkg" do
@@ -51,7 +51,7 @@ describe Hbc::Pkg, :cask do
 
     it "removes broken symlinks" do
       fake_dir  = Pathname.new(Dir.mktmpdir)
-      fake_root  = Pathname.new(Dir.mktmpdir)
+      fake_root = Pathname.new(Dir.mktmpdir)
       fake_file = fake_dir.join("ima_file").tap { |path| FileUtils.touch(path) }
 
       intact_symlink = fake_dir.join("intact_symlink").tap { |path| path.make_symlink(fake_file) }
-- 
GitLab