| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| FileUtils |
|
| 2.857142857142857;2.857 |
| 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 java.io.Closeable; | |
| 21 | import org.apache.commons.io.FilenameUtils; | |
| 22 | import org.slf4j.Logger; | |
| 23 | import org.slf4j.LoggerFactory; | |
| 24 | ||
| 25 | import java.io.File; | |
| 26 | import java.io.IOException; | |
| 27 | import java.util.UUID; | |
| 28 | import org.apache.commons.lang3.SystemUtils; | |
| 29 | ||
| 30 | /** | |
| 31 | * A collection of utilities for processing information about files. | |
| 32 | * | |
| 33 | * @author Jeremy Long | |
| 34 | */ | |
| 35 | public final class FileUtils { | |
| 36 | ||
| 37 | /** | |
| 38 | * The logger. | |
| 39 | */ | |
| 40 | 1 | private static final Logger LOGGER = LoggerFactory.getLogger(FileUtils.class); |
| 41 | /** | |
| 42 | * Bit bucket for non-Windows systems | |
| 43 | */ | |
| 44 | private static final String BIT_BUCKET_UNIX = "/dev/null"; | |
| 45 | ||
| 46 | /** | |
| 47 | * Bit bucket for Windows systems (yes, only one 'L') | |
| 48 | */ | |
| 49 | private static final String BIT_BUCKET_WIN = "NUL"; | |
| 50 | ||
| 51 | /** | |
| 52 | * Private constructor for a utility class. | |
| 53 | */ | |
| 54 | 0 | private FileUtils() { |
| 55 | 0 | } |
| 56 | ||
| 57 | /** | |
| 58 | * Returns the (lowercase) file extension for a specified file. | |
| 59 | * | |
| 60 | * @param fileName the file name to retrieve the file extension from. | |
| 61 | * @return the file extension. | |
| 62 | */ | |
| 63 | public static String getFileExtension(String fileName) { | |
| 64 | 3 | final String fileExt = FilenameUtils.getExtension(fileName); |
| 65 | 3 | return null == fileExt || fileExt.isEmpty() ? null : fileExt.toLowerCase(); |
| 66 | } | |
| 67 | ||
| 68 | /** | |
| 69 | * Deletes a file. If the File is a directory it will recursively delete the | |
| 70 | * contents. | |
| 71 | * | |
| 72 | * @param file the File to delete | |
| 73 | * @return true if the file was deleted successfully, otherwise false | |
| 74 | */ | |
| 75 | public static boolean delete(File file) { | |
| 76 | 3 | final boolean success = org.apache.commons.io.FileUtils.deleteQuietly(file); |
| 77 | 3 | if (!success) { |
| 78 | 0 | LOGGER.debug("Failed to delete file: {}; attempting to delete on exit.", file.getPath()); |
| 79 | 0 | file.deleteOnExit(); |
| 80 | } | |
| 81 | 3 | return success; |
| 82 | } | |
| 83 | ||
| 84 | /** | |
| 85 | * Creates a unique temporary directory in the given directory. | |
| 86 | * | |
| 87 | * @param base the base directory to create a temporary directory within | |
| 88 | * @return the temporary directory | |
| 89 | * @throws IOException thrown when a directory cannot be created within the | |
| 90 | * base directory | |
| 91 | */ | |
| 92 | public static File createTempDirectory(File base) throws IOException { | |
| 93 | 2 | final File tempDir = new File(base, "dctemp" + UUID.randomUUID().toString()); |
| 94 | 2 | if (tempDir.exists()) { |
| 95 | 0 | return createTempDirectory(base); |
| 96 | } | |
| 97 | 2 | if (!tempDir.mkdirs()) { |
| 98 | 0 | throw new IOException("Could not create temp directory `" + tempDir.getAbsolutePath() + "`"); |
| 99 | } | |
| 100 | 2 | return tempDir; |
| 101 | } | |
| 102 | ||
| 103 | /** | |
| 104 | * Generates a new temporary file name that is guaranteed to be unique. | |
| 105 | * | |
| 106 | * @param prefix the prefix for the file name to generate | |
| 107 | * @param extension the extension of the generated file name | |
| 108 | * @return a temporary File | |
| 109 | * @throws java.io.IOException thrown if the temporary folder could not be | |
| 110 | * created | |
| 111 | */ | |
| 112 | public static File getTempFile(String prefix, String extension) throws IOException { | |
| 113 | 0 | final File dir = Settings.getTempDirectory(); |
| 114 | 0 | final String tempFileName = String.format("%s%s.%s", prefix, UUID.randomUUID().toString(), extension); |
| 115 | 0 | final File tempFile = new File(dir, tempFileName); |
| 116 | 0 | if (tempFile.exists()) { |
| 117 | 0 | return getTempFile(prefix, extension); |
| 118 | } | |
| 119 | 0 | return tempFile; |
| 120 | } | |
| 121 | ||
| 122 | /** | |
| 123 | * Return the bit bucket for the OS. '/dev/null' for Unix and 'NUL' for | |
| 124 | * Windows | |
| 125 | * | |
| 126 | * @return a String containing the bit bucket | |
| 127 | */ | |
| 128 | public static String getBitBucket() { | |
| 129 | 0 | if (SystemUtils.IS_OS_WINDOWS) { |
| 130 | 0 | return BIT_BUCKET_WIN; |
| 131 | } else { | |
| 132 | 0 | return BIT_BUCKET_UNIX; |
| 133 | } | |
| 134 | } | |
| 135 | ||
| 136 | /** | |
| 137 | * Close the given {@link Closeable} instance, ignoring nulls, and logging | |
| 138 | * any thrown {@link IOException}. | |
| 139 | * | |
| 140 | * @param closeable to be closed | |
| 141 | */ | |
| 142 | public static void close(Closeable closeable) { | |
| 143 | 0 | if (null != closeable) { |
| 144 | try { | |
| 145 | 0 | closeable.close(); |
| 146 | 0 | } catch (IOException ex) { |
| 147 | 0 | LOGGER.trace("", ex); |
| 148 | 0 | } |
| 149 | } | |
| 150 | 0 | } |
| 151 | } |