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         InputStream in = location.openStream();
 137         FileOutputStream out = new FileOutputStream(file, append);
 138         byte[] buffer = new byte[1024];
 139         int len;
 140         while ((len = in.read(buffer)) != -1) {
 141             out.write(buffer, 0, len);
 142         }
 143         out.close();
 144         in.close();
 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         FileChannel source = null;
 158         FileChannel destination = null;
 159         source = new FileInputStream(sourceFile).getChannel();
 160         destination = new FileOutputStream(destFile).getChannel();
 161         if (destination != null && source != null) {
 162             destination.transferFrom(source, 0, source.size());
 163         }
 164         if (source != null) {
 165             source.close();
 166         }
 167         if (destination != null) {
 168             destination.close();
 169         }
 170 
 171         //preserve executable bit!
 172         if (sourceFile.canExecute()) {
 173             destFile.setExecutable(true, false);
 174         }
 175         if (!sourceFile.canWrite()) {
 176             destFile.setReadOnly();
 177         }
 178         destFile.setReadable(true, false);
 179     }
 180 
 181     public static long getFolderSize(File folder) {
 182         long foldersize = 0;
 183 
 184         File[] children = folder.listFiles();
 185         if (children != null) {
 186             for (File f : children) {
 187                 if (f.isDirectory()) {
 188                     foldersize += getFolderSize(f);
 189                 } else {
 190                     foldersize += f.length();
 191                 }
 192             }
 193         }
 194 
 195         return foldersize;
 196     }
 197 
 198     // run "launcher paramfile" in the directory where paramfile is kept
 199     public static void run(String launcher, File paramFile, boolean verbose)
 200             throws IOException {
 201         if (paramFile != null && paramFile.exists()) {
 202             ProcessBuilder pb =
 203                     new ProcessBuilder(launcher, paramFile.getName());
 204             pb = pb.directory(paramFile.getParentFile());
 205             exec(pb, verbose);
 206         }
 207     }
 208 
 209     public static void exec(ProcessBuilder pb, boolean verbose)
 210             throws IOException {
 211         exec(pb, verbose, false);
 212     }
 213 
 214     public static void exec(ProcessBuilder pb, boolean verbose,
 215             boolean testForPresenseOnly) throws IOException {
 216         exec(pb, verbose, testForPresenseOnly, null);
 217     }
 218 
 219     public static void exec(ProcessBuilder pb, boolean verbose,
 220             boolean testForPresenseOnly, PrintStream consumer)
 221             throws IOException {
 222         pb.redirectErrorStream(true);
 223         Log.verbose("Running "
 224                 + Arrays.toString(pb.command().toArray(new String[0]))
 225                 + (pb.directory() != null ? (" in " + pb.directory()) : ""));
 226         Process p = pb.start();
 227         InputStreamReader isr = new InputStreamReader(p.getInputStream());
 228         BufferedReader br = new BufferedReader(isr);
 229         String lineRead;
 230         while ((lineRead = br.readLine()) != null) {
 231             if (consumer != null) {
 232                 consumer.print(lineRead + '\n');
 233             } else {
 234                Log.verbose(lineRead);
 235             }
 236         }
 237         try {
 238             int ret = p.waitFor();
 239             if (ret != 0 && !(testForPresenseOnly && ret != 127)) {
 240                 throw new IOException("Exec failed with code "
 241                         + ret + " command ["
 242                         + Arrays.toString(pb.command().toArray(new String[0]))
 243                         + " in " + (pb.directory() != null ?
 244                                 pb.directory().getAbsolutePath() :
 245                                 "unspecified directory"));
 246             }
 247         } catch (InterruptedException ex) {
 248         }
 249     }
 250 
 251     @SuppressWarnings("unchecked")
 252     private static Process startProcess(Object... args) throws IOException {
 253         final ArrayList<String> argsList = new ArrayList<>();
 254         for (Object a : args) {
 255             if (a instanceof List) {
 256                 argsList.addAll((List)a);
 257             } else if (a instanceof String) {
 258                 argsList.add((String)a);
 259             }
 260         }
 261 
 262         return Runtime.getRuntime().exec(
 263                 argsList.toArray(new String[argsList.size()]));
 264     }
 265 
 266     private static void logErrorStream(Process p) {
 267         final BufferedReader err =
 268                 new BufferedReader(new InputStreamReader(p.getErrorStream()));
 269         Thread t = new Thread(() -> {
 270             try {
 271                 String line;
 272                 while ((line = err.readLine()) != null) {
 273                     Log.error(line);
 274                 }
 275             } catch (IOException ioe) {
 276                 Log.verbose(ioe);
 277             }
 278         });
 279         t.setDaemon(true);
 280         t.start();
 281     }
 282 
 283     public static int getProcessOutput(List<String> result, Object... args)
 284             throws IOException, InterruptedException {
 285         final Process p = startProcess(args);
 286 
 287         List<String> list = new ArrayList<>();
 288         final BufferedReader in =
 289                 new BufferedReader(new InputStreamReader(p.getInputStream()));
 290         Thread t = new Thread(() -> {
 291             try {
 292                 String line;
 293                 while ((line = in.readLine()) != null) {
 294                     list.add(line);
 295                 }
 296             } catch (IOException ioe) {
 297                 jdk.jpackage.internal.Log.verbose(ioe);
 298             }
 299         });
 300         t.setDaemon(true);
 301         t.start();
 302 
 303         logErrorStream(p);
 304 
 305         int ret = p.waitFor();
 306 
 307         result.clear();
 308         result.addAll(list);
 309 
 310         return ret;
 311     }
 312 }