@Override public long getFileSize(String filePath) throws FileBasedHelperException { try { ChannelSftp channelSftp = getSftpChannel(); long fileSize = channelSftp.lstat(filePath).getSize(); channelSftp.disconnect(); return fileSize; } catch (SftpException e) { throw new FileBasedHelperException( String.format("Failed to get size for file at path %s due to error %s", filePath, e.getMessage()), e); } }
long length = attr.getSize(); boolean isDir = attr.isDir(); boolean isLink = attr.isLink();
@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(); }
/** * @see com.jcraft.jsch.SftpATTRS#getSize() */ @Override public long getSize() { return this.attrs.getSize(); }
long size1 = attrs.getSize(); System.out.println("-Ensuring file is not being written to (waiting 1 minute)"); Thread.sleep(60000); //Wait a minute to make sure the file size isn't changing attrs = chanSftp.lstat(Main.oldestFile); long size2 = attrs.getSize(); if (size1 == size2) { System.out.println("-It isn't.");
@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()); }
/** * @see com.jcraft.jsch.SftpATTRS#getSize() */ @Override public long getSize() { return this.attrs.getSize(); }
private void assertLength6(SftpRemoteFileTemplate template) { LsEntry[] files = template.execute(session -> session.list("sftpTarget/appending.txt")); assertEquals(1, files.length); assertEquals(6, files[0].getAttrs().getSize()); }
private void processAttrs(SftpATTRS attrs) { isFile = !attrs.isDir(); size = attrs.getSize(); retrieved = true; }
@Test public void testSftpOutboundFlow() { IntegrationFlow flow = f -> f.handle(Sftp.outboundAdapter(sessionFactory(), FileExistsMode.FAIL) .useTemporaryFileName(false) .fileNameExpression("headers['" + FileHeaders.FILENAME + "']") .remoteDirectory("sftpTarget")); IntegrationFlowRegistration registration = this.flowContext.registration(flow).register(); String fileName = "foo.file"; registration.getInputChannel().send(MessageBuilder.withPayload("foo") .setHeader(FileHeaders.FILENAME, fileName) .build()); RemoteFileTemplate<ChannelSftp.LsEntry> template = new RemoteFileTemplate<>(sessionFactory()); ChannelSftp.LsEntry[] files = template.execute(session -> session.list(getTargetRemoteDirectory().getName() + "/" + fileName)); assertEquals(1, files.length); assertEquals(3, files[0].getAttrs().getSize()); registration.destroy(); }
try { SftpATTRS file = client.lstat("foo/foobar.txt"); assertEquals(6, file.getSize());
/** * Returns the size of the file content (in bytes). */ @Override protected long doGetContentSize() throws Exception { if (attrs == null || (attrs.getFlags() & SftpATTRS.SSH_FILEXFER_ATTR_SIZE) == 0) { throw new FileSystemException("vfs.provider.sftp/unknown-size.error"); } return attrs.getSize(); }
/** * Returns the size of the file content (in bytes). */ @Override protected long doGetContentSize() throws Exception { if (attrs == null || (attrs.getFlags() & SftpATTRS.SSH_FILEXFER_ATTR_SIZE) == 0) { throw new FileSystemException("vfs.provider.sftp/unknown-size.error"); } return attrs.getSize(); }
private LsEntry createSingleEntry(String fileName, long size, int mTime, boolean directory) { SftpATTRS attributes = mock(SftpATTRS.class); when(attributes.getSize()).thenReturn(size); when(attributes.getMTime()).thenReturn(mTime); LsEntry entry = mock(LsEntry.class); when(entry.getAttrs()).thenReturn(attributes); when(entry.getFilename()).thenReturn(fileName); when(entry.getAttrs().isDir()).thenReturn(directory); return entry; } }
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(); }
@Test public void testSftpOutboundFlow() { String fileName = "foo.file"; this.toSftpChannel.send(MessageBuilder.withPayload("foo") .setHeader(FileHeaders.FILENAME, fileName) .build()); RemoteFileTemplate<ChannelSftp.LsEntry> template = new RemoteFileTemplate<>(this.sftpSessionFactory); ChannelSftp.LsEntry[] files = template.execute(session -> session.list(this.sftpServer.getTargetSftpDirectory().getName() + "/" + fileName)); assertEquals(1, files.length); assertEquals(3, files[0].getAttrs().getSize()); }