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;
}
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;
}
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;
}
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);
}
}
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);
}
}
Aggregations