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 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 private FileUtils() {
54 }
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 final String fileExt = FilenameUtils.getExtension(fileName);
64 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
69 * contents.
70 *
71 * @param file the File to delete
72 * @return true if the file was deleted successfully, otherwise false
73 */
74 public static boolean delete(File file) {
75 final boolean success = org.apache.commons.io.FileUtils.deleteQuietly(file);
76 if (!success) {
77 LOGGER.debug("Failed to delete file: {}; attempting to delete on exit.", file.getPath());
78 file.deleteOnExit();
79 }
80 return success;
81 }
82
83 /**
84 * Creates a unique temporary directory in the given directory.
85 *
86 * @param base the base directory to create a temporary directory within
87 * @return the temporary directory
88 * @throws IOException thrown when a directory cannot be created within the
89 * base directory
90 */
91 public static File createTempDirectory(File base) throws IOException {
92 final File tempDir = new File(base, "dctemp" + UUID.randomUUID().toString());
93 if (tempDir.exists()) {
94 return createTempDirectory(base);
95 }
96 if (!tempDir.mkdirs()) {
97 throw new IOException("Could not create temp directory `" + tempDir.getAbsolutePath() + "`");
98 }
99 return tempDir;
100 }
101
102 /**
103 * Generates a new temporary file name that is guaranteed to be unique.
104 *
105 * @param prefix the prefix for the file name to generate
106 * @param extension the extension of the generated file name
107 * @return a temporary File
108 * @throws java.io.IOException thrown if the temporary folder could not be
109 * created
110 */
111 public static File getTempFile(String prefix, String extension) throws IOException {
112 final File dir = Settings.getTempDirectory();
113 final String tempFileName = String.format("%s%s.%s", prefix, UUID.randomUUID().toString(), extension);
114 final File tempFile = new File(dir, tempFileName);
115 if (tempFile.exists()) {
116 return getTempFile(prefix, extension);
117 }
118 return tempFile;
119 }
120
121 /**
122 * Return the bit bucket for the OS. '/dev/null' for Unix and 'NUL' for
123 * Windows
124 *
125 * @return a String containing the bit bucket
126 */
127 public static String getBitBucket() {
128 if (SystemUtils.IS_OS_WINDOWS) {
129 return BIT_BUCKET_WIN;
130 } else {
131 return BIT_BUCKET_UNIX;
132 }
133 }
134 }