Merge pull request #12890 from danielnachun/add_binary_grep
Add binary grep method to keg_relocate
This commit is contained in:
commit
76e4f6bc79
@ -183,4 +183,10 @@ class Keg
|
|||||||
# it's wrong. -O is a BSD-grep-only option.
|
# it's wrong. -O is a BSD-grep-only option.
|
||||||
"-lrO"
|
"-lrO"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def egrep_args
|
||||||
|
grep_bin = "egrep"
|
||||||
|
grep_args = recursive_fgrep_args
|
||||||
|
[grep_bin, grep_args]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -8,6 +8,8 @@ class Keg
|
|||||||
LIBRARY_PLACEHOLDER = "@@HOMEBREW_LIBRARY@@"
|
LIBRARY_PLACEHOLDER = "@@HOMEBREW_LIBRARY@@"
|
||||||
PERL_PLACEHOLDER = "@@HOMEBREW_PERL@@"
|
PERL_PLACEHOLDER = "@@HOMEBREW_PERL@@"
|
||||||
JAVA_PLACEHOLDER = "@@HOMEBREW_JAVA@@"
|
JAVA_PLACEHOLDER = "@@HOMEBREW_JAVA@@"
|
||||||
|
NULL_BYTE = "\x00"
|
||||||
|
NULL_BYTE_STRING = "\\x00"
|
||||||
|
|
||||||
class Relocation
|
class Relocation
|
||||||
extend T::Sig
|
extend T::Sig
|
||||||
@ -173,16 +175,42 @@ class Keg
|
|||||||
end
|
end
|
||||||
alias generic_recursive_fgrep_args recursive_fgrep_args
|
alias generic_recursive_fgrep_args recursive_fgrep_args
|
||||||
|
|
||||||
def each_unique_file_matching(string)
|
def egrep_args
|
||||||
|
grep_bin = "grep"
|
||||||
|
grep_args = recursive_fgrep_args
|
||||||
|
grep_args += "Pa"
|
||||||
|
[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)
|
||||||
Utils.popen_read("fgrep", recursive_fgrep_args, string, to_s) do |io|
|
Utils.popen_read("fgrep", recursive_fgrep_args, string, to_s) do |io|
|
||||||
hardlinks = Set.new
|
each_unique_file(io, block)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
until io.eof?
|
def each_unique_binary_file(&block)
|
||||||
file = Pathname.new(io.readline.chomp)
|
grep_bin, grep_args = egrep_args
|
||||||
next if file.symlink?
|
|
||||||
|
|
||||||
yield file if hardlinks.add? file.stat.ino
|
# We need to pass NULL_BYTE_STRING, the literal string "\x00", to grep
|
||||||
end
|
# 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
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
58
Library/Homebrew/test/keg_relocate/grep_spec.rb
Normal file
58
Library/Homebrew/test/keg_relocate/grep_spec.rb
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
# typed: false
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require "keg_relocate"
|
||||||
|
|
||||||
|
describe Keg do
|
||||||
|
subject(:keg) { described_class.new(HOMEBREW_CELLAR/"foo/1.0.0") }
|
||||||
|
|
||||||
|
let(:dir) { HOMEBREW_CELLAR/"foo/1.0.0" }
|
||||||
|
let(:text_file) { dir/"file.txt" }
|
||||||
|
let(:binary_file) { dir/"file.bin" }
|
||||||
|
|
||||||
|
before do
|
||||||
|
dir.mkpath
|
||||||
|
end
|
||||||
|
|
||||||
|
def setup_text_file
|
||||||
|
text_file.atomic_write <<~EOS
|
||||||
|
#{dir}/file.txt
|
||||||
|
/foo#{dir}/file.txt
|
||||||
|
foo/bar:#{dir}/file.txt
|
||||||
|
foo/bar:/foo#{dir}/file.txt
|
||||||
|
#{dir}/bar.txt:#{dir}/baz.txt
|
||||||
|
EOS
|
||||||
|
end
|
||||||
|
|
||||||
|
def setup_binary_file
|
||||||
|
binary_file.atomic_write <<~EOS
|
||||||
|
\x00
|
||||||
|
EOS
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#each_unique_file_matching" do
|
||||||
|
specify "find string matches to path" do
|
||||||
|
setup_text_file
|
||||||
|
|
||||||
|
string_matches = Set.new
|
||||||
|
keg.each_unique_file_matching(dir) do |file|
|
||||||
|
string_matches << file
|
||||||
|
end
|
||||||
|
|
||||||
|
expect(string_matches.size).to eq 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#each_unique_binary_file" do
|
||||||
|
specify "find null bytes in binaries" do
|
||||||
|
setup_binary_file
|
||||||
|
|
||||||
|
binary_matches = Set.new
|
||||||
|
keg.each_unique_binary_file do |file|
|
||||||
|
binary_matches << file
|
||||||
|
end
|
||||||
|
|
||||||
|
expect(binary_matches.size).to eq 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
x
Reference in New Issue
Block a user