فهرست منبع

HADOOP-2808. Minor fix to FileUtil::copy to mind the overwrite
formal. Contributed by Chris Douglas.



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

Christopher Douglas 17 سال پیش
والد
کامیت
0183f59448
3فایلهای تغییر یافته به همراه40 افزوده شده و 11 حذف شده
  1. 3 0
      CHANGES.txt
  2. 12 11
      src/java/org/apache/hadoop/fs/FileUtil.java
  3. 25 0
      src/test/org/apache/hadoop/fs/TestLocalFileSystem.java

+ 3 - 0
CHANGES.txt

@@ -79,6 +79,9 @@ Release 0.16.1 - Unrelease
     HADOOP-2391. Cleanup job output directory before declaring a job as
     HADOOP-2391. Cleanup job output directory before declaring a job as
     SUCCESSFUL. (Amareshwari Sri Ramadasu via ddas)
     SUCCESSFUL. (Amareshwari Sri Ramadasu via ddas)
 
 
+    HADOOP-2808. Minor fix to FileUtil::copy to mind the overwrite
+    formal. (cdouglas)
+
 Release 0.16.0 - 2008-02-07
 Release 0.16.0 - 2008-02-07
 
 
   INCOMPATIBLE CHANGES
   INCOMPATIBLE CHANGES

+ 12 - 11
src/java/org/apache/hadoop/fs/FileUtil.java

@@ -139,7 +139,7 @@ public class FileUtil {
                              boolean deleteSource,
                              boolean deleteSource,
                              boolean overwrite,
                              boolean overwrite,
                              Configuration conf) throws IOException {
                              Configuration conf) throws IOException {
-    dst = checkDest(src.getName(), dstFS, dst);
+    dst = checkDest(src.getName(), dstFS, dst, overwrite);
 
 
     if (srcFS.isDirectory(src)) {
     if (srcFS.isDirectory(src)) {
       checkDependencies(srcFS, src, dstFS, dst);
       checkDependencies(srcFS, src, dstFS, dst);
@@ -171,7 +171,7 @@ public class FileUtil {
                                   FileSystem dstFS, Path dstFile, 
                                   FileSystem dstFS, Path dstFile, 
                                   boolean deleteSource,
                                   boolean deleteSource,
                                   Configuration conf, String addString) throws IOException {
                                   Configuration conf, String addString) throws IOException {
-    dstFile = checkDest(srcDir.getName(), dstFS, dstFile);
+    dstFile = checkDest(srcDir.getName(), dstFS, dstFile, false);
 
 
     if (!srcFS.isDirectory(srcDir))
     if (!srcFS.isDirectory(srcDir))
       return false;
       return false;
@@ -210,7 +210,7 @@ public class FileUtil {
                              FileSystem dstFS, Path dst,
                              FileSystem dstFS, Path dst,
                              boolean deleteSource,
                              boolean deleteSource,
                              Configuration conf) throws IOException {
                              Configuration conf) throws IOException {
-    dst = checkDest(src.getName(), dstFS, dst);
+    dst = checkDest(src.getName(), dstFS, dst, false);
 
 
     if (src.isDirectory()) {
     if (src.isDirectory()) {
       if (!dstFS.mkdirs(dst)) {
       if (!dstFS.mkdirs(dst)) {
@@ -262,16 +262,17 @@ public class FileUtil {
     }
     }
   }
   }
 
 
-  private static Path checkDest(String srcName, FileSystem dstFS, Path dst)
-    throws IOException {
+  private static Path checkDest(String srcName, FileSystem dstFS, Path dst,
+      boolean overwrite) throws IOException {
     if (dstFS.exists(dst)) {
     if (dstFS.exists(dst)) {
-      if (!dstFS.isDirectory(dst)) {
-        throw new IOException("Target " + dst + " already exists");
-      } else {
-        dst = new Path(dst, srcName);
-        if (dstFS.exists(dst)) {
-          throw new IOException("Target " + dst + " already exists");
+      FileStatus sdst = dstFS.getFileStatus(dst);
+      if (sdst.isDir()) {
+        if (null == srcName) {
+          throw new IOException("Target " + dst + " is a directory");
         }
         }
+        return checkDest(null, dstFS, new Path(dst, srcName), overwrite);
+      } else if (!overwrite) {
+        throw new IOException("Target " + dst + " already exists");
       }
       }
     }
     }
     return dst;
     return dst;

+ 25 - 0
src/test/org/apache/hadoop/fs/TestLocalFileSystem.java

@@ -96,6 +96,31 @@ public class TestLocalFileSystem extends TestCase {
     }
     }
   }
   }
 
 
+  public void testCopy() throws IOException {
+    Configuration conf = new Configuration();
+    LocalFileSystem fs = FileSystem.getLocal(conf);
+    Path src = new Path(TEST_ROOT_DIR, "dingo");
+    Path dst = new Path(TEST_ROOT_DIR, "yak");
+    writeFile(fs, src);
+    assertTrue(FileUtil.copy(fs, src, fs, dst, true, false, conf));
+    assertTrue(!fs.exists(src) && fs.exists(dst));
+    assertTrue(FileUtil.copy(fs, dst, fs, src, false, false, conf));
+    assertTrue(fs.exists(src) && fs.exists(dst));
+    assertTrue(FileUtil.copy(fs, src, fs, dst, true, true, conf));
+    assertTrue(!fs.exists(src) && fs.exists(dst));
+    fs.mkdirs(src);
+    assertTrue(FileUtil.copy(fs, dst, fs, src, false, false, conf));
+    Path tmp = new Path(src, dst.getName());
+    assertTrue(fs.exists(tmp) && fs.exists(dst));
+    assertTrue(FileUtil.copy(fs, dst, fs, src, false, true, conf));
+    assertTrue(fs.delete(tmp));
+    fs.mkdirs(tmp);
+    try {
+      FileUtil.copy(fs, dst, fs, src, true, true, conf);
+      fail("Failed to detect existing dir");
+    } catch (IOException e) { }
+  }
+
   public void testHomeDirectory() throws IOException {
   public void testHomeDirectory() throws IOException {
     Configuration conf = new Configuration();
     Configuration conf = new Configuration();
     FileSystem fileSys = FileSystem.getLocal(conf);
     FileSystem fileSys = FileSystem.getLocal(conf);