PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkSFtp.pb"
Procedure ChilkatExample()
success.i = 0
; Demonstrates the SFtp.ReadLink method, which returns the target path stored in a remote
; symbolic link. The only argument is the link path.
sftp.i = CkSFtp::ckCreate()
If sftp.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
; Connect, authenticate, and initialize the SFTP subsystem.
port.i = 22
success = CkSFtp::ckConnect(sftp,"sftp.example.com",port)
If success = 0
Debug CkSFtp::ckLastErrorText(sftp)
CkSFtp::ckDispose(sftp)
ProcedureReturn
EndIf
; 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.
password.s = "mySshPassword"
success = CkSFtp::ckAuthenticatePw(sftp,"mySshLogin",password)
If success = 0
Debug CkSFtp::ckLastErrorText(sftp)
CkSFtp::ckDispose(sftp)
ProcedureReturn
EndIf
success = CkSFtp::ckInitializeSftp(sftp)
If success = 0
Debug CkSFtp::ckLastErrorText(sftp)
CkSFtp::ckDispose(sftp)
ProcedureReturn
EndIf
; 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.
target.s = CkSFtp::ckReadLink(sftp,"subdir/latest.log")
If CkSFtp::ckLastMethodSuccess(sftp) = 0
Debug CkSFtp::ckLastErrorText(sftp)
CkSFtp::ckDispose(sftp)
ProcedureReturn
EndIf
Debug "The link points to: " + target
; To resolve it to an absolute path, pass it to RealPath.
absTarget.s = CkSFtp::ckRealPath(sftp,target,"")
If CkSFtp::ckLastMethodSuccess(sftp) = 0
Debug CkSFtp::ckLastErrorText(sftp)
CkSFtp::ckDispose(sftp)
ProcedureReturn
EndIf
Debug "Absolute target: " + absTarget
CkSFtp::ckDisconnect(sftp)
CkSFtp::ckDispose(sftp)
ProcedureReturn
EndProcedure