Selaa lähdekoodia

svn merge -c 1092519 from trunk for HADOOP-7207.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/yahoo-merge@1126776 13f79535-47bb-0310-9956-ffa450edef68
Tsz-wo Sze 14 vuotta sitten
vanhempi
commit
5eb4a10f84

+ 2 - 0
CHANGES.txt

@@ -48,6 +48,8 @@ Trunk (unreleased changes)
     HADOOP-7216. Add FsCommand.runAll() with deprecated annotation for the
     transition of Command base class improvement.  (Daryn Sharp via szetszwo)
 
+    HADOOP-7207. fs member of FSShell is not really needed (boryas)
+
     HADOOP-7231. Fix synopsis for -count. (Daryn Sharp via eli).
 
 Release 0.22.0 - Unreleased

+ 23 - 13
src/java/org/apache/hadoop/fs/FsShell.java

@@ -56,7 +56,7 @@ import org.apache.hadoop.util.ToolRunner;
 @InterfaceAudience.Private
 public class FsShell extends Configured implements Tool {
 
-  protected FileSystem fs;
+  private FileSystem fs;
   private Trash trash;
   protected CommandFactory commandFactory;
 
@@ -88,14 +88,22 @@ public class FsShell extends Configured implements Tool {
     commandFactory = new CommandFactory();
   }
   
-  protected void init() throws IOException {
-    getConf().setQuietMode(true);
-    if (this.fs == null) {
-     this.fs = FileSystem.get(getConf());
-    }
+  protected FileSystem getFS() throws IOException {
+    if(fs == null)
+      fs = FileSystem.get(getConf());
+    
+    return fs;
+  }
+  
+  protected Trash getTrash() throws IOException {
     if (this.trash == null) {
       this.trash = new Trash(getConf());
     }
+    return this.trash;
+  }
+  
+  protected void init() throws IOException {
+    getConf().setQuietMode(true);
   }
 
   
@@ -506,11 +514,12 @@ public class FsShell extends Configured implements Tool {
       System.out.flush();
 
       boolean printWarning = false;
-      FileStatus status = fs.getFileStatus(f);
+      FileSystem pFS = f.getFileSystem(getConf());
+      FileStatus status = pFS.getFileStatus(f);
       long len = status.getLen();
 
       for(boolean done = false; !done; ) {
-        BlockLocation[] locations = fs.getFileBlockLocations(status, 0, len);
+        BlockLocation[] locations = pFS.getFileBlockLocations(status, 0, len);
         int i = 0;
         for(; i < locations.length && 
           locations[i].getHosts().length == rep; i++)
@@ -1080,7 +1089,8 @@ public class FsShell extends Configured implements Tool {
     //
     if (argv.length > 3) {
       Path dst = new Path(dest);
-      if (!fs.isDirectory(dst)) {
+      FileSystem pFS = dst.getFileSystem(conf);
+      if (!pFS.isDirectory(dst)) {
         throw new IOException("When copying multiple files, " 
                               + "destination " + dest + " should be a directory.");
       }
@@ -1188,15 +1198,15 @@ public class FsShell extends Configured implements Tool {
   }
 
   private void expunge() throws IOException {
-    trash.expunge();
-    trash.checkpoint();
+    getTrash().expunge();
+    getTrash().checkpoint();
   }
 
   /**
    * Returns the Trash object associated with this shell.
    */
-  public Path getCurrentTrashDir() {
-    return trash.getCurrentTrashDir();
+  public Path getCurrentTrashDir() throws IOException {
+    return getTrash().getCurrentTrashDir();
   }
 
   /**

+ 50 - 2
src/test/core/org/apache/hadoop/fs/TestFsShellReturnCode.java

@@ -271,6 +271,54 @@ public class TestFsShellReturnCode {
     }
   }
   
+  @Test
+  public void testInvalidDefautlFS() throws Exception {
+    // if default fs doesn't exist or is invalid, but the path provided in 
+    // arguments is valid - fsshell should work
+    FsShell shell = new FsShell();
+    Configuration conf = new Configuration();
+    FsConfig.setDefaultFS(conf, "hhhh://doesnotexist/");
+    shell.setConf(conf);
+    String [] args = new String[2];
+    args[0] = "-ls";
+    args[1] = "file:///"; // this is valid, so command should run
+    int res = shell.run(args);
+    System.out.println("res =" + res);
+    shell.setConf(conf);
+    final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+    final PrintStream out = new PrintStream(bytes);
+    final PrintStream oldErr = System.err;
+    System.setErr(out);
+    final String results;
+    try {
+      int run = shell.run(args);
+      results = bytes.toString();
+      LOG.info("result=" + results);
+      assertTrue("Return code should be 0", run == 0);
+    } finally {
+      IOUtils.closeStream(out);
+      System.setErr(oldErr);
+    }
+    
+  }
   
-  
-}
+  static class LocalFileSystemExtn extends RawLocalFileSystem {
+
+    private String username;
+    private String groupname;
+
+    @Override
+    public void setOwner(Path p, String username, String groupname)
+        throws IOException {
+      this.username = username;
+      this.groupname = groupname;
+
+    }
+
+    @Override
+    public FileStatus getFileStatus(Path f) throws IOException {
+      return new FileStatus();
+    }
+  }
+ 
+}