Explorar o código

HADOOP-2512. Fix error stream handling in Shell. Use exit code to
detect shell command errors in RawLocalFileSystem. (Raghu Angadi)


git-svn-id: https://svn.apache.org/repos/asf/lucene/hadoop/trunk@610124 13f79535-47bb-0310-9956-ffa450edef68

Raghu Angadi %!s(int64=17) %!d(string=hai) anos
pai
achega
6d8a04a906

+ 3 - 0
CHANGES.txt

@@ -354,6 +354,9 @@ Trunk (unreleased changes)
     Configuration changes to hadoop-default.xml:
       remove mapred.child.heap.size
 
+    HADOOP-2512. Fix error stream handling in Shell. Use exit code to
+    detect shell command errors in RawLocalFileSystem. (Raghu Angadi)
+
 Release 0.15.2 - 2008-01-02
 
   BUG FIXES

+ 10 - 7
src/java/org/apache/hadoop/fs/RawLocalFileSystem.java

@@ -397,6 +397,7 @@ public class RawLocalFileSystem extends FileSystem {
 
     /// loads permissions, owner, and group from `ls -ld`
     private void loadPermissionInfo() {
+      IOException e = null;
       try {
         StringTokenizer t = new StringTokenizer(
             execCommand(new File(getPath().toUri()), 
@@ -411,16 +412,18 @@ public class RawLocalFileSystem extends FileSystem {
         t.nextToken();
         setOwner(t.nextToken());
         setGroup(t.nextToken());
-      } catch (IOException e) {
-        if (e.getMessage().contains("No such file or directory")) {                                    
-          /* XXX This is a temporary hack till HADOOP-2344 goes in.
-           * will fix it soon.
-           */
+      } catch (Shell.ExitCodeException ioe) {
+        if (ioe.getExitCode() != 1) {
+          e = ioe;
+        } else {
           setPermission(null);
           setOwner(null);
           setGroup(null);
-        } else {
-          //this is not expected
+        }
+      } catch (IOException ioe) {
+        e = ioe;
+      } finally {
+        if (e != null) {
           throw new RuntimeException("Error while running command to get " +
                                      "file permissions : " + 
                                      StringUtils.stringifyException(e));

+ 26 - 14
src/java/org/apache/hadoop/util/Shell.java

@@ -130,7 +130,7 @@ abstract public class Shell {
           String line = errReader.readLine();
           while((line != null) && !isInterrupted()) {
             errMsg.append(line);
-            errMsg.append(System.getProperty("line.seperator"));
+            errMsg.append(System.getProperty("line.separator"));
             line = errReader.readLine();
           }
         } catch(IOException ioe) {
@@ -150,13 +150,16 @@ abstract public class Shell {
       }
       // wait for the process to finish and check the exit code
       exitCode = process.waitFor();
-      if (exitCode != 0) {
-        if (errMsg.length() == 0) {
-          errMsg.append("Command exit with status code " + exitCode);
-        }
-        throw new IOException(errMsg.toString());
+      try {
+        // make sure that the error thread exits
+        errThread.join();
+      } catch (InterruptedException ie) {
+        LOG.warn("Interrupted while reading the error stream", ie);
       }
       completed = true;
+      if (exitCode != 0) {
+        throw new ExitCodeException(exitCode, errMsg.toString());
+      }
     } catch (InterruptedException ie) {
       throw new IOException(ie.toString());
     } finally {
@@ -166,14 +169,7 @@ abstract public class Shell {
       } catch (IOException ioe) {
         LOG.warn("Error while closing the input stream", ioe);
       }
-      if (completed) {
-        try {
-          // make sure that the error thread exits
-          errThread.join();
-        } catch (InterruptedException ie) {
-          LOG.warn("Interrupted while reading the error stream", ie);
-        }
-      } else {
+      if (!completed) {
         errThread.interrupt();
       }
       try {
@@ -207,6 +203,22 @@ abstract public class Shell {
     return exitCode;
   }
 
+  /**
+   * This is an IOException with exit code added.
+   */
+  public static class ExitCodeException extends IOException {
+    int exitCode;
+    
+    public ExitCodeException(int exitCode, String message) {
+      super(message);
+      this.exitCode = exitCode;
+    }
+    
+    public int getExitCode() {
+      return exitCode;
+    }
+  }
+  
   /**
    * A simple shell command executor.
    *