1

This is a seemingly simple question however I could not find a satisfactory solution yet.

let f = FileInfo(e)
let fileFlag = f.Attributes.HasFlag(FileAttributes.Normal)
logger <| sprintf "%s :: %A :: %A" e fileFlag f.Attributes

This produces the following output:

/tmp/com.apple.launchd.qq7qSI6dyD/Listeners :: true :: Normal

To my surprise the path is a socket:

> file /tmp/com.apple.launchd.qq7qSI6dyD/Listeners
/tmp/com.apple.launchd.qq7qSI6dyD/Listeners: socket

According to the FileAttributes Enum documentation:

Normal 128

The file is a standard file that has no special attributes. This attribute is valid only if it is used alone. Normal is supported on Windows, Linux, and macOS.

Is there a way to check is a path really a normal file (not a socket, not a symlink, not a device, etc.).

For example in Python:

> python -c "import os; import sys; print(os.path.isfile(sys.argv[1]))"
/tmp/com.apple.launchd.qq7qSI6dyD/Listeners
False

Elaborating more:

> stat -f "%N: %HT%SY" /tmp/**/*
/tmp/b: Symbolic Link -> a
/tmp/com.apple.launchd.qq7qSI6dyD: Directory
/tmp/com.apple.launchd.qq7qSI6dyD/Listeners: Socket
/tmp/fseventsd-uuid: Regular File
/tmp/powerlog: Directory
/tmp/test: Regular File
/tmp/tmux-501: Directory
/tmp/tmux-501/default: Socket
Istvan
  • 7,500
  • 9
  • 59
  • 109
  • Isn't sockets, devices, symlinks, etc, all _types_ of file, in the same way that a `.txt` file is? – gunr2171 Dec 15 '21 at 22:12
  • I want the exact copy of the behaviour of os.path.isfile from Python. – Istvan Dec 15 '21 at 22:13
  • 1
    That seems like a runtime bug to me. A socket shouldn't have the attribute Normal IMO. You might want to submit that as an issue on Github. – N8allan Dec 15 '21 at 22:27
  • 1
    Under the hood, python's [`isfile`](https://github.com/python/cpython/blob/f4c03484da59049eb62a9bf7777b963e2267d187/Lib/genericpath.py#L27) uses the .[`stat`](https://github.com/python/cpython/blob/f4c03484da59049eb62a9bf7777b963e2267d187/Lib/nntplib.py#L731) program to determine that information. I don't think there's a native equvilent to that in .Net Core yet - but it might exist in [Mono](https://stackoverflow.com/questions/45132081/file-permissions-on-linux-unix-with-net-core). – gunr2171 Dec 15 '21 at 22:47
  • Yep, stat like behaviour would be great. – Istvan Dec 15 '21 at 22:55
  • 1
    Does this answer your question? [File permissions on Linux/Unix with .NET Core](https://stackoverflow.com/questions/45132081/file-permissions-on-linux-unix-with-net-core) – Corey Dec 15 '21 at 23:35
  • No. I do not need permissions I need stat like behaviour. – Istvan Dec 16 '21 at 09:18

1 Answers1

2

As of today (2021.12.16) there is no way in .net to identify files in the POSIX sense.

The workaround is to use Mono.Unix the following way:

open Mono.Unix

let fileStat =
    UnixFileSystemInfo.GetFileSystemEntry(pathEntry)

let isRegular = fileStat.IsRegularFile
Istvan
  • 7,500
  • 9
  • 59
  • 109