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