Don't crash on non-files when `--no-binary` is given
Fixes https://github.com/sjl/friendly-find/issues/12
author |
Steve Losh <steve@stevelosh.com> |
date |
Mon, 31 Oct 2016 13:58:04 +0000 |
parents |
59b3a26fd672
|
children |
e45034370c06
|
branches/tags |
v1.0.1 |
files |
README.markdown ffind |
Changes
--- a/README.markdown Wed Dec 09 12:11:24 2015 +0000
+++ b/README.markdown Mon Oct 31 13:58:04 2016 +0000
@@ -133,7 +133,7 @@
License
-------
-Copyright 2015 Steve Losh and contributors.
+Copyright 2016 Steve Losh and contributors.
Licensed under [version 3 of the GPL][gpl].
--- a/ffind Wed Dec 09 12:11:24 2015 +0000
+++ b/ffind Mon Oct 31 13:58:04 2016 +0000
@@ -101,7 +101,6 @@
# Global Options --------------------------------------------------------------
-# (it's a prototype, shut up)
options = None
@@ -120,7 +119,7 @@
sys.stderr.write('warning: ' + s + '\n')
-# Ingore Files ----------------------------------------------------------------
+# Ignore Files ----------------------------------------------------------------
def compile_re(line):
try:
r = re.compile(line)
@@ -423,9 +422,10 @@
def match(query, path, basename):
def _match():
- if options.type != TYPES_ALL:
- if get_type(path) not in options.type:
- return False
+ type = get_type(path)
+
+ if type not in options.type:
+ return False
if not query(path if options.entire else basename):
return False
@@ -447,14 +447,19 @@
if stat.st_mtime < options.after:
return False
- if not options.binary:
- # We open in non-blocking mode so things like file-based sockets
- # don't hang while waiting for their full kb.
- # TODO: Ignore those altogether for the binary check?
- fd = os.open(path, os.O_NONBLOCK)
- with os.fdopen(fd) as f:
- if '\0' in f.read(1024):
- return False
+ if (not options.binary) and (type in TYPES_FILE):
+ # Resolve symlinks
+ target = os.path.realpath(path) if type in TYPES_SYMLINK else path
+
+ # Ignore broken symlinks (treating them as non-binary)
+ if os.path.exists(target):
+ # We open in non-blocking mode so things like file-based sockets
+ # don't hang while waiting for their full kb.
+ # TODO: Ignore those altogether for the binary check?
+ fd = os.open(target, os.O_NONBLOCK)
+ with os.fdopen(fd) as f:
+ if '\0' in f.read(1024):
+ return False
return True