Search in sources :

Example 6 with FileMode

use of com.jn.agileway.ssh.client.sftp.attrs.FileMode 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);
    }
    if (attributes.mtime != null) {
        attrs.setModifyTime(attributes.mtime);
    }
    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 7 with FileMode

use of com.jn.agileway.ssh.client.sftp.attrs.FileMode 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 8 with FileMode

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

the class Ssh2SftpSession method mkdir.

@Override
public void mkdir(String directory, FileAttrs attributes) throws SftpException {
    try {
        FileMode fileMode = attributes == null ? null : attributes.getFileMode();
        int permissions = fileMode == null ? 0775 : fileMode.getMask();
        sftpClient.mkdir(directory, permissions);
    } catch (Throwable ex) {
        throw Ssh2Sftps.wrapSftpException(ex);
    }
}
Also used : FileMode(com.jn.agileway.ssh.client.sftp.attrs.FileMode)

Example 9 with FileMode

use of com.jn.agileway.ssh.client.sftp.attrs.FileMode 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 10 with FileMode

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

the class JschSftps method toSftpATTRS.

public static SftpATTRS toSftpATTRS(@NonNull SftpATTRS sftpATTRS, @Nullable FileAttrs attrs) {
    if (attrs == null) {
        return sftpATTRS;
    }
    Long size = attrs.getSize();
    if (size != null) {
        sftpATTRS.setSIZE(size);
    }
    Integer uid = attrs.getUid();
    Integer gid = attrs.getGid();
    if (uid != null || gid != null) {
        sftpATTRS.setUIDGID(Objs.useValueIfNull(uid, sftpATTRS.getUId()), Objs.useValueIfNull(gid, sftpATTRS.getGId()));
    }
    Integer atime = attrs.getAccessTime();
    Integer mtime = attrs.getModifyTime();
    if (atime != null || mtime != null) {
        sftpATTRS.setACMODTIME(Objs.useValueIfNull(atime, sftpATTRS.getATime()), Objs.useValueIfNull(mtime, sftpATTRS.getMTime()));
    }
    FileMode fileMode = attrs.getFileMode();
    if (fileMode != null) {
        sftpATTRS.setPERMISSIONS(fileMode.getMask());
    }
    return sftpATTRS;
}
Also used : FileMode(com.jn.agileway.ssh.client.sftp.attrs.FileMode)

Aggregations

FileMode (com.jn.agileway.ssh.client.sftp.attrs.FileMode)14 FileAttrs (com.jn.agileway.ssh.client.sftp.attrs.FileAttrs)6 PosixFilePermissions (com.jn.langx.util.io.file.PosixFilePermissions)3 NoSuchFileSftpException (com.jn.agileway.ssh.client.sftp.exception.NoSuchFileSftpException)2 SFTPv3FileHandle (ch.ethz.ssh2.SFTPv3FileHandle)1 FileType (com.jn.agileway.ssh.client.sftp.attrs.FileType)1 SftpException (com.jn.agileway.ssh.client.sftp.exception.SftpException)1 SftpFileAttributes (com.sshtools.common.sftp.SftpFileAttributes)1 UnsignedInteger32 (com.sshtools.common.util.UnsignedInteger32)1 UnsignedInteger64 (com.sshtools.common.util.UnsignedInteger64)1 SFTPv3FileHandle (com.trilead.ssh2.SFTPv3FileHandle)1