use of com.jn.agileway.ssh.client.sftp.attrs.FileAttrs in project agileway by fangjinuo.
the class SftpTests method _test.
void _test(SshConnectionFactory connectionFactory, final String testWorkingDirectory) throws IOException {
SshConnectionConfig connectionConfig = connectionFactory.newConfig();
// connectionConfig.setHost("192.168.234.128");
connectionConfig.setHost("192.168.1.70");
connectionConfig.setPort(22);
connectionConfig.setUser("fangjinuo");
connectionConfig.setPassword("fjn13570");
SshConnection connection = null;
SftpSession _session = null;
try {
connection = connectionFactory.get(connectionConfig);
final SftpSession session = connection.openSftpSession();
_session = session;
FileAttrs attrs = session.stat("/home/fangjinuo");
System.out.println(attrs);
// 确保testWorkingDirectory 存在,并且是 empty的
boolean testWorkingDirectoryExist = Sftps.existDirectory(session, testWorkingDirectory);
logger.info("directory exist? {}", testWorkingDirectoryExist);
if (!testWorkingDirectoryExist) {
session.mkdir(testWorkingDirectory, null);
testWorkingDirectoryExist = Sftps.existDirectory(session, testWorkingDirectory);
logger.info("directory exist? {}", testWorkingDirectoryExist);
}
List<SftpResourceInfo> children = session.listFiles(testWorkingDirectory);
if (!children.isEmpty()) {
Sftps.removeDir(session, testWorkingDirectory, true);
}
// 拷贝 agileway-sshclient 模块下所有的文件到 testWorkingDirectory
String localRootPath = SystemPropertys.getUserWorkDir();
File localRootDir = new File(localRootPath);
_copyDir(session, localRootDir, testWorkingDirectory);
} catch (Throwable ex) {
logger.error(ex.getMessage(), ex);
} finally {
IOs.close(_session);
IOs.close(connection);
}
}
use of com.jn.agileway.ssh.client.sftp.attrs.FileAttrs in project agileway by fangjinuo.
the class Sftps method chmod.
public static void chmod(final SftpSession session, String path, int permissions) throws IOException {
FileAttrs attrs = session.stat(path);
FileType fileType = attrs.getFileMode().getType();
FileAttrs attrs2 = new FileAttrs();
attrs2.setFileMode(FileMode.createFileMode(fileType, permissions));
session.setStat(path, attrs2);
}
use of com.jn.agileway.ssh.client.sftp.attrs.FileAttrs in project agileway by fangjinuo.
the class Ssh2SftpSession method open.
@Override
public SftpFile open(String filepath, int openMode, FileAttrs attrs) throws SftpException {
try {
SFTPv3FileHandle handle = null;
if (!Sftps.exists(this, filepath)) {
if (attrs == null) {
attrs = new FileAttrs();
}
FileMode fileMode = attrs.getFileMode();
if (fileMode == null) {
fileMode = FileMode.createFileMode(FileType.REGULAR, 0644);
attrs.setFileMode(fileMode);
}
if (OpenMode.isCreatable(openMode)) {
handle = sftpClient.createFileTruncate(filepath, Ssh2Sftps.toSsh2FileAttributes(attrs));
} else {
throw new NoSuchFileSftpException(StringTemplates.formatWithPlaceholder("no such file: {}", filepath));
}
} else {
if (OpenMode.isTruncated(openMode)) {
handle = sftpClient.createFileTruncate(filepath, Ssh2Sftps.toSsh2FileAttributes(attrs));
}
}
if (handle != null) {
sftpClient.closeFile(handle);
handle = null;
}
if (OpenMode.isWritable(openMode)) {
handle = sftpClient.openFileRW(filepath);
} else {
handle = sftpClient.openFileRO(filepath);
}
Ssh2SftpFile sftpFile = new Ssh2SftpFile(this, filepath);
sftpFile.setFileHandle(handle);
return sftpFile;
} catch (Throwable ex) {
throw Ssh2Sftps.wrapSftpException(ex);
}
}
use of com.jn.agileway.ssh.client.sftp.attrs.FileAttrs in project agileway by fangjinuo.
the class Ssh2SftpSession method open.
@Override
public SftpFile open(String filepath, int openMode, FileAttrs attrs) throws SftpException {
try {
SFTPv3FileHandle handle = null;
if (!Sftps.exists(this, filepath)) {
if (attrs == null) {
attrs = new FileAttrs();
}
FileMode fileMode = attrs.getFileMode();
if (fileMode == null) {
fileMode = FileMode.createFileMode(FileType.REGULAR, 0644);
attrs.setFileMode(fileMode);
}
if (OpenMode.isCreatable(openMode)) {
handle = sftpClient.createFileTruncate(filepath, Ssh2Sftps.toSsh2FileAttributes(attrs));
} else {
throw new NoSuchFileSftpException(StringTemplates.formatWithPlaceholder("no such file: {}", filepath));
}
} else {
if (OpenMode.isTruncated(openMode)) {
handle = sftpClient.createFileTruncate(filepath, Ssh2Sftps.toSsh2FileAttributes(attrs));
}
}
if (handle != null) {
sftpClient.closeFile(handle);
handle = null;
}
if (OpenMode.isWritable(openMode)) {
handle = sftpClient.openFileRW(filepath);
} else {
handle = sftpClient.openFileRO(filepath);
}
Ssh2SftpFile sftpFile = new Ssh2SftpFile(this, filepath);
sftpFile.setFileHandle(handle);
return sftpFile;
} catch (Throwable ex) {
throw new SftpException(ex);
}
}
use of com.jn.agileway.ssh.client.sftp.attrs.FileAttrs in project agileway by fangjinuo.
the class SshjSftps method fromFileAttributes.
public static FileAttrs fromFileAttributes(FileAttributes attributes) {
if (attributes == null) {
return null;
}
FileAttrs attrs = new FileAttrs();
if (attributes.getSize() >= 0L) {
attrs.setSize(attributes.getSize());
}
if (attributes.getAtime() != 0L) {
attrs.setAccessTime(((Long) attributes.getAtime()).intValue());
}
if (attributes.getMtime() != 0L) {
attrs.setModifyTime(((Long) attributes.getMtime()).intValue());
}
if (attributes.getUID() != 0L) {
attrs.setUid(attributes.getUID());
}
if (attributes.getGID() != 0L) {
attrs.setGid(attributes.getGID());
}
com.jn.agileway.ssh.client.sftp.attrs.FileMode fileMode = fromSshjFileMode(attributes.getMode());
attrs.setFileMode(fileMode);
return attrs;
}
Aggregations