# HG changeset patch # User Steve Losh # Date 1477922284 0 # Node ID 5126f5b37a97e99f8f4b820652a1ab70ab852b56 # Parent 59b3a26fd6724d77f0b49b3b5981ff167ee31e6a Don't crash on non-files when `--no-binary` is given Fixes https://github.com/sjl/friendly-find/issues/12 diff -r 59b3a26fd672 -r 5126f5b37a97 README.markdown --- 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]. diff -r 59b3a26fd672 -r 5126f5b37a97 ffind --- 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