Search in sources :

Example 1 with FileMode

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

the class SftpFileObject method doSetReadable.

@Override
protected boolean doSetReadable(boolean readable, boolean ownerOnly) throws Exception {
    PosixFilePermissions posixFilePermissions = Sftps.getPosixPermission(getSftpSession().open(relPath, OpenMode.READ, null));
    int oldPerm = posixFilePermissions.getPermissions();
    if (readable) {
        posixFilePermissions.addPermission(FilePermission.USR_R);
    } else {
        posixFilePermissions.removePermission(FilePermission.USR_R);
    }
    if (!ownerOnly) {
        posixFilePermissions.addPermission(FilePermission.GRP_R);
        posixFilePermissions.addPermission(FilePermission.OTH_R);
    }
    if (posixFilePermissions.getPermissions() == oldPerm) {
        return true;
    }
    FileMode fileMode = FileMode.createFileMode(getFileAttrs().getFileMode().getType(), posixFilePermissions.getPermissions());
    getFileAttrs().setFileMode(fileMode);
    flushStat();
    return true;
}
Also used : FileMode(com.jn.agileway.ssh.client.sftp.attrs.FileMode) PosixFilePermissions(com.jn.langx.util.io.file.PosixFilePermissions)

Example 2 with FileMode

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

the class SftpFileObject method doSetWritable.

@Override
protected boolean doSetWritable(boolean writable, boolean ownerOnly) throws Exception {
    PosixFilePermissions posixFilePermissions = Sftps.getPosixPermission(getSftpSession().open(relPath, OpenMode.READ, null));
    int oldPerm = posixFilePermissions.getPermissions();
    if (writable) {
        posixFilePermissions.addPermission(FilePermission.USR_W);
    } else {
        posixFilePermissions.removePermission(FilePermission.USR_W);
    }
    if (!ownerOnly) {
        posixFilePermissions.addPermission(FilePermission.GRP_W);
        posixFilePermissions.addPermission(FilePermission.OTH_W);
    }
    if (posixFilePermissions.getPermissions() == oldPerm) {
        return true;
    }
    FileMode fileMode = FileMode.createFileMode(getFileAttrs().getFileMode().getType(), posixFilePermissions.getPermissions());
    getFileAttrs().setFileMode(fileMode);
    flushStat();
    return true;
}
Also used : FileMode(com.jn.agileway.ssh.client.sftp.attrs.FileMode) PosixFilePermissions(com.jn.langx.util.io.file.PosixFilePermissions)

Example 3 with FileMode

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

the class SynergySftps method toSftpFileAttributes.

public static SftpFileAttributes toSftpFileAttributes(FileAttrs attrs, String encoding) {
    int fileType = SftpFileAttributes.SSH_FILEXFER_TYPE_UNKNOWN;
    switch(attrs.getFileType()) {
        case REGULAR:
            fileType = SftpFileAttributes.SSH_FILEXFER_TYPE_REGULAR;
            break;
        case DIRECTORY:
            fileType = SftpFileAttributes.SSH_FILEXFER_TYPE_DIRECTORY;
            break;
        case SYMBOLIC_LINK:
            fileType = SftpFileAttributes.SSH_FILEXFER_TYPE_SYMLINK;
            break;
        case FIFO_SPECIAL:
            fileType = SftpFileAttributes.SSH_FILEXFER_TYPE_FIFO;
            break;
        case BLOCK_SPECIAL:
            fileType = SftpFileAttributes.SSH_FILEXFER_TYPE_BLOCK_DEVICE;
            break;
        case CHAR_SPECIAL:
            fileType = SftpFileAttributes.SSH_FILEXFER_TYPE_CHAR_DEVICE;
            break;
        case SOCKET_SPECIAL:
            fileType = SftpFileAttributes.SSH_FILEXFER_TYPE_SOCKET;
            break;
        default:
            break;
    }
    SftpFileAttributes attributes = new SftpFileAttributes(fileType, encoding);
    if (attrs.getSize() != null) {
        attributes.setSize(new UnsignedInteger64(attrs.getSize()));
    }
    UnsignedInteger64 atime = null;
    UnsignedInteger64 mtime = null;
    if (attrs.getAccessTime() != null) {
        atime = new UnsignedInteger64(attrs.getAccessTime());
    }
    if (attrs.getModifyTime() != null) {
        mtime = new UnsignedInteger64(attrs.getModifyTime());
    }
    attributes.setTimes(atime, mtime);
    if (attrs.getGid() != null) {
        attributes.setGID(attrs.getGid().toString());
    }
    if (attrs.getUid() != null) {
        attributes.setUID(attrs.getUid().toString());
    }
    FileMode fileMode = attrs.getFileMode();
    attributes.setPermissions(new UnsignedInteger32(fileMode.getPermissionsMask()));
    return attributes;
}
Also used : FileMode(com.jn.agileway.ssh.client.sftp.attrs.FileMode) SftpFileAttributes(com.sshtools.common.sftp.SftpFileAttributes) UnsignedInteger64(com.sshtools.common.util.UnsignedInteger64) UnsignedInteger32(com.sshtools.common.util.UnsignedInteger32)

Example 4 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 5 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 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)

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