Search in sources :

Example 1 with FileAttrs

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);
    }
}
Also used : FileAttrs(com.jn.agileway.ssh.client.sftp.attrs.FileAttrs) SshConnection(com.jn.agileway.ssh.client.SshConnection) File(java.io.File) SshConnectionConfig(com.jn.agileway.ssh.client.SshConnectionConfig)

Example 2 with FileAttrs

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);
}
Also used : FileAttrs(com.jn.agileway.ssh.client.sftp.attrs.FileAttrs) FileType(com.jn.agileway.ssh.client.sftp.attrs.FileType)

Example 3 with FileAttrs

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);
    }
}
Also used : FileAttrs(com.jn.agileway.ssh.client.sftp.attrs.FileAttrs) FileMode(com.jn.agileway.ssh.client.sftp.attrs.FileMode) NoSuchFileSftpException(com.jn.agileway.ssh.client.sftp.exception.NoSuchFileSftpException) SFTPv3FileHandle(ch.ethz.ssh2.SFTPv3FileHandle)

Example 4 with FileAttrs

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);
    }
}
Also used : FileAttrs(com.jn.agileway.ssh.client.sftp.attrs.FileAttrs) FileMode(com.jn.agileway.ssh.client.sftp.attrs.FileMode) NoSuchFileSftpException(com.jn.agileway.ssh.client.sftp.exception.NoSuchFileSftpException) SFTPv3FileHandle(com.trilead.ssh2.SFTPv3FileHandle) NoSuchFileSftpException(com.jn.agileway.ssh.client.sftp.exception.NoSuchFileSftpException) SftpException(com.jn.agileway.ssh.client.sftp.exception.SftpException)

Example 5 with FileAttrs

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;
}
Also used : FileAttrs(com.jn.agileway.ssh.client.sftp.attrs.FileAttrs)

Aggregations

FileAttrs (com.jn.agileway.ssh.client.sftp.attrs.FileAttrs)12 FileMode (com.jn.agileway.ssh.client.sftp.attrs.FileMode)6 FileType (com.jn.agileway.ssh.client.sftp.attrs.FileType)2 NoSuchFileSftpException (com.jn.agileway.ssh.client.sftp.exception.NoSuchFileSftpException)2 SFTPv3FileHandle (ch.ethz.ssh2.SFTPv3FileHandle)1 SshConnection (com.jn.agileway.ssh.client.SshConnection)1 SshConnectionConfig (com.jn.agileway.ssh.client.SshConnectionConfig)1 SftpException (com.jn.agileway.ssh.client.sftp.exception.SftpException)1 PosixFilePermissions (com.jn.langx.util.io.file.PosixFilePermissions)1 SFTPv3FileHandle (com.trilead.ssh2.SFTPv3FileHandle)1 File (java.io.File)1