use of org.apache.commons.compress.archivers.ar.ArArchiveEntry in project buck by facebook.
the class ArchiveStepIntegrationTest method inputDirs.
@Test
public void inputDirs() throws IOException, InterruptedException {
assumeTrue(Platform.detect() == Platform.MACOS || Platform.detect() == Platform.LINUX);
ProjectFilesystem filesystem = new ProjectFilesystem(tmp.getRoot());
CxxPlatform platform = CxxPlatformUtils.build(new CxxBuckConfig(FakeBuckConfig.builder().build()));
// Build up the paths to various files the archive step will use.
SourcePathResolver sourcePathResolver = new SourcePathResolver(new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer())));
Archiver archiver = platform.getAr();
Path output = filesystem.getPath("output.a");
Path input = filesystem.getPath("foo/blah.dat");
filesystem.mkdirs(input.getParent());
filesystem.writeContentsToPath("blah", input);
// Build an archive step.
ArchiveStep archiveStep = new ArchiveStep(filesystem, archiver.getEnvironment(sourcePathResolver), archiver.getCommandPrefix(sourcePathResolver), ImmutableList.of(), getArchiveOptions(false), output, ImmutableList.of(input.getParent()), archiver);
// Execute the archive step and verify it ran successfully.
ExecutionContext executionContext = TestExecutionContext.newInstance();
TestConsole console = (TestConsole) executionContext.getConsole();
int exitCode = archiveStep.execute(executionContext).getExitCode();
assertEquals("archive step failed: " + console.getTextWrittenToStdErr(), 0, exitCode);
// zero'd out.
try (ArArchiveInputStream stream = new ArArchiveInputStream(new FileInputStream(filesystem.resolve(output).toFile()))) {
ArArchiveEntry entry = stream.getNextArEntry();
assertThat(entry.getName(), Matchers.equalTo("blah.dat"));
}
}
use of org.apache.commons.compress.archivers.ar.ArArchiveEntry in project buck by facebook.
the class ArchiveStepIntegrationTest method thinArchives.
@Test
public void thinArchives() throws IOException, InterruptedException {
assumeTrue(Platform.detect() == Platform.MACOS || Platform.detect() == Platform.LINUX);
ProjectFilesystem filesystem = new ProjectFilesystem(tmp.getRoot());
CxxPlatform platform = CxxPlatformUtils.build(new CxxBuckConfig(FakeBuckConfig.builder().build()));
assumeTrue(platform.getAr().supportsThinArchives());
// Build up the paths to various files the archive step will use.
SourcePathResolver sourcePathResolver = new SourcePathResolver(new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer())));
Archiver archiver = platform.getAr();
Path output = filesystem.getPath("foo/libthin.a");
filesystem.mkdirs(output.getParent());
// Create a really large input file so it's obvious that the archive is thin.
Path input = filesystem.getPath("bar/blah.dat");
filesystem.mkdirs(input.getParent());
byte[] largeInputFile = new byte[1024 * 1024];
byte[] fillerToRepeat = "hello\n".getBytes(StandardCharsets.UTF_8);
for (int i = 0; i < largeInputFile.length; i++) {
largeInputFile[i] = fillerToRepeat[i % fillerToRepeat.length];
}
filesystem.writeBytesToPath(largeInputFile, input);
// Build an archive step.
ArchiveStep archiveStep = new ArchiveStep(filesystem, archiver.getEnvironment(sourcePathResolver), archiver.getCommandPrefix(sourcePathResolver), ImmutableList.of(), getArchiveOptions(true), output, ImmutableList.of(input), archiver);
// Execute the archive step and verify it ran successfully.
ExecutionContext executionContext = TestExecutionContext.newInstance();
TestConsole console = (TestConsole) executionContext.getConsole();
int exitCode = archiveStep.execute(executionContext).getExitCode();
assertEquals("archive step failed: " + console.getTextWrittenToStdErr(), 0, exitCode);
// Verify that the thin header is present.
assertThat(filesystem.readFirstLine(output), Matchers.equalTo(Optional.of("!<thin>")));
// Verify that even though the archived contents is really big, the archive is still small.
assertThat(filesystem.getFileSize(output), Matchers.lessThan(1000L));
// can parse the archive contents.
try (OutputStream outputStream = Files.newOutputStream(filesystem.resolve(output), StandardOpenOption.WRITE)) {
outputStream.write(ObjectFileScrubbers.GLOBAL_HEADER);
}
// zero'd out.
try (ArArchiveInputStream stream = new ArArchiveInputStream(new FileInputStream(filesystem.resolve(output).toFile()))) {
ArArchiveEntry entry = stream.getNextArEntry();
// Verify that the input names are relative paths from the outputs parent dir.
assertThat(entry.getName(), Matchers.equalTo(output.getParent().relativize(input).toString()));
}
}
use of org.apache.commons.compress.archivers.ar.ArArchiveEntry in project buck by facebook.
the class ArchiveStepIntegrationTest method thatGeneratedArchivesAreDeterministic.
@Test
@SuppressWarnings("PMD.AvoidUsingOctalValues")
public void thatGeneratedArchivesAreDeterministic() throws IOException, InterruptedException {
assumeTrue(Platform.detect() == Platform.MACOS || Platform.detect() == Platform.LINUX);
ProjectFilesystem filesystem = new ProjectFilesystem(tmp.getRoot());
CxxPlatform platform = CxxPlatformUtils.build(new CxxBuckConfig(FakeBuckConfig.builder().build()));
// Build up the paths to various files the archive step will use.
SourcePathResolver sourcePathResolver = new SourcePathResolver(new SourcePathRuleFinder(new BuildRuleResolver(TargetGraph.EMPTY, new DefaultTargetNodeToBuildRuleTransformer())));
Archiver archiver = platform.getAr();
Path output = filesystem.getPath("output.a");
Path input = filesystem.getPath("input.dat");
filesystem.writeContentsToPath("blah", input);
Preconditions.checkState(filesystem.resolve(input).toFile().setExecutable(true));
// Build an archive step.
ArchiveStep archiveStep = new ArchiveStep(filesystem, archiver.getEnvironment(sourcePathResolver), archiver.getCommandPrefix(sourcePathResolver), ImmutableList.of(), getArchiveOptions(false), output, ImmutableList.of(input), archiver);
FileScrubberStep fileScrubberStep = new FileScrubberStep(filesystem, output, platform.getAr().getScrubbers());
// Execute the archive step and verify it ran successfully.
ExecutionContext executionContext = TestExecutionContext.newInstance();
TestConsole console = (TestConsole) executionContext.getConsole();
int exitCode = archiveStep.execute(executionContext).getExitCode();
assertEquals("archive step failed: " + console.getTextWrittenToStdErr(), 0, exitCode);
exitCode = fileScrubberStep.execute(executionContext).getExitCode();
assertEquals("archive scrub step failed: " + console.getTextWrittenToStdErr(), 0, exitCode);
// zero'd out.
try (ArArchiveInputStream stream = new ArArchiveInputStream(new FileInputStream(filesystem.resolve(output).toFile()))) {
ArArchiveEntry entry = stream.getNextArEntry();
assertEquals(ObjectFileCommonModificationDate.COMMON_MODIFICATION_TIME_STAMP, entry.getLastModified());
assertEquals(0, entry.getUserId());
assertEquals(0, entry.getGroupId());
assertEquals(String.format("0%o", entry.getMode()), 0100644, entry.getMode());
}
}
use of org.apache.commons.compress.archivers.ar.ArArchiveEntry in project buck by facebook.
the class CxxLibraryIntegrationTest method thinArchivesDoNotContainAbsolutePaths.
@Test
public void thinArchivesDoNotContainAbsolutePaths() throws IOException {
CxxPlatform cxxPlatform = CxxPlatformUtils.build(new CxxBuckConfig(FakeBuckConfig.builder().build()));
assumeTrue(cxxPlatform.getAr().supportsThinArchives());
ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "cxx_library", tmp);
workspace.setUp();
Path archive = workspace.buildAndReturnOutput("-c", "cxx.archive_contents=thin", "//:foo#default,static");
// can parse the archive contents.
try (OutputStream outputStream = Files.newOutputStream(workspace.getPath(archive), StandardOpenOption.WRITE)) {
outputStream.write(ObjectFileScrubbers.GLOBAL_HEADER);
}
// Now iterate the archive and verify it contains no absolute paths.
try (ArArchiveInputStream stream = new ArArchiveInputStream(new FileInputStream(workspace.getPath(archive).toFile()))) {
ArArchiveEntry entry;
while ((entry = stream.getNextArEntry()) != null) {
if (!entry.getName().isEmpty()) {
assertFalse("found absolute path: " + entry.getName(), workspace.getDestPath().getFileSystem().getPath(entry.getName()).isAbsolute());
}
}
}
}
Aggregations