Sample code for 30+ languages & platforms
DataFlex

Read the Target of a Symbolic Link

See more SFTP Examples

Demonstrates the Chilkat SFtp.ReadLink method, which returns the target path stored in a remote symbolic link. The only argument is the link path. The example then resolves the target to an absolute path with RealPath.

Background: ReadLink returns the raw string stored in the link exactly as it was created — which may be relative, absolute, or even dangling — without resolving it. That distinction matters: to learn where a link actually leads you follow it with RealPath, which canonicalizes the path on the server. Reading the raw target is the right choice when you need to inspect or preserve the link itself rather than its destination.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSftp
    Integer iPort
    String sPassword
    String sTarget
    String sAbsTarget
    String sTemp1
    Boolean bTemp1

    Move False To iSuccess

    //  Demonstrates the SFtp.ReadLink method, which returns the target path stored in a remote
    //  symbolic link.  The only argument is the link path.

    Get Create (RefClass(cComChilkatSFtp)) To hoSftp
    If (Not(IsComObjectCreated(hoSftp))) Begin
        Send CreateComObject of hoSftp
    End

    //  Connect, authenticate, and initialize the SFTP subsystem.
    Move 22 To iPort
    Get ComConnect Of hoSftp "sftp.example.com" iPort To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSftp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Normally you would not hard-code the password in source.  You should instead obtain it
    //  from an interactive prompt, environment variable, or a secrets vault.
    Move "mySshPassword" To sPassword

    Get ComAuthenticatePw Of hoSftp "mySshLogin" sPassword To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSftp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComInitializeSftp Of hoSftp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSftp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Read the target of a symbolic link.  The returned value is the raw target string stored in
    //  the link; it is not canonicalized and can be relative or absolute.
    Get ComReadLink Of hoSftp "subdir/latest.log" To sTarget
    Get ComLastMethodSuccess Of hoSftp To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoSftp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "The link points to: " sTarget

    //  To resolve it to an absolute path, pass it to RealPath.
    Get ComRealPath Of hoSftp sTarget "" To sAbsTarget
    Get ComLastMethodSuccess Of hoSftp To bTemp1
    If (bTemp1 = False) Begin
        Get ComLastErrorText Of hoSftp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Absolute target: " sAbsTarget

    Send ComDisconnect To hoSftp


End_Procedure