1 /*
   2  * Copyright (c) 2012, 2019, 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 
  26 package jdk.jpackage.internal;
  27 
  28 import java.io.*;
  29 import java.net.URL;
  30 import java.util.Arrays;
  31 import java.nio.channels.FileChannel;
  32 import java.nio.file.FileVisitResult;
  33 import java.nio.file.Files;
  34 import java.nio.file.Path;
  35 import java.nio.file.SimpleFileVisitor;
  36 import java.nio.file.attribute.BasicFileAttributes;
  37 import java.util.ArrayList;
  38 import java.util.List;
  39 
  40 /**
  41  * IOUtils
  42  *
  43  * A collection of static utility methods.
  44  */
  45 public class IOUtils {
  46 
  47     public static void deleteRecursive(File path) throws IOException {
  48         if (!path.exists()) {
  49             return;
  50         }
  51         Path directory = path.toPath();
  52         Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {
  53             @Override
  54             public FileVisitResult visitFile(Path file,
  55                             BasicFileAttributes attr) throws IOException {
  56                 if (Platform.getPlatform() == Platform.WINDOWS) {
  57                     Files.setAttribute(file, "dos:readonly", false);
  58                 }
  59                 Files.delete(file);
  60                 return FileVisitResult.CONTINUE;
  61             }
  62 
  63             @Override
  64             public FileVisitResult preVisitDirectory(Path dir,
  65                             BasicFileAttributes attr) throws IOException {
  66                 if (Platform.getPlatform() == Platform.WINDOWS) {
  67                     Files.setAttribute(dir, "dos:readonly", false);
  68                 }
  69                 return FileVisitResult.CONTINUE;
  70             }
  71 
  72             @Override
  73             public FileVisitResult postVisitDirectory(Path dir, IOException e)
  74                             throws IOException {
  75                 Files.delete(dir);
  76                 return FileVisitResult.CONTINUE;
  77             }
  78         });
  79     }
  80 
  81     public static void copyRecursive(Path src, Path dest) throws IOException {
  82         Files.walkFileTree(src, new SimpleFileVisitor<Path>() {
  83             @Override
  84             public FileVisitResult preVisitDirectory(final Path dir,
  85                     final BasicFileAttributes attrs) throws IOException {
  86                 Files.createDirectories(dest.resolve(src.relativize(dir)));
  87                 return FileVisitResult.CONTINUE;
  88             }
  89 
  90             @Override
  91             public FileVisitResult visitFile(final Path file,
  92                     final BasicFileAttributes attrs) throws IOException {
  93                 Files.copy(file, dest.resolve(src.relativize(file)));
  94                 return FileVisitResult.CONTINUE;
  95             }
  96         });
  97     }
  98 
  99     public static void copyRecursive(Path src, Path dest,
 100             final List<String> excludes) throws IOException {
 101         Files.walkFileTree(src, new SimpleFileVisitor<Path>() {
 102             @Override
 103             public FileVisitResult preVisitDirectory(final Path dir,
 104                     final BasicFileAttributes attrs) throws IOException {
 105                 if (excludes.contains(dir.toFile().getName())) {
 106                     return FileVisitResult.SKIP_SUBTREE;
 107                 } else {
 108                     Files.createDirectories(dest.resolve(src.relativize(dir)));
 109                     return FileVisitResult.CONTINUE;
 110                 }
 111             }
 112 
 113             @Override
 114             public FileVisitResult visitFile(final Path file,
 115                     final BasicFileAttributes attrs) throws IOException {
 116                 if (!excludes.contains(file.toFile().getName())) {
 117                     Files.copy(file, dest.resolve(src.relativize(file)));
 118                 }
 119                 return FileVisitResult.CONTINUE;
 120             }
 121         });
 122     }
 123 
 124     public static void copyFromURL(URL location, File file) throws IOException {
 125         copyFromURL(location, file, false);
 126     }
 127 
 128     public static void copyFromURL(URL location, File file, boolean append)
 129             throws IOException {
 130         if (location == null) {
 131             throw new IOException("Missing input resource!");
 132         }
 133         if (file.exists() && !append) {
 134            file.delete();
 135         }
 136         try (InputStream in = location.openStream();
 137             FileOutputStream out = new FileOutputStream(file, append)) {
 138 
 139             byte[] buffer = new byte[1024];
 140             int len;
 141             while ((len = in.read(buffer)) != -1) {
 142                 out.write(buffer, 0, len);
 143             }
 144         }
 145         file.setReadOnly();
 146         file.setReadable(true, false);
 147     }
 148 
 149     public static void copyFile(File sourceFile, File destFile)
 150             throws IOException {
 151         destFile.getParentFile().mkdirs();
 152 
 153         //recreate the file as existing copy may have weird permissions
 154         destFile.delete();
 155         destFile.createNewFile();
 156 
 157         try (FileChannel source = new FileInputStream(sourceFile).getChannel();
 158             FileChannel destination =
 159                     new FileOutputStream(destFile).getChannel()) {
 160 
 161             if (destination != null && source != null) {
 162                 destination.transferFrom(source, 0, source.size());
 163             }
 164         }
 165 
 166         //preserve executable bit!
 167         if (sourceFile.canExecute()) {
 168             destFile.setExecutable(true, false);
 169         }
 170         if (!sourceFile.canWrite()) {
 171             destFile.setReadOnly();
 172         }
 173         destFile.setReadable(true, false);
 174     }
 175 
 176     public static long getFolderSize(File folder) {
 177         long foldersize = 0;
 178 
 179         File[] children = folder.listFiles();
 180         if (children != null) {
 181             for (File f : children) {
 182                 if (f.isDirectory()) {
 183                     foldersize += getFolderSize(f);
 184                 } else {
 185                     foldersize += f.length();
 186                 }
 187             }
 188         }
 189 
 190         return foldersize;
 191     }
 192 
 193     // run "launcher paramfile" in the directory where paramfile is kept
 194     public static void run(String launcher, File paramFile)
 195             throws IOException {
 196         if (paramFile != null && paramFile.exists()) {
 197             ProcessBuilder pb =
 198                     new ProcessBuilder(launcher, paramFile.getName());
 199             pb = pb.directory(paramFile.getParentFile());
 200             exec(pb);
 201         }
 202     }
 203 
 204     public static void exec(ProcessBuilder pb)
 205             throws IOException {
 206         exec(pb, false, null);
 207     }
 208 
 209     public static void exec(ProcessBuilder pb, boolean testForPresenseOnly,
 210             PrintStream consumer) throws IOException {
 211         pb.redirectErrorStream(true);
 212         Log.verbose("Running "
 213                 + Arrays.toString(pb.command().toArray(new String[0]))
 214                 + (pb.directory() != null ? (" in " + pb.directory()) : ""));
 215         Process p = pb.start();
 216         InputStreamReader isr = new InputStreamReader(p.getInputStream());
 217         BufferedReader br = new BufferedReader(isr);
 218         String lineRead;
 219         while ((lineRead = br.readLine()) != null) {
 220             if (consumer != null) {
 221                 consumer.print(lineRead + '\n');
 222             } else {
 223                Log.verbose(lineRead);
 224             }
 225         }
 226         try {
 227             int ret = p.waitFor();
 228             if (ret != 0 && !(testForPresenseOnly && ret != 127)) {
 229                 throw new IOException("Exec failed with code "
 230                         + ret + " command ["
 231                         + Arrays.toString(pb.command().toArray(new String[0]))
 232                         + " in " + (pb.directory() != null ?
 233                                 pb.directory().getAbsolutePath() :
 234                                 "unspecified directory"));
 235             }
 236         } catch (InterruptedException ex) {
 237         }
 238     }
 239 
 240     public static int getProcessOutput(List<String> result, String... args)
 241             throws IOException, InterruptedException {
 242 
 243         ProcessBuilder pb = new ProcessBuilder(args);
 244 
 245         final Process p = pb.start();
 246 
 247         List<String> list = new ArrayList<>();
 248 
 249         final BufferedReader in =
 250                 new BufferedReader(new InputStreamReader(p.getInputStream()));
 251         final BufferedReader err =
 252                 new BufferedReader(new InputStreamReader(p.getErrorStream()));
 253 
 254         Thread t = new Thread(() -> {
 255             try {
 256                 String line;
 257                 while ((line = in.readLine()) != null) {
 258                     list.add(line);
 259                 }
 260             } catch (IOException ioe) {
 261                 Log.verbose(ioe);
 262             }
 263 
 264             try {
 265                 String line;
 266                 while ((line = err.readLine()) != null) {
 267                     Log.error(line);
 268                 }
 269             } catch (IOException ioe) {
 270                   Log.verbose(ioe);
 271             }
 272         });
 273         t.setDaemon(true);
 274         t.start();
 275 
 276         int ret = p.waitFor();
 277 
 278         result.clear();
 279         result.addAll(list);
 280 
 281         return ret;
 282     }
 283 
 284     static void writableOutputDir(Path outdir) throws PackagerException {
 285         File file = outdir.toFile();
 286 
 287         if (!file.isDirectory() && !file.mkdirs()) {
 288             throw new PackagerException("error.cannot-create-output-dir",
 289                     file.getAbsolutePath());
 290         }
 291         if (!file.canWrite()) {
 292             throw new PackagerException("error.cannot-write-to-output-dir",
 293                     file.getAbsolutePath());
 294         }
 295     }
 296 }