Search in sources :

Example 6 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 7 with FileAttrs

use of com.jn.agileway.ssh.client.sftp.attrs.FileAttrs in project agileway by fangjinuo.

the class JschSftps method fromSftpATTRS.

public static FileAttrs fromSftpATTRS(SftpATTRS sftpATTRS) {
    if (sftpATTRS == null) {
        return null;
    }
    FileAttrs attrs = new FileAttrs();
    attrs.setSize(sftpATTRS.getSize());
    attrs.setUid(sftpATTRS.getUId());
    attrs.setGid(sftpATTRS.getGId());
    attrs.setAccessTime(sftpATTRS.getATime());
    attrs.setModifyTime(sftpATTRS.getMTime());
    FileMode fileMode = new FileMode(sftpATTRS.getPermissions());
    attrs.setFileMode(fileMode);
    return attrs;
}
Also used : FileAttrs(com.jn.agileway.ssh.client.sftp.attrs.FileAttrs) FileMode(com.jn.agileway.ssh.client.sftp.attrs.FileMode)

Example 8 with FileAttrs

use of com.jn.agileway.ssh.client.sftp.attrs.FileAttrs in project agileway by fangjinuo.

the class SynergySftps method fromSftpFileAttributes.

public static FileAttrs fromSftpFileAttributes(SftpFileAttributes attributes) {
    if (attributes == null) {
        return null;
    }
    FileAttrs attrs = new FileAttrs();
    if (attributes.hasSize()) {
        attrs.setSize(attributes.getSize().longValue());
    }
    if (attributes.hasAccessTime()) {
        attrs.setAccessTime(attributes.getAccessedTime().bigIntValue().intValue());
    }
    if (attributes.hasModifiedTime()) {
        attrs.setModifyTime(attributes.getModifiedTime().bigIntValue().intValue());
    }
    if (attributes.hasUID()) {
        String uid = attributes.getUID();
        if (Numbers.isNumber(uid)) {
            attrs.setUid(Integer.parseInt(uid));
        }
    }
    if (attributes.hasGID()) {
        String gid = attributes.getGID();
        if (Numbers.isNumber(gid)) {
            attrs.setGid(Integer.parseInt(gid));
        }
    }
    FileType fileType = FileType.UNKNOWN;
    if (attributes.isFile()) {
        fileType = FileType.REGULAR;
    } else if (attributes.isDirectory()) {
        fileType = FileType.DIRECTORY;
    } else if (attributes.isBlock()) {
        fileType = FileType.BLOCK_SPECIAL;
    } else if (attributes.isCharacter()) {
        fileType = FileType.CHAR_SPECIAL;
    } else if (attributes.isFifo()) {
        fileType = FileType.FIFO_SPECIAL;
    } else if (attributes.isSocket()) {
        fileType = FileType.SOCKET_SPECIAL;
    } else if (attributes.isLink()) {
        fileType = FileType.SYMBOLIC_LINK;
    }
    int permissions = attributes.getPermissions().intValue();
    com.jn.agileway.ssh.client.sftp.attrs.FileMode fileMode = FileMode.createFileMode(fileType, permissions);
    attrs.setFileMode(fileMode);
    return attrs;
}
Also used : FileAttrs(com.jn.agileway.ssh.client.sftp.attrs.FileAttrs) FileMode(com.jn.agileway.ssh.client.sftp.attrs.FileMode) FileType(com.jn.agileway.ssh.client.sftp.attrs.FileType)

Example 9 with FileAttrs

use of com.jn.agileway.ssh.client.sftp.attrs.FileAttrs in project agileway by fangjinuo.

the class Ssh2Sftps method fromSsh2FileAttributes.

public static FileAttrs fromSsh2FileAttributes(SFTPv3FileAttributes attributes) {
    if (attributes == null) {
        return null;
    }
    FileAttrs attrs = new FileAttrs();
    if (attributes.size != null) {
        attrs.setSize(attributes.size);
    }
    if (attributes.atime != null) {
        attrs.setAccessTime(attributes.atime.intValue());
    }
    if (attributes.mtime != null) {
        attrs.setModifyTime(attributes.mtime.intValue());
    }
    if (attributes.uid != null) {
        attrs.setUid(attributes.uid);
    }
    if (attributes.gid != null) {
        attrs.setGid(attributes.gid);
    }
    if (attributes.permissions != null) {
        FileMode fileMode = new FileMode(attributes.permissions);
        attrs.setFileMode(fileMode);
    }
    return attrs;
}
Also used : FileAttrs(com.jn.agileway.ssh.client.sftp.attrs.FileAttrs) FileMode(com.jn.agileway.ssh.client.sftp.attrs.FileMode)

Example 10 with FileAttrs

use of com.jn.agileway.ssh.client.sftp.attrs.FileAttrs in project agileway by fangjinuo.

the class Sftps method chgrp.

public static void chgrp(final SftpSession session, String path, int gid) throws IOException {
    FileAttrs attrs = session.stat(path);
    FileAttrs attrs2 = new FileAttrs();
    attrs2.setUid(attrs.getUid());
    attrs2.setGid(gid);
    session.setStat(path, attrs2);
}
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