SFTPFileSystem extends FileSystem { private boolean rename(ChannelSftp channel, Path src, Path dst) throws IOException { Path workDir; try { workDir = new Path(channel.pwd()); } catch (SftpException e) { throw new IOException(e); } Path absoluteSrc = makeAbsolute(workDir, src); Path absoluteDst = makeAbsolute(workDir, dst); if (!exists(channel, absoluteSrc)) { throw new IOException(String.format(E_SPATH_NOTEXIST, src)); } if (exists(channel, absoluteDst)) { throw new IOException(String.format(E_DPATH_EXIST, dst)); } boolean renamed = true; try { final String previousCwd = channel.pwd(); channel.cd("/"); channel.rename(src.toUri().getPath(), dst.toUri().getPath()); channel.cd(previousCwd); } catch (SftpException e) { renamed = false; } return renamed; }  @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(expected=java.io.IOException.class) public void testRenameNonExistFile() throws Exception { Path file1 = new Path(localDir, name.getMethodName().toLowerCase() + "1"); Path file2 = new Path(localDir, name.getMethodName().toLowerCase() + "2"); sftpFs.rename(file1, file2); }
@Test(expected=java.io.IOException.class) public void testRenamingFileOntoExistingFile() throws Exception { Path file1 = touch(localFs, name.getMethodName().toLowerCase() + "1"); Path file2 = touch(localFs, name.getMethodName().toLowerCase() + "2"); sftpFs.rename(file1, file2); }