private FileStatus getFileStatus(Path path) throws IOException { try { if (fs != null) { return fs.getFileStatus(path); } else { return fc.getFileStatus(path); } } catch (FileNotFoundException e) { return null; } }
/** * Does the file exist? * Note: Avoid using this method if you already have FileStatus in hand. * Instead reuse the FileStatus * @param f the file or dir to be checked * * @throws AccessControlException If access is denied * @throws IOException If an I/O error occurred * @throws UnsupportedFileSystemException If file system for <code>f</code> is * not supported * * Exceptions applicable to file systems accessed over RPC: * @throws RpcClientException If an exception occurred in the RPC client * @throws RpcServerException If an exception occurred in the RPC server * @throws UnexpectedServerException If server implementation throws * undeclared exception to RPC server */ public boolean exists(final Path f) throws AccessControlException, UnsupportedFileSystemException, IOException { try { FileStatus fs = FileContext.this.getFileStatus(f); assert fs != null; return true; } catch (FileNotFoundException e) { return false; } }
/** Construct given a {@link FileContext} and a {@link Path}. */ public AvroFSInput(final FileContext fc, final Path p) throws IOException { FileStatus status = fc.getFileStatus(p); this.len = status.getLen(); this.stream = fc.open(p); }
/** * Check if copying srcName to dst would overwrite an existing * file or directory. * @param srcName File or directory to be copied. * @param dst Destination to copy srcName to. * @param overwrite Whether it's ok to overwrite an existing file. * @throws AccessControlException If access is denied. * @throws IOException If dst is an existing directory, or dst is an * existing file and the overwrite option is not passed. */ private void checkDest(String srcName, Path dst, boolean overwrite) throws AccessControlException, IOException { try { FileStatus dstFs = getFileStatus(dst); if (dstFs.isDirectory()) { if (null == srcName) { throw new IOException("Target " + dst + " is a directory"); } // Recurse to check if dst/srcName exists. checkDest(null, new Path(dst, srcName), overwrite); } else if (!overwrite) { throw new IOException("Target " + new Path(dst, srcName) + " already exists"); } } catch (FileNotFoundException e) { // dst does not exist - OK to copy. } }
FileStatus status = getFileStatus(newWorkingDir); if (status.isFile()) { throw new FileNotFoundException("Cannot setWD to a file");
/** * Process the input stat. * If it is a file, return the file stat. * If it is a directory, traverse the directory if recursive is true; * ignore it if recursive is false. * If it is a symlink, resolve the symlink first and then process it * depending on if it is a file or directory. * @param stat input status * @throws AccessControlException if access is denied * @throws FileNotFoundException if file is not found * @throws UnsupportedFileSystemException if fs is not supported * @throws IOException for all other IO errors */ private void handleFileStat(LocatedFileStatus stat) throws IOException { if (stat.isFile()) { // file curFile = stat; } else if (stat.isSymlink()) { // symbolic link // resolve symbolic link FileStatus symstat = FileContext.this.getFileStatus( stat.getSymlink()); if (symstat.isFile() || (recursive && symstat.isDirectory())) { itors.push(curItor); curItor = listLocatedStatus(stat.getPath()); } } else if (recursive) { // directory itors.push(curItor); curItor = listLocatedStatus(stat.getPath()); } }
Path qDst = makeQualified(dst); checkDest(qSrc.getName(), qDst, overwrite); FileStatus fs = FileContext.this.getFileStatus(qSrc); if (fs.isDirectory()) { checkDependencies(qSrc, qDst);
throws AccessControlException, FileNotFoundException, UnsupportedFileSystemException, IOException { FileStatus status = FileContext.this.getFileStatus(f); if (status.isFile()) { long length = status.getLen();
@Private @VisibleForTesting public boolean isRollover(final FileContext fc, final Path candidate) throws IOException { FileStatus fs = fc.getFileStatus(candidate); return fs.getLen() >= this.logRollOverMaxFileSize; }
@Override public FileStatus getFileStatus(Path f) throws AccessControlException, FileNotFoundException, UnsupportedFileSystemException, IOException { return fc.getFileStatus(f); }
/** Construct given a {@link FileContext} and a {@link Path}. */ public AvroFSInput(final FileContext fc, final Path p) throws IOException { FileStatus status = fc.getFileStatus(p); this.len = status.getLen(); this.stream = fc.open(p); }
/** Construct given a {@link FileContext} and a {@link Path}. */ public AvroFSInput(final FileContext fc, final Path p) throws IOException { FileStatus status = fc.getFileStatus(p); this.len = status.getLen(); this.stream = fc.open(p); }
public boolean isDir(Path p) throws IOException { try { return fc.getFileStatus(p).isDirectory(); } catch (FileNotFoundException e) { return false; } }
public boolean isDir(Path p) throws IOException { try { return fc.getFileStatus(p).isDirectory(); } catch (FileNotFoundException e) { return false; } }
@Test(expected=AccessControlException.class) public void testInternalRename2() throws IOException { Assert.assertTrue("linkTODir2 should be a dir", fcView.getFileStatus(new Path("/internalDir/linkToDir2")).isDirectory()); fcView.rename(new Path("/internalDir/linkToDir2"), new Path("/internalDir/dir1")); } @Test(expected=AccessControlException.class)
private static void writeData(FileContext fc, Path p, FSDataOutputStream out, byte[] data, long expectedLen) throws IOException { out.write(data, 0, data.length); out.close(); Assert.assertTrue("Exists", exists(fc, p)); Assert.assertEquals("Length", expectedLen, fc.getFileStatus(p).getLen()); }
@Test public void testGetFileStatusThrowsExceptionForNonExistentFile() throws Exception { try { fc.getFileStatus(getTestRootPath(fc, "test/hadoop/file")); Assert.fail("Should throw FileNotFoundException"); } catch (FileNotFoundException e) { // expected } }
static LocalResource createFile(FileContext files, Path p, int len, Random r, LocalResourceVisibility vis) throws IOException { createFile(files, p, len, r); LocalResource ret = recordFactory.newRecordInstance(LocalResource.class); ret.setResource(URL.fromPath(p)); ret.setSize(len); ret.setType(LocalResourceType.FILE); ret.setVisibility(vis); ret.setTimestamp(files.getFileStatus(p).getModificationTime()); return ret; }
@Test public void testDefaultFilePermission() throws IOException { Path file = fileContextTestHelper.getTestRootPath(fc, "testDefaultFilePermission"); FileContextTestHelper.createFile(fc, file); FsPermission expect = FileContext.FILE_DEFAULT_PERM.applyUMask(fc.getUMask()); Assert.assertEquals(expect, fc.getFileStatus(file) .getPermission()); } }
public void testCreateRecursiveWithExistingDir(FsPermission umask, FsPermission expectedPerms) throws IOException { Path f = fileContextTestHelper.getTestRootPath(fc,"foo"); fc.setUMask(umask); createFile(fc, f); Assert.assertTrue(isFile(fc, f)); Assert.assertEquals("permissions on file are wrong", expectedPerms , fc.getFileStatus(f).getPermission()); }