Browse Source

HADOOP-15522. Deprecate Shell#ReadLink by using native java code. Contributed by Giovanni Matteo Fumarola.

Inigo Goiri 7 years ago
parent
commit
866646eb3b

+ 12 - 9
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileUtil.java

@@ -196,22 +196,25 @@ public class FileUtil {
    *         a symlink.
    */
   public static String readLink(File f) {
-    /* NB: Use readSymbolicLink in java.nio.file.Path once available. Could
-     * use getCanonicalPath in File to get the target of the symlink but that
-     * does not indicate if the given path refers to a symlink.
-     */
 
     if (f == null) {
       LOG.warn("Can not read a null symLink");
       return "";
     }
 
-    try {
-      return Shell.execCommand(
-          Shell.getReadlinkCommand(f.toString())).trim();
-    } catch (IOException x) {
-      return "";
+    if (Files.isSymbolicLink(f.toPath())) {
+      java.nio.file.Path p = null;
+      try {
+        p = Files.readSymbolicLink(f.toPath());
+      } catch (Exception e) {
+        LOG.warn("Exception while reading the symbolic link "
+            + f.getAbsolutePath() + ". Exception= " + e.getMessage());
+        return "";
+      }
+      return p.toAbsolutePath().toString();
     }
+    LOG.warn("The file " + f.getAbsolutePath() + " is not a symbolic link.");
+    return "";
   }
 
   /*

+ 7 - 1
hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/Shell.java

@@ -309,7 +309,13 @@ public abstract class Shell {
        : new String[] { "ln", "-s", target, link };
   }
 
-  /** Return a command to read the target of the a symbolic link. */
+  /**
+   * Return a command to read the target of the a symbolic link.
+   *
+   * Deprecated and likely to be deleted in the near future. Please use
+   * FileUtil.symlink().
+   */
+  @Deprecated
   public static String[] getReadlinkCommand(String link) {
     return WINDOWS ?
         new String[] { getWinUtilsPath(), "readlink", link }