String user = Integer.toString(attr.getUId()); String group = Integer.toString(attr.getGId()); Path filePath = new Path(parentPath, sftpFile.getFilename());
@Override public FileStatus getFileStatus(Path path) throws IOException { ChannelSftp channelSftp = null; ChannelExec channelExec1 = null; ChannelExec channelExec2 = null; try { channelSftp = this.fsHelper.getSftpChannel(); SftpATTRS sftpAttrs = channelSftp.stat(HadoopUtils.toUriPath(path)); FsPermission permission = new FsPermission((short) sftpAttrs.getPermissions()); channelExec1 = this.fsHelper.getExecChannel("id " + sftpAttrs.getUId()); String userName = IOUtils.toString(channelExec1.getInputStream()); channelExec2 = this.fsHelper.getExecChannel("id " + sftpAttrs.getGId()); String groupName = IOUtils.toString(channelExec2.getInputStream()); FileStatus fs = new FileStatus(sftpAttrs.getSize(), sftpAttrs.isDir(), 1, 0l, sftpAttrs.getMTime(), sftpAttrs.getATime(), permission, StringUtils.trimToEmpty(userName), StringUtils.trimToEmpty(groupName), path); return fs; } catch (SftpException e) { throw new IOException(e); } finally { safeDisconnect(channelSftp); safeDisconnect(channelExec1); safeDisconnect(channelExec2); } }
private FileInfo newFileInfo(final LsEntry entry, String path) { if (entry == null) { return null; } final File newFullPath = new File(path, entry.getFilename()); final String newFullForwardPath = newFullPath.getPath().replace("\\", "/"); String perms = entry.getAttrs().getPermissionsString(); if (perms.length() > 9) { perms = perms.substring(perms.length() - 9); } FileInfo.Builder builder = new FileInfo.Builder() .filename(entry.getFilename()) .fullPathFileName(newFullForwardPath) .directory(entry.getAttrs().isDir()) .size(entry.getAttrs().getSize()) .lastModifiedTime(entry.getAttrs().getMTime() * 1000L) .permissions(perms) .owner(Integer.toString(entry.getAttrs().getUId())) .group(Integer.toString(entry.getAttrs().getGId())); return builder.build(); }
@Override public FileAbstractModel toFileAbstractModel(String path, ChannelSftp.LsEntry file) { return new FileAbstractModel( file.getFilename(), file.getAttrs().isDir(), // We are using here the local TimeZone as a reference. If the remote system is under another TZ, this might cause issues LocalDateTime.ofInstant(Instant.ofEpochMilli(file.getAttrs().getMTime()*1000L), ZoneId.systemDefault()), // We don't have the creation date null, // We are using here the local TimeZone as a reference. If the remote system is under another TZ, this might cause issues LocalDateTime.ofInstant(Instant.ofEpochMilli(file.getAttrs().getATime()*1000L), ZoneId.systemDefault()), FilenameUtils.getExtension(file.getFilename()), path, path.concat("/").concat(file.getFilename()), file.getAttrs().getSize(), Integer.toString(file.getAttrs().getUId()), Integer.toString(file.getAttrs().getGId()), file.getAttrs().getPermissions()); }
@Override public int uid() { return attrs.getUId(); }
@Override public UserPrincipal owner() { String user = Integer.toString(attributes.getUId()); return new SimpleUserPrincipal(user); }
private boolean accessAllowed(String path) throws SftpException, JSchException { boolean result = false; ChannelSftp channel = (ChannelSftp) session.openChannel("sftp"); SftpATTRS attrs = channel.lstat(path); result = attrs != null && ((attrs.getPermissions() & 00200) != 0) && attrs.getUId() != 0; return result; }
ChannelSftp channel = (ChannelSftp)session.openChannel("sftp"); SftpATTRS attrs = channel.lstat(fileOnServer) boolean userHasPermissionsToWriteFile = attrs != null && ((attrs.getPermissions() & 00200) != 0) && attrs.getUId() != 0;
@Override public int getUid() throws IOException { ChannelSftp sftp; try { sftp = alloc(); try { return sftp.stat(escape(slashPath)).getUId(); } finally { free(sftp); } } catch (SftpException e) { throw new IOException(e); } catch (JSchException e) { throw new IOException(e); } }
public String toString() { return (getPermissionsString()+" "+getUId()+" "+getGId()+" "+getSize()+" "+getMtimeString()); } /*
/** * creates a string representation of this object. * This string contains permissions, UID, GID, size and modification time. */ public String toString() { return (getPermissionsString()+" "+getUId()+" "+getGId()+" "+getSize()+" "+getMtimeString()); } /*
public String toString() { return (getPermissionsString() + " " + getUId() + " " + getGId() + " " + getSize() + " " + getMtimeString()); } }
public String toString() { return (getPermissionsString()+" "+getUId()+" "+getGId()+" "+getSize()+" "+getMtimeString()); } /*
public String toString() { return (getPermissionsString()+" "+getUId()+" "+getGId()+" "+getSize()+" "+getMtimeString()); } /*
public void chgrp(String path, int gid) throws SshException { try { SftpATTRS attrs = channel.stat(path); attrs.setUIDGID(attrs.getUId(), gid); channel.setStat(path, attrs); } catch (SftpException e) { throw new net.sf.sshapi.sftp.SftpException(e.id, e.getLocalizedMessage(), e); } }
@Override public SshPrincipal getOwner() throws ModeException { ChannelSftp sftp; try { sftp = alloc(); try { return new SshPrincipal(sftp.stat(escape(slashPath)).getUId()); } finally { free(sftp); } } catch (SftpException | JSchException e) { throw new ModeException(this, e); } }
public SftpFile stat(String path) throws SshException { try { SftpATTRS attrs = channel.stat(path); return new SftpFile(convertType(attrs), path, attrs.getSize(), attrs.getMTime() * 1000, 0, attrs.getATime() * 1000, attrs.getGId(), attrs.getUId(), attrs.getPermissions()); } catch (SftpException e) { throw new net.sf.sshapi.sftp.SftpException(e.id, e.getLocalizedMessage(), e); } }
protected void fillInAttributes(RemoteFile file, SftpATTRS attributes) { file.setDirectory(attributes.isDir()); file.setPermissions(attributes.getPermissions()); file.setUserId(attributes.getUId()); file.setGroupId(attributes.getGId()); file.setSize(attributes.getSize()); file.setStatus(Status.EXISTS); }
protected RemoteFile fillInAttributes(String path, SftpATTRS attributes) { boolean directory = attributes.isDir(); int permissions = attributes.getPermissions(); int userId = attributes.getUId(); int groupId = attributes.getGId(); long size = attributes.getSize(); Status status = Status.EXISTS; return new RemoteFile.Builder(path).directory(directory).permissions(permissions).userId(userId).groupId(groupId).size(size).status(status).build(); }
protected RemoteFile fillInAttributes(String path, SftpATTRS attributes) { boolean directory = attributes.isDir(); int permissions = attributes.getPermissions(); int userId = attributes.getUId(); int groupId = attributes.getGId(); long size = attributes.getSize(); Status status = Status.EXISTS; return new RemoteFile.Builder(path).directory(directory).permissions(permissions).userId(userId).groupId(groupId).size(size).status(status).build(); }