Lianja
Lianja
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 Lianja Downloads
llSuccess = .F.
// Demonstrates the SFtp.ReadLink method, which returns the target path stored in a remote
// symbolic link. The only argument is the link path.
loSftp = createobject("CkSFtp")
// Connect, authenticate, and initialize the SFTP subsystem.
lnPort = 22
llSuccess = loSftp.Connect("sftp.example.com",lnPort)
if (llSuccess = .F.) then
? loSftp.LastErrorText
release loSftp
return
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.
lcPassword = "mySshPassword"
llSuccess = loSftp.AuthenticatePw("mySshLogin",lcPassword)
if (llSuccess = .F.) then
? loSftp.LastErrorText
release loSftp
return
endif
llSuccess = loSftp.InitializeSftp()
if (llSuccess = .F.) then
? loSftp.LastErrorText
release loSftp
return
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.
lcTarget = loSftp.ReadLink("subdir/latest.log")
if (loSftp.LastMethodSuccess = .F.) then
? loSftp.LastErrorText
release loSftp
return
endif
? "The link points to: " + lcTarget
// To resolve it to an absolute path, pass it to RealPath.
lcAbsTarget = loSftp.RealPath(lcTarget,"")
if (loSftp.LastMethodSuccess = .F.) then
? loSftp.LastErrorText
release loSftp
return
endif
? "Absolute target: " + lcAbsTarget
loSftp.Disconnect()
release loSftp