1 /*
   2  * Copyright (c) 2015, 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  * Copy a resource: file or directory recursively, using relative path(src and dst)
  37  * which are applied to test source directory(src) and current directory(dst)
  38  */
  39 public class FileInstaller {
  40     /**
  41      * @param args source and destination
  42      * @throws IOException if an I/O error occurs
  43      */
  44     public static void main(String[] args) throws IOException {
  45         if (args.length != 2) {
  46             throw new IllegalArgumentException("Unexpected number of arguments for file copy");
  47         }
  48         Path src = Paths.get(Utils.TEST_SRC, args[0]).toAbsolutePath();
  49         Path dst = Paths.get(args[1]).toAbsolutePath();
  50         if (src.toFile().exists()) {
  51             if (src.toFile().isDirectory()) {
  52                 Files.walkFileTree(src, new CopyFileVisitor(src, dst));
  53             } else {
  54                 Path dstDir = dst.getParent();
  55                 if (!dstDir.toFile().exists()) {
  56                     Files.createDirectories(dstDir);
  57                 }
  58                 Files.copy(src, dst, StandardCopyOption.REPLACE_EXISTING);
  59             }
  60         } else {
  61             throw new IOException("Can't find source " + src);
  62         }
  63     }
  64 
  65     private static class CopyFileVisitor extends SimpleFileVisitor<Path> {
  66         private final Path copyFrom;
  67         private final Path copyTo;
  68 
  69         public CopyFileVisitor(Path copyFrom, Path copyTo) {
  70             this.copyFrom = copyFrom;
  71             this.copyTo = copyTo;
  72         }
  73 
  74         @Override
  75         public FileVisitResult preVisitDirectory(Path file,
  76                 BasicFileAttributes attrs) throws IOException {
  77             Path relativePath = file.relativize(copyFrom);
  78             Path destination = copyTo.resolve(relativePath);
  79             if (!destination.toFile().exists()) {
  80                 Files.createDirectories(destination);
  81             }
  82             return FileVisitResult.CONTINUE;
  83         }
  84 
  85         @Override
  86         public FileVisitResult visitFile(Path file,
  87                 BasicFileAttributes attrs) throws IOException {
  88             if (!file.toFile().isFile()) {
  89                 return FileVisitResult.CONTINUE;
  90             }
  91             Path relativePath = copyFrom.relativize(file);
  92             Path destination = copyTo.resolve(relativePath);
  93             Files.copy(file, destination, StandardCopyOption.COPY_ATTRIBUTES);
  94             return FileVisitResult.CONTINUE;
  95         }
  96     }
  97 }