SFTPFileSystem extends FileSystem { private boolean delete(ChannelSftp channel, Path file, boolean recursive) throws IOException { Path workDir; try { workDir = new Path(channel.pwd()); } catch (SftpException e) { throw new IOException(e); } Path absolute = makeAbsolute(workDir, file); String pathName = absolute.toUri().getPath(); FileStatus fileStat = null; try { fileStat = getFileStatus(channel, absolute); } catch (FileNotFoundException e) { return false; } if (!fileStat.isDirectory()) { boolean status = true; try { channel.rm(pathName); } catch (SftpException e) { status = false; } return status; } else { boolean status = true; FileStatus[] dirEntries = listStatus(channel, absolute); if (dirEntries != null && dirEntries.length > 0) { if (!recursive) { throw new IOException(String.format(E_DIR_NOTEMPTY, file)); } for (int i = 0; i < dirEntries.length; ++i) { delete(channel, new Path(absolute, dirEntries[i].getPath()), recursive); } } try { channel.rmdir(pathName); } catch (SftpException e) { status = false; } return status; } }  @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 Log 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_NOT_SUPPORTED; 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(expected=java.io.IOException.class) public void testDeleteNonEmptyDir() throws Exception { Path file = touch(localFs, name.getMethodName().toLowerCase()); sftpFs.delete(localDir, false); }
@Test public void testDeleteNonExistFile() throws Exception { Path file = new Path(localDir, name.getMethodName().toLowerCase()); assertFalse(sftpFs.delete(file, false)); }