SFTPFileSystem extends FileSystem { private boolean mkdirs(ChannelSftp client, Path file, FsPermission permission) throws IOException { boolean created = true; Path workDir; try { workDir = new Path(client.pwd()); } catch (SftpException e) { throw new IOException(e); } Path absolute = makeAbsolute(workDir, file); String pathName = absolute.getName(); if (!exists(client, absolute)) { Path parent = absolute.getParent(); created = (parent == null || mkdirs(client, parent, FsPermission.getDefault())); if (created) { String parentDir = parent.toUri().getPath(); boolean succeeded = true; try { final String previousCwd = client.pwd(); client.cd(parentDir); client.mkdir(pathName); client.cd(previousCwd); } catch (SftpException e) { throw new IOException(String.format(E_MAKE_DIR_FORPATH, pathName, parentDir)); } created = created & succeeded; } } else if (isFile(client, absolute)) { throw new IOException(String.format(E_DIR_CREATE_FROMFILE, absolute)); } return created; }  @Override void initialize(URI uriInfo, Configuration conf); @Override URI getUri(); @Override FSDataInputStream open(Path f, int bufferSize); @Override FSDataOutputStream create(Path f, FsPermission permission,
      boolean overwrite, int bufferSize, short replication, long blockSize,
      Progressable progress); @Override FSDataOutputStream append(Path f, int bufferSize,
      Progressable progress); @Override boolean rename(Path src, Path dst); @Override boolean delete(Path f, boolean recursive); @Override FileStatus[] listStatus(Path f); @Override void setWorkingDirectory(Path newDir); @Override Path getWorkingDirectory(); @Override Path getHomeDirectory(); @Override boolean mkdirs(Path f, FsPermission permission); @Override FileStatus getFileStatus(Path f); static final Logger LOG; static final int DEFAULT_BUFFER_SIZE; static final int DEFAULT_BLOCK_SIZE; static final String FS_SFTP_USER_PREFIX; static final String FS_SFTP_PASSWORD_PREFIX; static final String FS_SFTP_HOST; static final String FS_SFTP_HOST_PORT; static final String FS_SFTP_KEYFILE; static final String FS_SFTP_CONNECTION_MAX; static final String E_SAME_DIRECTORY_ONLY; static final String E_HOST_NULL; static final String E_USER_NULL; static final String E_PATH_DIR; static final String E_FILE_STATUS; static final String E_FILE_NOTFOUND; static final String E_FILE_EXIST; static final String E_CREATE_DIR; static final String E_DIR_CREATE_FROMFILE; static final String E_MAKE_DIR_FORPATH; static final String E_DIR_NOTEMPTY; static final String E_FILE_CHECK_FAILED; static final String E_SPATH_NOTEXIST; static final String E_DPATH_EXIST; static final String E_FAILED_GETHOME; static final String E_FAILED_DISCONNECT; }
@Test public void testMkDirs() throws IOException { Path path = new Path(localDir.toUri().getPath(), new Path(name.getMethodName(), "subdirectory")); sftpFs.mkdirs(path); assertTrue(localFs.exists(path)); assertTrue(localFs.getFileStatus(path).isDirectory()); assertThat(((SFTPFileSystem) sftpFs).getConnectionPool().getLiveConnCount(), is(1)); }