在 Erlang 中从 IoDevice 获取文件名

Getting filename from IoDevice in Erlang

我有一个尝试从打开的 IoDevice 读取的函数。 在 {error, Reason} 的情况下,我想打印出文件名。

我该怎么做?

源代码:

read_file(IoDevice) ->
    case read_file(IoDevice, []) of
        {ok, OpCodes} ->
            OpCodes;
        {error, Reason} ->
            io:format("Unable to read file ~s: ~s ~n", [File, Reason]),
            []
    end.

IoDevice 可以是进程 ID 或文件描述符。如果是进程ID,可以用file:pid2name/1获取文件名:

1> {ok,IoDevice} = file:open("/tmp/x.erl", [read]).
{ok,<0.43.0>}
2> {ok, Filename} = file:pid2name(IoDevice).
{ok,"/tmp/x.erl"}

但是,如果 IoDevice 是一个文件描述符,pid2name 将不起作用,而且我不知道在那种情况下获取文件名的方法。