Best way to resolve a soft link?

This forum is for general developer support questions.
colinward
Beta Tester
Beta Tester
Posts: 8
Joined: Sun Jan 06, 2013 9:29 am

Best way to resolve a soft link?

Post by colinward »

Hello all.

I have a situation where I need to resolve the target of a softlink programatically.

We have the dos.library function ReadSoftLink() but this is a low level function that sends a message to the filesystem. From its API it is obviously too low level for application programs and its documentation says to use the new ExamineData related API functions to do this. By this I assume it means the ExamineDir() function, which can resolve soft links. But this function is for scanning directories, not individual files.

Is there a function that can examine an individual file and return its target, if it is a link?
User avatar
tonyw
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 1479
Joined: Wed Mar 09, 2011 1:36 pm
Location: Sydney, Australia

Re: Best way to resolve a soft link?

Post by tonyw »

No, because a link is strictly not a file, but a different sort of object. The only way to read the "contents" string of a soft link is to scan the directory. If you try to get information about the link, you will be redirected to its target and you will get information about the target instead.
cheers
tony
xenic
Posts: 1185
Joined: Sun Jun 19, 2011 12:06 am

Re: Best way to resolve a soft link?

Post by xenic »

colinward wrote: Is there a function that can examine an individual file and return its target, if it is a link?
You don't need ReadSoftLink() unless you are using obsolete AmigaDOS functions. OS4 AmigaDOS automatically resolves links. When you Lock() a softlink, OS4 AmigaDOS is actually returning a lock on the file that the link points to (the target of the link). If you need to know if you locked the file through a link, you can use the new LockTagList() function. I had to read the AmigaDOS autodocs to understand how OS4 AmigaDOS works now. You can't necessarily rely on your knowledge of OS3 AmigaDOS to use OS4 AmigaDOS.
AmigaOne X1000 with 2GB memory - OS4.1 FE
User avatar
tonyw
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 1479
Joined: Wed Mar 09, 2011 1:36 pm
Location: Sydney, Australia

Re: Best way to resolve a soft link?

Post by tonyw »

Perhaps I should have asked: Colin, why do you want to resolve the link yourself??? As Xenic says, it's done automatically: as soon as you reference the link, you actually reference the target.
cheers
tony
User avatar
colinw
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 207
Joined: Mon Aug 15, 2011 9:20 am
Location: Brisbane, QLD. Australia.

Re: Best way to resolve a soft link?

Post by colinw »

colinward wrote: I have a situation where I need to resolve the target of a softlink programatically.
Is there a function that can examine an individual file and return its target, if it is a link?
Looks like I need some more example code in the autodocs.

Softlink target object type checking might also be done on targets which are on a volume that isn't currently
mounted, it is recommended that DOS requesters be disabled during the ExamineObject() call to prevent
the "Please Insert Volume..." requester appearing.

Otherwise, just use the ExamineObjectTags() function to get info for the resolved target.
Simply Lock()'ing the link will resolve it to the target, that is done inside ExamineObjectTags().

Here is some example code I just wrote from memory, watch out for typos, I havn't checked it.

Code: Select all


APTR context = IDOS->ObtainDirContextTags(EX_StringNameInput,"SYS:",
                       EX_DataFields,(EXF_NAME|EXF_LINK|EXF_TYPE),
                       TAG_END);
