Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FileRenameAction when the source file doesn't exist #3021

Open
wants to merge 5 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.io.PrintStream;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.Issue;

public class FileRenameActionTest {

Expand Down Expand Up @@ -97,4 +98,28 @@ public void testNoParent() throws Exception {
assertTrue(dest.exists(), "Renamed file does not exist");
assertFalse(file.exists(), "Old file exists");
}

@Test
@Issue("https://github.com/apache/logging-log4j2/issues/2592")
public void testRenameForMissingFile() throws Exception {
final File file = new File("fileRename.log");
final File dest = new File(tempDir, "newFile.log");
FileRenameAction action = new FileRenameAction(file, dest, true);
boolean renameResult = action.execute();
assertTrue(renameResult, "Rename action returned false");
assertTrue(dest.exists(), "Renamed file does not exist");
assertFalse(file.exists(), "Old file exists");
}

@Test
@Issue("https://github.com/apache/logging-log4j2/issues/2592")
public void testRenameForMissingFileWithoutEmptyFilesRenaming() throws Exception {
final File file = new File("fileRename.log");
final File dest = new File(tempDir, "newFile.log");
FileRenameAction action = new FileRenameAction(file, dest, false);
boolean renameResult = action.execute();
assertTrue(renameResult, "Rename action returned false");
assertFalse(dest.exists(), "Renamed file should not exist");
assertFalse(file.exists(), "Old file exists");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.logging.log4j.core.appender.rolling.action;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
Expand Down Expand Up @@ -166,12 +167,38 @@ public static boolean execute(final File source, final File destination, final b
}
}
} catch (final IOException exCopy) {
LOGGER.error(
"Unable to copy file {} to {}: {} {}",
source.getAbsolutePath(),
destination.getAbsolutePath(),
exCopy.getClass().getName(),
exCopy.getMessage());
if (source.isFile()) {
LOGGER.error(
"Unable to copy file {} to {}: {} {}",
source.getAbsolutePath(),
destination.getAbsolutePath(),
exCopy.getClass().getName(),
exCopy.getMessage());
} else {
LOGGER.debug(
"Unable to copy file {} to {}: {} {} - will try to create destination since source file does not exist",
source.getAbsolutePath(),
destination.getAbsolutePath(),
exCopy.getClass().getName(),
exCopy.getMessage());
try {
Files.copy(
new ByteArrayInputStream(new byte[0]),
destination.toPath(),
StandardCopyOption.REPLACE_EXISTING);
result = true;
LOGGER.trace(
"Created file {} since {} does not exist",
destination.getAbsolutePath(),
source.getAbsolutePath());
} catch (final IOException exCreate) {
LOGGER.error(
"Unable to create file {}: {} {}",
destination.getAbsolutePath(),
exCreate.getClass().getName(),
exCreate.getMessage());
}
}
}
} else {
LOGGER.trace(
Expand All @@ -191,7 +218,8 @@ public static boolean execute(final File source, final File destination, final b
}
} else {
try {
return source.delete();
Files.deleteIfExists(source.toPath());
return true;
} catch (final Exception exDelete) {
LOGGER.error(
"Unable to delete empty file {}: {} {}",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://logging.apache.org/xml/ns"
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
type="fixed">
<issue id="2592" link="https://github.com/apache/logging-log4j2/issues/2592"/>
<description format="asciidoc">Fixed `FileRenameAction` when the source file doesn't exist</description>
</entry>