View Javadoc
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  
28  /**
29   * A collection of utilities for processing information about files.
30   *
31   * @author Jeremy Long
32   */
33  public final class FileUtils {
34  
35      /**
36       * The logger.
37       */
38      private static final Logger LOGGER = LoggerFactory.getLogger(FileUtils.class);
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          final String fileExt = FilenameUtils.getExtension(fileName);
63          return null == fileExt || fileExt.isEmpty() ? null : fileExt.toLowerCase();
64      }
65  
66      /**
67       * Deletes a file. If the File is a directory it will recursively delete the contents.
68       *
69       * @param file the File to delete
70       * @return true if the file was deleted successfully, otherwise false
71       */
72      public static boolean delete(File file) {
73          final boolean success = org.apache.commons.io.FileUtils.deleteQuietly(file);
74          if (!success) {
75              LOGGER.debug("Failed to delete file: {}; attempting to delete on exit.", file.getPath());
76              file.deleteOnExit();
77          }
78          return success;
79      }
80  
81      /**
82       * Generates a new temporary file name that is guaranteed to be unique.
83       *
84       * @param prefix the prefix for the file name to generate
85       * @param extension the extension of the generated file name
86       * @return a temporary File
87       * @throws java.io.IOException thrown if the temporary folder could not be created
88       */
89      public static File getTempFile(String prefix, String extension) throws IOException {
90          final File dir = Settings.getTempDirectory();
91          final String tempFileName = String.format("%s%s.%s", prefix, UUID.randomUUID().toString(), extension);
92          final File tempFile = new File(dir, tempFileName);
93          if (tempFile.exists()) {
94              return getTempFile(prefix, extension);
95          }
96          return tempFile;
97      }
98  
99      /**
100      * Return the bit bucket for the OS. '/dev/null' for Unix and 'NUL' for Windows
101      *
102      * @return a String containing the bit bucket
103      */
104     public static String getBitBucket() {
105         if (System.getProperty("os.name").startsWith("Windows")) {
106             return BIT_BUCKET_WIN;
107         } else {
108             return BIT_BUCKET_UNIX;
109         }
110     }
111 }