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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package jdk.tools.jlink.internal.plugins;
  26 
  27 import java.io.File;
  28 import java.io.IOException;
  29 import java.io.UncheckedIOException;
  30 import java.nio.file.FileVisitResult;
  31 import java.nio.file.FileVisitor;
  32 import java.nio.file.Files;
  33 import java.nio.file.Path;
  34 import java.nio.file.Paths;
  35 import java.nio.file.attribute.BasicFileAttributes;
  36 import java.util.ArrayList;
  37 import java.util.List;
  38 import java.util.Map;
  39 import java.util.Objects;
  40 import jdk.tools.jlink.internal.PathResourcePoolEntry;
  41 import jdk.tools.jlink.plugin.PluginException;
  42 import jdk.tools.jlink.plugin.ResourcePool;
  43 import jdk.tools.jlink.plugin.ResourcePoolBuilder;
  44 import jdk.tools.jlink.plugin.ResourcePoolEntry;
  45 import jdk.tools.jlink.plugin.Plugin;
  46 import jdk.tools.jlink.internal.Utils;
  47 
  48 /**
  49  *
  50  * Copy files to image from various locations.
  51  */
  52 public class FileCopierPlugin implements Plugin {
  53 
  54     public static final String NAME = "copy-files";
  55 
  56     private static final class CopiedFile {
  57 
  58         Path source;
  59         Path target;
  60     }
  61     private final List<CopiedFile> files = new ArrayList<>();
  62 
  63     /**
  64      * Symbolic link to another path.
  65      */
  66     public static abstract class SymImageFile extends PathResourcePoolEntry {
  67 
  68         private final String targetPath;
  69 
  70         public SymImageFile(String targetPath, String module, String path,
  71                 ResourcePoolEntry.Type type, Path file) {
  72             super(module, path, type, file);
  73             this.targetPath = targetPath;
  74         }
  75 
  76         public String getTargetPath() {
  77             return targetPath;
  78         }
  79     }
  80 
  81     private static final class SymImageFileImpl extends SymImageFile {
  82 
  83         public SymImageFileImpl(String targetPath, Path file, String module,
  84                 String path, ResourcePoolEntry.Type type) {
  85             super(targetPath, module, path, type, file);
  86         }
  87     }
  88 
  89     private static final class DirectoryCopy implements FileVisitor<Path> {
  90 
  91         private final Path source;
  92         private final ResourcePoolBuilder pool;
  93         private final String targetDir;
  94         private final List<SymImageFile> symlinks = new ArrayList<>();
  95 
  96         DirectoryCopy(Path source, ResourcePoolBuilder pool, String targetDir) {
  97             this.source = source;
  98             this.pool = pool;
  99             this.targetDir = targetDir;
 100         }
 101 
 102         @Override
 103         public FileVisitResult preVisitDirectory(Path dir,
 104                 BasicFileAttributes attrs) throws IOException {
 105             return FileVisitResult.CONTINUE;
 106         }
 107 
 108         @Override
 109         public FileVisitResult visitFile(Path file,
 110                 BasicFileAttributes attrs) throws IOException {
 111             Objects.requireNonNull(file);
 112             Objects.requireNonNull(attrs);
 113             String path = targetDir + "/" + source.relativize(file);
 114             if (attrs.isSymbolicLink()) {
 115                 Path symTarget = Files.readSymbolicLink(file);
 116                 if (!Files.exists(symTarget)) {
 117                     // relative to file parent?
 118                     Path parent = file.getParent();
 119                     if (parent != null) {
 120                         symTarget = parent.resolve(symTarget);
 121                     }
 122                 }
 123                 if (!Files.exists(symTarget)) {
 124                     System.err.println("WARNING: Skipping sym link, target "
 125                             + Files.readSymbolicLink(file) + "not found");
 126                     return FileVisitResult.CONTINUE;
 127                 }
 128                 SymImageFileImpl impl = new SymImageFileImpl(symTarget.toString(),
 129                         file, path, Objects.requireNonNull(file.getFileName()).toString(),
 130                         ResourcePoolEntry.Type.OTHER);
 131                 symlinks.add(impl);
 132             } else {
 133                 addFile(pool, file, path);
 134             }
 135             return FileVisitResult.CONTINUE;
 136         }
 137 
 138         @Override
 139         public FileVisitResult postVisitDirectory(Path dir, IOException exc)
 140                 throws IOException {
 141             if (exc != null) {
 142                 throw exc;
 143             }
 144             return FileVisitResult.CONTINUE;
 145         }
 146 
 147         @Override
 148         public FileVisitResult visitFileFailed(Path file, IOException exc)
 149                 throws IOException {
 150             throw exc;
 151         }
 152     }
 153 
 154     private static void addFile(ResourcePoolBuilder pool, Path file, String path)
 155             throws IOException {
 156         Objects.requireNonNull(pool);
 157         Objects.requireNonNull(file);
 158         Objects.requireNonNull(path);
 159         ResourcePoolEntry impl = ResourcePoolEntry.create(
 160                 "/java.base/other/" + path,
 161                 ResourcePoolEntry.Type.OTHER, file);
 162         try {
 163             pool.add(impl);
 164         } catch (Exception ex) {
 165             throw new IOException(ex);
 166         }
 167     }
 168 
 169     @Override
 170     public void configure(Map<String, String> config) {
 171         List<String> arguments = Utils.parseList(config.get(NAME));
 172         if (arguments.isEmpty()) {
 173             throw new RuntimeException("Invalid argument for " + NAME);
 174         }
 175 
 176         String javahome = System.getProperty("java.home");
 177         for (String a : arguments) {
 178             int i = a.indexOf("=");
 179             CopiedFile cf = new CopiedFile();
 180             if (i == -1) {
 181                 Path file = Paths.get(a);
 182                 if (file.isAbsolute()) {
 183                     cf.source = file;
 184                     // The target is the image root directory.
 185                     cf.target = file.getFileName();
 186                 } else {
 187                     file = new File(javahome, a).toPath();
 188                     cf.source = file;
 189                     cf.target = Paths.get(a);
 190                 }
 191             } else {
 192                 String target = a.substring(i + 1);
 193                 String f = a.substring(0, i);
 194                 Path file = Paths.get(f);
 195                 if (file.isAbsolute()) {
 196                     cf.source = file;
 197                 } else {
 198                     cf.source = new File(javahome,
 199                             file.toFile().getPath()).toPath();
 200                 }
 201                 cf.target = Paths.get(target);
 202             }
 203             if (!Files.exists(cf.source)) {
 204                 System.err.println("Skipping file " + cf.source
 205                         + ", it doesn't exist");
 206             } else {
 207                 files.add(cf);
 208             }
 209         }
 210     }
 211 
 212     @Override
 213     public ResourcePool transform(ResourcePool in, ResourcePoolBuilder out) {
 214         in.transformAndCopy((file) -> {
 215             return file;
 216         }, out);
 217 
 218         // Add new files.
 219         try {
 220             for (CopiedFile file : files) {
 221                 if (Files.isRegularFile(file.source)) {
 222                     addFile(out, file.source, file.target.toString());
 223                 } else if (Files.isDirectory(file.source)) {
 224                     DirectoryCopy dc = new DirectoryCopy(file.source,
 225                             out, file.target.toString());
 226                     Files.walkFileTree(file.source, dc);
 227                     // Add symlinks after actual content
 228                     for (SymImageFile imf : dc.symlinks) {
 229                         try {
 230                             out.add(imf);
 231                         } catch (Exception ex) {
 232                             throw new PluginException(ex);
 233                         }
 234                     }
 235                 }
 236             }
 237         } catch (IOException ex) {
 238             throw new UncheckedIOException(ex);
 239         }
 240 
 241         return out.build();
 242     }
 243 
 244     @Override
 245     public String getName() {
 246         return NAME;
 247     }
 248 
 249     @Override
 250     public String getDescription() {
 251         return PluginsResourceBundle.getDescription(NAME);
 252     }
 253 
 254     @Override
 255     public boolean hasArguments() {
 256         return true;
 257     }
 258 
 259     @Override
 260     public String getArgumentsDescription() {
 261        return PluginsResourceBundle.getArgument(NAME);
 262     }
 263 }