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 copyFile(File sourceFile, File destFile)
 125             throws IOException {
 126         destFile.getParentFile().mkdirs();
 127 
 128         //recreate the file as existing copy may have weird permissions
 129         destFile.delete();
 130         destFile.createNewFile();
 131 
 132         try (FileChannel source = new FileInputStream(sourceFile).getChannel();
 133             FileChannel destination =
 134                     new FileOutputStream(destFile).getChannel()) {
 135 
 136             if (destination != null && source != null) {
 137                 destination.transferFrom(source, 0, source.size());
 138             }
 139         }
 140 
 141         //preserve executable bit!
 142         if (sourceFile.canExecute()) {
 143             destFile.setExecutable(true, false);
 144         }
 145         if (!sourceFile.canWrite()) {
 146             destFile.setReadOnly();
 147         }
 148         destFile.setReadable(true, false);
 149     }
 150 
 151     public static long getFolderSize(File folder) {
 152         long foldersize = 0;
 153 
 154         File[] children = folder.listFiles();
 155         if (children != null) {
 156             for (File f : children) {
 157                 if (f.isDirectory()) {
 158                     foldersize += getFolderSize(f);
 159                 } else {
 160                     foldersize += f.length();
 161                 }
 162             }
 163         }
 164 
 165         return foldersize;
 166     }
 167 
 168     // run "launcher paramfile" in the directory where paramfile is kept
 169     public static void run(String launcher, File paramFile)
 170             throws IOException {
 171         if (paramFile != null && paramFile.exists()) {
 172             ProcessBuilder pb =
 173                     new ProcessBuilder(launcher, paramFile.getName());
 174             pb = pb.directory(paramFile.getParentFile());
 175             exec(pb);
 176         }
 177     }
 178 
 179     public static void exec(ProcessBuilder pb)
 180             throws IOException {
 181         exec(pb, false, null);
 182     }
 183 
 184     public static void exec(ProcessBuilder pb, boolean testForPresenseOnly,
 185             PrintStream consumer) throws IOException {
 186         pb.redirectErrorStream(true);
 187         Log.verbose("Running "
 188                 + Arrays.toString(pb.command().toArray(new String[0]))
 189                 + (pb.directory() != null ? (" in " + pb.directory()) : ""));
 190         Process p = pb.start();
 191         InputStreamReader isr = new InputStreamReader(p.getInputStream());
 192         BufferedReader br = new BufferedReader(isr);
 193         String lineRead;
 194         while ((lineRead = br.readLine()) != null) {
 195             if (consumer != null) {
 196                 consumer.print(lineRead + '\n');
 197             } else {
 198                Log.verbose(lineRead);
 199             }
 200         }
 201         try {
 202             int ret = p.waitFor();
 203             if (ret != 0 && !(testForPresenseOnly && ret != 127)) {
 204                 throw new IOException("Exec failed with code "
 205                         + ret + " command ["
 206                         + Arrays.toString(pb.command().toArray(new String[0]))
 207                         + " in " + (pb.directory() != null ?
 208                                 pb.directory().getAbsolutePath() :
 209                                 "unspecified directory"));
 210             }
 211         } catch (InterruptedException ex) {
 212         }
 213     }
 214 
 215     public static int getProcessOutput(List<String> result, String... args)
 216             throws IOException, InterruptedException {
 217 
 218         ProcessBuilder pb = new ProcessBuilder(args);
 219 
 220         final Process p = pb.start();
 221 
 222         List<String> list = new ArrayList<>();
 223 
 224         final BufferedReader in =
 225                 new BufferedReader(new InputStreamReader(p.getInputStream()));
 226         final BufferedReader err =
 227                 new BufferedReader(new InputStreamReader(p.getErrorStream()));
 228 
 229         Thread t = new Thread(() -> {
 230             try {
 231                 String line;
 232                 while ((line = in.readLine()) != null) {
 233                     list.add(line);
 234                 }
 235             } catch (IOException ioe) {
 236                 Log.verbose(ioe);
 237             }
 238 
 239             try {
 240                 String line;
 241                 while ((line = err.readLine()) != null) {
 242                     Log.error(line);
 243                 }
 244             } catch (IOException ioe) {
 245                   Log.verbose(ioe);
 246             }
 247         });
 248         t.setDaemon(true);
 249         t.start();
 250 
 251         int ret = p.waitFor();
 252 
 253         result.clear();
 254         result.addAll(list);
 255 
 256         return ret;
 257     }
 258 
 259     static void writableOutputDir(Path outdir) throws PackagerException {
 260         File file = outdir.toFile();
 261 
 262         if (!file.isDirectory() && !file.mkdirs()) {
 263             throw new PackagerException("error.cannot-create-output-dir",
 264                     file.getAbsolutePath());
 265         }
 266         if (!file.canWrite()) {
 267             throw new PackagerException("error.cannot-write-to-output-dir",
 268                     file.getAbsolutePath());
 269         }
 270     }
 271 }