001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.util;
018
019import java.io.File;
020import java.io.IOException;
021import java.io.UnsupportedEncodingException;
022import java.net.MalformedURLException;
023import java.net.URI;
024import java.net.URISyntaxException;
025import java.net.URL;
026import java.net.URLDecoder;
027import java.nio.charset.StandardCharsets;
028import java.util.regex.Pattern;
029
030import org.apache.logging.log4j.Logger;
031import org.apache.logging.log4j.status.StatusLogger;
032
033/**
034 * File utilities.
035 */
036public final class FileUtils {
037
038    /** Constant for the file URL protocol.*/
039    private static final String PROTOCOL_FILE = "file";
040
041    private static final String JBOSS_FILE = "vfsfile";
042
043    private static final Logger LOGGER = StatusLogger.getLogger();
044    private static final Pattern WINDOWS_DIRECTORY_SEPARATOR = Pattern.compile("\\\\+");
045
046    private FileUtils() {
047    }
048
049      /**
050     * Tries to convert the specified URI to a file object. If this fails,
051     * <b>null</b> is returned.
052     *
053     * @param uri the URI
054     * @return the resulting file object
055     */
056    public static File fileFromUri(URI uri) {
057        // There MUST be a better way to do this. TODO Search other ASL projects...
058        if (uri == null
059                || (uri.getScheme() != null && (!PROTOCOL_FILE.equals(uri.getScheme()) && !JBOSS_FILE.equals(uri
060                        .getScheme())))) {
061            return null;
062        }
063        if (uri.getScheme() == null) {
064            File file = new File(uri.toString());
065            if (file.exists()) {
066                return file;
067            }
068            try {
069                final String path = uri.getPath();
070                file = new File(path);
071                if (file.exists()) {
072                    return file;
073                }
074                uri = new File(path).toURI();
075            } catch (final Exception ex) {
076                LOGGER.warn("Invalid URI {}", uri);
077                return null;
078            }
079        }
080        final String charsetName = StandardCharsets.UTF_8.name();
081        try {
082            String fileName = uri.toURL().getFile();
083            if (new File(fileName).exists()) { // LOG4J2-466
084                return new File(fileName); // allow files with '+' char in name
085            }
086            fileName = URLDecoder.decode(fileName, charsetName);
087            return new File(fileName);
088        } catch (final MalformedURLException ex) {
089            LOGGER.warn("Invalid URL {}", uri, ex);
090        } catch (final UnsupportedEncodingException uee) {
091            LOGGER.warn("Invalid encoding: {}", charsetName, uee);
092        }
093        return null;
094    }
095
096    public static boolean isFile(final URL url) {
097        return url != null && (url.getProtocol().equals(PROTOCOL_FILE) || url.getProtocol().equals(JBOSS_FILE));
098    }
099
100    /**
101     * Asserts that the given directory exists and creates it if necessary.
102     * @param dir the directory that shall exist
103     * @param createDirectoryIfNotExisting specifies if the directory shall be created if it does not exist.
104     * @throws java.io.IOException thrown if the directory could not be created.
105     */
106    public static void mkdir(final File dir, final boolean createDirectoryIfNotExisting ) throws IOException {
107        // commons io FileUtils.forceMkdir would be useful here, we just want to omit this dependency
108        if (!dir.exists()) {
109            if(!createDirectoryIfNotExisting) {
110                throw new IOException("The directory " + dir.getAbsolutePath() + " does not exist.");
111            }
112            if(!dir.mkdirs()) {
113                throw new IOException("Could not create directory " + dir.getAbsolutePath());
114            }
115        }
116        if (!dir.isDirectory()) {
117            throw new IOException("File " + dir + " exists and is not a directory. Unable to create directory.");
118        }
119    }
120}