Refine search
@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); } }
@Test public void testEnsureDirectoryExistsParentNotExisted() throws IOException, SftpException { final ProcessContext processContext = mock(ProcessContext.class); final ChannelSftp channel = mock(ChannelSftp.class); // stat for the dir1 was successful, simulating that dir1 exists, but no dir2 and dir3. when(channel.stat("/dir1/dir2/dir3")).thenThrow(new SftpException(SSH_FX_NO_SUCH_FILE, "No such file")); when(channel.stat("/dir1/dir2")).thenThrow(new SftpException(SSH_FX_NO_SUCH_FILE, "No such file")); final SFTPTransfer sftpTransfer = createSftpTransfer(processContext, channel); final MockFlowFile flowFile = new MockFlowFile(0); final File remoteDir = new File("/dir1/dir2/dir3"); sftpTransfer.ensureDirectoryExists(flowFile, remoteDir); // Dir existence check should be done by stat verify(channel).stat(eq("/dir1/dir2/dir3")); // dir3 was not found verify(channel).stat(eq("/dir1/dir2")); // dir2 was not found, too verify(channel).stat(eq("/dir1")); // dir1 was found verify(channel).mkdir(eq("/dir1/dir2")); // dir1 existed, so dir2 was created. verify(channel).mkdir(eq("/dir1/dir2/dir3")); // then dir3 was created. }
public void chmod(int permissions, String path) throws SftpException{ try{ path=remoteAbsolutePath(path); Vector v=glob_remote(path); int vsize=v.size(); for(int j=0; j<vsize; j++){ path=(String)(v.elementAt(j)); SftpATTRS attr=_stat(path); attr.setFLAGS(0); attr.setPERMISSIONS(permissions); _setStat(path, attr); } } catch(Exception e){ if(e instanceof SftpException) throw (SftpException)e; if(e instanceof Throwable) throw new SftpException(SSH_FX_FAILURE, "", (Throwable)e); throw new SftpException(SSH_FX_FAILURE, ""); } }
/** * This method will check if the given string can be expanded to the * unique string. If it can be expanded to mutiple files, SftpException * will be thrown. * @return the returned string is unquoted. */ private String isUnique(String path) throws SftpException, Exception{ Vector v=glob_remote(path); if(v.size()!=1){ throw new SftpException(SSH_FX_FAILURE, path+" is not unique: "+v.toString()); } return (String)(v.elementAt(0)); }
public void setMtime(String path, int mtime) throws SftpException{ try{ path=remoteAbsolutePath(path); Vector v=glob_remote(path); int vsize=v.size(); for(int j=0; j<vsize; j++){ path=(String)(v.elementAt(j)); SftpATTRS attr=_stat(path); attr.setFLAGS(0); attr.setACMODTIME(attr.getATime(), mtime); _setStat(path, attr); } } catch(Exception e){ if(e instanceof SftpException) throw (SftpException)e; if(e instanceof Throwable) throw new SftpException(SSH_FX_FAILURE, "", (Throwable)e); throw new SftpException(SSH_FX_FAILURE, ""); } }
public SftpFile[] ls(String path) throws SshException { Vector paths; try { paths = channel.ls(path); } catch (SftpException e) { throw new net.sf.sshapi.sftp.SftpException(e.id, e.getLocalizedMessage(), e); } List files = new ArrayList(); for (Enumeration e = paths.elements(); e.hasMoreElements();) { ChannelSftp.LsEntry entry = (ChannelSftp.LsEntry) e.nextElement(); SftpFile file = new SftpFile(convertType(entry.getAttrs()), entry.getFilename(), entry.getAttrs().getSize(), convertIntDate(entry.getAttrs().getMTime()), 0, convertIntDate(entry.getAttrs().getATime()), entry.getAttrs() .getGId(), entry.getAttrs().getUId(), entry.getAttrs().getPermissions()); files.add(file); } return (SftpFile[]) files.toArray(new SftpFile[paths.size()]); }
public void setStat(String path, SftpATTRS attr) throws SftpException{ try{ path=remoteAbsolutePath(path); Vector v=glob_remote(path); int vsize=v.size(); for(int j=0; j<vsize; j++){ path=(String)(v.elementAt(j)); _setStat(path, attr); } } catch(Exception e){ if(e instanceof SftpException) throw (SftpException)e; if(e instanceof Throwable) throw new SftpException(SSH_FX_FAILURE, "", (Throwable)e); throw new SftpException(SSH_FX_FAILURE, ""); } } private void _setStat(String path, SftpATTRS attr) throws SftpException{
@Test public void shouldCatchAndRethrowExceptionIfCaught() throws SftpException { expectedException.expect(FTPException.class); expectedException.expectMessage(equalTo("Unable to delete file on remote server")); when(mockChannel.ls("/some/directory/file.name/")).thenReturn(new Vector<LsEntry>()); FTPFile file = new FTPFile("file.name", 0, "/some/directory", 0, true); doThrow(new SftpException(0, "")).when(mockChannel).rmdir("/some/directory/file.name"); sftpConnection.deleteRemoteFile(file); }
protected boolean fastExistsFile(String name) throws GenericFileOperationFailedException { LOG.trace("fastExistsFile({})", name); try { @SuppressWarnings("rawtypes") Vector files = channel.ls(name); if (files == null) { return false; } return files.size() >= 1; } catch (SftpException e) { // or an exception can be thrown with id 2 which means file does not // exists if (ChannelSftp.SSH_FX_NO_SUCH_FILE == e.id) { return false; } // otherwise its a more serious error so rethrow throw new GenericFileOperationFailedException(e.getMessage(), e); } }
private void doMultipleTransfer() throws IOException, JSchException { final ChannelSftp channel = openSftpChannel(); try { channel.connect(); channel.stat(remotePath); } catch (final SftpException e) { if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) { channel.mkdir(remotePath); channel.chmod(getDirMode(), remotePath); } else { } catch (final SftpException e) { throw new JSchException("Could not CD to '" + remotePath + "' - " + e.toString(), e);
/** * Carry out the transfer. * @throws IOException on i/o errors * @throws JSchException on errors detected by scp */ @Override public void execute() throws IOException, JSchException { final ChannelSftp channel = openSftpChannel(); try { channel.connect(); try { final SftpATTRS attrs = channel.stat(remoteFile); if (attrs.isDir() && !remoteFile.endsWith("/")) { remoteFile += "/"; } } catch (final SftpException ee) { // Ignored } getDir(channel, remoteFile, localFile); } catch (final SftpException e) { throw new JSchException("Could not get '" + remoteFile + "' to '" + localFile + "' - " + e.toString(), e); } finally { if (channel != null) { channel.disconnect(); } } log("done\n"); }
@Override public long getFileMTime(String filePath) throws FileBasedHelperException { ChannelSftp channelSftp = null; try { channelSftp = getSftpChannel(); int modificationTime = channelSftp.lstat(filePath).getMTime(); return modificationTime; } catch (SftpException e) { throw new FileBasedHelperException( String.format("Failed to get modified timestamp for file at path %s due to error %s", filePath, e.getMessage()), e); } finally { if (channelSftp != null) { channelSftp.disconnect(); } } }
protected static void doSingleSftpransfer(Session session, File localFile, String remoteFilePath) throws IOException, JSchException { ChannelSftp channel = openSftpChannel(session); try { channel.connect(); try { sendFileToRemote(channel, localFile, remoteFilePath); } catch (SftpException e) { throw new JSchException(e.toString()); } } finally { if (channel != null) { channel.disconnect(); } } }
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); } }
public static String test(final SshNode workerNode, final AskUserForPw askUserForPw) { try { Session sshSession = SshPool.getSshSession(workerNode, askUserForPw); ChannelSftp sftpTest = (ChannelSftp) sshSession.openChannel("sftp"); sftpTest.connect(); sftpTest.cd(workerNode.getDirectory()); sftpTest.disconnect(); sshSession.disconnect(); } catch (JSchException e) { return e.toString(); } catch (SftpException e) { return e.toString(); } return null; }
/** * Executes a get SftpCommand and returns an input stream to the file * @param cmd is the command to execute * @param sftp is the channel to execute the command on * @throws SftpException */ @Override public InputStream getFileStream(String file) throws FileBasedHelperException { SftpGetMonitor monitor = new SftpGetMonitor(); try { ChannelSftp channel = getSftpChannel(); return new SftpFsFileInputStream(channel.get(file, monitor), channel); } catch (SftpException e) { throw new FileBasedHelperException("Cannot download file " + file + " due to " + e.getMessage(), e); } }
@Override FileStream open(final String path) throws IOException { try { final SftpATTRS a = ftp.lstat(path); return new FileStream(ftp.get(path), a.getSize()); } catch (SftpException je) { if (je.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) throw new FileNotFoundException(path); throw new TransportException(MessageFormat.format( JGitText.get().cannotGetObjectsPath, objectsPath, path, je.getMessage()), je); } }
final String forwardPaths = dir.getPath().replaceAll(Matcher.quoteReplacement("\\"), Matcher.quoteReplacement("/")); try { currentWorkingDirectory = sftp.pwd(); logger.debug(proc + " attempting to change directory from " + currentWorkingDirectory + " to " + dir.getPath()); sftp.cd(forwardPaths); dirExists = true; logger.debug(proc + " changed working directory to '" + forwardPaths + "' from '" + currentWorkingDirectory + "'"); sftp.cd(dirName); } catch (final SftpException sftpe) { logger.debug(proc + " creating new directory and changing to it " + dirName); sftp.cd(dirName); } catch (final SftpException e) { throw new IOException(proc + " could not make/change directory to [" + dirName + "] [" + e.getLocalizedMessage() + "]", e);
public InputStream get(String path) throws SshException { try { return channel.get(path); } catch (SftpException e) { throw new net.sf.sshapi.sftp.SftpException(e.id, e.getLocalizedMessage(), e); } }