src/share/sample/nio/file/Copy.java
author alanb
Sat Jun 27 21:46:53 2009 +0100 (4 months ago)
changeset 1319 5208d0c90d73
parent 1144e281812be4ce
permissions -rw-r--r--
6838333: New I/O: Update file system API to jsr203/nio2-b101
6844313: New I/O: File timestamps should be represented by a FileTime rather than a long+TimeUnit
Reviewed-by: sherman
        1 /*
        2  * Copyright 2008-2009 Sun Microsystems, Inc.  All Rights Reserved.
        3  *
        4  * Redistribution and use in source and binary forms, with or without
        5  * modification, are permitted provided that the following conditions
        6  * are met:
        7  *
        8  *   - Redistributions of source code must retain the above copyright
        9  *     notice, this list of conditions and the following disclaimer.
       10  *
       11  *   - Redistributions in binary form must reproduce the above copyright
       12  *     notice, this list of conditions and the following disclaimer in the
       13  *     documentation and/or other materials provided with the distribution.
       14  *
       15  *   - Neither the name of Sun Microsystems nor the names of its
       16  *     contributors may be used to endorse or promote products derived
       17  *     from this software without specific prior written permission.
       18  *
       19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
       20  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
       21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
       23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
       24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
       25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
       26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
       27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
       28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
       29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       30  */
       31 
       32 import java.nio.file.*;
       33 import static java.nio.file.StandardCopyOption.*;
       34 import java.nio.file.attribute.*;
       35 import static java.nio.file.FileVisitResult.*;
       36 import java.io.IOException;
       37 import java.util.*;
       38 
       39 /**
       40  * Sample code that copies files in a similar manner to the cp(1) program.
       41  */
       42 
       43 public class Copy {
       44 
       45     /**
       46      * Returns {@code true} if okay to overwrite a  file ("cp -i")
       47      */
       48     static boolean okayToOverwrite(FileRef file) {
       49         String answer = System.console().readLine("overwrite %s (yes/no)? ", file);
       50         return (answer.equalsIgnoreCase("y") || answer.equalsIgnoreCase("yes"));
       51     }
       52 
       53     /**
       54      * Copy source file to target location. If {@code prompt} is true then
       55      * prompt user to overwrite target if it exists. The {@code preserve}
       56      * parameter determines if file attributes should be copied/preserved.
       57      */
       58     static void copyFile(Path source, Path target, boolean prompt, boolean preserve) {
       59         CopyOption[] options = (preserve) ?
       60             new CopyOption[] { COPY_ATTRIBUTES, REPLACE_EXISTING } :
       61             new CopyOption[] { REPLACE_EXISTING };
       62         if (!prompt || target.notExists() || okayToOverwrite(target)) {
       63             try {
       64                 source.copyTo(target, options);
       65             } catch (IOException x) {
       66                 System.err.format("Unable to copy: %s: %s%n", source, x);
       67             }
       68         }
       69     }
       70 
       71     /**
       72      * A {@code FileVisitor} that copies a file-tree ("cp -r")
       73      */
       74     static class TreeCopier implements FileVisitor<Path> {
       75         private final Path source;
       76         private final Path target;
       77         private final boolean prompt;
       78         private final boolean preserve;
       79 
       80         TreeCopier(Path source, Path target, boolean prompt, boolean preserve) {
       81             this.source = source;
       82             this.target = target;
       83             this.prompt = prompt;
       84             this.preserve = preserve;
       85         }
       86 
       87         @Override
       88         public FileVisitResult preVisitDirectory(Path dir) {
       89             // before visiting entries in a directory we copy the directory
       90             // (okay if directory already exists).
       91             CopyOption[] options = (preserve) ?
       92                 new CopyOption[] { COPY_ATTRIBUTES } : new CopyOption[0];
       93 
       94             Path newdir = target.resolve(source.relativize(dir));
       95             try {
       96                 dir.copyTo(newdir, options);
       97             } catch (FileAlreadyExistsException x) {
       98                 // ignore
       99             } catch (IOException x) {
      100                 System.err.format("Unable to create: %s: %s%n", newdir, x);
      101                 return SKIP_SUBTREE;
      102             }
      103             return CONTINUE;
      104         }
      105 
      106         @Override
      107         public FileVisitResult preVisitDirectoryFailed(Path dir, IOException exc) {
      108             System.err.format("Unable to copy: %s: %s%n", dir, exc);
      109             return CONTINUE;
      110         }
      111 
      112         @Override
      113         public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
      114             if (attrs.isDirectory()) {
      115                 System.err.println("cycle detected: " + file);
      116             } else {
      117                 copyFile(file, target.resolve(source.relativize(file)),
      118                          prompt, preserve);
      119             }
      120             return CONTINUE;
      121         }
      122 
      123         @Override
      124         public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
      125             // fix up modification time of directory when done
      126             if (exc == null && preserve) {
      127                 Path newdir = target.resolve(source.relativize(dir));
      128                 try {
      129                     BasicFileAttributes attrs = Attributes.readBasicFileAttributes(dir);
      130                     Attributes.setLastModifiedTime(newdir, attrs.lastModifiedTime());
      131                 } catch (IOException x) {
      132                     System.err.format("Unable to copy all attributes to: %s: %s%n", newdir, x);
      133                 }
      134             }
      135             return CONTINUE;
      136         }
      137 
      138         @Override
      139         public FileVisitResult visitFileFailed(Path file, IOException exc) {
      140             System.err.format("Unable to copy: %s: %s%n", file, exc);
      141             return CONTINUE;
      142         }
      143     }
      144 
      145     static void usage() {
      146         System.err.println("java Copy [-ip] source... target");
      147         System.err.println("java Copy -r [-ip] source-dir... target");
      148         System.exit(-1);
      149     }
      150 
      151     public static void main(String[] args) throws IOException {
      152         boolean recursive = false;
      153         boolean prompt = false;
      154         boolean preserve = false;
      155 
      156         // process options
      157         int argi = 0;
      158         while (argi < args.length) {
      159             String arg = args[argi];
      160             if (!arg.startsWith("-"))
      161                 break;
      162             if (arg.length() < 2)
      163                 usage();
      164             for (int i=1; i<arg.length(); i++) {
      165                 char c = arg.charAt(i);
      166                 switch (c) {
      167                     case 'r' : recursive = true; break;
      168                     case 'i' : prompt = true; break;
      169                     case 'p' : preserve = true; break;
      170                     default : usage();
      171                 }
      172             }
      173             argi++;
      174         }
      175 
      176         // remaining arguments are the source files(s) and the target location
      177         int remaining = args.length - argi;
      178         if (remaining < 2)
      179             usage();
      180         Path[] source = new Path[remaining-1];
      181         int i=0;
      182         while (remaining > 1) {
      183             source[i++] = Paths.get(args[argi++]);
      184             remaining--;
      185         }
      186         Path target = Paths.get(args[argi]);
      187 
      188         // check if target is a directory
      189         boolean isDir = false;
      190         try {
      191             isDir = Attributes.readBasicFileAttributes(target).isDirectory();
      192         } catch (IOException x) {
      193             // ignore (probably target does not exist)
      194         }
      195 
      196         // copy each source file/directory to target
      197         for (i=0; i<source.length; i++) {
      198             Path dest = (isDir) ? target.resolve(source[i].getName()) : target;
      199 
      200             if (recursive) {
      201                 // follow links when copying files
      202                 EnumSet<FileVisitOption> opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
      203                 TreeCopier tc = new TreeCopier(source[i], dest, prompt, preserve);
      204                 Files.walkFileTree(source[i], opts, Integer.MAX_VALUE, tc);
      205             } else {
      206                 // not recursive so source must not be a directory
      207                 try {
      208                     if (Attributes.readBasicFileAttributes(source[i]).isDirectory()) {
      209                         System.err.format("%s: is a directory%n", source[i]);
      210                         continue;
      211                     }
      212                 } catch (IOException x) {
      213                     // assume not directory
      214                 }
      215                 copyFile(source[i], dest, prompt, preserve);
      216             }
      217         }
      218     }
      219 }