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.File;
21 import java.io.IOException;
22 import java.io.UnsupportedEncodingException;
23 import java.net.URLDecoder;
24 import java.util.UUID;
25 import java.util.logging.Level;
26 import java.util.logging.Logger;
27
28 /**
29 * A collection of utilities for processing information about files.
30 *
31 * @author Jeremy Long <jeremy.long@owasp.org>
32 */
33 public final class FileUtils {
34
35 /**
36 * The logger.
37 */
38 private static final Logger LOGGER = Logger.getLogger(FileUtils.class.getName());
39 /**
40 * Bit bucket for non-Windows systems
41 */
42 private static final String BIT_BUCKET_UNIX = "/dev/null";
43
44 /**
45 * Bit bucket for Windows systems (yes, only one 'L')
46 */
47 private static final String BIT_BUCKET_WIN = "NUL";
48
49 /**
50 * Private constructor for a utility class.
51 */
52 private FileUtils() {
53 }
54
55 /**
56 * Returns the (lowercase) file extension for a specified file.
57 *
58 * @param fileName the file name to retrieve the file extension from.
59 * @return the file extension.
60 */
61 public static String getFileExtension(String fileName) {
62 String ret = null;
63 final int pos = fileName.lastIndexOf(".");
64 if (pos >= 0) {
65 ret = fileName.substring(pos + 1, fileName.length()).toLowerCase();
66 }
67 return ret;
68 }
69
70 /**
71 * Deletes a file. If the File is a directory it will recursively delete the contents.
72 *
73 * @param file the File to delete
74 * @return true if the file was deleted successfully, otherwise false
75 */
76 public static boolean delete(File file) {
77 boolean success = true;
78 if (!org.apache.commons.io.FileUtils.deleteQuietly(file)) {
79 success = false;
80 final String msg = String.format("Failed to delete file: %s; attempting to delete on exit.", file.getPath());
81 LOGGER.log(Level.INFO, msg);
82 file.deleteOnExit();
83 }
84 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 final File dir = Settings.getTempDirectory();
97 final String tempFileName = String.format("%s%s.%s", prefix, UUID.randomUUID().toString(), extension);
98 final File tempFile = new File(dir, tempFileName);
99 if (tempFile.exists()) {
100 return getTempFile(prefix, extension);
101 }
102 return tempFile;
103 }
104
105 /**
106 * Returns the data directory. If a path was specified in dependencycheck.properties or was specified using the
107 * Settings object, and the path exists, that path will be returned as a File object. If it does not exist, then a
108 * File object will be 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
115 * how the data directory should be set.
116 */
117 @java.lang.Deprecated
118 public static File getDataDirectory(String configuredFilePath, Class clazz) throws IOException {
119 final File file = new File(configuredFilePath);
120 if (file.isDirectory() && file.canWrite()) {
121 return new File(file.getCanonicalPath());
122 } else {
123 final File exePath = getPathToJar(clazz);
124 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
130 * contained a 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 final String filePath = clazz.getProtectionDomain().getCodeSource().getLocation().getPath();
140 final String decodedPath = URLDecoder.decode(filePath, "UTF-8");
141 final File jarPath = new File(decodedPath);
142 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 if (System.getProperty("os.name").startsWith("Windows")) {
152 return BIT_BUCKET_WIN;
153 } else {
154 return BIT_BUCKET_UNIX;
155 }
156 }
157 }