From dd4302ae9bd75f05be2b9a1a3b44f8925d1ea700 Mon Sep 17 00:00:00 2001 From: Jack Nagel Date: Thu, 27 Sep 2012 17:03:43 -0500 Subject: Improve text_executable heuristic Previously we detected this by reading the first line of the file. However, "first line" is meaningless when dealing with binary files, but IO#readline will happily keep reading until it finds a newline character, which can result in some unnecessarily large buffers. Aside from the performance issue, this causes an additional problem under Ruby 1.9: trying to match the binary string against a pattern will raise ArgumentError (unless the binary string just happens to also be valid UTF-8, heh). Fix both issues: only read the first 1024 bytes, as no sane shebang will ever be that long, and use a plain read(), which returns an ASCII encoded string even on 1.9. Signed-off-by: Jack Nagel --- Library/Homebrew/extend/pathname.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'Library') diff --git a/Library/Homebrew/extend/pathname.rb b/Library/Homebrew/extend/pathname.rb index 8ea1b9a93..9f6f1910b 100644 --- a/Library/Homebrew/extend/pathname.rb +++ b/Library/Homebrew/extend/pathname.rb @@ -199,9 +199,7 @@ class Pathname end def text_executable? - %r[^#!\s*.+] === open('r') { |f| f.readline } - rescue EOFError - false + %r[^#!\s*\S+] === open('r') { |f| f.read(1024) } end def incremental_hash(hasher) -- cgit v1.2.3