Coverage Report - org.owasp.dependencycheck.utils.FileUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
FileUtils
28%
9/32
16%
2/12
2.286
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  *
 16  
  * Copyright (c) 2012 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.utils;
 19  
 
 20  
 import org.slf4j.Logger;
 21  
 import org.slf4j.LoggerFactory;
 22  
 
 23  
 import java.io.File;
 24  
 import java.io.IOException;
 25  
 import java.io.UnsupportedEncodingException;
 26  
 import java.net.URLDecoder;
 27  
 import java.util.UUID;
 28  
 
 29  
 /**
 30  
  * A collection of utilities for processing information about files.
 31  
  *
 32  
  * @author Jeremy Long
 33  
  */
 34  
 public final class FileUtils {
 35  
 
 36  
     /**
 37  
      * The logger.
 38  
      */
 39  8
     private static final Logger LOGGER = LoggerFactory.getLogger(FileUtils.class);
 40  
     /**
 41  
      * Bit bucket for non-Windows systems
 42  
      */
 43  
     private static final String BIT_BUCKET_UNIX = "/dev/null";
 44  
 
 45  
     /**
 46  
      * Bit bucket for Windows systems (yes, only one 'L')
 47  
      */
 48  
     private static final String BIT_BUCKET_WIN = "NUL";
 49  
 
 50  
     /**
 51  
      * Private constructor for a utility class.
 52  
      */
 53  0
     private FileUtils() {
 54  0
     }
 55  
 
 56  
     /**
 57  
      * Returns the (lowercase) file extension for a specified file.
 58  
      *
 59  
      * @param fileName the file name to retrieve the file extension from.
 60  
      * @return the file extension.
 61  
      */
 62  
     public static String getFileExtension(String fileName) {
 63  16
         String ret = null;
 64  16
         final int pos = fileName.lastIndexOf(".");
 65  16
         if (pos >= 0) {
 66  16
             ret = fileName.substring(pos + 1, fileName.length()).toLowerCase();
 67  
         }
 68  16
         return ret;
 69  
     }
 70  
 
 71  
     /**
 72  
      * Deletes a file. If the File is a directory it will recursively delete the contents.
 73  
      *
 74  
      * @param file the File to delete
 75  
      * @return true if the file was deleted successfully, otherwise false
 76  
      */
 77  
     public static boolean delete(File file) {
 78  24
         boolean success = true;
 79  24
         if (!org.apache.commons.io.FileUtils.deleteQuietly(file)) {
 80  0
             success = false;
 81  0
             LOGGER.debug("Failed to delete file: {}; attempting to delete on exit.", file.getPath());
 82  0
             file.deleteOnExit();
 83  
         }
 84  24
         return success;
 85  
     }
 86  
 
 87  
     /**
 88  
      * Generates a new temporary file name that is guaranteed to be unique.
 89  
      *
 90  
      * @param prefix the prefix for the file name to generate
 91  
      * @param extension the extension of the generated file name
 92  
      * @return a temporary File
 93  
      * @throws java.io.IOException thrown if the temporary folder could not be created
 94  
      */
 95  
     public static File getTempFile(String prefix, String extension) throws IOException {
 96  0
         final File dir = Settings.getTempDirectory();
 97  0
         final String tempFileName = String.format("%s%s.%s", prefix, UUID.randomUUID().toString(), extension);
 98  0
         final File tempFile = new File(dir, tempFileName);
 99  0
         if (tempFile.exists()) {
 100  0
             return getTempFile(prefix, extension);
 101  
         }
 102  0
         return tempFile;
 103  
     }
 104  
 
 105  
     /**
 106  
      * Returns the data directory. If a path was specified in dependencycheck.properties or was specified using the Settings
 107  
      * object, and the path exists, that path will be returned as a File object. If it does not exist, then a File object will be
 108  
      * created based on the file location of the JAR containing the specified class.
 109  
      *
 110  
      * @param configuredFilePath the configured relative or absolute path
 111  
      * @param clazz the class to resolve the path
 112  
      * @return a File object
 113  
      * @throws IOException is thrown if the path could not be decoded
 114  
      * @deprecated This method should no longer be used. See the implementation in dependency-check-cli/App.java to see how the
 115  
      * data directory should be set.
 116  
      */
 117  
     @java.lang.Deprecated
 118  
     public static File getDataDirectory(String configuredFilePath, Class clazz) throws IOException {
 119  0
         final File file = new File(configuredFilePath);
 120  0
         if (file.isDirectory() && file.canWrite()) {
 121  0
             return new File(file.getCanonicalPath());
 122  
         } else {
 123  0
             final File exePath = getPathToJar(clazz);
 124  0
             return new File(exePath, configuredFilePath);
 125  
         }
 126  
     }
 127  
 
 128  
     /**
 129  
      * Retrieves the physical path to the parent directory containing the provided class. For example, if a JAR file contained a
 130  
      * class org.something.clazz this method would return the parent directory of the JAR file.
 131  
      *
 132  
      * @param clazz the class to determine the parent directory of
 133  
      * @return the parent directory of the file containing the specified class.
 134  
      * @throws UnsupportedEncodingException thrown if UTF-8 is not supported.
 135  
      * @deprecated this should no longer be used.
 136  
      */
 137  
     @java.lang.Deprecated
 138  
     public static File getPathToJar(Class clazz) throws UnsupportedEncodingException {
 139  0
         final String filePath = clazz.getProtectionDomain().getCodeSource().getLocation().getPath();
 140  0
         final String decodedPath = URLDecoder.decode(filePath, "UTF-8");
 141  0
         final File jarPath = new File(decodedPath);
 142  0
         return jarPath.getParentFile();
 143  
     }
 144  
 
 145  
     /**
 146  
      * Return the bit bucket for the OS. '/dev/null' for Unix and 'NUL' for Windows
 147  
      *
 148  
      * @return a String containing the bit bucket
 149  
      */
 150  
     public static String getBitBucket() {
 151  0
         if (System.getProperty("os.name").startsWith("Windows")) {
 152  0
             return BIT_BUCKET_WIN;
 153  
         } else {
 154  0
             return BIT_BUCKET_UNIX;
 155  
         }
 156  
     }
 157  
 }