feat(sftp): Sftp 现在支持获取用户主目录.

鉴于当前尚不支持使用"~"指向用户主目录, 为此提供解决方法.
This commit is contained in:
LamGC 2021-09-10 02:33:33 +08:00
parent 2006097d5f
commit ab69189d5b
Signed by: LamGC
GPG Key ID: 6C5AE2A913941E1D

View File

@ -1,10 +1,12 @@
package net.lamgc.oracle.sentry.oci.compute.ssh;
import net.lamgc.oracle.sentry.common.LazyLoader;
import org.apache.sshd.sftp.client.SftpClient;
import org.apache.sshd.sftp.common.SftpConstants;
import org.apache.sshd.sftp.common.SftpException;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.DirectoryNotEmptyException;
import java.nio.file.NoSuchFileException;
import java.util.HashSet;
@ -30,6 +32,7 @@ public class SftpSession implements Closeable {
}
private final SftpClient sftpClient;
private final LazyLoader<String> userHome;
/**
* 创建 Sftp 会话.
@ -37,6 +40,20 @@ public class SftpSession implements Closeable {
*/
SftpSession(SftpClient sftpClient) {
this.sftpClient = sftpClient;
this.userHome = new LazyLoader<>(() -> {
try {
CommandExecSession echoHOME = new CommandExecSession(sftpClient.getSession().createExecChannel("echo $HOME"));
ByteArrayOutputStream outBuffer = new ByteArrayOutputStream();
echoHOME.setOut(outBuffer);
echoHOME.exec();
if (echoHOME.exitCode() != null && echoHOME.exitCode() == 0) {
return outBuffer.toString(StandardCharsets.UTF_8).trim();
}
throw new IOException("Command execution failed.(ExitCode: " + echoHOME.exitCode() + ")");
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
/**
@ -247,4 +264,17 @@ public class SftpSession implements Closeable {
sftpClient.close();
}
/**
* 获取用户主目录.
* @return 返回用户主目录, 例如: "/root", 末尾无符号.
* @throws IOException 如果操作失败, 则抛出异常.
*/
public String getUserHome() throws IOException {
try {
return userHome.getInstance();
} catch (RuntimeException e) {
throw (IOException) e.getCause();
}
}
}