This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git


The following commit(s) were added to refs/heads/master by this push:
     new 2addb4e01 [IO-845] test links to targets outside the source directory 
(#571)
2addb4e01 is described below

commit 2addb4e0138ced2f67d5697f7648bd32366d1709
Author: Elliotte Rusty Harold <elh...@users.noreply.github.com>
AuthorDate: Mon Jan 29 16:55:10 2024 +0000

    [IO-845] test links to targets outside the source directory (#571)
    
    * test links to targets outside the source directory
    
    * final
---
 .../java/org/apache/commons/io/FileUtilsTest.java  | 32 ++++++++++++++++++++++
 1 file changed, 32 insertions(+)

diff --git a/src/test/java/org/apache/commons/io/FileUtilsTest.java 
b/src/test/java/org/apache/commons/io/FileUtilsTest.java
index f19ed32b7..173f7a3d9 100644
--- a/src/test/java/org/apache/commons/io/FileUtilsTest.java
+++ b/src/test/java/org/apache/commons/io/FileUtilsTest.java
@@ -750,6 +750,38 @@ public class FileUtilsTest extends AbstractTempDirTest {
         assertTrue(FileUtils.contentEqualsIgnoreEOL(file1, file2, null));
     }
 
+    /**
+     * Test what happens when copyDirectory copies a directory that contains a 
symlink
+     * to a file outside the copied directory.
+     */
+    @Test
+    public void testCopyDirectory_symLinkExternalFile() throws Exception {
+        // make a file
+        final File content = new File(tempDirFile, "hello.txt");
+        FileUtils.writeStringToFile(content, "HELLO WORLD", "UTF8");
+
+        // Make a directory
+        final File realDirectory = new File(tempDirFile, "real_directory");
+        realDirectory.mkdir();
+
+        // Make a symlink to the file
+        final Path linkPath = realDirectory.toPath().resolve("link_to_file");
+        Files.createSymbolicLink(linkPath, content.toPath());
+
+        // Now copy the directory
+        final File destination = new File(tempDirFile, "destination");
+        FileUtils.copyDirectory(realDirectory, destination);
+
+        // test that the copied directory contains a link to the original file
+        final File copiedLink = new File(destination, "link_to_file");
+        assertTrue(Files.isSymbolicLink(copiedLink.toPath()));
+        final String actual = FileUtils.readFileToString(copiedLink, "UTF8");
+        assertEquals("HELLO WORLD", actual);
+
+        final Path source = Files.readSymbolicLink(copiedLink.toPath());
+        assertEquals(content.toPath(), source);
+    }
+
     /**
      * See what happens when copyDirectory copies a directory that is a symlink
      * to another directory containing non-symlinked files.

Reply via email to