Value retrieval from read_link_info, as like read_file_info record return

First day erlang, pretty cool! I wrote an escript with recursive directory copying, throwing exceptions, anon function, etc. Reading learnyousomeerlang, all over the docs all day, yt erlang tutorial on records, searched the forum and I’m all busted on read_link_info(Name, Opts) though.

Docs say it returns a record which looks like a tuple in a tuple. Converting to lists + methods doesn’t work; members, keyfind. Records seem to use # but not getting it and feeling stupid and blocked, I’m missing something huge, it’s so simple.

Objective: Check the returned record for type symlink.

information about the link is returned in the file_info record and the type field of the record is set to symlink .

Both in erl and with escript with header library imported.

e.g.

{ok, FileInfo} = file:read_link_info("Makefile").
FileInfo#file_info.type.

I’ve tried like every variation on that, assignments, just variable FileInfo with the ok tuple leader, omitted file_info, using name, and got no where with anything.

1 Like

What is the exact error you get? Is it during compilation or execution?

I’ve written a simple module that does the job, you probably can spot where the error is:

-module(a).

-export([link_type/1]).

-include_lib("kernel/include/file.hrl").

link_type(Path) ->
    {ok, F} = file:read_link_info(Path),
    F#file_info.type.

After compiling it (erlc a.erl), run erl and test it by calling a:link_type(Path).

1 Like

That’s the exact same semantics I used! It must be that it can only be done in compiled Erlang and not erl or escript. Kinda makes sense, but when I did it in escript and erl it would output something like:

{ok,{file_info,16,symlink,read_write,
               {{2024,11,6},{22,56,15}},
               {{2024,11,6},{22,56,15}},
               {{2024,11,6},{22,56,15}},
               41453,1,16777220,0,3733472,501,20}}

So I thought I could retrieve the value in the intended form. WRONG. I was wondering where the keys were! Apparently the lib also provides the return record data structure as well.

I did exactly as you said, screenshot at the Tildeverse-like Shared Linux Host envs.net since as a new user I cannot upload - just wanted you to know I did the work for the help.

Thank you much.

I wanted, verge of needed, it to work in escript so bad, I’m too headstrong for my own good sometimes! Guess I’ll have to use some conversion muck in the end, dang. Sucks for escript as read_file_info et al. is a pretty basic need for sysadmin scripts.

Here is an escipt that also works:

#! /usr/bin/env escript

-include_lib("kernel/include/file.hrl").

main([Path]=_Args) ->
    Result = link_type(Path),
    erlang:display(Result).

link_type(Path) ->
    {ok, F} = file:read_link_info(Path),
    F#file_info.type.

It probably can be done in shell too, via rr command but it’s not very convenient to include records in shell. In few years of Erlang, I’ve done it for the first time today:

1 Like

Oh man, it was F-Secure Antivirus blocking it with no notification. I got it figured out. Even though you would not have known, I was gonna keep thinking this was an erlang escript issue.

Way to hang in there and disabusing me of my assumption, I really do appreciate that.