2 * Copyright 2008-2009 Sun Microsystems, Inc. All Rights Reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
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;
40 * Sample code that copies files in a similar manner to the cp(1) program.
46 * Returns {@code true} if okay to overwrite a file ("cp -i")
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"));
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.
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)) {
64 source.copyTo(target, options);
65 } catch (IOException x) {
66 System.err.format("Unable to copy: %s: %s%n", source, x);
72 * A {@code FileVisitor} that copies a file-tree ("cp -r")
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;
80 TreeCopier(Path source, Path target, boolean prompt, boolean preserve) {
84 this.preserve = preserve;
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];
94 Path newdir = target.resolve(source.relativize(dir));
96 dir.copyTo(newdir, options);
97 } catch (FileAlreadyExistsException x) {
99 } catch (IOException x) {
100 System.err.format("Unable to create: %s: %s%n", newdir, x);
107 public FileVisitResult preVisitDirectoryFailed(Path dir, IOException exc) {
108 System.err.format("Unable to copy: %s: %s%n", dir, exc);
113 public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
114 if (attrs.isDirectory()) {
115 System.err.println("cycle detected: " + file);
117 copyFile(file, target.resolve(source.relativize(file)),
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));
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);
139 public FileVisitResult visitFileFailed(Path file, IOException exc) {
140 System.err.format("Unable to copy: %s: %s%n", file, exc);
145 static void usage() {
146 System.err.println("java Copy [-ip] source... target");
147 System.err.println("java Copy -r [-ip] source-dir... target");
151 public static void main(String[] args) throws IOException {
152 boolean recursive = false;
153 boolean prompt = false;
154 boolean preserve = false;
158 while (argi < args.length) {
159 String arg = args[argi];
160 if (!arg.startsWith("-"))
162 if (arg.length() < 2)
164 for (int i=1; i<arg.length(); i++) {
165 char c = arg.charAt(i);
167 case 'r' : recursive = true; break;
168 case 'i' : prompt = true; break;
169 case 'p' : preserve = true; break;
176 // remaining arguments are the source files(s) and the target location
177 int remaining = args.length - argi;
180 Path[] source = new Path[remaining-1];
182 while (remaining > 1) {
183 source[i++] = Paths.get(args[argi++]);
186 Path target = Paths.get(args[argi]);
188 // check if target is a directory
189 boolean isDir = false;
191 isDir = Attributes.readBasicFileAttributes(target).isDirectory();
192 } catch (IOException x) {
193 // ignore (probably target does not exist)
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;
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);
206 // not recursive so source must not be a directory
208 if (Attributes.readBasicFileAttributes(source[i]).isDirectory()) {
209 System.err.format("%s: is a directory%n", source[i]);
212 } catch (IOException x) {
213 // assume not directory
215 copyFile(source[i], dest, prompt, preserve);