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?
Best way to resolve a soft link?
- tonyw
- AmigaOS Core Developer
- Posts: 1479
- Joined: Wed Mar 09, 2011 1:36 pm
- Location: Sydney, Australia
Re: Best way to resolve a soft link?
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
tony
Re: Best way to resolve a soft 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.colinward wrote: Is there a function that can examine an individual file and return its target, if it is a link?
AmigaOne X1000 with 2GB memory - OS4.1 FE
- tonyw
- AmigaOS Core Developer
- Posts: 1479
- Joined: Wed Mar 09, 2011 1:36 pm
- Location: Sydney, Australia
Re: Best way to resolve a soft link?
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
tony
- colinw
- 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?
Looks like I need some more example code in the autodocs.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?
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 */
- nbache
- Beta Tester
- Posts: 1716
- Joined: Mon Dec 20, 2010 7:25 pm
- Location: Copenhagen, Denmark
- Contact:
Re: Best way to resolve a soft link?
Hmm ... what happens if you have a link pointing to another link (which is maybe even pointing to yet another ... ad nauseam)? Just wondering.colinw wrote:Here is some example code I just wrote from memory, watch out for typos, I havn't checked it.
Best regards,
Niels
Re: Best way to resolve a soft link?
You computer sinks into an infinite recursion, creating an electronic vortex that converges with a wormhole and transports you to another dimensionnbache 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.colinw wrote:Here is some example code I just wrote from memory, watch out for typos, I havn't checked it.
AmigaOne X1000 with 2GB memory - OS4.1 FE
- colinw
- 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?
Nothing so phantasmagorical, you just get to level 15 and then it fails with ERROR_TOO_MANY_LEVELS.xenic wrote:You computer sinks into an infinite recursion, creating an electronic vortex that converges with a wormhole and transports you to another dimensionnbache 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.
- nbache
- Beta Tester
- Posts: 1716
- Joined: Mon Dec 20, 2010 7:25 pm
- Location: Copenhagen, Denmark
- Contact:
Re: Best way to resolve a soft link?
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
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
- colinw
- 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?
Always 15 softlinks in any path to the target object, or for a softlinked recursive loop.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.