if( context )
{
    struct ExamineData *dat, *target;

    while((dat = IDOS->ExamineDir(context)))  /* until no more data.*/
    {
        if( EXD_IS_LINK(dat) ) /* all links - must check for these first ! */
        {
            if( EXD_IS_SOFTLINK(dat) )        /* a FFS style softlink */
            {
                CONST_STRPTR target_type = "unavailable";   /* default  */
                APTR oldwin = IDOS->SetProcWindow((APTR)-1); 
                target = IDOS->ExamineObjectTags(EX_StringNameInput,dat->Name,TAG_END);
                IDOS->SetProcWindow(oldwin); 
                 
                if( target )
                {
                    if( EXD_IS_FILE(target) )
                    {
                         target_type = "file";
                    }
                    if( EXD_IS_DIRECTORY(target) )
                    {
                         target_type = "dir";
                    }
                    IDOS->FreeDosObject(DOS_EXAMINEDATA,target); /* Free target data when done */
                }
                IDOS->Printf("softlink=%s points to %s and it is a %s\n", dat->Name,dat->Link,target_type);
            }
            else if( EXD_IS_FILE(dat) )       /* hardlink file */
            {
                IDOS->Printf("file hardlink=%s points to %s\n", dat->Name, dat->Link);
            }
            else if( EXD_IS_DIRECTORY(dat) )  /* hardlink dir */
            {
                IDOS->Printf("dir hardlink=%s points to %s\n", dat->Name, dat->Link);
            }
        }

        else if( EXD_IS_FILE(dat) )           /* a plain file */
        {
            IDOS->Printf("filename=%s\n", dat->Name);
        }
        else if ( EXD_IS_DIRECTORY(dat) )     /* a plain directory */
        {
            IDOS->Printf("dirname=%s\n",  dat->Name); 
        }
        ....
    }
    if( ERROR_NO_MORE_ENTRIES != IDOS->IoErr() )
    {
        IDOS->PrintFault(IDOS->IoErr(),NULL); /* failure - why ? */
    }
}
else
{
    IDOS->PrintFault(IDOS->IoErr(),NULL);     /* failure - why ? */
}

IDOS->ReleaseDirContext(context);             /* NULL safe */
User avatar
nbache
Beta Tester
Beta Tester
Posts: 1714
Joined: Mon Dec 20, 2010 7:25 pm
Location: Copenhagen, Denmark
Contact:

Re: Best way to resolve a soft link?

Post by nbache »

colinw wrote:Here is some example code I just wrote from memory, watch out for typos, I havn't checked it.
Hmm ... what happens if you have a link pointing to another link (which is maybe even pointing to yet another ... ad nauseam)? Just wondering.

Best regards,

Niels
xenic
Posts: 1185
Joined: Sun Jun 19, 2011 12:06 am

Re: Best way to resolve a soft link?

Post by xenic »

nbache wrote:
colinw wrote:Here is some example code I just wrote from memory, watch out for typos, I havn't checked it.
Hmm ... what happens if you have a link pointing to another link (which is maybe even pointing to yet another ... ad nauseam)? Just wondering.
You computer sinks into an infinite recursion, creating an electronic vortex that converges with a wormhole and transports you to another dimension :-)
AmigaOne X1000 with 2GB memory - OS4.1 FE
User avatar
colinw
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 207
Joined: Mon Aug 15, 2011 9:20 am
Location: Brisbane, QLD. Australia.

Re: Best way to resolve a soft link?

Post by colinw »

xenic wrote:
nbache wrote:Hmm ... what happens if you have a link pointing to another link (which is maybe even pointing to yet another ... ad nauseam)? Just wondering.
You computer sinks into an infinite recursion, creating an electronic vortex that converges with a wormhole and transports you to another dimension :-)
Nothing so phantasmagorical, you just get to level 15 and then it fails with ERROR_TOO_MANY_LEVELS.
User avatar
nbache
Beta Tester
Beta Tester
Posts: 1714
Joined: Mon Dec 20, 2010 7:25 pm
Location: Copenhagen, Denmark
Contact:

Re: Best way to resolve a soft link?

Post by nbache »

He-he ...

But I wasn't talking about a looping definition, just a couple of levels of links pointing to links until reaching one that points to a real file.

Or, at its simplest: A link pointing to a link pointing to a file.

Colin, your example code doesn't seem (at a glance) to cater for this possibility, is that because it would be an invalid situation in some way (I haven't actually tried setting it up as yet), or is it just because it was a quick example "from memory"?

Best regards,

Niels
User avatar
colinw
AmigaOS Core Developer
AmigaOS Core Developer
Posts: 207
Joined: Mon Aug 15, 2011 9:20 am
Location: Brisbane, QLD. Australia.

Re: Best way to resolve a soft link?

Post by colinw »

nbache wrote:He-he ...
But I wasn't talking about a looping definition, just a couple of levels of links pointing to links until reaching
one that points to a real file. Or, at its simplest: A link pointing to a link pointing to a file.
Always 15 softlinks in any path to the target object, or for a softlinked recursive loop.
Post Reply