How to detect first-in first-out special file (named pipe) with dotnet?

71 views Asked by At

I'd like to identify files that are first-in first-out special file (named pipe) on Linux with dotnet

Here is a kind of file returned by ls:

$ ls -al /home/xxxxxx/.mkmkmmkk/sdfsdf.pipe
prw------- 1 xxxxxx xxxxxx 0  4 nov  2018 /home/xxxxxx/.mkmkmmkk/sdfsdf.pipe

this kind of file can be created with mkfifo someFileName

How can I retrieve the p attribute from dotnet without running an external process ?

Thanks

1

There are 1 answers

0
manuc66 On

By using interop it's possible to identify if the files are FIFO:

Sys.LStat("/home/xxxxxx/.mkmkmmkk/sdfsdf.pipe", out Sys.FileStatus fileStatus);
Console.WriteLine((fileStatus.Mode & Sys.FileTypes.S_IFMT) == Sys.FileTypes.S_IFIFO);

Here is the supporting interops:

static class Sys
{
    [StructLayout(LayoutKind.Sequential)]
    internal struct FileStatus
    {
        internal FileStatusFlags Flags;
        internal int Mode;
        internal uint Uid;
        internal uint Gid;
        internal long Size;
        internal long ATime;
        internal long ATimeNsec;
        internal long MTime;
        internal long MTimeNsec;
        internal long CTime;
        internal long CTimeNsec;
        internal long BirthTime;
        internal long BirthTimeNsec;
        internal long Dev;
        internal long Ino;
        internal uint UserFlags;
    }

    internal static class FileTypes
    {
        internal const int S_IFMT = 0xF000;
        internal const int S_IFIFO = 0x1000;
        internal const int S_IFCHR = 0x2000;
        internal const int S_IFDIR = 0x4000;
        internal const int S_IFREG = 0x8000;
        internal const int S_IFLNK = 0xA000;
        internal const int S_IFSOCK = 0xC000;
    }

    [Flags]
    internal enum FileStatusFlags
    {
        None = 0,
        HasBirthTime = 1,
    }

    [DllImport("libSystem.Native", EntryPoint = "SystemNative_FStat", SetLastError = true)]
    internal static extern int FStat(SafeHandle fd, out FileStatus output);

    [DllImport("libSystem.Native", EntryPoint = "SystemNative_Stat", SetLastError = true)]
    internal static extern int Stat(string path, out FileStatus output);

    [DllImport("libSystem.Native", EntryPoint = "SystemNative_LStat", SetLastError = true)]
    internal static extern int LStat(string path, out FileStatus output);
}