[ 
https://issues.apache.org/jira/browse/COMPRESS-690?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17904784#comment-17904784
 ] 

Gary D. Gregory commented on COMPRESS-690:
------------------------------------------

Hello [~ahmadin] 

In CPIO, the link is the file body, see CpioTest.testSymbolicLinkFileEntry():

{code:java}
    /**
     * Tests COMPRESS-690.
     */
    @Test
    public void testSymbolicLinkFileEntry() throws Exception {
        final File tmp = createTempFile();
        final File archive = createTempFile("test.", ".cpio");
        final String name = "EntryName";
        final String nameLink = "LinkName";
        final Charset charset = StandardCharsets.UTF_8;
        try (CpioArchiveOutputStream tos = new 
CpioArchiveOutputStream(Files.newOutputStream(archive.toPath()))) {
            final CpioArchiveEntry entry = new CpioArchiveEntry(name);
            entry.setTime(tmp.lastModified() / 1000);
            entry.setSize(nameLink.length());
            entry.setMode(CpioConstants.C_ISLNK);
            assertTrue(entry.isSymbolicLink());
            tos.putArchiveEntry(entry);
            tos.write(nameLink.getBytes(charset));
            tos.closeArchiveEntry();
        }
        final CpioArchiveEntry entry;
        try (CpioArchiveInputStream tis = new 
CpioArchiveInputStream(Files.newInputStream(archive.toPath()))) {
            entry = tis.getNextEntry();
            assertEquals(nameLink, IOUtils.toString(tis, charset));
        }
        assertNotNull(entry);
        assertEquals(name, entry.getName());
        assertTrue(entry.isSymbolicLink());
        assertEquals(nameLink.length(), entry.getSize());
        assertEquals(tmp.lastModified() / 1000, 
entry.getLastModifiedDate().getTime() / 1000);
        assertFalse(entry.isDirectory());
    }
{code}

I don't know if CPIO specifies the byte encoding, probably ASCII but UTF-9 in 
the test above.
 

> CpioArchiveEntry is missing getLinkName
> ---------------------------------------
>
>                 Key: COMPRESS-690
>                 URL: https://issues.apache.org/jira/browse/COMPRESS-690
>             Project: Commons Compress
>          Issue Type: Bug
>          Components: Archivers
>    Affects Versions: 1.27.1
>            Reporter: Ahmadin Badruddin
>            Priority: Major
>
> When trying to extract a cpio file that contains a symbolic link, there is a 
> method to check if 
> CpioArchiveEntry is a symbolic link (entry.isSymbolicLink()), but there is no 
> entry.getLinkName. There is a getName, but we cannot create a symlink without 
> the target link.
>  
> I'm {color:#FF0000}*hoping*{color} I can write a code to extract symlink from 
> a cpio like below code snippets
> {code:java}
> public void extractCpioSymbolicLink(File archiveFile, File targetDirectory) 
> throws IOException {
>     try (CpioArchiveInputStream cpioInputStream = new 
> CpioArchiveInputStream(new FileInputStream(archiveFile))) {
>         CpioArchiveEntry entry;
>         while ((entry = cpioInputStream.getNextEntry()) != null) {
>             if (entry.isSymbolicLink()) {
>                 String linkTarget = entry.getLinkName(); // Get the link 
> target
>                 File linkFile = new File(targetDirectory, entry.getName()); 
>                 // Create the symbolic link using the linkTarget on the 
> target system
>                 Files.createSymbolicLink(linkFile.toPath(), 
> Paths.get(linkTarget)); 
>             } else {
>                 // Handle other file types (regular files, directories) as 
> needed
>             }
>         }
>     }
> } {code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to