From 3d2d1edaa32762de8dd401f55a5a854fd44455bb Mon Sep 17 00:00:00 2001 From: danielnachun Date: Sat, 5 Mar 2022 19:23:50 -0800 Subject: [PATCH 1/3] keg_relocate.rb: add binary_file? method --- Library/Homebrew/keg_relocate.rb | 42 +++++++++++++++----------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/Library/Homebrew/keg_relocate.rb b/Library/Homebrew/keg_relocate.rb index ad779064bf..197b01a942 100644 --- a/Library/Homebrew/keg_relocate.rb +++ b/Library/Homebrew/keg_relocate.rb @@ -177,41 +177,39 @@ class Keg def egrep_args grep_bin = "grep" - grep_args = recursive_fgrep_args - grep_args += "Pa" + grep_args = [ + "--files-with-matches", + "--perl-regexp", + "--binary-files=text", + ] + [grep_bin, grep_args] end alias generic_egrep_args egrep_args - def each_unique_file(io, block) - hardlinks = Set.new - - until io.eof? - file = Pathname.new(io.readline.chomp) - # Don't return symbolic links. - next if file.symlink? - - # To avoid returning hardlinks, only return files with unique inodes. - # Hardlinks will have the same inode as the file they point to. - block.call file if hardlinks.add? file.stat.ino - end - end - - def each_unique_file_matching(string, &block) + def each_unique_file_matching(string) Utils.popen_read("fgrep", recursive_fgrep_args, string, to_s) do |io| - each_unique_file(io, block) + hardlinks = Set.new + + until io.eof? + file = Pathname.new(io.readline.chomp) + # Don't return symbolic links. + next if file.symlink? + + # To avoid returning hardlinks, only return files with unique inodes. + # Hardlinks will have the same inode as the file they point to. + yield file if hardlinks.add? file.stat.ino + end end end - def each_unique_binary_file(&block) + def binary_file?(file) grep_bin, grep_args = egrep_args # We need to pass NULL_BYTE_STRING, the literal string "\x00", to grep # rather than NULL_BYTE, a literal null byte, because grep will internally # convert the literal string "\x00" to a null byte. - Utils.popen_read(grep_bin, grep_args, NULL_BYTE_STRING, to_s) do |io| - each_unique_file(io, block) - end + Utils.popen_read(grep_bin, *grep_args, NULL_BYTE_STRING, file).present? end def lib From a9dd3a76eb8a00ff65a6662a1dccf1d8457a4fff Mon Sep 17 00:00:00 2001 From: danielnachun Date: Sat, 5 Mar 2022 19:24:27 -0800 Subject: [PATCH 2/3] extend/os/mac/keg_relocate: change egrep_args --- Library/Homebrew/extend/os/mac/keg_relocate.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Library/Homebrew/extend/os/mac/keg_relocate.rb b/Library/Homebrew/extend/os/mac/keg_relocate.rb index d59c3ee113..a58c3bd6f0 100644 --- a/Library/Homebrew/extend/os/mac/keg_relocate.rb +++ b/Library/Homebrew/extend/os/mac/keg_relocate.rb @@ -186,7 +186,7 @@ class Keg def egrep_args grep_bin = "egrep" - grep_args = recursive_fgrep_args + grep_args = "--files-with-matches" [grep_bin, grep_args] end end From 1faa4448bd0d77898cb093bfe86b9dcff92f229c Mon Sep 17 00:00:00 2001 From: danielnachun Date: Sat, 5 Mar 2022 19:24:55 -0800 Subject: [PATCH 3/3] test/keg_relocate/grep_spec.rb: update unit test --- Library/Homebrew/test/keg_relocate/grep_spec.rb | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/Library/Homebrew/test/keg_relocate/grep_spec.rb b/Library/Homebrew/test/keg_relocate/grep_spec.rb index 993fa4b875..889ed7904c 100644 --- a/Library/Homebrew/test/keg_relocate/grep_spec.rb +++ b/Library/Homebrew/test/keg_relocate/grep_spec.rb @@ -43,16 +43,15 @@ describe Keg do end end - describe "#each_unique_binary_file" do - specify "find null bytes in binaries" do + describe "#binary_file?" do + specify "test if file has null bytes" do setup_binary_file - binary_matches = Set.new - keg.each_unique_binary_file do |file| - binary_matches << file - end + expect(keg.binary_file?(binary_file)).to be true - expect(binary_matches.size).to eq 1 + setup_text_file + + expect(keg.binary_file?(text_file)).to be false end end end