Browse Source

Tar: preserve symlinks from <tarfileset> sources

master
Marc Strapetz 4 years ago
parent
commit
2e2ed8ad31
4 changed files with 44 additions and 0 deletions
  1. +8
    -0
      src/etc/testcases/taskdefs/tar.xml
  2. BIN
      src/etc/testcases/testtarwithsymlinks.tar.gz
  3. +8
    -0
      src/main/org/apache/tools/ant/taskdefs/Tar.java
  4. +28
    -0
      src/tests/junit/org/apache/tools/ant/taskdefs/TarTest.java

+ 8
- 0
src/etc/testcases/taskdefs/tar.xml View File

@@ -203,4 +203,12 @@
<untar src="${output}/testout/test.tar" dest="${output}/untar"/>
</target>

<target name="testTarFilesetWithSymlinks">
<gunzip src="../testtarwithsymlinks.tar.gz" dest="${output}/source.tar"/>
<tar destfile="${output}/result.tar">
<tarfileset prefix="pre" src="${output}/source.tar"/>
</tar>
<echo message="asd ${output}"/>
</target>

</project>

BIN
src/etc/testcases/testtarwithsymlinks.tar.gz View File


+ 8
- 0
src/main/org/apache/tools/ant/taskdefs/Tar.java View File

@@ -459,6 +459,14 @@ public class Tar extends MatchingTask {
te.setUserId(tr.getLongUid());
te.setGroupName(tr.getGroup());
te.setGroupId(tr.getLongGid());
String linkName = tr.getLinkName();
byte linkFlag = tr.getLinkFlag();
if (linkFlag == TarConstants.LF_LINK &&
linkName != null && linkName.length() > 0 && !linkName.startsWith("/")) {
linkName = getCanonicalPrefix(tarFileSet, this.getProject()) + linkName;
}
te.setLinkName(linkName);
te.setLinkFlag(linkFlag);
}
}



+ 28
- 0
src/tests/junit/org/apache/tools/ant/taskdefs/TarTest.java View File

@@ -19,16 +19,21 @@
package org.apache.tools.ant.taskdefs;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.BuildFileRule;
import org.apache.tools.ant.FileUtilities;
import org.apache.tools.tar.TarEntry;
import org.apache.tools.tar.TarInputStream;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

public class TarTest {
@@ -186,4 +191,27 @@ public class TarTest {
public void testtestTarFilesetWithReference() {
buildRule.executeTarget("testTarFilesetWithReference");
}

@Test
public void testTarFilesetWithSymlinks() throws IOException {
buildRule.executeTarget("testTarFilesetWithSymlinks");
final File f = new File(buildRule.getProject().getProperty("output"), "result.tar");
final TarInputStream tis = new TarInputStream(new FileInputStream(f));
try {
final TarEntry e1 = tis.getNextEntry();
assertEquals("pre/dir/file", e1.getName());
assertEquals("", e1.getLinkName());
assertEquals(48, e1.getLinkFlag());

final TarEntry e2 = tis.getNextEntry();
assertEquals("pre/sub/file", e2.getName());
assertEquals("../dir/file", e2.getLinkName());
assertEquals(50, e2.getLinkFlag());

assertNull(tis.getNextEntry());
}
finally {
tis.close();
}
}
}

Loading…
Cancel
Save