| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| FileUtils |
|
| 2.4;2.4 |
| 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.apache.commons.io.FilenameUtils; | |
| 21 | import org.slf4j.Logger; | |
| 22 | import org.slf4j.LoggerFactory; | |
| 23 | ||
| 24 | import java.io.File; | |
| 25 | import java.io.IOException; | |
| 26 | import java.util.UUID; | |
| 27 | import org.apache.commons.lang3.SystemUtils; | |
| 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 | 1 | 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 | 3 | final String fileExt = FilenameUtils.getExtension(fileName); |
| 64 | 3 | return null == fileExt || fileExt.isEmpty() ? null : fileExt.toLowerCase(); |
| 65 | } | |
| 66 | ||
| 67 | /** | |
| 68 | * Deletes a file. If the File is a directory it will recursively delete the contents. | |
| 69 | * | |
| 70 | * @param file the File to delete | |
| 71 | * @return true if the file was deleted successfully, otherwise false | |
| 72 | */ | |
| 73 | public static boolean delete(File file) { | |
| 74 | 3 | final boolean success = org.apache.commons.io.FileUtils.deleteQuietly(file); |
| 75 | 3 | if (!success) { |
| 76 | 0 | LOGGER.debug("Failed to delete file: {}; attempting to delete on exit.", file.getPath()); |
| 77 | 0 | file.deleteOnExit(); |
| 78 | } | |
| 79 | 3 | return success; |
| 80 | } | |
| 81 | ||
| 82 | /** | |
| 83 | * Generates a new temporary file name that is guaranteed to be unique. | |
| 84 | * | |
| 85 | * @param prefix the prefix for the file name to generate | |
| 86 | * @param extension the extension of the generated file name | |
| 87 | * @return a temporary File | |
| 88 | * @throws java.io.IOException thrown if the temporary folder could not be created | |
| 89 | */ | |
| 90 | public static File getTempFile(String prefix, String extension) throws IOException { | |
| 91 | 0 | final File dir = Settings.getTempDirectory(); |
| 92 | 0 | final String tempFileName = String.format("%s%s.%s", prefix, UUID.randomUUID().toString(), extension); |
| 93 | 0 | final File tempFile = new File(dir, tempFileName); |
| 94 | 0 | if (tempFile.exists()) { |
| 95 | 0 | return getTempFile(prefix, extension); |
| 96 | } | |
| 97 | 0 | return tempFile; |
| 98 | } | |
| 99 | ||
| 100 | /** | |
| 101 | * Return the bit bucket for the OS. '/dev/null' for Unix and 'NUL' for Windows | |
| 102 | * | |
| 103 | * @return a String containing the bit bucket | |
| 104 | */ | |
| 105 | public static String getBitBucket() { | |
| 106 | 0 | if (SystemUtils.IS_OS_WINDOWS) { |
| 107 | 0 | return BIT_BUCKET_WIN; |
| 108 | } else { | |
| 109 | 0 | return BIT_BUCKET_UNIX; |
| 110 | } | |
| 111 | } | |
| 112 | } |