diff --git a/common/src/main/java/org/apache/rocketmq/common/utils/IOTinyUtils.java b/common/src/main/java/org/apache/rocketmq/common/utils/IOTinyUtils.java index acba540d3a5..0aa91b8f9c2 100644 --- a/common/src/main/java/org/apache/rocketmq/common/utils/IOTinyUtils.java +++ b/common/src/main/java/org/apache/rocketmq/common/utils/IOTinyUtils.java @@ -30,6 +30,7 @@ import java.io.Writer; import java.nio.channels.FileChannel; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.util.ArrayList; import java.util.List; @@ -81,6 +82,9 @@ static public void copyFile(String source, String target) throws IOException { throw new IllegalArgumentException("source file does not exist."); } File tf = new File(target); + if (tf.exists() && Files.isSameFile(sf.toPath(), tf.toPath())) { + throw new IOException("source and target refer to the same file"); + } tf.getParentFile().mkdirs(); if (!tf.exists() && !tf.createNewFile()) { throw new RuntimeException("failed to create target file."); diff --git a/common/src/test/java/org/apache/rocketmq/common/utils/IOTinyUtilsSameFileTest.java b/common/src/test/java/org/apache/rocketmq/common/utils/IOTinyUtilsSameFileTest.java new file mode 100644 index 00000000000..64da6ed05fd --- /dev/null +++ b/common/src/test/java/org/apache/rocketmq/common/utils/IOTinyUtilsSameFileTest.java @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.rocketmq.common.utils; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import org.junit.Assume; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.fail; + +public class IOTinyUtilsSameFileTest { + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + @Test + public void copyFileToItselfDoesNotDestroySource() throws Exception { + Path source = folder.newFile("source").toPath(); + assertCopyRejected(source, source); + } + + @Test + public void copyFileToHardLinkDoesNotDestroySource() throws Exception { + Path source = folder.newFile("source").toPath(); + Path link = source.resolveSibling("link"); + try { + Files.createLink(link, source); + } catch (IOException | UnsupportedOperationException e) { + Assume.assumeNoException("Hard links are unavailable on this filesystem", e); + } + assertCopyRejected(source, link); + } + + @Test + public void copyFileToDifferentFileStillWorks() throws Exception { + Path source = folder.newFile("source").toPath(); + Path target = folder.newFile("target").toPath(); + byte[] data = "important data".getBytes(StandardCharsets.UTF_8); + Files.write(source, data); + IOTinyUtils.copyFile(source.toString(), target.toString()); + assertArrayEquals(data, Files.readAllBytes(target)); + } + + private void assertCopyRejected(Path source, Path target) throws Exception { + byte[] data = "important data".getBytes(StandardCharsets.UTF_8); + Files.write(source, data); + try { + IOTinyUtils.copyFile(source.toString(), target.toString()); + assertArrayEquals(data, Files.readAllBytes(source)); + fail("Expected an IOException for identical files"); + } catch (IOException expected) { + assertArrayEquals(data, Files.readAllBytes(source)); + } + } +}