com.jcraft.jsch.SftpATTRS Java Examples
The following examples show how to use
com.jcraft.jsch.SftpATTRS.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SftpClient.java From hop with Apache License 2.0 | 6 votes |
public FileType getFileType( String filename ) throws HopWorkflowException { try { SftpATTRS attrs = c.stat( filename ); if ( attrs == null ) { return FileType.IMAGINARY; } if ( ( attrs.getFlags() & SftpATTRS.SSH_FILEXFER_ATTR_PERMISSIONS ) == 0 ) { throw new HopWorkflowException( "Unknown permissions error" ); } if ( attrs.isDir() ) { return FileType.FOLDER; } else { return FileType.FILE; } } catch ( Exception e ) { throw new HopWorkflowException( e ); } }
Example #2
Source File: SFTPClient.java From pentaho-kettle with Apache License 2.0 | 6 votes |
public boolean folderExists( String foldername ) { boolean retval = false; try { SftpATTRS attrs = c.stat( foldername ); if ( attrs == null ) { return false; } if ( ( attrs.getFlags() & SftpATTRS.SSH_FILEXFER_ATTR_PERMISSIONS ) == 0 ) { throw new KettleJobException( "Unknown permissions error" ); } retval = attrs.isDir(); } catch ( Exception e ) { // Folder can not be found! } return retval; }
Example #3
Source File: SFTPClient.java From pentaho-kettle with Apache License 2.0 | 6 votes |
public FileType getFileType( String filename ) throws KettleJobException { try { SftpATTRS attrs = c.stat( filename ); if ( attrs == null ) { return FileType.IMAGINARY; } if ( ( attrs.getFlags() & SftpATTRS.SSH_FILEXFER_ATTR_PERMISSIONS ) == 0 ) { throw new KettleJobException( "Unknown permissions error" ); } if ( attrs.isDir() ) { return FileType.FOLDER; } else { return FileType.FILE; } } catch ( Exception e ) { throw new KettleJobException( e ); } }
Example #4
Source File: SftpRemoteFileSystem.java From cloudhopper-commons with Apache License 2.0 | 6 votes |
public boolean exists(String filename) throws FileSystemException { // we have to be connected if (channel == null) { throw new FileSystemException("Not yet connected to SFTP server"); } // easiest way to check if a file already exists is to do a file stat // this method will error out if the remote file does not exist! try { SftpATTRS attrs = channel.stat(filename); // if we get here, then file exists return true; } catch (SftpException e) { // map "no file" message to return correct result if (e.getMessage().toLowerCase().indexOf("no such file") >= 0) { return false; } // otherwise, this means an underlying error occurred throw new FileSystemException("Underlying error with SFTP session while checking if file exists", e); } }
Example #5
Source File: AbstractSftpProviderTestCase.java From commons-vfs with Apache License 2.0 | 6 votes |
private SftpAttrs(final Buffer buf) { int flags = 0; flags = buf.getInt(); if ((flags & SftpATTRS.SSH_FILEXFER_ATTR_SIZE) != 0) { size = buf.getLong(); } if ((flags & SftpATTRS.SSH_FILEXFER_ATTR_UIDGID) != 0) { uid = buf.getInt(); gid = buf.getInt(); } if ((flags & SftpATTRS.SSH_FILEXFER_ATTR_PERMISSIONS) != 0) { permissions = buf.getInt(); } if ((flags & SftpATTRS.SSH_FILEXFER_ATTR_ACMODTIME) != 0) { atime = buf.getInt(); } if ((flags & SftpATTRS.SSH_FILEXFER_ATTR_ACMODTIME) != 0) { mtime = buf.getInt(); } }
Example #6
Source File: SftpFileObject.java From commons-vfs with Apache License 2.0 | 6 votes |
/** * Determines the type of this file, returns null if the file does not exist. */ @Override protected FileType doGetType() throws Exception { if (attrs == null) { statSelf(); } if (attrs == null) { return FileType.IMAGINARY; } if ((attrs.getFlags() & SftpATTRS.SSH_FILEXFER_ATTR_PERMISSIONS) == 0) { throw new FileSystemException("vfs.provider.sftp/unknown-permissions.error"); } if (attrs.isDir()) { return FileType.FOLDER; } return FileType.FILE; }
Example #7
Source File: SFTPRepository.java From ant-ivy with Apache License 2.0 | 6 votes |
/** * This method is similar to getResource, except that the returned resource is fully initialized * (resolved in the sftp repository), and that the given string is a full remote path * * @param path * the full remote path in the repository of the resource * @return a fully initialized resource, able to answer to all its methods without needing any * further connection */ @SuppressWarnings("unchecked") public Resource resolveResource(String path) { try { List<LsEntry> r = getSftpChannel(path).ls(getPath(path)); if (r != null) { SftpATTRS attrs = r.get(0).getAttrs(); return new BasicResource(path, true, attrs.getSize(), attrs.getMTime() * MILLIS_PER_SECOND, false); } } catch (Exception e) { Message.debug("Error while resolving resource " + path, e); // silent fail, return nonexistent resource } return new BasicResource(path, false, 0, 0, false); }
Example #8
Source File: ClasspathServiceTest.java From embeddedlinux-jvmdebugger-intellij with Apache License 2.0 | 6 votes |
@Before public void setUp() throws Exception { classpathService = new ClasspathService(project); hostFiles = Arrays.asList(jar1, jar2, output); when(jar1.getName()).thenReturn(JAR1_NAME); when(jar1.lastModified()).thenReturn(JAR1_LAST_MODIFIED); when(jar1.length()).thenReturn(JAR1_SIZE); when(jar2.getName()).thenReturn(JAR2_NAME); when(jar2.length()).thenReturn(JAR2_SIZE); when(jar2.lastModified()).thenReturn(JAR2_LAST_MODIFIED); when(output.getName()).thenReturn("untitledproject"); when(jar1Server.getFilename()).thenReturn(JAR1_NAME); when(jar2Server.getFilename()).thenReturn(JAR2_NAME); when(jar1Server.getAttrs()).thenReturn(mock(SftpATTRS.class)); when(jar2Server.getAttrs()).thenReturn(mock(SftpATTRS.class)); Vector vector = new Vector(); vector.add(jar1Server); vector.add(jar2Server); when(target.getAlreadyDeployedLibraries()).thenReturn(vector); }
Example #9
Source File: SFtpULoader.java From Aria with Apache License 2.0 | 6 votes |
@Override public void addComponent(IInfoTask infoTask) { mInfoTask = infoTask; infoTask.setCallback(new IInfoTask.Callback() { @Override public void onSucceed(String key, CompleteInfo info) { if (info.code == SFtpUInfoTask.ISCOMPLETE) { getListener().onComplete(); } else { startThreadTask((SftpATTRS) info.obj); } } @Override public void onFail(AbsEntity entity, AriaException e, boolean needRetry) { getListener().onFail(needRetry, e); } }); }
Example #10
Source File: SFTPFileSystem.java From sftp-fs with Apache License 2.0 | 6 votes |
private void copyAcrossFileSystems(Channel sourceChannel, SFTPPath source, SftpATTRS sourceAttributes, SFTPPath target, CopyOptions options) throws IOException { @SuppressWarnings("resource") SFTPFileSystem targetFileSystem = target.getFileSystem(); try (Channel targetChannel = targetFileSystem.channelPool.getOrCreate()) { SftpATTRS targetAttributes = findAttributes(targetChannel, target, false); if (targetAttributes != null) { if (options.replaceExisting) { targetChannel.delete(target.path(), targetAttributes.isDir()); } else { throw new FileAlreadyExistsException(target.path()); } } if (sourceAttributes.isDir()) { targetChannel.mkdir(target.path()); } else { copyFile(sourceChannel, source, targetChannel, target, options); } } }
Example #11
Source File: SFTPFileSystem.java From sftp-fs with Apache License 2.0 | 6 votes |
DirectoryStream<Path> newDirectoryStream(final SFTPPath path, Filter<? super Path> filter) throws IOException { try (Channel channel = channelPool.get()) { List<LsEntry> entries = channel.listFiles(path.path()); boolean isDirectory = false; for (Iterator<LsEntry> i = entries.iterator(); i.hasNext(); ) { LsEntry entry = i.next(); String filename = entry.getFilename(); if (CURRENT_DIR.equals(filename)) { isDirectory = true; i.remove(); } else if (PARENT_DIR.equals(filename)) { i.remove(); } } if (!isDirectory) { // https://github.com/robtimus/sftp-fs/issues/4: don't fail immediately but check the attributes // Follow links to ensure the directory attribute can be read correctly SftpATTRS attributes = channel.readAttributes(path.path(), true); if (!attributes.isDir()) { throw new NotDirectoryException(path.path()); } } return new SFTPPathDirectoryStream(path, entries, filter); } }
Example #12
Source File: SFTPFileSystem.java From sftp-fs with Apache License 2.0 | 6 votes |
@SuppressWarnings("resource") SeekableByteChannel newByteChannel(SFTPPath path, Set<? extends OpenOption> options, FileAttribute<?>... attrs) throws IOException { if (attrs.length > 0) { throw Messages.fileSystemProvider().unsupportedCreateFileAttribute(attrs[0].name()); } OpenOptions openOptions = OpenOptions.forNewByteChannel(options); try (Channel channel = channelPool.get()) { if (openOptions.read) { // use findAttributes instead of getAttributes, to let the opening of the stream provide the correct error message SftpATTRS attributes = findAttributes(channel, path, false); InputStream in = newInputStream(channel, path, openOptions); long size = attributes == null ? 0 : attributes.getSize(); return FileSystemProviderSupport.createSeekableByteChannel(in, size); } // if append then we need the attributes, to find the initial position of the channel boolean requireAttributes = openOptions.append; SFTPAttributesAndOutputStreamPair outPair = newOutputStream(channel, path, requireAttributes, openOptions); long initialPosition = outPair.attributes == null ? 0 : outPair.attributes.getSize(); return FileSystemProviderSupport.createSeekableByteChannel(outPair.out, initialPosition); } }
Example #13
Source File: SFTPFileSystem.java From sftp-fs with Apache License 2.0 | 6 votes |
@SuppressWarnings("resource") private SFTPAttributesAndOutputStreamPair newOutputStream(Channel channel, SFTPPath path, boolean requireAttributes, OpenOptions options) throws IOException { // retrieve the attributes unless create is true and createNew is false, because then the file can be created SftpATTRS attributes = null; if (!options.create || options.createNew) { attributes = findAttributes(channel, path, false); if (attributes != null && attributes.isDir()) { throw Messages.fileSystemProvider().isDirectory(path.path()); } if (!options.createNew && attributes == null) { throw new NoSuchFileException(path.path()); } else if (options.createNew && attributes != null) { throw new FileAlreadyExistsException(path.path()); } } // else the file can be created if necessary if (attributes == null && requireAttributes) { attributes = findAttributes(channel, path, false); } OutputStream out = channel.newOutputStream(path.path(), options); return new SFTPAttributesAndOutputStreamPair(attributes, out); }
Example #14
Source File: SftpConnection.java From spring-boot with Apache License 2.0 | 6 votes |
/** * Determines whether a directory exists or not * 如果是文件,也会抛出异常,返回 false * * @param remoteDirectoryPath * @return true if exists, false otherwise * @throws IOException thrown if any I/O error occurred. */ @Override public boolean existsDirectory(String remoteDirectoryPath) { try { // System.out.println(channel.realpath(remoteFilePath)); SftpATTRS attrs = channel.stat(remoteDirectoryPath); return attrs.isDir(); } catch (SftpException e) { // e.printStackTrace(); return false; } // String originalWorkingDirectory = getWorkingDirectory(); // try { // changeDirectory(remoteDirectoryPath); // } catch (FtpException e) { // //文件夹不存在,会抛出异常。 // return false; // } // //恢复 ftpClient 当前工作目录属性 // changeDirectory(originalWorkingDirectory); // return true; }
Example #15
Source File: SftpClient.java From hop with Apache License 2.0 | 6 votes |
public boolean folderExists( String foldername ) { boolean retval = false; try { SftpATTRS attrs = c.stat( foldername ); if ( attrs == null ) { return false; } if ( ( attrs.getFlags() & SftpATTRS.SSH_FILEXFER_ATTR_PERMISSIONS ) == 0 ) { throw new HopWorkflowException( "Unknown permissions error" ); } retval = attrs.isDir(); } catch ( Exception e ) { // Folder can not be found! } return retval; }
Example #16
Source File: SftpSupport.java From netbeans with Apache License 2.0 | 6 votes |
private StatInfo createStatInfo(String dirName, String baseName, SftpATTRS attrs, ChannelSftp cftp) throws SftpException { String linkTarget = null; if (attrs.isLink()) { String path = dirName + '/' + baseName; RemoteStatistics.ActivityID activityID = RemoteStatistics.startChannelActivity("readlink", path); // NOI18N try { if (LOG.isLoggable(Level.FINE)) { LOG.log(Level.FINE, "performing readlink {0}", path); } linkTarget = cftp.readlink(path); } finally { RemoteStatistics.stopChannelActivity(activityID); } } Date lastModified = new Date(attrs.getMTime() * 1000L); StatInfo result = new FileInfoProvider.StatInfo(baseName, attrs.getUId(), attrs.getGId(), attrs.getSize(), attrs.isDir(), attrs.isLink(), linkTarget, attrs.getPermissions(), lastModified); return result; }
Example #17
Source File: SFTPUtil.java From xnx3 with Apache License 2.0 | 6 votes |
private List<FileBean> _buildFiles(Vector ls,String dir) throws Exception { if (ls != null && ls.size() >= 0) { List<FileBean> list = new ArrayList<FileBean>(); for (int i = 0; i < ls.size(); i++) { LsEntry f = (LsEntry) ls.get(i); String nm = f.getFilename(); if (nm.equals(".") || nm.equals("..")) continue; SftpATTRS attr = f.getAttrs(); FileBean fileBean=new FileBean(); if (attr.isDir()) { fileBean.setDir(true); } else { fileBean.setDir(false); } fileBean.setAttrs(attr); fileBean.setFilePath(dir); fileBean.setFileName(nm); list.add(fileBean); } return list; } return null; }
Example #18
Source File: SFTPFileSystem.java From sftp-fs with Apache License 2.0 | 5 votes |
private SftpATTRS findAttributes(Channel channel, SFTPPath path, boolean followLinks) throws IOException { try { return getAttributes(channel, path, followLinks); } catch (@SuppressWarnings("unused") NoSuchFileException e) { return null; } }
Example #19
Source File: SftpLightWeightFileSystem.java From incubator-gobblin with Apache License 2.0 | 5 votes |
@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); } }
Example #20
Source File: SFTPRepository.java From ant-ivy with Apache License 2.0 | 5 votes |
private void mkdirs(String directory, ChannelSftp c) throws SftpException { try { SftpATTRS att = c.stat(directory); if (att != null && att.isDir()) { return; } } catch (SftpException ex) { if (directory.indexOf('/') != -1) { mkdirs(directory.substring(0, directory.lastIndexOf('/')), c); } c.mkdir(directory); } }
Example #21
Source File: SftpResourceAccessor.java From Pushjet-Android with BSD 2-Clause "Simplified" License | 5 votes |
private ExternalResourceMetaData toMetaData(URI uri, SftpATTRS attributes) { long lastModified = -1; long contentLength = -1; if ((attributes.getFlags() & SftpATTRS.SSH_FILEXFER_ATTR_ACMODTIME) != 0) { lastModified = attributes.getMTime() * 1000; } if ((attributes.getFlags() & SftpATTRS.SSH_FILEXFER_ATTR_SIZE) != 0) { contentLength = attributes.getSize(); } return new DefaultExternalResourceMetaData(uri, lastModified, contentLength, null, null); }
Example #22
Source File: SftpResourceAccessor.java From Pushjet-Android with BSD 2-Clause "Simplified" License | 5 votes |
public ExternalResourceMetaData getMetaData(URI uri) throws IOException { LockableSftpClient sftpClient = sftpClientFactory.createSftpClient(uri, credentials); try { SftpATTRS attributes = sftpClient.getSftpClient().lstat(uri.getPath()); return attributes != null ? toMetaData(uri, attributes) : null; } catch (com.jcraft.jsch.SftpException e) { if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) { return null; } throw new SftpException(String.format("Could not get resource '%s'.", uri), e); } finally { sftpClientFactory.releaseSftpClient(sftpClient); } }
Example #23
Source File: SFTPConnectionTest.java From davos with MIT License | 5 votes |
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; }
Example #24
Source File: SFtpDInfoTask.java From Aria with Apache License 2.0 | 5 votes |
@Override protected void getFileInfo(Session session) throws JSchException, UnsupportedEncodingException, SftpException { SFtpTaskOption option = (SFtpTaskOption) getWrapper().getTaskOption(); ChannelSftp channel = (ChannelSftp) session.openChannel("sftp"); channel.connect(1000); //channel.setFilenameEncoding(option.getCharSet()); //channel.setFilenameEncoding("gbk"); String remotePath = option.getUrlEntity().remotePath; String temp = CommonUtil.convertSFtpChar(option.getCharSet(), remotePath); SftpATTRS attr = null; try { attr = channel.stat(temp); } catch (Exception e) { ALog.e(TAG, String.format("文件不存在,remotePath:%s", remotePath)); } if (attr != null) { getWrapper().getEntity().setFileSize(attr.getSize()); CompleteInfo info = new CompleteInfo(); info.code = 200; callback.onSucceed(getWrapper().getKey(), info); } else { callback.onFail(getWrapper().getEntity(), new AriaSFTPException(String.format("文件不存在,remotePath:%s", remotePath)), false); } channel.disconnect(); }
Example #25
Source File: SFtpUInfoTask.java From Aria with Apache License 2.0 | 5 votes |
@Override protected void getFileInfo(Session session) throws JSchException, UnsupportedEncodingException, SftpException { SFtpTaskOption option = (SFtpTaskOption) getWrapper().getTaskOption(); ChannelSftp channel = (ChannelSftp) session.openChannel("sftp"); channel.connect(1000); String remotePath = option.getUrlEntity().remotePath; String temp = CommonUtil.convertSFtpChar(getOption().getCharSet(), remotePath) + "/" + getWrapper().getEntity().getFileName(); SftpATTRS attr = null; try { attr = channel.stat(temp); } catch (Exception e) { ALog.d(TAG, String.format("文件不存在,remotePath:%s", remotePath)); } boolean isComplete = false; UploadEntity entity = getWrapper().getEntity(); if (attr != null && attr.getSize() == entity.getFileSize()) { isComplete = true; } CompleteInfo info = new CompleteInfo(); info.code = isComplete ? ISCOMPLETE : 200; info.obj = attr; channel.disconnect(); callback.onSucceed(getWrapper().getKey(), info); }
Example #26
Source File: SftpFileObject.java From commons-vfs with Apache License 2.0 | 5 votes |
@Override protected long doGetLastModifiedTime() throws Exception { if (attrs == null || (attrs.getFlags() & SftpATTRS.SSH_FILEXFER_ATTR_ACMODTIME) == 0) { throw new FileSystemException("vfs.provider.sftp/unknown-modtime.error"); } return attrs.getMTime() * MOD_TIME_FACTOR; }
Example #27
Source File: SFtpULoader.java From Aria with Apache License 2.0 | 5 votes |
private void startThreadTask(SftpATTRS attrs) { if (isBreak()) { return; } if (getListener() instanceof IDLoadListener) { ((IDLoadListener) getListener()).onPostPre(getEntity().getFileSize()); } File file = new File(getEntity().getFilePath()); if (file.getParentFile() != null && !file.getParentFile().exists()) { FileUtil.createDir(file.getPath()); } // 处理记录、初始化状态管理器 SFtpURecordHandler recordHandler = (SFtpURecordHandler) mRecordHandler; recordHandler.setFtpAttrs(attrs); mRecord = recordHandler.getRecord(getFileSize()); mStateManager.setLooper(mRecord, looper); // 创建线程任务 getTaskList().addAll(mTTBuilder.buildThreadTask(mRecord, new Handler(looper, mStateManager.getHandlerCallback()))); mStateManager.updateCurrentProgress(getEntity().getCurrentProgress()); if (mStateManager.getCurrentProgress() > 0) { getListener().onResume(mStateManager.getCurrentProgress()); } else { getListener().onStart(mStateManager.getCurrentProgress()); } // 启动线程任务 for (IThreadTask threadTask : getTaskList()) { ThreadTaskManager.getInstance().startThread(mTaskWrapper.getKey(), threadTask); } // 启动定时器 startTimer(); }
Example #28
Source File: SSHChannelPool.java From sftp-fs with Apache License 2.0 | 5 votes |
SftpATTRS readAttributes(String path, boolean followLinks) throws IOException { try { return followLinks ? channel.stat(path) : channel.lstat(path); } catch (SftpException e) { throw exceptionFactory.createGetFileException(path, e); } }
Example #29
Source File: SftpFileObject.java From commons-vfs with Apache License 2.0 | 5 votes |
/** * 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(); }
Example #30
Source File: SftpResourceAccessor.java From pushfish-android with BSD 2-Clause "Simplified" License | 5 votes |
private ExternalResourceMetaData toMetaData(URI uri, SftpATTRS attributes) { long lastModified = -1; long contentLength = -1; if ((attributes.getFlags() & SftpATTRS.SSH_FILEXFER_ATTR_ACMODTIME) != 0) { lastModified = attributes.getMTime() * 1000; } if ((attributes.getFlags() & SftpATTRS.SSH_FILEXFER_ATTR_SIZE) != 0) { contentLength = attributes.getSize(); } return new DefaultExternalResourceMetaData(uri, lastModified, contentLength, null, null); }