changeset 1144:e281812be4ce

6831461: (sample) Copy -r fails with IllegalArgumentexception: 'maxDepth' is negative Reviewed-by: chegar
author alanb
date Mon, 20 Apr 2009 13:27:23 +0100
parents ccd08d4b19cf
children 697bf0cf039b
files src/share/sample/nio/file/Copy.java
diffstat 1 files changed, 9 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/sample/nio/file/Copy.java	Mon Apr 20 09:30:50 2009 +0100
+++ b/src/share/sample/nio/file/Copy.java	Mon Apr 20 13:27:23 2009 +0100
@@ -52,7 +52,7 @@
 
     /**
      * Copy source file to target location. If {@code prompt} is true then
-     * prompted user to overwrite target if it exists. The {@code preserve}
+     * prompt user to overwrite target if it exists. The {@code preserve}
      * parameter determines if file attributes should be copied/preserved.
      */
     static void copyFile(Path source, Path target, boolean prompt, boolean preserve) {
@@ -63,7 +63,7 @@
             try {
                 source.copyTo(target, options);
             } catch (IOException x) {
-                System.err.format("Unable to create: %s: %s%n", target, x);
+                System.err.format("Unable to copy: %s: %s%n", source, x);
             }
         }
     }
@@ -124,13 +124,13 @@
         public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
             // fix up modification time of directory when done
             if (exc == null && preserve) {
+                Path newdir = target.resolve(source.relativize(dir));
                 try {
                     BasicFileAttributes attrs = Attributes.readBasicFileAttributes(dir);
-                    Path newdir = target.resolve(source.relativize(dir));
                     Attributes.setLastModifiedTime(newdir,
                         attrs.lastModifiedTime(), attrs.resolution());
                 } catch (IOException x) {
-                    // ignore
+                    System.err.format("Unable to copy all attributes to: %s: %s%n", newdir, x);
                 }
             }
             return CONTINUE;
@@ -191,6 +191,7 @@
         try {
             isDir = Attributes.readBasicFileAttributes(target).isDirectory();
         } catch (IOException x) {
+            // ignore (probably target does not exist)
         }
 
         // copy each source file/directory to target
@@ -201,7 +202,7 @@
                 // follow links when copying files
                 EnumSet<FileVisitOption> opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
                 TreeCopier tc = new TreeCopier(source[i], dest, prompt, preserve);
-                Files.walkFileTree(source[i], opts, -1, tc);
+                Files.walkFileTree(source[i], opts, Integer.MAX_VALUE, tc);
             } else {
                 // not recursive so source must not be a directory
                 try {
@@ -209,7 +210,9 @@
                         System.err.format("%s: is a directory%n", source[i]);
                         continue;
                     }
-                } catch (IOException x) { }
+                } catch (IOException x) {
+                    // assume not directory
+                }
                 copyFile(source[i], dest, prompt, preserve);
             }
         }