public SFTPEngine(SessionFactory ssh, String pathSep) throws SSHException { Session session = ssh.startSession(); loggerFactory = session.getLoggerFactory(); log = loggerFactory.getLogger(getClass()); sub = session.startSubsystem("sftp"); out = sub.getOutputStream(); reader = new PacketReader(this); pathHelper = new PathHelper(new PathHelper.Canonicalizer() { @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); }
throws IOException { if (path.equals(pathSep)) { return getComponents("", "/"); return getComponents(getDotDir()); final String withoutTrailSep = trimTrailingSeparator(path); final int lastSep = withoutTrailSep.lastIndexOf(pathSep); String parent; return getComponents(canonicalizer.canonicalize(path)); } else { return getComponents(parent, name);
public void mkdirs(String path) throws IOException { final Deque<String> dirsToMake = new LinkedList<String>(); for (PathComponents current = engine.getPathHelper().getComponents(path); ; current = engine.getPathHelper().getComponents(current.getParent())) { final FileAttributes attrs = statExistence(current.getPath()); if (attrs == null) { dirsToMake.push(current.getPath()); } else if (attrs.getType() != FileMode.Type.DIRECTORY) { throw new SFTPException(current.getPath() + " exists but is not a directory"); } else { break; } } while (!dirsToMake.isEmpty()) { mkdir(dirsToMake.pop()); } }
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); }
@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); }
/** * Divide the path into {@code PathComponents(parent, name)} while making sure {@code name != "." && name != ".."} * * @param path to convert * * @return PathComponents * * @throws IOException */ public PathComponents getComponents(final String path) throws IOException { if (path.equals(pathSep)) return getComponents("", ""); if (path.isEmpty() || path.equals(".") || path.equals("." + pathSep)) return getComponents(getDotDir()); final String withoutTrailSep = trimTrailingSeparator(path); final int lastSep = withoutTrailSep.lastIndexOf(pathSep); final String parent = (lastSep == -1) ? "" : withoutTrailSep.substring(0, lastSep); final String name = (lastSep == -1) ? withoutTrailSep : withoutTrailSep.substring(lastSep + pathSep.length()); if (name.equals(".") || name.equals("..")) { return getComponents(canonicalizer.canonicalize(path)); } else { return getComponents(parent, name); } }
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 String uploadDir(final TransferListener listener, final LocalSourceFile local, final String remote) throws IOException { makeDirIfNotExists(remote); for (LocalSourceFile f : local.getChildren(getUploadFilter())) upload(listener, f, engine.getPathHelper().adjustForParent(remote, f.getName())); return remote; }
@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); }
throws IOException { if (path.equals(pathSep)) { return getComponents("", "/"); return getComponents(getDotDir()); final String withoutTrailSep = trimTrailingSeparator(path); final int lastSep = withoutTrailSep.lastIndexOf(pathSep); String parent; return getComponents(canonicalizer.canonicalize(path)); } else { return getComponents(parent, name);
public void mkdirs(String path) throws IOException { final Deque<String> dirsToMake = new LinkedList<String>(); for (PathComponents current = engine.getPathHelper().getComponents(path); ; current = engine.getPathHelper().getComponents(current.getParent())) { final FileAttributes attrs = statExistence(current.getPath()); if (attrs == null) { dirsToMake.push(current.getPath()); } else if (attrs.getType() != FileMode.Type.DIRECTORY) { throw new SFTPException(current.getPath() + " exists but is not a directory"); } else { break; } } while (!dirsToMake.isEmpty()) { mkdir(dirsToMake.pop()); } }
private void upload(final TransferListener listener) throws IOException { if (source.isDirectory()) { makeDirIfNotExists(remote); // Ensure that the directory exists uploadDir(listener.directory(source.getName()), source, remote); setAttributes(source, remote); } else if (source.isFile() && isDirectory(remote)) { String adjustedRemote = engine.getPathHelper().adjustForParent(this.remote, source.getName()); uploadFile(listener.file(source.getName(), source.getLength()), source, adjustedRemote); setAttributes(source, adjustedRemote); } else if (source.isFile()) { uploadFile(listener.file(source.getName(), source.getLength()), source, remote); setAttributes(source, remote); } else { throw new IOException(source + " is not a file or directory"); } }
public SFTPEngine(SessionFactory ssh, String pathSep) throws SSHException { sub = ssh.startSession().startSubsystem("sftp"); out = sub.getOutputStream(); reader = new PacketReader(this); pathHelper = new PathHelper(new PathHelper.Canonicalizer() { @Override public String canonicalize(String path) throws IOException { return SFTPEngine.this.canonicalize(path); } }, pathSep); }
public void mkdirs(String path) throws IOException { final Deque<String> dirsToMake = new LinkedList<String>(); for (PathComponents current = engine.getPathHelper().getComponents(path); ; current = engine.getPathHelper().getComponents(current.getParent())) { final FileAttributes attrs = statExistence(current.getPath()); if (attrs == null) { dirsToMake.push(current.getPath()); } else if (attrs.getType() != FileMode.Type.DIRECTORY) { throw new SFTPException(current.getPath() + " exists but is not a directory"); } else { break; } } while (!dirsToMake.isEmpty()) { mkdir(dirsToMake.pop()); } }
private synchronized String cwdify(String path) { return engine.getPathHelper().adjustForParent(cwd, path); }
public SFTPEngine(SessionFactory ssh, String pathSep) throws SSHException { Session session = ssh.startSession(); loggerFactory = session.getLoggerFactory(); log = loggerFactory.getLogger(getClass()); sub = session.startSubsystem("sftp"); out = sub.getOutputStream(); reader = new PacketReader(this); pathHelper = new PathHelper(new PathHelper.Canonicalizer() { @Override public String canonicalize(String path) throws IOException { return SFTPEngine.this.canonicalize(path); } }, pathSep); }
@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 synchronized String cwdify(String path) { return engine.getPathHelper().adjustForParent(cwd, path); }
@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); }