1 /* 2 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package jdk.test.lib; 25 26 import java.io.IOException; 27 import java.nio.file.FileVisitResult; 28 import java.nio.file.Files; 29 import java.nio.file.Path; 30 import java.nio.file.Paths; 31 import java.nio.file.SimpleFileVisitor; 32 import java.nio.file.StandardCopyOption; 33 import java.nio.file.attribute.BasicFileAttributes; 34 35 // !!! 36 // NOTE: this class is widely used. DO NOT depend on any other classes in any test library, or else 37 // you may see intermittent ClassNotFoundException as in JDK-8188828 38 // !!! 39 40 /** 41 * Copy a resource: file or directory recursively, using relative path(src and dst) 42 * which are applied to test source directory(src) and current directory(dst) 43 */ 44 public class FileInstaller { 45 public static final String TEST_SRC = System.getProperty("test.src", "").trim(); 46 47 /** 48 * @param args source and destination 49 * @throws IOException if an I/O error occurs 50 */ 51 public static void main(String[] args) throws IOException { 52 if (args.length != 2) { 53 throw new IllegalArgumentException("Unexpected number of arguments for file copy"); 54 } 55 Path src = Paths.get(TEST_SRC, args[0]).toAbsolutePath().normalize(); 56 Path dst = Paths.get(args[1]).toAbsolutePath().normalize(); 57 if (src.toFile().exists()) { 58 System.out.printf("copying %s to %s%n", src, dst); 59 if (src.toFile().isDirectory()) { 60 // can't use Files::copy for dirs, as 'dst' might exist already 61 Files.walkFileTree(src, new CopyFileVisitor(src, dst)); 62 } else { 63 Path dstDir = dst.getParent(); 64 if (!dstDir.toFile().exists()) { 65 Files.createDirectories(dstDir); 66 } 67 Files.copy(src, dst, StandardCopyOption.REPLACE_EXISTING); 68 } 69 } else { 70 throw new IOException("Can't find source " + src); 71 } 72 } 73 74 private static class CopyFileVisitor extends SimpleFileVisitor<Path> { 75 private final Path copyFrom; 76 private final Path copyTo; 77 78 public CopyFileVisitor(Path copyFrom, Path copyTo) { 79 this.copyFrom = copyFrom; 80 this.copyTo = copyTo; 81 } 82 83 @Override 84 public FileVisitResult preVisitDirectory(Path file, 85 BasicFileAttributes attrs) throws IOException { 86 Path relativePath = copyFrom.relativize(file); 87 Path destination = copyTo.resolve(relativePath); 88 if (!destination.toFile().exists()) { 89 Files.createDirectories(destination); 90 } 91 return FileVisitResult.CONTINUE; 92 } 93 94 @Override 95 public FileVisitResult visitFile(Path file, 96 BasicFileAttributes attrs) throws IOException { 97 if (!file.toFile().isFile()) { 98 return FileVisitResult.CONTINUE; 99 } 100 Path relativePath = copyFrom.relativize(file); 101 Path destination = copyTo.resolve(relativePath); 102 Files.copy(file, destination, StandardCopyOption.COPY_ATTRIBUTES); 103 return FileVisitResult.CONTINUE; 104 } 105 } 106 }