HADOOP-13885. Implement getLinkTarget for ViewFileSystem. Contributed by Manoj 
Govindassamy.


Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo
Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/511d39e0
Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/511d39e0
Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/511d39e0

Branch: refs/heads/YARN-5734
Commit: 511d39e0740f36bf937e7bcf974e1050f0e7c1e0
Parents: 603cbcd
Author: Andrew Wang <w...@apache.org>
Authored: Mon Jan 9 18:01:10 2017 -0800
Committer: Andrew Wang <w...@apache.org>
Committed: Mon Jan 9 18:01:37 2017 -0800

----------------------------------------------------------------------
 .../hadoop/fs/viewfs/ChRootedFileSystem.java    |  5 ++
 .../apache/hadoop/fs/viewfs/ViewFileSystem.java | 11 +++
 .../fs/viewfs/ViewFileSystemBaseTest.java       | 73 ++++++++++++++++++++
 3 files changed, 89 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hadoop/blob/511d39e0/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/viewfs/ChRootedFileSystem.java
----------------------------------------------------------------------
diff --git 
a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/viewfs/ChRootedFileSystem.java
 
b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/viewfs/ChRootedFileSystem.java
index 272433f..fe2f8a9 100644
--- 
a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/viewfs/ChRootedFileSystem.java
+++ 
b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/viewfs/ChRootedFileSystem.java
@@ -234,6 +234,11 @@ class ChRootedFileSystem extends FilterFileSystem {
   }
 
   @Override
+  public Path getLinkTarget(Path f) throws IOException {
+    return super.getLinkTarget(fullPath(f));
+  }
+
+  @Override
   public void access(Path path, FsAction mode) throws AccessControlException,
       FileNotFoundException, IOException {
     super.access(fullPath(path), mode);

http://git-wip-us.apache.org/repos/asf/hadoop/blob/511d39e0/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/viewfs/ViewFileSystem.java
----------------------------------------------------------------------
diff --git 
a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/viewfs/ViewFileSystem.java
 
b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/viewfs/ViewFileSystem.java
index 8be666c..e16da64 100644
--- 
a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/viewfs/ViewFileSystem.java
+++ 
b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/viewfs/ViewFileSystem.java
@@ -876,6 +876,17 @@ public class ViewFileSystem extends FileSystem {
     }
   }
 
+  @Override
+  public Path getLinkTarget(Path path) throws IOException {
+    InodeTree.ResolveResult<FileSystem> res;
+    try {
+      res = fsState.resolve(getUriPath(path), true);
+    } catch (FileNotFoundException e) {
+      throw new NotInMountpointException(path, "getLinkTarget");
+    }
+    return res.targetFileSystem.getLinkTarget(res.remainingPath);
+  }
+
   /**
    * An instance of this class represents an internal dir of the viewFs
    * that is internal dir of the mount table.

http://git-wip-us.apache.org/repos/asf/hadoop/blob/511d39e0/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/ViewFileSystemBaseTest.java
----------------------------------------------------------------------
diff --git 
a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/ViewFileSystemBaseTest.java
 
b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/ViewFileSystemBaseTest.java
index 9a0bf02..61295b4 100644
--- 
a/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/ViewFileSystemBaseTest.java
+++ 
b/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/fs/viewfs/ViewFileSystemBaseTest.java
@@ -51,6 +51,7 @@ import org.apache.hadoop.security.AccessControlException;
 import org.apache.hadoop.security.Credentials;
 import org.apache.hadoop.security.UserGroupInformation;
 import org.apache.hadoop.security.token.Token;
+import org.junit.Assume;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import static org.apache.hadoop.fs.FileSystemTestHelper.*;
@@ -1137,4 +1138,76 @@ abstract public class ViewFileSystemBaseTest {
         "the mounted FileSystem!",
         usedSpaceByPathViaTargetFs, usedSpaceByPathViaViewFs);
   }
+
+  @Test
+  public void testLinkTarget() throws Exception {
+
+    Assume.assumeTrue(fsTarget.supportsSymlinks() &&
+        fsTarget.areSymlinksEnabled());
+
+    // Symbolic link
+    final String targetFileName = "debug.log";
+    final String linkFileName = "debug.link";
+    final Path targetFile = new Path(targetTestRoot, targetFileName);
+    final Path symLink = new Path(targetTestRoot, linkFileName);
+
+    FileSystemTestHelper.createFile(fsTarget, targetFile);
+    fsTarget.createSymlink(targetFile, symLink, false);
+
+    final Path mountTargetRootPath = new Path("/targetRoot");
+    final Path mountTargetSymLinkPath = new Path(mountTargetRootPath,
+        linkFileName);
+    final Path expectedMountLinkTarget = fsTarget.makeQualified(
+        new Path(targetTestRoot, targetFileName));
+    final Path actualMountLinkTarget = fsView.getLinkTarget(
+        mountTargetSymLinkPath);
+
+    assertEquals("Resolved link target path not matching!",
+        expectedMountLinkTarget, actualMountLinkTarget);
+
+    // Relative symbolic link
+    final String relativeFileName = "dir2/../" + targetFileName;
+    final String link2FileName = "dir2/rel.link";
+    final Path relTargetFile = new Path(targetTestRoot, relativeFileName);
+    final Path relativeSymLink = new Path(targetTestRoot, link2FileName);
+    fsTarget.createSymlink(relTargetFile, relativeSymLink, true);
+
+    final Path mountTargetRelativeSymLinkPath = new Path(mountTargetRootPath,
+        link2FileName);
+    final Path expectedMountRelLinkTarget = fsTarget.makeQualified(
+        new Path(targetTestRoot, relativeFileName));
+    final Path actualMountRelLinkTarget = fsView.getLinkTarget(
+        mountTargetRelativeSymLinkPath);
+
+    assertEquals("Resolved relative link target path not matching!",
+        expectedMountRelLinkTarget, actualMountRelLinkTarget);
+
+    try {
+      fsView.getLinkTarget(new Path("/linkToAFile"));
+      fail("Resolving link target for a ViewFs mount link should fail!");
+    } catch (Exception e) {
+      LOG.info("Expected exception: " + e);
+      assertThat(e.getMessage(),
+          containsString("not a symbolic link"));
+    }
+
+    try {
+      fsView.getLinkTarget(fsView.makeQualified(
+          new Path(mountTargetRootPath, targetFileName)));
+      fail("Resolving link target for a non sym link should fail!");
+    } catch (Exception e) {
+      LOG.info("Expected exception: " + e);
+      assertThat(e.getMessage(),
+          containsString("not a symbolic link"));
+    }
+
+    try {
+      fsView.getLinkTarget(new Path("/targetRoot/non-existing-file"));
+      fail("Resolving link target for a non existing link should fail!");
+    } catch (Exception e) {
+      LOG.info("Expected exception: " + e);
+      assertThat(e.getMessage(),
+          containsString("File does not exist:"));
+    }
+  }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: common-commits-unsubscr...@hadoop.apache.org
For additional commands, e-mail: common-commits-h...@hadoop.apache.org

Reply via email to