-
Notifications
You must be signed in to change notification settings - Fork 48
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
TAR: Implement extraction and archiving of hardlinks. #286
base: master
Are you sure you want to change the base?
Changes from all commits
913783b
95c0f44
dd5e14f
cea72cc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,11 @@ | |
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.LinkOption; | ||
import java.nio.file.Path; | ||
import java.nio.file.attribute.BasicFileAttributeView; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.zip.GZIPOutputStream; | ||
|
||
import org.apache.commons.compress.archivers.tar.TarArchiveEntry; | ||
|
@@ -39,6 +44,7 @@ | |
import org.codehaus.plexus.archiver.util.Streams; | ||
import org.codehaus.plexus.components.io.attributes.PlexusIoResourceAttributes; | ||
import org.codehaus.plexus.components.io.functions.SymlinkDestinationSupplier; | ||
import org.codehaus.plexus.components.io.resources.PlexusIoFileResource; | ||
import org.codehaus.plexus.components.io.resources.PlexusIoResource; | ||
import org.codehaus.plexus.util.IOUtil; | ||
import org.codehaus.plexus.util.StringUtils; | ||
|
@@ -65,6 +71,8 @@ public class TarArchiver extends AbstractArchiver { | |
|
||
private TarArchiveOutputStream tOut; | ||
|
||
private final Map<Object, String> seenFiles = new HashMap<>(10); | ||
|
||
/** | ||
* Set how to handle long files, those with a path>100 chars. | ||
* Optional, default=warn. | ||
|
@@ -194,7 +202,7 @@ protected void tarFile(ArchiveEntry entry, TarArchiveOutputStream tOut, String v | |
InputStream fIn = null; | ||
|
||
try { | ||
TarArchiveEntry te; | ||
TarArchiveEntry te = null; | ||
if (!longFileMode.isGnuMode() | ||
&& pathLength >= org.apache.commons.compress.archivers.tar.TarConstants.NAMELEN) { | ||
int maxPosixPathLen = org.apache.commons.compress.archivers.tar.TarConstants.NAMELEN | ||
|
@@ -233,13 +241,39 @@ protected void tarFile(ArchiveEntry entry, TarArchiveOutputStream tOut, String v | |
} | ||
} | ||
|
||
boolean isLink = false; | ||
if (entry.getType() == ArchiveEntry.SYMLINK) { | ||
final SymlinkDestinationSupplier plexusIoSymlinkResource = | ||
(SymlinkDestinationSupplier) entry.getResource(); | ||
|
||
te = new TarArchiveEntry(vPath, TarArchiveEntry.LF_SYMLINK); | ||
te.setLinkName(plexusIoSymlinkResource.getSymlinkDestination()); | ||
} else { | ||
isLink = true; | ||
} else if (options.getPreserveHardLinks() | ||
&& entry.getResource().isFile() | ||
&& entry.getResource() instanceof PlexusIoFileResource) { | ||
final PlexusIoFileResource fileResource = (PlexusIoFileResource) entry.getResource(); | ||
final Path file = fileResource.getFile().toPath(); | ||
if (Files.exists(file)) { | ||
final BasicFileAttributeView fileAttributeView = | ||
Files.getFileAttributeView(file, BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS); | ||
if (fileAttributeView != null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be best to extract this check to separate method to make this method easier to read. |
||
final Object fileKey = | ||
fileAttributeView.readAttributes().fileKey(); | ||
if (fileKey != null) { | ||
final String seenFile = this.seenFiles.get(fileKey); | ||
if (seenFile != null) { | ||
te = new TarArchiveEntry(vPath, TarArchiveEntry.LF_LINK); | ||
te.setLinkName(seenFile); | ||
isLink = true; | ||
} else { | ||
this.seenFiles.put(fileKey, vPath); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
if (te == null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be honest I don't follow why this is added. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
te = new TarArchiveEntry(vPath); | ||
} | ||
|
||
|
@@ -253,7 +287,7 @@ protected void tarFile(ArchiveEntry entry, TarArchiveOutputStream tOut, String v | |
te.setModTime(getLastModifiedTime().toMillis()); | ||
} | ||
|
||
if (entry.getType() == ArchiveEntry.SYMLINK) { | ||
if (isLink) { | ||
te.setSize(0); | ||
|
||
} else if (!entry.getResource().isDirectory()) { | ||
|
@@ -289,7 +323,7 @@ protected void tarFile(ArchiveEntry entry, TarArchiveOutputStream tOut, String v | |
tOut.putArchiveEntry(te); | ||
|
||
try { | ||
if (entry.getResource().isFile() && !(entry.getType() == ArchiveEntry.SYMLINK)) { | ||
if (entry.getResource().isFile() && !isLink) { | ||
fIn = entry.getInputStream(); | ||
|
||
Streams.copyFullyDontCloseOutput(fIn, tOut, "xAR"); | ||
|
@@ -320,6 +354,8 @@ public class TarOptions { | |
|
||
private boolean preserveLeadingSlashes = false; | ||
|
||
private boolean preserveHardLinks = true; | ||
|
||
/** | ||
* The username for the tar entry | ||
* This is not the same as the UID. | ||
|
@@ -405,6 +441,14 @@ public boolean getPreserveLeadingSlashes() { | |
public void setPreserveLeadingSlashes(boolean preserveLeadingSlashes) { | ||
this.preserveLeadingSlashes = preserveLeadingSlashes; | ||
} | ||
|
||
public boolean getPreserveHardLinks() { | ||
return preserveHardLinks; | ||
} | ||
|
||
public void setPreserveHardLinks(boolean preserveHardLinks) { | ||
this.preserveHardLinks = preserveHardLinks; | ||
} | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.codehaus.plexus.archiver; | ||
|
||
import java.io.File; | ||
import java.nio.file.Files; | ||
|
||
import org.codehaus.plexus.archiver.tar.TarArchiver; | ||
import org.codehaus.plexus.archiver.tar.TarLongFileMode; | ||
import org.codehaus.plexus.archiver.tar.TarUnArchiver; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.condition.DisabledOnOs; | ||
import org.junit.jupiter.api.condition.OS; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
/** | ||
* @author Kristian Rosenvold | ||
*/ | ||
public class HardlinkTest extends TestSupport { | ||
|
||
@Test | ||
@DisabledOnOs(OS.WINDOWS) | ||
public void testHardlinkTar() throws Exception { | ||
// Extract test files | ||
final File archiveFile = getTestFile("src/test/resources/hardlinks/hardlinks.tar"); | ||
File output = getTestFile("target/output/untaredHardlinks"); | ||
output.mkdirs(); | ||
TarUnArchiver unarchiver = (TarUnArchiver) lookup(UnArchiver.class, "tar"); | ||
unarchiver.setSourceFile(archiveFile); | ||
unarchiver.setDestFile(output); | ||
unarchiver.extract(); | ||
// Check that we have hardlinks | ||
assertTrue(Files.isSameFile( | ||
output.toPath().resolve("fileR.txt"), output.toPath().resolve("hardlink"))); | ||
|
||
// Archive the extracted hardlinks to new archive | ||
TarArchiver archiver = (TarArchiver) lookup(Archiver.class, "tar"); | ||
archiver.setLongfile(TarLongFileMode.posix); | ||
archiver.addDirectory(output); | ||
final File testFile = getTestFile("target/output/untaredHardlinks2.tar"); | ||
archiver.setDestFile(testFile); | ||
archiver.createArchive(); | ||
|
||
// Check that our created archive actually contains hardlinks when extracted | ||
unarchiver = (TarUnArchiver) lookup(UnArchiver.class, "tar"); | ||
output = getTestFile("target/output/untaredHardlinks2"); | ||
output.mkdirs(); | ||
unarchiver.setSourceFile(testFile); | ||
unarchiver.setDestFile(output); | ||
unarchiver.extract(); | ||
assertTrue(Files.isSameFile( | ||
output.toPath().resolve("fileR.txt"), output.toPath().resolve("hardlink"))); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is breaking change. I wonder if we can avoid it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see how.