public RemoteFile open(String filename, Set<OpenMode> modes) throws IOException { return open(filename, modes, FileAttributes.EMPTY); }
public void makeDir(String path) throws IOException { makeDir(path, FileAttributes.EMPTY); }
public FileAttributes stat(String path) throws IOException { return engine.stat(path); }
/** * @return Instantiated {@link SFTPClient} implementation. * * @throws IOException if there is an error starting the {@code sftp} subsystem * @see StatefulSFTPClient */ public SFTPClient newSFTPClient() throws IOException { checkConnected(); checkAuthenticated(); return new SFTPClient(new SFTPEngine(this).init()); }
public PacketReader(SFTPEngine engine) { this.engine = engine; log = engine.getLoggerFactory().getLogger(getClass()); this.in = engine.getSubsystem().getInputStream(); setName("sftp reader"); }
@Override public void download(String source, LocalDestFile dest) throws IOException { final PathComponents pathComponents = engine.getPathHelper().getComponents(source); final FileAttributes attributes = engine.stat(source); new Downloader().download(getTransferListener(), new RemoteResourceInfo(pathComponents, attributes), dest); }
private LocalDestFile downloadFile(final StreamCopier.Listener listener, final RemoteResourceInfo remote, final LocalDestFile local) throws IOException { final LocalDestFile adjusted = local.getTargetFile(remote.getName()); final RemoteFile rf = engine.open(remote.getPath()); try { final RemoteFile.ReadAheadRemoteFileInputStream rfis = rf.new ReadAheadRemoteFileInputStream(16); final OutputStream os = adjusted.getOutputStream(); try { new StreamCopier(rfis, os, engine.getLoggerFactory()) .bufSize(engine.getSubsystem().getLocalMaxPacketSize()) .keepFlushing(false) .listener(listener) .copy(); } finally { rfis.close(); os.close(); } } finally { rf.close(); } return adjusted; }
SSHClient ssh = fixture.setupConnectedDefaultClient(); ssh.authPassword("test", "test"); SFTPEngine sftp = new SFTPEngine(ssh).init(); rf = sftp.open(file.getPath(), EnumSet.of(OpenMode.WRITE, OpenMode.CREAT)); byte[] data = new byte[8192]; new Random(53).nextBytes(data); rf = sftp.open(file.getPath()); InputStream rs = rf.new ReadAheadRemoteFileInputStream(16 /*maxUnconfirmedReads*/);
private String uploadFile(final StreamCopier.Listener listener, final LocalSourceFile local, final String remote) throws IOException { final String adjusted = prepareFile(local, remote); final RemoteFile rf = engine.open(adjusted, EnumSet.of(OpenMode.WRITE, OpenMode.CREAT, OpenMode.TRUNC)); try { final InputStream fis = local.getInputStream(); final RemoteFile.RemoteFileOutputStream rfos = rf.new RemoteFileOutputStream(0, 16); try { new StreamCopier(fis, rfos) .bufSize(engine.getSubsystem().getRemoteMaxPacketSize() - rf.getOutgoingPacketOverhead()) .keepFlushing(false) .listener(listener) .copy(); } finally { fis.close(); rfos.close(); } } finally { rf.close(); } return adjusted; }
@Before public void setPathHelper() throws Exception { PathHelper helper = new PathHelper(new PathHelper.Canonicalizer() { /** * Very basic, it does not try to canonicalize relative bits in the middle of a path. */ @Override public String canonicalize(String path) throws IOException { if ("".equals(path) || ".".equals(path) || "./".equals(path)) return "/home/me"; if ("..".equals(path) || "../".equals(path)) return "/home"; return path; } }, DEFAULT_PATH_SEPARATOR); when(sftpEngine.getPathHelper()).thenReturn(helper); when(sftpEngine.stat("/")).thenReturn(new FileAttributes.Builder().withType(FileMode.Type.DIRECTORY).build()); when(sftpEngine.getLoggerFactory()).thenReturn(LoggerFactory.DEFAULT); }
final Response res = requester.request(newRequest(PacketType.READDIR)) .retrieve(requester.getTimeoutMs(), TimeUnit.MILLISECONDS); switch (res.getType()) { final PathComponents comps = requester.getPathHelper().getComponents(path, name); final RemoteResourceInfo inf = new RemoteResourceInfo(comps, attrs); if (!(".".equals(name) || "..".equals(name)) && (filter == null || filter.accept(inf))) {
private boolean makeDirIfNotExists(final String remote) throws IOException { try { FileAttributes attrs = engine.stat(remote); if (attrs.getMode().getType() != FileMode.Type.DIRECTORY) { throw new IOException(remote + " exists and should be a directory, but was a " + attrs.getMode().getType()); } // Was not created, but existed. return false; } catch (SFTPException e) { if (e.getStatusCode() == StatusCode.NO_SUCH_FILE) { log.debug("makeDir: {} does not exist, creating", remote); engine.makeDir(remote); return true; } else { throw e; } } }
private String prepareDir(final LocalSourceFile local, final String remote) throws IOException { final FileAttributes attrs; try { attrs = engine.stat(remote); } catch (SFTPException e) { if (e.getStatusCode() == StatusCode.NO_SUCH_FILE) { log.debug("probeDir: {} does not exist, creating", remote); engine.makeDir(remote); return remote; } else throw e; } if (attrs.getMode().getType() == FileMode.Type.DIRECTORY) if (engine.getPathHelper().getComponents(remote).getName().equals(local.getName())) { log.debug("probeDir: {} already exists", remote); return remote; } else { log.debug("probeDir: {} already exists, path adjusted for {}", remote, local.getName()); return prepareDir(local, engine.getPathHelper().adjustForParent(remote, local.getName())); } else throw new IOException(attrs.getMode().getType() + " file already exists at " + remote); }
if(file.isSymbolicLink()) { session.sftp().remove(file.getAbsolute()); flags = EnumSet.of(OpenMode.CREAT, OpenMode.TRUNC, OpenMode.WRITE); final RemoteFile handle = session.sftp().open(file.getAbsolute(), flags); final int maxUnconfirmedWrites = this.getMaxUnconfirmedWrites(status); if(log.isInfoEnabled()) {
@Override public String canonicalize(String path) throws IOException { return SFTPEngine.this.canonicalize(path); } }, pathSep);
private synchronized String cwdify(String path) { return engine.getPathHelper().adjustForParent(cwd, path); }
public int version() { return engine.getOperativeProtocolVersion(); }
PathAttributes attr; try { final String link = session.sftp().readLink(file.getAbsolute()); if(link.startsWith(String.valueOf(Path.DELIMITER))) { target = new Path(PathNormalizer.normalize(link), EnumSet.of(Path.Type.file)); final FileAttributes stat = session.sftp().stat(target.getAbsolute()); if(stat.getType().equals(FileMode.Type.DIRECTORY)) { type = Path.Type.directory;
@Override public void close() throws IOException { engine.close(); }