HADOOP-6678. Remove FileContext#isFile, isDirectory and exists. Contributed by Eli Collins.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/trunk@939140 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Hairong Kuang 2010-04-28 23:58:25 +00:00
parent c59b4cd966
commit 8991eb7959
10 changed files with 243 additions and 250 deletions

View File

@ -238,6 +238,9 @@ Trunk (unreleased changes)
HADOOP-6677. InterfaceAudience.LimitedPrivate should take a string not an
enum. (tomwhite)
HADOOP-678. Remove FileContext#isFile, isDirectory, and exists.
(Eli Collins via hairong)
OPTIMIZATIONS
HADOOP-6467. Improve the performance on HarFileSystem.listStatus(..).

View File

@ -1212,7 +1212,7 @@ public final class FileContext {
* directory. All other functions fully resolve, ie follow, the symlink.
* These are: open, setReplication, setOwner, setTimes, setWorkingDirectory,
* setPermission, getFileChecksum, setVerifyChecksum, getFileBlockLocations,
* getFsStatus, getFileStatus, isDirectory, isFile, exists, and listStatus.
* getFsStatus, getFileStatus, exists, and listStatus.
*
* Symlink targets are stored as given to createSymlink, assuming the
* underlying file system is capable of storign a fully qualified URI.
@ -1284,93 +1284,6 @@ public final class FileContext {
}
}.resolve(this, nonRelLink);
}
/**
* 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
*
* 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,
IOException {
try {
return getFileStatus(f) != null;
} catch (FileNotFoundException e) {
return false;
} catch (UnsupportedFileSystemException e) {
return false;
} catch (UnresolvedLinkException e) {
return false;
}
}
/**
* Is a directory?
* Note: Avoid using this method if you already have FileStatus in hand.
* Instead reuse the FileStatus
* returned by getFileStatus() or listStatus() methods.
*
* @param f Path to evaluate
*
* @return True iff the named path is a directory.
*
* @throws AccessControlException If access is denied
* @throws UnsupportedFileSystemException If file system for <code>f</code> is
* not supported
* @throws IOException If an I/O error occurred
*
* 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 isDirectory(final Path f) throws AccessControlException,
UnsupportedFileSystemException, IOException {
try {
final Path absF = fixRelativePart(f);
return getFileStatus(absF).isDir();
} catch (FileNotFoundException e) {
return false;
}
}
/** True iff the named path is a regular file.
* Note: Avoid using this method if you already have FileStatus in hand
* Instead reuse the FileStatus returned by getFileStatus() or listStatus()
* methods.
*
* @param f Path to evaluate
*
* @throws AccessControlException If access is denied
* @throws UnsupportedFileSystemException If file system for <code>f</code>
* is not supported
* @throws IOException If an I/O error occurred
*
* 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 isFile(final Path f) throws AccessControlException,
UnsupportedFileSystemException, IOException {
try {
final Path absF = fixRelativePart(f);
return !getFileStatus(absF).isDir();
} catch (FileNotFoundException e) {
return false; // f does not exist
}
}
/**
* List the statuses of the files/directories in the given path if the path is
@ -1425,7 +1338,7 @@ public final class FileContext {
*/
public boolean deleteOnExit(Path f) throws AccessControlException,
IOException {
if (!exists(f)) {
if (!this.util().exists(f)) {
return false;
}
synchronized (DELETE_ON_EXIT) {
@ -1456,6 +1369,34 @@ public final class FileContext {
* changes to the same part of the name space.
*/
public class Util {
/**
* 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;
}
}
/**
* Return a list of file status objects that corresponds to supplied paths
* excluding those non-existent paths.
@ -1941,8 +1882,9 @@ public final class FileContext {
checkNotSchemeWithRelative(dst);
Path qSrc = makeQualified(src);
Path qDst = makeQualified(dst);
checkDest(qSrc.getName(), qDst, false);
if (isDirectory(qSrc)) {
checkDest(qSrc.getName(), qDst, overwrite);
FileStatus fs = FileContext.this.getFileStatus(qSrc);
if (fs.isDir()) {
checkDependencies(qSrc, qDst);
mkdir(qDst, FsPermission.getDefault(), true);
FileStatus[] contents = listStatus(qSrc);
@ -2095,23 +2037,32 @@ public final class FileContext {
+ " for glob " + pattern + " at " + pos);
}
}
//
// Check destionation. Throw IOException if destination already exists
// and overwrite is not true
//
/**
* 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 {
if (exists(dst)) {
if (isDirectory(dst)) {
// TBD not very clear
FileStatus dstFs = getFileStatus(dst);
try {
if (dstFs.isDir()) {
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 " + dst + " already exists");
}
} catch (FileNotFoundException e) {
// dst does not exist - OK to copy.
}
}

View File

@ -84,7 +84,7 @@ public abstract class FileContextCreateMkdirBaseTest {
public void testMkdirNonRecursiveWithExistingDir() throws IOException {
Path f = getTestRootPath(fc, "aDir");
fc.mkdir(f, FileContext.DEFAULT_PERM, false);
Assert.assertTrue(fc.isDirectory(f));
Assert.assertTrue(isDir(fc, f));
}
@Test
@ -103,7 +103,7 @@ public abstract class FileContextCreateMkdirBaseTest {
public void testMkdirRecursiveWithExistingDir() throws IOException {
Path f = getTestRootPath(fc, "aDir");
fc.mkdir(f, FileContext.DEFAULT_PERM, true);
Assert.assertTrue(fc.isDirectory(f));
Assert.assertTrue(isDir(fc, f));
}
@ -111,7 +111,7 @@ public abstract class FileContextCreateMkdirBaseTest {
public void testMkdirRecursiveWithNonExistingDir() throws IOException {
Path f = getTestRootPath(fc, "NonExistant2/aDir");
fc.mkdir(f, FileContext.DEFAULT_PERM, true);
Assert.assertTrue(fc.isDirectory(f));
Assert.assertTrue(isDir(fc, f));
}
///////////////////////
@ -121,7 +121,7 @@ public abstract class FileContextCreateMkdirBaseTest {
public void testCreateNonRecursiveWithExistingDir() throws IOException {
Path f = getTestRootPath(fc, "foo");
createFile(fc, f);
Assert.assertTrue(fc.isFile(f));
Assert.assertTrue(isFile(fc, f));
}
@Test
@ -139,7 +139,7 @@ public abstract class FileContextCreateMkdirBaseTest {
public void testCreateRecursiveWithExistingDir() throws IOException {
Path f = getTestRootPath(fc,"foo");
createFile(fc, f);
Assert.assertTrue(fc.isFile(f));
Assert.assertTrue(isFile(fc, f));
}
@ -147,6 +147,6 @@ public abstract class FileContextCreateMkdirBaseTest {
public void testCreateRecursiveWithNonExistingDir() throws IOException {
Path f = getTestRootPath(fc,"NonExisting/foo");
createFile(fc, f);
Assert.assertTrue(fc.isFile(f));
Assert.assertTrue(isFile(fc, f));
}
}

View File

@ -178,35 +178,35 @@ public abstract class FileContextMainOperationsBaseTest {
@Test
public void testMkdirs() throws Exception {
Path testDir = getTestRootPath(fc, "test/hadoop");
Assert.assertFalse(fc.exists(testDir));
Assert.assertFalse(fc.isFile(testDir));
Assert.assertFalse(exists(fc, testDir));
Assert.assertFalse(isFile(fc, testDir));
fc.mkdir(testDir, FsPermission.getDefault(), true);
Assert.assertTrue(fc.exists(testDir));
Assert.assertFalse(fc.isFile(testDir));
Assert.assertTrue(exists(fc, testDir));
Assert.assertFalse(isFile(fc, testDir));
fc.mkdir(testDir, FsPermission.getDefault(), true);
Assert.assertTrue(fc.exists(testDir));
Assert.assertFalse(fc.isFile(testDir));
Assert.assertTrue(exists(fc, testDir));
Assert.assertFalse(isFile(fc, testDir));
Path parentDir = testDir.getParent();
Assert.assertTrue(fc.exists(parentDir));
Assert.assertFalse(fc.isFile(parentDir));
Assert.assertTrue(exists(fc, parentDir));
Assert.assertFalse(isFile(fc, parentDir));
Path grandparentDir = parentDir.getParent();
Assert.assertTrue(fc.exists(grandparentDir));
Assert.assertFalse(fc.isFile(grandparentDir));
Assert.assertTrue(exists(fc, grandparentDir));
Assert.assertFalse(isFile(fc, grandparentDir));
}
@Test
public void testMkdirsFailsForSubdirectoryOfExistingFile() throws Exception {
Path testDir = getTestRootPath(fc, "test/hadoop");
Assert.assertFalse(fc.exists(testDir));
Assert.assertFalse(exists(fc, testDir));
fc.mkdir(testDir, FsPermission.getDefault(), true);
Assert.assertTrue(fc.exists(testDir));
Assert.assertTrue(exists(fc, testDir));
createFile(getTestRootPath(fc, "test/hadoop/file"));
@ -217,7 +217,7 @@ public abstract class FileContextMainOperationsBaseTest {
} catch (IOException e) {
// expected
}
Assert.assertFalse(fc.exists(testSubDir));
Assert.assertFalse(exists(fc, testSubDir));
Path testDeepSubDir = getTestRootPath(fc, "test/hadoop/file/deep/sub/dir");
try {
@ -226,7 +226,7 @@ public abstract class FileContextMainOperationsBaseTest {
} catch (IOException e) {
// expected
}
Assert.assertFalse(fc.exists(testDeepSubDir));
Assert.assertFalse(exists(fc, testDeepSubDir));
}
@ -257,7 +257,7 @@ public abstract class FileContextMainOperationsBaseTest {
getTestRootPath(fc, "test/hadoop/a"),
getTestRootPath(fc, "test/hadoop/b"),
getTestRootPath(fc, "test/hadoop/c/1"), };
Assert.assertFalse(fc.exists(testDirs[0]));
Assert.assertFalse(exists(fc, testDirs[0]));
for (Path path : testDirs) {
fc.mkdir(path, FsPermission.getDefault(), true);
@ -316,7 +316,7 @@ public abstract class FileContextMainOperationsBaseTest {
getTestRootPath(fc, TEST_DIR_AXA),
getTestRootPath(fc, TEST_DIR_AXX), };
if (fc.exists(testDirs[0]) == false) {
if (exists(fc, testDirs[0]) == false) {
for (Path path : testDirs) {
fc.mkdir(path, FsPermission.getDefault(), true);
}
@ -336,7 +336,7 @@ public abstract class FileContextMainOperationsBaseTest {
getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AAA2), };
if (fc.exists(testDirs[0]) == false) {
if (exists(fc, testDirs[0]) == false) {
for (Path path : testDirs) {
fc.mkdir(path, FsPermission.getDefault(), true);
}
@ -373,7 +373,7 @@ public abstract class FileContextMainOperationsBaseTest {
getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AAA2), };
if (fc.exists(testDirs[0]) == false) {
if (exists(fc, testDirs[0]) == false) {
for (Path path : testDirs) {
fc.mkdir(path, FsPermission.getDefault(), true);
}
@ -393,7 +393,7 @@ public abstract class FileContextMainOperationsBaseTest {
getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AAA2), };
if (fc.exists(testDirs[0]) == false) {
if (exists(fc, testDirs[0]) == false) {
for (Path path : testDirs) {
fc.mkdir(path, FsPermission.getDefault(), true);
}
@ -417,7 +417,7 @@ public abstract class FileContextMainOperationsBaseTest {
getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AAA2), };
if (fc.exists(testDirs[0]) == false) {
if (exists(fc, testDirs[0]) == false) {
for (Path path : testDirs) {
fc.mkdir(path, FsPermission.getDefault(), true);
}
@ -442,7 +442,7 @@ public abstract class FileContextMainOperationsBaseTest {
getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AAA2), };
if (fc.exists(testDirs[0]) == false) {
if (exists(fc, testDirs[0]) == false) {
for (Path path : testDirs) {
fc.mkdir(path, FsPermission.getDefault(), true);
}
@ -466,7 +466,7 @@ public abstract class FileContextMainOperationsBaseTest {
getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AXX), };
if (fc.exists(testDirs[0]) == false) {
if (exists(fc, testDirs[0]) == false) {
for (Path path : testDirs) {
fc.mkdir(path, FsPermission.getDefault(), true);
}
@ -488,7 +488,7 @@ public abstract class FileContextMainOperationsBaseTest {
getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AXX), };
if (fc.exists(testDirs[0]) == false) {
if (exists(fc, testDirs[0]) == false) {
for (Path path : testDirs) {
fc.mkdir(path, FsPermission.getDefault(), true);
}
@ -516,7 +516,7 @@ public abstract class FileContextMainOperationsBaseTest {
getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AXX), };
if (fc.exists(testDirs[0]) == false) {
if (exists(fc, testDirs[0]) == false) {
for (Path path : testDirs) {
fc.mkdir(path, FsPermission.getDefault(), true);
}
@ -544,7 +544,7 @@ public abstract class FileContextMainOperationsBaseTest {
getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AXX), };
if (fc.exists(testDirs[0]) == false) {
if (exists(fc, testDirs[0]) == false) {
for (Path path : testDirs) {
fc.mkdir(path, FsPermission.getDefault(), true);
}
@ -570,7 +570,7 @@ public abstract class FileContextMainOperationsBaseTest {
getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AXX), };
if (fc.exists(testDirs[0]) == false) {
if (exists(fc, testDirs[0]) == false) {
for (Path path : testDirs) {
fc.mkdir(path, FsPermission.getDefault(), true);
}
@ -592,7 +592,7 @@ public abstract class FileContextMainOperationsBaseTest {
getTestRootPath(fc, TEST_DIR_AXX),
getTestRootPath(fc, TEST_DIR_AXX), };
if (fc.exists(testDirs[0]) == false) {
if (exists(fc, testDirs[0]) == false) {
for (Path path : testDirs) {
fc.mkdir(path, FsPermission.getDefault(), true);
}
@ -646,7 +646,7 @@ public abstract class FileContextMainOperationsBaseTest {
out.write(data, 0, len);
out.close();
Assert.assertTrue("Exists", fc.exists(path));
Assert.assertTrue("Exists", exists(fc, path));
Assert.assertEquals("Length", len, fc.getFileStatus(path).getLen());
FSDataInputStream in = fc.open(path);
@ -661,7 +661,7 @@ public abstract class FileContextMainOperationsBaseTest {
Assert.assertTrue("Deleted", fc.delete(path, false));
Assert.assertFalse("No longer exists", fc.exists(path));
Assert.assertFalse("No longer exists", exists(fc, path));
}
@ -673,7 +673,7 @@ public abstract class FileContextMainOperationsBaseTest {
createFile(path);
Assert.assertTrue("Exists", fc.exists(path));
Assert.assertTrue("Exists", exists(fc, path));
Assert.assertEquals("Length", data.length, fc.getFileStatus(path).getLen());
try {
@ -687,7 +687,7 @@ public abstract class FileContextMainOperationsBaseTest {
out.write(data, 0, data.length);
out.close();
Assert.assertTrue("Exists", fc.exists(path));
Assert.assertTrue("Exists", exists(fc, path));
Assert.assertEquals("Length", data.length, fc.getFileStatus(path).getLen());
}
@ -695,18 +695,18 @@ public abstract class FileContextMainOperationsBaseTest {
@Test
public void testWriteInNonExistentDirectory() throws IOException {
Path path = getTestRootPath(fc, "test/hadoop/file");
Assert.assertFalse("Parent doesn't exist", fc.exists(path.getParent()));
Assert.assertFalse("Parent doesn't exist", exists(fc, path.getParent()));
createFile(path);
Assert.assertTrue("Exists", fc.exists(path));
Assert.assertTrue("Exists", exists(fc, path));
Assert.assertEquals("Length", data.length, fc.getFileStatus(path).getLen());
Assert.assertTrue("Parent exists", fc.exists(path.getParent()));
Assert.assertTrue("Parent exists", exists(fc, path.getParent()));
}
@Test
public void testDeleteNonExistentFile() throws IOException {
Path path = getTestRootPath(fc, "test/hadoop/file");
Assert.assertFalse("Doesn't exist", fc.exists(path));
Assert.assertFalse("Doesn't exist", exists(fc, path));
Assert.assertFalse("No deletion", fc.delete(path, true));
}
@ -719,9 +719,9 @@ public abstract class FileContextMainOperationsBaseTest {
createFile(file);
fc.mkdir(subdir,FsPermission.getDefault(), true);
Assert.assertTrue("File exists", fc.exists(file));
Assert.assertTrue("Dir exists", fc.exists(dir));
Assert.assertTrue("Subdir exists", fc.exists(subdir));
Assert.assertTrue("File exists", exists(fc, file));
Assert.assertTrue("Dir exists", exists(fc, dir));
Assert.assertTrue("Subdir exists", exists(fc, subdir));
try {
fc.delete(dir, false);
@ -729,23 +729,23 @@ public abstract class FileContextMainOperationsBaseTest {
} catch (IOException e) {
// expected
}
Assert.assertTrue("File still exists", fc.exists(file));
Assert.assertTrue("Dir still exists", fc.exists(dir));
Assert.assertTrue("Subdir still exists", fc.exists(subdir));
Assert.assertTrue("File still exists", exists(fc, file));
Assert.assertTrue("Dir still exists", exists(fc, dir));
Assert.assertTrue("Subdir still exists", exists(fc, subdir));
Assert.assertTrue("Deleted", fc.delete(dir, true));
Assert.assertFalse("File doesn't exist", fc.exists(file));
Assert.assertFalse("Dir doesn't exist", fc.exists(dir));
Assert.assertFalse("Subdir doesn't exist", fc.exists(subdir));
Assert.assertFalse("File doesn't exist", exists(fc, file));
Assert.assertFalse("Dir doesn't exist", exists(fc, dir));
Assert.assertFalse("Subdir doesn't exist", exists(fc, subdir));
}
@Test
public void testDeleteEmptyDirectory() throws IOException {
Path dir = getTestRootPath(fc, "test/hadoop");
fc.mkdir(dir, FsPermission.getDefault(), true);
Assert.assertTrue("Dir exists", fc.exists(dir));
Assert.assertTrue("Dir exists", exists(fc, dir));
Assert.assertTrue("Deleted", fc.delete(dir, false));
Assert.assertFalse("Dir doesn't exist", fc.exists(dir));
Assert.assertFalse("Dir doesn't exist", exists(fc, dir));
}
@Test
@ -952,13 +952,13 @@ public abstract class FileContextMainOperationsBaseTest {
rename(src, dst, true, false, true, options);
Assert.assertFalse("Nested file1 exists",
fc.exists(getTestRootPath(fc, "test/hadoop/dir/file1")));
exists(fc, getTestRootPath(fc, "test/hadoop/dir/file1")));
Assert.assertFalse("Nested file2 exists",
fc.exists(getTestRootPath(fc, "test/hadoop/dir/subdir/file2")));
Assert.assertTrue("Renamed nested file1 exists", fc
.exists(getTestRootPath(fc, "test/new/newdir/file1")));
exists(fc, getTestRootPath(fc, "test/hadoop/dir/subdir/file2")));
Assert.assertTrue("Renamed nested file1 exists",
exists(fc, getTestRootPath(fc, "test/new/newdir/file1")));
Assert.assertTrue("Renamed nested exists",
fc.exists(getTestRootPath(fc, "test/new/newdir/subdir/file2")));
exists(fc, getTestRootPath(fc, "test/new/newdir/subdir/file2")));
}
@Test
@ -1098,8 +1098,8 @@ public abstract class FileContextMainOperationsBaseTest {
fc.rename(src, dst, options);
if (!renameShouldSucceed)
Assert.fail("rename should have thrown exception");
Assert.assertEquals("Source exists", srcExists, fc.exists(src));
Assert.assertEquals("Destination exists", dstExists, fc.exists(dst));
Assert.assertEquals("Source exists", srcExists, exists(fc, src));
Assert.assertEquals("Destination exists", dstExists, exists(fc, dst));
}
private boolean containsPath(Path path, FileStatus[] filteredPaths)
throws IOException {

View File

@ -77,10 +77,10 @@ public abstract class FileContextPermissionBase {
fc.delete(getTestRootPath(fc), true);
}
private void cleanupFile(FileContext theFc, Path name) throws IOException {
Assert.assertTrue(theFc.exists(name));
theFc.delete(name, true);
Assert.assertTrue(!theFc.exists(name));
private void cleanupFile(FileContext fc, Path name) throws IOException {
Assert.assertTrue(exists(fc, name));
fc.delete(name, true);
Assert.assertTrue(!exists(fc, name));
}
@Test

View File

@ -30,6 +30,7 @@ import org.apache.hadoop.fs.CreateFlag;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FSDataInputStream;
import static org.apache.hadoop.fs.FileContextTestHelper.*;
import static org.junit.Assert.*;
import org.junit.Test;
@ -180,7 +181,7 @@ public abstract class FileContextSymlinkBaseTest {
} catch (IOException x) {
// Expected. Need to create testBaseDir2() first.
}
assertFalse(fc.exists(new Path(testBaseDir2())));
assertFalse(exists(fc, new Path(testBaseDir2())));
fc.createSymlink(file, link, true);
readFile(link);
}
@ -231,8 +232,8 @@ public abstract class FileContextSymlinkBaseTest {
assertFalse(fc.getFileStatus(link).isDir());
assertTrue(fc.getFileLinkStatus(link).isSymlink());
assertFalse(fc.getFileLinkStatus(link).isDir());
assertTrue(fc.isFile(link));
assertFalse(fc.isDirectory(link));
assertTrue(isFile(fc, link));
assertFalse(isDir(fc, link));
assertEquals(file.toUri().getPath(), fc.getLinkTarget(link).toString());
}
@ -246,8 +247,8 @@ public abstract class FileContextSymlinkBaseTest {
assertTrue(fc.getFileStatus(link).isDir());
assertTrue(fc.getFileLinkStatus(link).isSymlink());
assertFalse(fc.getFileLinkStatus(link).isDir());
assertFalse(fc.isFile(link));
assertTrue(fc.isDirectory(link));
assertFalse(isFile(fc, link));
assertTrue(isDir(fc, link));
assertEquals(dir.toUri().getPath(), fc.getLinkTarget(link).toString());
}
@ -312,8 +313,8 @@ public abstract class FileContextSymlinkBaseTest {
throws IOException {
Path dir = new Path(testBaseDir1());
// isFile/Directory
assertTrue(fc.isFile(linkAbs));
assertFalse(fc.isDirectory(linkAbs));
assertTrue(isFile(fc, linkAbs));
assertFalse(isDir(fc, linkAbs));
// Check getFileStatus
assertFalse(fc.getFileStatus(linkAbs).isSymlink());
@ -542,8 +543,8 @@ public abstract class FileContextSymlinkBaseTest {
Path linkToDir = new Path(testBaseDir2(), "linkToDir");
createAndWriteFile(file);
fc.createSymlink(dir1, linkToDir, false);
assertFalse(fc.isFile(linkToDir));
assertTrue(fc.isDirectory(linkToDir));
assertFalse(isFile(fc, linkToDir));
assertTrue(isDir(fc, linkToDir));
assertTrue(fc.getFileStatus(linkToDir).isDir());
assertTrue(fc.getFileLinkStatus(linkToDir).isSymlink());
}
@ -556,13 +557,13 @@ public abstract class FileContextSymlinkBaseTest {
Path fileViaLink = new Path(linkToDir, "file");
fc.createSymlink(dir, linkToDir, false);
createAndWriteFile(fileViaLink);
assertTrue(fc.isFile(fileViaLink));
assertFalse(fc.isDirectory(fileViaLink));
assertTrue(isFile(fc, fileViaLink));
assertFalse(isDir(fc, fileViaLink));
assertFalse(fc.getFileLinkStatus(fileViaLink).isSymlink());
assertFalse(fc.getFileStatus(fileViaLink).isDir());
readFile(fileViaLink);
fc.delete(fileViaLink, true);
assertFalse(fc.exists(fileViaLink));
assertFalse(exists(fc, fileViaLink));
}
@Test
@ -576,8 +577,8 @@ public abstract class FileContextSymlinkBaseTest {
fc.mkdir(subDirViaLink, FileContext.DEFAULT_PERM, true);
assertTrue(fc.getFileStatus(subDirViaLink).isDir());
fc.delete(subDirViaLink, false);
assertFalse(fc.exists(subDirViaLink));
assertFalse(fc.exists(subDir));
assertFalse(exists(fc, subDirViaLink));
assertFalse(exists(fc, subDir));
}
@Test
@ -595,7 +596,7 @@ public abstract class FileContextSymlinkBaseTest {
createAndWriteFile(file);
fc.createSymlink(dir1, linkToDir, false);
fc.createSymlink(fileViaLink, linkToFile, false);
assertTrue(fc.isFile(linkToFile));
assertTrue(isFile(fc, linkToFile));
assertTrue(fc.getFileLinkStatus(linkToFile).isSymlink());
readFile(linkToFile);
assertEquals(fileSize, fc.getFileStatus(linkToFile).getLen());
@ -648,8 +649,8 @@ public abstract class FileContextSymlinkBaseTest {
createAndWriteFile(file);
fc.createSymlink(dir1, linkToDir, false);
fc.createSymlink(linkToDir, linkToLink, false);
assertTrue(fc.isFile(fileViaLink));
assertFalse(fc.isDirectory(fileViaLink));
assertTrue(isFile(fc, fileViaLink));
assertFalse(isDir(fc, fileViaLink));
assertFalse(fc.getFileLinkStatus(fileViaLink).isSymlink());
assertFalse(fc.getFileStatus(fileViaLink).isDir());
readFile(fileViaLink);
@ -766,9 +767,9 @@ public abstract class FileContextSymlinkBaseTest {
createAndWriteFile(file);
fc.createSymlink(dir1, linkToDir, false);
fc.rename(fileViaLink, fileNewViaLink, Rename.OVERWRITE);
assertFalse(fc.exists(fileViaLink));
assertFalse(fc.exists(file));
assertTrue(fc.exists(fileNewViaLink));
assertFalse(exists(fc, fileViaLink));
assertFalse(exists(fc, file));
assertTrue(exists(fc, fileNewViaLink));
}
@Test
@ -831,9 +832,9 @@ public abstract class FileContextSymlinkBaseTest {
assertTrue(unwrapException(e) instanceof FileAlreadyExistsException);
}
// Check the rename didn't happen
assertTrue(fc.isFile(file));
assertTrue(fc.exists(link));
assertTrue(fc.getFileLinkStatus(link).isSymlink());
assertTrue(isFile(fc, file));
assertTrue(exists(fc, link));
assertTrue(isSymlink(fc, link));
assertEquals(file, fc.getLinkTarget(link));
try {
fc.rename(link, file, Rename.OVERWRITE);
@ -842,9 +843,9 @@ public abstract class FileContextSymlinkBaseTest {
assertTrue(unwrapException(e) instanceof FileAlreadyExistsException);
}
// Check the rename didn't happen
assertTrue(fc.isFile(file));
assertTrue(fc.exists(link));
assertTrue(fc.getFileLinkStatus(link).isSymlink());
assertTrue(isFile(fc, file));
assertTrue(exists(fc, link));
assertTrue(isSymlink(fc, link));
assertEquals(file, fc.getLinkTarget(link));
}
@ -869,9 +870,9 @@ public abstract class FileContextSymlinkBaseTest {
assertTrue(unwrapException(e) instanceof FileAlreadyExistsException);
}
// Check the rename didn't happen
assertTrue(fc.isDirectory(dir));
assertTrue(fc.exists(link));
assertTrue(fc.getFileLinkStatus(link).isSymlink());
assertTrue(isDir(fc, dir));
assertTrue(exists(fc, link));
assertTrue(isSymlink(fc, link));
assertEquals(dir, fc.getLinkTarget(link));
try {
fc.rename(link, dir, Rename.OVERWRITE);
@ -880,9 +881,9 @@ public abstract class FileContextSymlinkBaseTest {
assertTrue(unwrapException(e) instanceof FileAlreadyExistsException);
}
// Check the rename didn't happen
assertTrue(fc.isDirectory(dir));
assertTrue(fc.exists(link));
assertTrue(fc.getFileLinkStatus(link).isSymlink());
assertTrue(isDir(fc, dir));
assertTrue(exists(fc, link));
assertTrue(isSymlink(fc, link));
assertEquals(dir, fc.getLinkTarget(link));
}

View File

@ -18,6 +18,7 @@
package org.apache.hadoop.fs;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.EnumSet;
import org.apache.hadoop.fs.Options.CreateOpts;
@ -115,5 +116,33 @@ public final class FileContextTestHelper {
public static void createFileNonRecursive(FileContext fc, Path path)
throws IOException {
createFile(fc, path, DEFAULT_NUM_BLOCKS, CreateOpts.donotCreateParent());
}
}
public static boolean exists(FileContext fc, Path p) throws IOException {
return fc.util().exists(p);
}
public static boolean isFile(FileContext fc, Path p) throws IOException {
try {
return !fc.getFileStatus(p).isDir();
} catch (FileNotFoundException e) {
return false;
}
}
public static boolean isDir(FileContext fc, Path p) throws IOException {
try {
return fc.getFileStatus(p).isDir();
} catch (FileNotFoundException e) {
return false;
}
}
public static boolean isSymlink(FileContext fc, Path p) throws IOException {
try {
return fc.getFileLinkStatus(p).isSymlink();
} catch (FileNotFoundException e) {
return false;
}
}
}

View File

@ -86,12 +86,12 @@ public abstract class FileContextURIBase {
// Create a file on fc2's file system using fc1
Path testPath = qualifiedPath(f, fc2);
// Ensure file does not exist
Assert.assertFalse(fc2.exists(testPath));
Assert.assertFalse(exists(fc2, testPath));
// Now create file
createFile(fc1, testPath);
// Ensure fc2 has the created file
Assert.assertTrue(fc2.exists(testPath));
Assert.assertTrue(exists(fc2, testPath));
}
}
@ -103,7 +103,7 @@ public abstract class FileContextURIBase {
Path testPath = qualifiedPath(fileName, fc2);
// Ensure file does not exist
Assert.assertFalse(fc2.exists(testPath));
Assert.assertFalse(exists(fc2, testPath));
// Create a file on fc2's file system using fc1
createFile(fc1, testPath);
@ -120,7 +120,7 @@ public abstract class FileContextURIBase {
Path testPath = qualifiedPath(fileName, fc2);
// Ensure file does not exist
Assert.assertFalse(fc2.exists(testPath));
Assert.assertFalse(exists(fc2, testPath));
// Create a file on fc2's file system using fc1
createFile(fc1, testPath);
@ -134,7 +134,7 @@ public abstract class FileContextURIBase {
}
// Ensure fc2 has the created file
Assert.assertTrue(fc2.exists(testPath));
Assert.assertTrue(exists(fc2, testPath));
}
@Test
@ -144,15 +144,15 @@ public abstract class FileContextURIBase {
Path testPath = qualifiedPath(fileName, fc2);
// Ensure file does not exist
Assert.assertFalse(fc2.exists(testPath));
Assert.assertFalse(exists(fc2, testPath));
// Create a file on fc2's file system using fc1
createFile(fc1, testPath);
// Ensure using fc2 that file is created
Assert.assertTrue(fc2.isDirectory(testPath.getParent()));
Assert.assertTrue(isDir(fc2, testPath.getParent()));
Assert.assertEquals("testDir", testPath.getParent().getName());
Assert.assertTrue(fc2.exists(testPath));
Assert.assertTrue(exists(fc2, testPath));
}
@ -164,17 +164,17 @@ public abstract class FileContextURIBase {
Path subDirPath = qualifiedPath("dir0", fc2);
// Ensure that testPath does not exist in fc1
Assert.assertFalse(fc1.exists(path));
Assert.assertFalse(fc1.isFile(path));
Assert.assertFalse(fc1.isDirectory(path));
Assert.assertFalse(exists(fc1, path));
Assert.assertFalse(isFile(fc1, path));
Assert.assertFalse(isDir(fc1, path));
// Create a directory on fc2's file system using fc1
fc1.mkdir(path, FsPermission.getDefault(), true);
// Ensure fc2 has directory
Assert.assertTrue(fc2.isDirectory(path));
Assert.assertTrue(fc2.exists(path));
Assert.assertFalse(fc2.isFile(path));
Assert.assertTrue(isDir(fc2, path));
Assert.assertTrue(exists(fc2, path));
Assert.assertFalse(isFile(fc2, path));
// Test to create same dir twice, (HDFS mkdir is similar to mkdir -p )
fc1.mkdir(subDirPath, FsPermission.getDefault(), true);
@ -186,17 +186,17 @@ public abstract class FileContextURIBase {
// Check parent dir
Path parentDir = path.getParent();
Assert.assertTrue(fc2.exists(parentDir));
Assert.assertFalse(fc2.isFile(parentDir));
Assert.assertTrue(exists(fc2, parentDir));
Assert.assertFalse(isFile(fc2, parentDir));
// Check parent parent dir
Path grandparentDir = parentDir.getParent();
Assert.assertTrue(fc2.exists(grandparentDir));
Assert.assertFalse(fc2.isFile(grandparentDir));
Assert.assertTrue(exists(fc2, grandparentDir));
Assert.assertFalse(isFile(fc2, grandparentDir));
// Negative test cases
Assert.assertFalse(fc2.exists(falsePath));
Assert.assertFalse(fc2.isDirectory(falsePath));
Assert.assertFalse(exists(fc2, falsePath));
Assert.assertFalse(isDir(fc2, falsePath));
// TestCase - Create multiple directories
String dirNames[] = {
@ -210,13 +210,13 @@ public abstract class FileContextURIBase {
// Create a file on fc2's file system using fc1
Path testPath = qualifiedPath(f, fc2);
// Ensure file does not exist
Assert.assertFalse(fc2.exists(testPath));
Assert.assertFalse(exists(fc2, testPath));
// Now create directory
fc1.mkdir(testPath, FsPermission.getDefault(), true);
// Ensure fc2 has the created directory
Assert.assertTrue(fc2.exists(testPath));
Assert.assertTrue(fc2.isDirectory(testPath));
Assert.assertTrue(exists(fc2, testPath));
Assert.assertTrue(isDir(fc2, testPath));
}
}
@ -224,9 +224,9 @@ public abstract class FileContextURIBase {
@Test
public void testMkdirsFailsForSubdirectoryOfExistingFile() throws Exception {
Path testDir = qualifiedPath("test/hadoop", fc2);
Assert.assertFalse(fc2.exists(testDir));
Assert.assertFalse(exists(fc2, testDir));
fc2.mkdir(testDir, FsPermission.getDefault(), true);
Assert.assertTrue(fc2.exists(testDir));
Assert.assertTrue(exists(fc2, testDir));
// Create file on fc1 using fc2 context
createFile(fc1, qualifiedPath("test/hadoop/file", fc2));
@ -238,7 +238,7 @@ public abstract class FileContextURIBase {
} catch (IOException e) {
// expected
}
Assert.assertFalse(fc1.exists(testSubDir));
Assert.assertFalse(exists(fc1, testSubDir));
Path testDeepSubDir = qualifiedPath("test/hadoop/file/deep/sub/dir", fc1);
try {
@ -247,7 +247,7 @@ public abstract class FileContextURIBase {
} catch (IOException e) {
// expected
}
Assert.assertFalse(fc1.exists(testDeepSubDir));
Assert.assertFalse(exists(fc1, testDeepSubDir));
}
@ -265,11 +265,11 @@ public abstract class FileContextURIBase {
fc1.mkdir(existingPath, FsPermission.getDefault(), true);
// Ensure fc2 has directory
Assert.assertTrue(fc2.isDirectory(existingPath));
Assert.assertTrue(fc2.isDirectory(pathToRootDir));
Assert.assertTrue(isDir(fc2, existingPath));
Assert.assertTrue(isDir(fc2, pathToRootDir));
// Negative test case
Assert.assertFalse(fc2.isDirectory(nonExistingPath));
Assert.assertFalse(isDir(fc2, nonExistingPath));
}
@ -278,19 +278,19 @@ public abstract class FileContextURIBase {
Path testPath = qualifiedPath("testFile", fc2);
// Ensure file does not exist
Assert.assertFalse(fc2.exists(testPath));
Assert.assertFalse(exists(fc2, testPath));
// First create a file on file system using fc1
createFile(fc1, testPath);
// Ensure file exist
Assert.assertTrue(fc2.exists(testPath));
Assert.assertTrue(exists(fc2, testPath));
// Delete file using fc2
fc2.delete(testPath, false);
// Ensure fc2 does not have deleted file
Assert.assertFalse(fc2.exists(testPath));
Assert.assertFalse(exists(fc2, testPath));
}
@ -301,7 +301,7 @@ public abstract class FileContextURIBase {
// TestCase1 : Test delete on file never existed
// Ensure file does not exist
Assert.assertFalse(fc2.exists(testPath));
Assert.assertFalse(exists(fc2, testPath));
// Delete on non existing file should return false
Assert.assertFalse(fc2.delete(testPath, false));
@ -310,12 +310,12 @@ public abstract class FileContextURIBase {
// Create a file on fc2's file system using fc1
createFile(fc1, testPath);
// Ensure file exist
Assert.assertTrue(fc2.exists(testPath));
Assert.assertTrue(exists(fc2, testPath));
// Delete test file, deleting existing file should return true
Assert.assertTrue(fc2.delete(testPath, false));
// Ensure file does not exist
Assert.assertFalse(fc2.exists(testPath));
Assert.assertFalse(exists(fc2, testPath));
// Delete on non existing file should return false
Assert.assertFalse(fc2.delete(testPath, false));
@ -328,7 +328,7 @@ public abstract class FileContextURIBase {
// TestCase1 : Test delete on file never existed
// Ensure file does not exist
Assert.assertFalse(fc2.exists(testPath));
Assert.assertFalse(exists(fc2, testPath));
// Delete on non existing file should return false
Assert.assertFalse(fc2.delete(testPath, false));
@ -337,12 +337,12 @@ public abstract class FileContextURIBase {
// Create a file on fc2's file system using fc1
createFile(fc1, testPath);
// Ensure file exist
Assert.assertTrue(fc2.exists(testPath));
Assert.assertTrue(exists(fc2, testPath));
// Delete test file, deleting existing file should return true
Assert.assertTrue(fc2.delete(testPath, false));
// Ensure file does not exist
Assert.assertFalse(fc2.exists(testPath));
Assert.assertFalse(exists(fc2, testPath));
// Delete on non existing file should return false
Assert.assertFalse(fc2.delete(testPath, false));
@ -353,19 +353,19 @@ public abstract class FileContextURIBase {
String dirName = "dirTest";
Path testDirPath = qualifiedPath(dirName, fc2);
// Ensure directory does not exist
Assert.assertFalse(fc2.exists(testDirPath));
Assert.assertFalse(exists(fc2, testDirPath));
// Create a directory on fc2's file system using fc1
fc1.mkdir(testDirPath, FsPermission.getDefault(), true);
// Ensure dir is created
Assert.assertTrue(fc2.exists(testDirPath));
Assert.assertTrue(fc2.isDirectory(testDirPath));
Assert.assertTrue(exists(fc2, testDirPath));
Assert.assertTrue(isDir(fc2, testDirPath));
fc2.delete(testDirPath, true);
// Ensure that directory is deleted
Assert.assertFalse(fc2.isDirectory(testDirPath));
Assert.assertFalse(isDir(fc2, testDirPath));
// TestCase - Create and delete multiple directories
String dirNames[] = {
@ -379,18 +379,18 @@ public abstract class FileContextURIBase {
// Create a file on fc2's file system using fc1
Path testPath = qualifiedPath(f, fc2);
// Ensure file does not exist
Assert.assertFalse(fc2.exists(testPath));
Assert.assertFalse(exists(fc2, testPath));
// Now create directory
fc1.mkdir(testPath, FsPermission.getDefault(), true);
// Ensure fc2 has the created directory
Assert.assertTrue(fc2.exists(testPath));
Assert.assertTrue(fc2.isDirectory(testPath));
Assert.assertTrue(exists(fc2, testPath));
Assert.assertTrue(isDir(fc2, testPath));
// Delete dir
Assert.assertTrue(fc2.delete(testPath, true));
// verify if directory is deleted
Assert.assertFalse(fc2.exists(testPath));
Assert.assertFalse(fc2.isDirectory(testPath));
Assert.assertFalse(exists(fc2, testPath));
Assert.assertFalse(isDir(fc2, testPath));
}
}
@ -401,7 +401,7 @@ public abstract class FileContextURIBase {
// TestCase1 : Test delete on directory never existed
// Ensure directory does not exist
Assert.assertFalse(fc2.exists(testPath));
Assert.assertFalse(exists(fc2, testPath));
// Delete on non existing directory should return false
Assert.assertFalse(fc2.delete(testPath, false));
@ -411,12 +411,12 @@ public abstract class FileContextURIBase {
fc1.mkdir(testPath, FsPermission.getDefault(), true);
// Ensure dir exist
Assert.assertTrue(fc2.exists(testPath));
Assert.assertTrue(exists(fc2, testPath));
// Delete test file, deleting existing file should return true
Assert.assertTrue(fc2.delete(testPath, false));
// Ensure file does not exist
Assert.assertFalse(fc2.exists(testPath));
Assert.assertFalse(exists(fc2, testPath));
// Delete on non existing file should return false
Assert.assertFalse(fc2.delete(testPath, false));
}
@ -496,7 +496,7 @@ public abstract class FileContextURIBase {
for (String d : dirs) {
testDirs.add(qualifiedPath(d, fc2));
}
Assert.assertFalse(fc1.exists(testDirs.get(0)));
Assert.assertFalse(exists(fc1, testDirs.get(0)));
for (Path path : testDirs) {
fc1.mkdir(path, FsPermission.getDefault(), true);

View File

@ -82,8 +82,8 @@ public class TestFileContextDeleteOnExit {
FileContext.FINALIZER.start();
FileContext.FINALIZER.join();
checkDeleteOnExitData(0, fc, new Path[0]);
Assert.assertFalse(fc.exists(file1));
Assert.assertFalse(fc.exists(file2));
Assert.assertFalse(fc.exists(dir));
Assert.assertFalse(exists(fc, file1));
Assert.assertFalse(exists(fc, file2));
Assert.assertFalse(exists(fc, dir));
}
}

View File

@ -196,6 +196,15 @@ public class TestPath extends TestCase {
new Path("file").makeQualified(defaultUri, new Path(wd)));
}
public void testGetName() {
assertEquals("", new Path("/").getName());
assertEquals("foo", new Path("foo").getName());
assertEquals("foo", new Path("/foo").getName());
assertEquals("foo", new Path("/foo/").getName());
assertEquals("bar", new Path("/foo/bar").getName());
assertEquals("bar", new Path("hdfs://host/foo/bar").getName());
}
public void testAvroReflect() throws Exception {
AvroTestUtil.testReflect
(new Path("foo"),