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.appender.rolling.action;
018
019import java.io.File;
020import java.io.IOException;
021import java.nio.file.Files;
022
023/**
024 * File rename action.
025 */
026public class FileRenameAction extends AbstractAction {
027
028    /**
029     * Source.
030     */
031    private final File source;
032
033    /**
034     * Destination.
035     */
036    private final File destination;
037
038    /**
039     * If true, rename empty files, otherwise delete empty files.
040     */
041    private final boolean renameEmptyFiles;
042
043    /**
044     * Creates an FileRenameAction.
045     *
046     * @param src              current file name.
047     * @param dst              new file name.
048     * @param renameEmptyFiles if true, rename file even if empty, otherwise delete empty files.
049     */
050    public FileRenameAction(final File src, final File dst, final boolean renameEmptyFiles) {
051        source = src;
052        destination = dst;
053        this.renameEmptyFiles = renameEmptyFiles;
054    }
055
056    /**
057     * Rename file.
058     *
059     * @return true if successfully renamed.
060     */
061    @Override
062    public boolean execute() {
063        return execute(source, destination, renameEmptyFiles);
064    }
065
066    /**
067     * Rename file.
068     *
069     * @param source           current file name.
070     * @param destination      new file name.
071     * @param renameEmptyFiles if true, rename file even if empty, otherwise delete empty files.
072     * @return true if successfully renamed.
073     */
074    public static boolean execute(final File source, final File destination, final boolean renameEmptyFiles) {
075        if (renameEmptyFiles || source.length() > 0) {
076            final File parent = destination.getParentFile();
077            if (parent != null && !parent.exists()) {
078                // LOG4J2-679: ignore mkdirs() result: in multithreaded scenarios,
079                // if one thread succeeds the other thread returns false
080                // even though directories have been created. Check if dir exists instead.
081                parent.mkdirs();
082                if (!parent.exists()) {
083                    LOGGER.error("Unable to create directory {}", parent.getAbsolutePath());
084                    return false;
085                }
086            }
087            try {
088                if (!source.renameTo(destination)) {
089                    try {
090                        copyFile(source, destination);
091                        return source.delete();
092                    } catch (final IOException iex) {
093                        LOGGER.error("Unable to rename file {} to {} - {}", source.getAbsolutePath(),
094                            destination.getAbsolutePath(), iex.getMessage());
095                    }
096                }
097                return true;
098            } catch (final Exception ex) {
099                try {
100                    copyFile(source, destination);
101                    return source.delete();
102                } catch (final IOException iex) {
103                    LOGGER.error("Unable to rename file {} to {} - {}", source.getAbsolutePath(),
104                        destination.getAbsolutePath(), iex.getMessage());
105                }
106            }
107        } else {
108            try {
109                source.delete();
110            } catch (final Exception ex) {
111                LOGGER.error("Unable to delete empty file " + source.getAbsolutePath());
112            }
113        }
114
115        return false;
116    }
117
118    private static void copyFile(final File source, final File destination) throws IOException {
119        if (!destination.exists()) {
120            destination.createNewFile();
121        }
122        Files.copy(source.toPath(), destination.toPath());
123    }
124
125    @Override
126    public String toString() {
127        return FileRenameAction.class.getSimpleName() + '[' + source + " to " + destination //
128                + ", renameEmptyFiles=" + renameEmptyFiles + ']';
129    }
130}