SFTPFileSystem extends FileSystem { @SuppressWarnings("unchecked") private FileStatus getFileStatus(ChannelSftp client, Path file) throws IOException { FileStatus fileStat = null; Path workDir; try { workDir = new Path(client.pwd()); } catch (SftpException e) { throw new IOException(e); } Path absolute = makeAbsolute(workDir, file); Path parentPath = absolute.getParent(); if (parentPath == null) { long length = -1; boolean isDir = true; int blockReplication = 1; long blockSize = DEFAULT_BLOCK_SIZE; long modTime = -1; Path root = new Path("/"); return new FileStatus(length, isDir, blockReplication, blockSize, modTime, root.makeQualified(this.getUri(), this.getWorkingDirectory())); } String pathName = parentPath.toUri().getPath(); Vector<LsEntry> sftpFiles; try { sftpFiles = (Vector<LsEntry>) client.ls(pathName); } catch (SftpException e) { throw new FileNotFoundException(String.format(E_FILE_NOTFOUND, file)); } if (sftpFiles != null) { for (LsEntry sftpFile : sftpFiles) { if (sftpFile.getFilename().equals(file.getName())) { fileStat = getFileStatus(client, sftpFile, parentPath); break; } } if (fileStat == null) { throw new FileNotFoundException(String.format(E_FILE_NOTFOUND, file)); } } else { throw new FileNotFoundException(String.format(E_FILE_NOTFOUND, file)); } return fileStat; }  @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 testGetAccessTime() throws IOException { Path file = touch(localFs, name.getMethodName().toLowerCase()); LocalFileSystem local = (LocalFileSystem)localFs; java.nio.file.Path path = (local).pathToFile(file).toPath(); long accessTime1 = Files.readAttributes(path, BasicFileAttributes.class) .lastAccessTime().toMillis(); accessTime1 = (accessTime1 / 1000) * 1000; long accessTime2 = sftpFs.getFileStatus(file).getAccessTime(); assertEquals(accessTime1, accessTime2); }
@Test public void testGetModifyTime() throws IOException { Path file = touch(localFs, name.getMethodName().toLowerCase() + "1"); java.io.File localFile = ((LocalFileSystem) localFs).pathToFile(file); long modifyTime1 = localFile.lastModified(); modifyTime1 = (modifyTime1 / 1000) * 1000; long modifyTime2 = sftpFs.getFileStatus(file).getModificationTime(); assertEquals(modifyTime1, modifyTime2); }