2

I want to run the file(1) command on the output of another command, but all I get is

$ file -sL <(echo \#include \<stdio.h\>)
/dev/fd/63: ERROR: (null)

This works as expected in file-5.04 (Red Hat Enterprise Linux Server release 6.8), but not in file-5.14 (Wind River Linux 6.0.0.17).

1 Answers1

3

It's a bug in the libmagic library that file is using.

A simple workaround is a "useless use of cat":

echo '#! /bin/sh' | file -
cat /path/to/fifo-or-special | file -

The bug was first introduced in https://github.com/file/file/commit/fb6084e0f08:

commit fb6084e0f08aef8991afcb3eb74168a456601908
Author: Christos Zoulas <christos@zoulas.com>
Date:   Tue May 28 21:24:31 2013 +0000

    don't print a space if there was an error. (from Jan Kaluza)

It was then fixed, but only for block and character devices, NOT for FIFOs in https://github.com/file/file/commit/a9124dcb4e. An incomplete fix for <(...) on Linux was in https://github.com/file/file/commit/adbe541c32.

The fix for devices could be replicated for FIFOs. See the patch at the end of the answer (apply by hand because this site mangles tabs and keep in mind that it's against a MIRROR ONLY repo).

But that still leaves:

mkfifo fifo; file -s fifo

fifo: writable, no read permission

Stupid and wrong, because the FIFO has read permission.

Repairing this will mean either rewriting half of libmagic, or add yet another couple of ifdef spaghetti and special cases to the horrible mess.

diff --git a/src/fsmagic.c b/src/fsmagic.c
index 5204f20d..20b7f438 100644
--- a/src/fsmagic.c
+++ b/src/fsmagic.c
@@ -270,8 +270,10 @@ file_fsmagic(struct magic_set *ms, const char *fn, struct stat *sb)
    /* TODO add code to handle V7 MUX and Blit MUX files */
 #ifdef S_IFIFO
    case S_IFIFO:
-       if((ms->flags & MAGIC_DEVICES) != 0)
+       if((ms->flags & MAGIC_DEVICES) != 0) {
+           return 0;
            break;
+       }
        if (mime) {
            if (handle_mime(ms, mime, "fifo") == -1)
                return -1;
  • Did your compiler not tell you that you could delete the break? (-: – JdeBP Mar 13 '20 at 09:44
  • Not to excuse me for just monkeying the previous change, but it did not (gcc (Debian 8.3.0-6) 8.3.0). –  Mar 13 '20 at 09:55