001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.internal;
014
015import static org.apache.juneau.internal.ThrowableUtils.*;
016
017import java.io.*;
018import java.nio.file.*;
019
020import org.apache.juneau.*;
021
022/**
023 * File utilities.
024 */
025public class FileUtils {
026
027   /**
028    * Same as {@link File#mkdirs()} except throws a RuntimeExeption if directory could not be created.
029    *
030    * @param f The directory to create.  Must not be <jk>null</jk>.
031    * @param clean If <jk>true</jk>, deletes the contents of the directory if it already exists.
032    * @return The same file.
033    * @throws RuntimeException if directory could not be created.
034    */
035   public static File mkdirs(File f, boolean clean) {
036      assertFieldNotNull(f, "f");
037      if (f.exists()) {
038         if (clean) {
039            if (! delete(f))
040               throw new FormattedRuntimeException("Could not clean directory ''{0}''", f.getAbsolutePath());
041         } else {
042            return f;
043         }
044      }
045      if (! f.mkdirs())
046         throw new FormattedRuntimeException("Could not create directory ''{0}''", f.getAbsolutePath());
047      return f;
048   }
049
050   /**
051    * Same as {@link #mkdirs(String, boolean)} but uses String path.
052    *
053    * @param path The path of the directory to create.  Must not be <jk>null</jk>
054    * @param clean If <jk>true</jk>, deletes the contents of the directory if it already exists.
055    * @return The directory.
056    */
057   public static File mkdirs(String path, boolean clean) {
058      assertFieldNotNull(path, "path");
059      return mkdirs(new File(path), clean);
060   }
061
062   /**
063    * Recursively deletes a file or directory.
064    *
065    * @param f The file or directory to delete.
066    * @return <jk>true</jk> if file or directory was successfully deleted.
067    */
068   public static boolean delete(File f) {
069      if (f == null)
070         return true;
071      if (f.isDirectory()) {
072         File[] cf = f.listFiles();
073         if (cf != null)
074            for (File c : cf)
075               delete(c);
076      }
077      return f.delete();
078   }
079
080   /**
081    * Creates a file if it doesn't already exist using {@link File#createNewFile()}.
082    *
083    * <p>
084    * Throws a {@link RuntimeException} if the file could not be created.
085    *
086    * @param f The file to create.
087    */
088   public static void create(File f) {
089      if (f.exists())
090         return;
091      try {
092         if (! f.createNewFile())
093            throw new FormattedRuntimeException("Could not create file ''{0}''", f.getAbsolutePath());
094      } catch (IOException e) {
095         throw new RuntimeException(e);
096      }
097   }
098
099   /**
100    * Updates the modified timestamp on the specified file.
101    *
102    * <p>
103    * Method ensures that the timestamp changes even if it's been modified within the past millisecond.
104    *
105    * @param f The file to modify the modified timestamp on.
106    */
107   public static void modifyTimestamp(File f) {
108      long lm = f.lastModified();
109      long l = System.currentTimeMillis();
110      if (lm == l)
111         l++;
112      if (! f.setLastModified(l))
113         throw new FormattedRuntimeException("Could not modify timestamp on file ''{0}''", f.getAbsolutePath());
114
115      // Linux only gives 1s precision, so set the date 1s into the future.
116      if (lm == f.lastModified()) {
117         l += 1000;
118         if (! f.setLastModified(l))
119            throw new FormattedRuntimeException("Could not modify timestamp on file ''{0}''", f.getAbsolutePath());
120      }
121   }
122
123   /**
124    * Create a temporary file with the specified name.
125    *
126    * <p>
127    * The name is broken into file name and suffix, and the parts are passed to
128    * {@link File#createTempFile(String, String)}.
129    *
130    * <p>
131    * {@link File#deleteOnExit()} is called on the resulting file before being returned by this method.
132    *
133    * @param name The file name
134    * @return A newly-created temporary file.
135    * @throws IOException Thrown by underlying stream.
136    */
137   public static File createTempFile(String name) throws IOException {
138      String[] parts = name.split("\\.");
139      File f = File.createTempFile(parts[0], "." + parts[1]);
140      f.deleteOnExit();
141      return f;
142   }
143
144   /**
145    * Strips the extension from a file name.
146    *
147    * @param name The file name.
148    * @return The file name without the extension, or <jk>null</jk> if name was <jk>null</jk>.
149    */
150   public static String getBaseName(String name) {
151      if (name == null)
152         return null;
153      int i = name.lastIndexOf('.');
154      if (i == -1)
155         return name;
156      return name.substring(0, i);
157   }
158
159   /**
160    * Returns the extension from a file name.
161    *
162    * @param name The file name.
163    * @return The the extension, or <jk>null</jk> if name was <jk>null</jk>.
164    */
165   public static String getExtension(String name) {
166      if (name == null)
167         return null;
168      int i = name.lastIndexOf('.');
169      if (i == -1)
170         return "";
171      return name.substring(i+1);
172   }
173
174   /**
175    * Returns <jk>true</jk> if the specified file exists in the specified directory.
176    *
177    * @param dir The directory.
178    * @param fileName The file name.
179    * @return <jk>true</jk> if the specified file exists in the specified directory.
180    */
181   public static boolean exists(File dir, String fileName) {
182      if (dir == null || fileName == null)
183         return false;
184      return Files.exists(dir.toPath().resolve(fileName));
185   }
186
187   /**
188    * Returns <jk>true</jk> if the specified file name contains the specified extension.
189    *
190    * @param name The file name.
191    * @param ext The extension.
192    * @return <jk>true</jk> if the specified file name contains the specified extension.
193    */
194   public static boolean hasExtension(String name, String ext) {
195      if (name == null || ext == null)
196         return false;
197      return ext.equals(getExtension(name));
198   }
199}