From fefbee31e7a1f84ab88401fdc91db14889873d28 Mon Sep 17 00:00:00 2001 From: Muhammad Umar Nadeem Date: Sat, 14 Nov 2026 23:30:00 +0500 Subject: [PATCH] Identify named pipes without reading their contents --- README.md | 2 +- identify/identify.py | 5 ++++- tests/identify_test.py | 13 +++++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index c98afe27..aab5e014 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ If you have an actual file on disk, you can get the most information possible When using a file on disk, the checks performed are: -* File type (file, symlink, directory, socket) +* File type (file, symlink, directory, socket, fifo) * Mode (is it executable?) * File name (mostly based on extension) * If executable, the shebang is read and the interpreter interpreted diff --git a/identify/identify.py b/identify/identify.py index 0279ba8e..80642392 100644 --- a/identify/identify.py +++ b/identify/identify.py @@ -20,13 +20,14 @@ DIRECTORY = 'directory' SYMLINK = 'symlink' SOCKET = 'socket' +FIFO = 'fifo' FILE = 'file' EXECUTABLE = 'executable' NON_EXECUTABLE = 'non-executable' TEXT = 'text' BINARY = 'binary' -TYPE_TAGS = frozenset((DIRECTORY, FILE, SYMLINK, SOCKET)) +TYPE_TAGS = frozenset((DIRECTORY, FILE, SYMLINK, SOCKET, FIFO)) MODE_TAGS = frozenset((EXECUTABLE, NON_EXECUTABLE)) ENCODING_TAGS = frozenset((BINARY, TEXT)) _ALL_TAGS = {*TYPE_TAGS, *MODE_TAGS, *ENCODING_TAGS} @@ -50,6 +51,8 @@ def tags_from_path(path: str) -> set[str]: return {SYMLINK} if stat.S_ISSOCK(mode): return {SOCKET} + if stat.S_ISFIFO(mode): + return {FIFO} tags = {FILE} diff --git a/tests/identify_test.py b/tests/identify_test.py index a82f353c..7c7e48d7 100644 --- a/tests/identify_test.py +++ b/tests/identify_test.py @@ -20,6 +20,7 @@ def test_all_tags_includes_basic_ones(): assert 'executable' in identify.ALL_TAGS assert 'text' in identify.ALL_TAGS assert 'socket' in identify.ALL_TAGS + assert 'fifo' in identify.ALL_TAGS @pytest.mark.parametrize( @@ -71,6 +72,18 @@ def test_tags_from_path_socket(): assert tags == {'socket'} +@pytest.mark.skipif(not hasattr(os, 'mkfifo'), reason='requires os.mkfifo') +@pytest.mark.parametrize('filename', ('pipe', 'pipe.py')) +def test_tags_from_path_fifo(tmpdir, filename): + path = tmpdir.join(filename).strpath + os.mkfifo(path) + + with mock.patch.object( + builtins, 'open', side_effect=AssertionError('must not read a FIFO'), + ): + assert identify.tags_from_path(path) == {'fifo'} + + def test_tags_from_path_broken_symlink(tmpdir): x = tmpdir.join('foo') x.mksymlinkto(tmpdir.join('lol'))