1 /*
   2  * Copyright (c) 2012, 2016, 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 com.oracle.tools.packager;
  27 
  28 import com.oracle.tools.packager.Log;
  29 import java.io.*;
  30 import java.net.URL;
  31 import java.nio.channels.FileChannel;
  32 import java.util.Arrays;
  33 
  34 /**
  35  * @deprecated use {@link ToolProvider} to locate the {@code "javapackager"} tool instead.
  36  */
  37 @Deprecated(since="10", forRemoval=true)
  38 public class IOUtils {
  39 
  40     public static boolean deleteRecursive(File path) throws FileNotFoundException {
  41         boolean ret = true;
  42         if (!path.exists()) { //nothing to do
  43             return true;
  44         }
  45         if (path.isDirectory()) {
  46             File[] children = path.listFiles();
  47             if (children != null) {
  48                 for (File f : children) {
  49                     ret = ret && deleteRecursive(f);
  50                 }
  51             }
  52         }
  53         return ret && path.delete();
  54     }
  55 
  56     public static void copyFromURL(URL location, File file) throws IOException {
  57         copyFromURL(location, file, false);
  58     }
  59 
  60     public static void copyFromURL(URL location, File file, boolean append) throws IOException {
  61         if (location == null) {
  62             throw new IOException("Missing input resource!");
  63         }
  64         if (file.exists() && !append) {
  65            file.delete();
  66         }
  67         InputStream in = location.openStream();
  68         FileOutputStream out = new FileOutputStream(file, append);
  69         byte[] buffer = new byte[1024];
  70         int len;
  71         while ((len = in.read(buffer)) != -1) {
  72             out.write(buffer, 0, len);
  73         }
  74         out.close();
  75         in.close();
  76         file.setReadOnly();
  77         file.setReadable(true, false);
  78     }
  79 
  80     public static void copyFile(File sourceFile, File destFile)
  81             throws IOException {
  82         destFile.getParentFile().mkdirs();
  83 
  84         //recreate the file as existing copy may have weird permissions
  85         destFile.delete();
  86         destFile.createNewFile();
  87 
  88         FileChannel source = null;
  89         FileChannel destination = null;
  90         source = new FileInputStream(sourceFile).getChannel();
  91         destination = new FileOutputStream(destFile).getChannel();
  92         if (destination != null && source != null) {
  93             destination.transferFrom(source, 0, source.size());
  94         }
  95         if (source != null) {
  96             source.close();
  97         }
  98         if (destination != null) {
  99             destination.close();
 100         }
 101 
 102         //preserve executable bit!
 103         if (sourceFile.canExecute()) {
 104             destFile.setExecutable(true, false);
 105         }
 106         if (!sourceFile.canWrite()) {
 107             destFile.setReadOnly();
 108         }
 109         destFile.setReadable(true, false);
 110     }
 111 
 112     public static long getFolderSize(File folder) {
 113         long foldersize = 0;
 114 
 115         File[] children = folder.listFiles();
 116         if (children != null) {
 117             for (File f : children) {
 118                 if (f.isDirectory()) {
 119                     foldersize += getFolderSize(f);
 120                 } else {
 121                     foldersize += f.length();
 122                 }
 123             }
 124         }
 125 
 126         return foldersize;
 127     }
 128 
 129     //run "launcher paramfile" in the directory where paramfile is kept
 130     public static void run(String launcher, File paramFile, boolean verbose)
 131             throws IOException {
 132         if (paramFile != null && paramFile.exists()) {
 133             ProcessBuilder pb = new ProcessBuilder(launcher, paramFile.getName());
 134             pb = pb.directory(paramFile.getParentFile());
 135             exec(pb, verbose);
 136         }
 137     }
 138 
 139     public static void exec(ProcessBuilder pb, boolean verbose) throws IOException {
 140         exec(pb, verbose, false);
 141     }
 142 
 143 
 144     public static void exec(ProcessBuilder pb, boolean verbose,
 145             boolean testForPresenseOnly) throws IOException {
 146         exec(pb, verbose, testForPresenseOnly, null);
 147     }
 148 
 149     public static void exec(ProcessBuilder pb, boolean verbose,
 150             boolean testForPresenseOnly, PrintStream consumer) throws IOException {
 151         pb.redirectErrorStream(true);
 152         Log.verbose("Running " + Arrays.toString(pb.command().toArray(new String[0]))
 153                                 + (pb.directory() != null ? (" in " + pb.directory()) : ""));
 154         Process p = pb.start();
 155         InputStreamReader isr = new InputStreamReader(p.getInputStream());
 156         BufferedReader br = new BufferedReader(isr);
 157         String lineRead;
 158         while ((lineRead = br.readLine()) != null) {
 159             if (consumer != null) {
 160                 consumer.print(lineRead + '\n');
 161             } else if (verbose) {
 162                Log.info(lineRead);
 163             } else {
 164                Log.debug(lineRead);
 165             }
 166         }
 167         try {
 168             int ret = p.waitFor();
 169             if (ret != 0 && !(testForPresenseOnly && ret != 127)) {
 170                 throw new IOException("Exec failed with code " + ret +
 171                         " command [" + Arrays.toString(pb.command().toArray(new String[0])) +
 172                         " in " + (pb.directory() != null ?
 173                            pb.directory().getAbsolutePath() : "unspecified directory"));
 174             }
 175         } catch (InterruptedException ex) {
 176         }
 177     }
 178 
 179     //no good test if we are running pre-JRE7
 180     //use heuristic approach
 181     // "false positive" is better than wrong answer
 182     public static boolean isNotSymbolicLink(File file) {
 183         //no symlinks on windows
 184         if (Platform.getPlatform() == Platform.WINDOWS) {
 185             return true;
 186         }
 187         try {
 188             if (file == null || file.getParent() == null) {
 189                 return false;
 190             }
 191             File file_canonical = new File(
 192                     file.getParentFile().getCanonicalFile(), file.getName());
 193             if (file_canonical.getCanonicalFile().equals(
 194                        file_canonical.getAbsoluteFile())) {
 195                 return true;
 196             }
 197         } catch (IOException ioe) {}
 198         return false;
 199     }
 200 
 201     public static byte[] readFully(File f) throws IOException {
 202         InputStream inp = new FileInputStream(f);
 203         //read fully into memory
 204         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 205         byte[] buffer = new byte[1024];
 206         int length = 0;
 207         while ((length = inp.read(buffer)) != -1) {
 208             baos.write(buffer, 0, length);
 209         }
 210         baos.close();
 211         return baos.toByteArray();
 212     }
 213 }