Coverage Report - org.owasp.dependencycheck.utils.FileUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
FileUtils
48%
41/85
50%
20/40
5.375
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Dependency-check-core is free software: you can redistribute it and/or modify it
 5  
  * under the terms of the GNU General Public License as published by the Free
 6  
  * Software Foundation, either version 3 of the License, or (at your option) any
 7  
  * later version.
 8  
  *
 9  
  * Dependency-check-core is distributed in the hope that it will be useful, but
 10  
  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  
  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 12  
  * details.
 13  
  *
 14  
  * You should have received a copy of the GNU General Public License along with
 15  
  * dependency-check-core. If not, see http://www.gnu.org/licenses/.
 16  
  *
 17  
  * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
 18  
  */
 19  
 package org.owasp.dependencycheck.utils;
 20  
 
 21  
 import java.io.BufferedInputStream;
 22  
 import java.io.BufferedOutputStream;
 23  
 import java.io.File;
 24  
 import java.io.FileInputStream;
 25  
 import java.io.FileNotFoundException;
 26  
 import java.io.FileOutputStream;
 27  
 import java.io.IOException;
 28  
 import java.io.UnsupportedEncodingException;
 29  
 import java.net.URLDecoder;
 30  
 import java.util.logging.Level;
 31  
 import java.util.logging.Logger;
 32  
 import java.util.zip.ZipEntry;
 33  
 import java.util.zip.ZipInputStream;
 34  
 import org.owasp.dependencycheck.Engine;
 35  
 
 36  
 /**
 37  
  * A collection of utilities for processing information about files.
 38  
  *
 39  
  * @author Jeremy Long (jeremy.long@owasp.org)
 40  
  */
 41  
 public final class FileUtils {
 42  
 
 43  
     /**
 44  
      * The buffer size to use when extracting files from the archive.
 45  
      */
 46  
     private static final int BUFFER_SIZE = 4096;
 47  
 
 48  
     /**
 49  
      * Private constructor for a utility class.
 50  
      */
 51  0
     private FileUtils() {
 52  0
     }
 53  
 
 54  
     /**
 55  
      * Returns the (lowercase) file extension for a specified file.
 56  
      *
 57  
      * @param fileName the file name to retrieve the file extension from.
 58  
      * @return the file extension.
 59  
      */
 60  
     public static String getFileExtension(String fileName) {
 61  199
         String ret = null;
 62  199
         final int pos = fileName.lastIndexOf(".");
 63  199
         if (pos >= 0) {
 64  189
             ret = fileName.substring(pos + 1, fileName.length()).toLowerCase();
 65  
         }
 66  199
         return ret;
 67  
     }
 68  
 
 69  
     /**
 70  
      * Deletes a file. If the File is a directory it will recursively delete the
 71  
      * contents.
 72  
      *
 73  
      * @param file the File to delete
 74  
      * @throws IOException is thrown if the file could not be deleted
 75  
      */
 76  
     public static void delete(File file) throws IOException {
 77  35
         if (file.isDirectory()) {
 78  32
             for (File c : file.listFiles()) {
 79  28
                 delete(c);
 80  
             }
 81  
         }
 82  35
         if (!org.apache.commons.io.FileUtils.deleteQuietly(file)) {
 83  
             //if (!file.delete()) {
 84  0
             throw new FileNotFoundException("Failed to delete file: " + file);
 85  
         } else {
 86  35
             file.deleteOnExit();
 87  
         }
 88  35
     }
 89  
 
 90  
     /**
 91  
      * Deletes a file. If the File is a directory it will recursively delete the
 92  
      * contents.
 93  
      *
 94  
      * @param file the File to delete
 95  
      * @param deleteOnExit setting this to true will cause errors to be ignored
 96  
      * and if there is an error deleting the file it will be setup to be deleted
 97  
      * when the JVM exits.
 98  
      * @throws IOException is thrown if the file could not be deleted
 99  
      */
 100  
     public static void delete(File file, boolean deleteOnExit) throws IOException {
 101  0
         if (file.isDirectory()) {
 102  0
             for (File c : file.listFiles()) {
 103  0
                 delete(c);
 104  
             }
 105  
         }
 106  0
         if (!org.apache.commons.io.FileUtils.deleteQuietly(file)) {
 107  
             //if (!file.delete()) {
 108  0
             if (deleteOnExit) {
 109  0
                 file.deleteOnExit();
 110  
             } else {
 111  0
                 throw new FileNotFoundException("Failed to delete file: " + file);
 112  
             }
 113  
         }
 114  0
     }
 115  
 
 116  
     /**
 117  
      * Returns the data directory. If a path was specified in
 118  
      * dependencycheck.properties or was specified using the Settings object,
 119  
      * and the path exists, that path will be returned as a File object. If it
 120  
      * does not exist, then a File object will be created based on the file
 121  
      * location of the JAR containing the specified class.
 122  
      *
 123  
      * @param configuredFilePath the configured relative or absolute path
 124  
      * @param clazz the class whos path will be resolved
 125  
      * @return a File object
 126  
      * @throws IOException is thrown if the path could not be decoded
 127  
      * @deprecated This method should no longer be used. See the implementation
 128  
      * in dependency-check-cli/App.java to see how the data directory should be
 129  
      * set.
 130  
      */
 131  
     @java.lang.Deprecated
 132  
     public static File getDataDirectory(String configuredFilePath, Class clazz) throws IOException {
 133  0
         final File file = new File(configuredFilePath);
 134  0
         if (file.isDirectory() && file.canWrite()) {
 135  0
             return new File(file.getCanonicalPath());
 136  
         } else {
 137  0
             final File exePath = getPathToJar(clazz);
 138  0
             return new File(exePath, configuredFilePath);
 139  
         }
 140  
     }
 141  
 
 142  
     /**
 143  
      * Retrieves the physical path to the parent directory containing the
 144  
      * provided class. For example, if a JAR file contained a class
 145  
      * org.something.clazz this method would return the parent directory of the
 146  
      * JAR file.
 147  
      *
 148  
      * @param clazz the class to determine the parent directory of
 149  
      * @return the parent directory of the file containing the specified class.
 150  
      * @throws UnsupportedEncodingException thrown if UTF-8 is not supported.
 151  
      * @deprecated this should no longer be used.
 152  
      */
 153  
     @java.lang.Deprecated
 154  
     public static File getPathToJar(Class clazz) throws UnsupportedEncodingException {
 155  0
         final String filePath = clazz.getProtectionDomain().getCodeSource().getLocation().getPath();
 156  0
         final String decodedPath = URLDecoder.decode(filePath, "UTF-8");
 157  0
         final File jarPath = new File(decodedPath);
 158  0
         return jarPath.getParentFile();
 159  
     }
 160  
 
 161  
     /**
 162  
      * Extracts the contents of an archive into the specified directory.
 163  
      *
 164  
      * @param archive an archive file such as a WAR or EAR
 165  
      * @param extractTo a directory to extract the contents to
 166  
      * @throws ExtractionException thrown if an exception occurs while
 167  
      * extracting the files
 168  
      */
 169  
     public static void extractFiles(File archive, File extractTo) throws ExtractionException {
 170  2
         extractFiles(archive, extractTo, null);
 171  2
     }
 172  
 
 173  
     /**
 174  
      * Extracts the contents of an archive into the specified directory. The
 175  
      * files are only extracted if they are supported by the analyzers loaded
 176  
      * into the specified engine. If the engine is specified as null then all
 177  
      * files are extracted.
 178  
      *
 179  
      * @param archive an archive file such as a WAR or EAR
 180  
      * @param extractTo a directory to extract the contents to
 181  
      * @param engine the scanning engine
 182  
      * @throws ExtractionException thrown if there is an error extracting the
 183  
      * files
 184  
      */
 185  
     public static void extractFiles(File archive, File extractTo, Engine engine) throws ExtractionException {
 186  2
         if (archive == null || extractTo == null) {
 187  0
             return;
 188  
         }
 189  
 
 190  2
         FileInputStream fis = null;
 191  2
         ZipInputStream zis = null;
 192  
 
 193  
         try {
 194  2
             fis = new FileInputStream(archive);
 195  0
         } catch (FileNotFoundException ex) {
 196  0
             Logger.getLogger(FileUtils.class.getName()).log(Level.INFO, null, ex);
 197  0
             throw new ExtractionException("Archive file was not found.", ex);
 198  2
         }
 199  2
         zis = new ZipInputStream(new BufferedInputStream(fis));
 200  
         ZipEntry entry;
 201  
         try {
 202  36
             while ((entry = zis.getNextEntry()) != null) {
 203  34
                 if (entry.isDirectory()) {
 204  4
                     final File d = new File(extractTo, entry.getName());
 205  4
                     if (!d.exists() && !d.mkdirs()) {
 206  0
                         final String msg = String.format("Unable to create '%s'.", d.getAbsolutePath());
 207  0
                         throw new ExtractionException(msg);
 208  
                     }
 209  4
                 } else {
 210  30
                     final File file = new File(extractTo, entry.getName());
 211  30
                     final String ext = getFileExtension(file.getName());
 212  30
                     if (engine == null || engine.supportsExtension(ext)) {
 213  30
                         BufferedOutputStream bos = null;
 214  
                         FileOutputStream fos;
 215  
                         try {
 216  30
                             fos = new FileOutputStream(file);
 217  30
                             bos = new BufferedOutputStream(fos, BUFFER_SIZE);
 218  
                             int count;
 219  30
                             final byte data[] = new byte[BUFFER_SIZE];
 220  191432
                             while ((count = zis.read(data, 0, BUFFER_SIZE)) != -1) {
 221  191402
                                 bos.write(data, 0, count);
 222  
                             }
 223  30
                             bos.flush();
 224  0
                         } catch (FileNotFoundException ex) {
 225  0
                             Logger.getLogger(FileUtils.class.getName()).log(Level.FINE, null, ex);
 226  0
                             final String msg = String.format("Unable to find file '%s'.", file.getName());
 227  0
                             throw new ExtractionException(msg, ex);
 228  0
                         } catch (IOException ex) {
 229  0
                             Logger.getLogger(FileUtils.class.getName()).log(Level.FINE, null, ex);
 230  0
                             final String msg = String.format("IO Exception while parsing file '%s'.", file.getName());
 231  0
                             throw new ExtractionException(msg, ex);
 232  
                         } finally {
 233  30
                             if (bos != null) {
 234  
                                 try {
 235  30
                                     bos.close();
 236  0
                                 } catch (IOException ex) {
 237  0
                                     Logger.getLogger(FileUtils.class.getName()).log(Level.FINEST, null, ex);
 238  30
                                 }
 239  
                             }
 240  
                         }
 241  
                     }
 242  30
                 }
 243  
             }
 244  0
         } catch (IOException ex) {
 245  0
             final String msg = String.format("Exception reading archive '%s'.", archive.getName());
 246  0
             Logger.getLogger(FileUtils.class.getName()).log(Level.FINE, msg, ex);
 247  0
             throw new ExtractionException(msg, ex);
 248  
         } finally {
 249  0
             try {
 250  2
                 zis.close();
 251  0
             } catch (IOException ex) {
 252  0
                 Logger.getLogger(FileUtils.class.getName()).log(Level.FINEST, null, ex);
 253  2
             }
 254  0
         }
 255  2
     }
 256  
 }