Browse Source

HADOOP-6886. LocalFileSystem Needs createNonRecursive API. Contributed by Nicolas Spiegelberg.

git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/branch-0.20-security@1199043 13f79535-47bb-0310-9956-ffa450edef68
Jitendra Nath Pandey 13 years ago
parent
commit
bf803b396c

+ 3 - 0
CHANGES.txt

@@ -81,6 +81,9 @@ Release 0.20.205.1 - unreleased
     HADOOP-6840. Support non-recursive create() in FileSystem & 
     HADOOP-6840. Support non-recursive create() in FileSystem & 
     SequenceFile.Writer. (Nicolas Spiegelberg via jitendra)
     SequenceFile.Writer. (Nicolas Spiegelberg via jitendra)
 
 
+    HADOOP-6886. LocalFileSystem Needs createNonRecursive API.
+    (Nicolas Spiegelberg via jitendra)
+
   BUG FIXES
   BUG FIXES
 
 
     HADOOP-7740. Fixed security audit logger configuration. 
     HADOOP-7740. Fixed security audit logger configuration. 

+ 24 - 2
src/core/org/apache/hadoop/fs/ChecksumFileSystem.java

@@ -361,9 +361,22 @@ public abstract class ChecksumFileSystem extends FilterFileSystem {
   public FSDataOutputStream create(Path f, FsPermission permission,
   public FSDataOutputStream create(Path f, FsPermission permission,
       boolean overwrite, int bufferSize, short replication, long blockSize,
       boolean overwrite, int bufferSize, short replication, long blockSize,
       Progressable progress) throws IOException {
       Progressable progress) throws IOException {
+    return create(f, permission, overwrite, true, bufferSize, 
+        replication, blockSize, progress);
+  }
+
+  private FSDataOutputStream create(Path f, FsPermission permission,
+      boolean overwrite, boolean createParent, int bufferSize, 
+      short replication, long blockSize,
+      Progressable progress) throws IOException {
     Path parent = f.getParent();
     Path parent = f.getParent();
-    if (parent != null && !mkdirs(parent)) {
-      throw new IOException("Mkdirs failed to create " + parent);
+    if (parent != null) {
+      if (!createParent && !exists(parent)) {
+        throw new FileNotFoundException("Parent directory doesn't exist: "
+            + parent);
+      } else if (!mkdirs(parent)) {
+        throw new IOException("Mkdirs failed to create " + parent);
+      }
     }
     }
     final FSDataOutputStream out = new FSDataOutputStream(
     final FSDataOutputStream out = new FSDataOutputStream(
         new ChecksumFSOutputSummer(this, f, overwrite, bufferSize, replication,
         new ChecksumFSOutputSummer(this, f, overwrite, bufferSize, replication,
@@ -374,6 +387,15 @@ public abstract class ChecksumFileSystem extends FilterFileSystem {
     return out;
     return out;
   }
   }
 
 
+  /** {@inheritDoc} */
+  @Override
+  public FSDataOutputStream createNonRecursive(Path f, FsPermission permission,
+      boolean overwrite, int bufferSize, short replication, long blockSize,
+      Progressable progress) throws IOException {
+    return create(f, permission, overwrite, false, bufferSize, replication, 
+        blockSize, progress);
+  }
+
   /**
   /**
    * Set replication for an existing file.
    * Set replication for an existing file.
    * Implement the abstract <tt>setReplication</tt> of <tt>FileSystem</tt>
    * Implement the abstract <tt>setReplication</tt> of <tt>FileSystem</tt>

+ 2 - 1
src/core/org/apache/hadoop/fs/FileSystem.java

@@ -623,7 +623,8 @@ public abstract class FileSystem extends Configured implements Closeable {
       boolean overwrite,
       boolean overwrite,
       int bufferSize, short replication, long blockSize,
       int bufferSize, short replication, long blockSize,
       Progressable progress) throws IOException {
       Progressable progress) throws IOException {
-    throw new IOException("createNonRecursive unsupported for this filesystem");
+    throw new IOException("createNonRecursive unsupported for this filesystem "
+        + this.getClass());
   }
   }
 
 
   /**
   /**

+ 28 - 3
src/core/org/apache/hadoop/fs/RawLocalFileSystem.java

@@ -229,15 +229,28 @@ public class RawLocalFileSystem extends FileSystem {
   }
   }
 
 
   /** {@inheritDoc} */
   /** {@inheritDoc} */
+  @Override
   public FSDataOutputStream create(Path f, boolean overwrite, int bufferSize,
   public FSDataOutputStream create(Path f, boolean overwrite, int bufferSize,
                                    short replication, long blockSize, Progressable progress)
                                    short replication, long blockSize, Progressable progress)
+  throws IOException {
+    return create(f, overwrite, true, bufferSize, replication, blockSize, progress);
+  }
+
+  private FSDataOutputStream create(Path f, boolean overwrite, 
+      boolean createParent, int bufferSize,
+      short replication, long blockSize, Progressable progress)
     throws IOException {
     throws IOException {
     if (exists(f) && !overwrite) {
     if (exists(f) && !overwrite) {
       throw new IOException("File already exists:"+f);
       throw new IOException("File already exists:"+f);
     }
     }
     Path parent = f.getParent();
     Path parent = f.getParent();
-    if (parent != null && !mkdirs(parent)) {
-      throw new IOException("Mkdirs failed to create " + parent.toString());
+    if (parent != null) {
+      if (!createParent && !exists(parent)) {
+        throw new FileNotFoundException("Parent directory doesn't exist: "
+            + parent);
+      } else if (!mkdirs(parent)) {
+        throw new IOException("Mkdirs failed to create " + parent);
+      }
     }
     }
     return new FSDataOutputStream(new BufferedOutputStream(
     return new FSDataOutputStream(new BufferedOutputStream(
         new LocalFSFileOutputStream(f, false), bufferSize), statistics);
         new LocalFSFileOutputStream(f, false), bufferSize), statistics);
@@ -253,7 +266,19 @@ public class RawLocalFileSystem extends FileSystem {
     setPermission(f, permission);
     setPermission(f, permission);
     return out;
     return out;
   }
   }
-  
+
+  /** {@inheritDoc} */
+  @Override
+  public FSDataOutputStream createNonRecursive(Path f, FsPermission permission,
+      boolean overwrite,
+      int bufferSize, short replication, long blockSize,
+      Progressable progress) throws IOException {
+    FSDataOutputStream out = create(f,
+        overwrite, false, bufferSize, replication, blockSize, progress);
+    setPermission(f, permission);
+    return out;
+  }
+
   public boolean rename(Path src, Path dst) throws IOException {
   public boolean rename(Path src, Path dst) throws IOException {
     if (pathToFile(src).renameTo(pathToFile(dst))) {
     if (pathToFile(src).renameTo(pathToFile(dst))) {
       return true;
       return true;