Search in sources :

Example 86 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project buck by facebook.

the class UnixArchiveTest method testReadingArchive.

@Test
public void testReadingArchive() throws IOException {
    Path path = tmp.resolve("test_archive.a");
    createArchiveAtPath(path);
    UnixArchive archive = new UnixArchive(FileChannel.open(path, StandardOpenOption.READ, StandardOpenOption.WRITE), new NulTerminatedCharsetDecoder(StandardCharsets.UTF_8.newDecoder()));
    assertThat(archive.getEntries().size(), equalTo(3));
    assertThat(archive.getEntries().get(0).getFileName(), equalToObject("__.SYMDEF SORTED"));
    assertThat(archive.getEntries().get(0).getFileModificationTimestamp(), equalTo(1463146035L));
    assertThat(archive.getEntries().get(0).getOwnerId(), equalTo(10727));
    assertThat(archive.getEntries().get(0).getGroupId(), equalTo(11706));
    assertThat(archive.getEntries().get(0).getFileMode(), equalTo(100644));
    assertThat(archive.getEntries().get(1).getFileName(), equalToObject("file1.txt"));
    assertThat(archive.getEntries().get(1).getFileModificationTimestamp(), equalTo(1463145840L));
    assertThat(archive.getEntries().get(1).getFileSize(), equalTo(16L));
    MappedByteBuffer buffer1 = archive.getMapForEntry(archive.getEntries().get(1));
    byte[] strBytes1 = new byte[(int) archive.getEntries().get(1).getFileSize()];
    buffer1.get(strBytes1, 0, (int) archive.getEntries().get(1).getFileSize());
    assertThat(new String(strBytes1), equalToObject("FILE1_CONTENTS\n\n"));
    assertThat(archive.getEntries().get(2).getFileName(), equalToObject("file2.txt"));
    assertThat(archive.getEntries().get(2).getFileModificationTimestamp(), equalTo(1463145848L));
    assertThat(archive.getEntries().get(2).getFileSize(), equalTo(32L));
    MappedByteBuffer buffer2 = archive.getMapForEntry(archive.getEntries().get(2));
    byte[] strBytes2 = new byte[(int) archive.getEntries().get(2).getFileSize()];
    buffer2.get(strBytes2, 0, (int) archive.getEntries().get(2).getFileSize());
    assertThat(new String(strBytes2), equalToObject("FILE2_CONTENTS_FILE2_CONTENTS\n\n\n"));
    archive.close();
}
Also used : Path(java.nio.file.Path) MappedByteBuffer(java.nio.MappedByteBuffer) Matchers.containsString(org.hamcrest.Matchers.containsString) NulTerminatedCharsetDecoder(com.facebook.buck.charset.NulTerminatedCharsetDecoder) Test(org.junit.Test)

Example 87 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project buck by facebook.

the class UnixArchiveTest method testModifyingArchive.

@Test
public void testModifyingArchive() throws IOException {
    Path path = tmp.resolve("test_archive.a");
    createArchiveAtPath(path);
    UnixArchive archive = new UnixArchive(FileChannel.open(path, StandardOpenOption.READ, StandardOpenOption.WRITE), new NulTerminatedCharsetDecoder(StandardCharsets.UTF_8.newDecoder()));
    MappedByteBuffer buffer = archive.getMapForEntry(archive.getEntries().get(1));
    byte[] bytes = new byte[(int) archive.getEntries().get(1).getFileSize()];
    buffer.get(bytes, 0, (int) archive.getEntries().get(1).getFileSize());
    assertThat(new String(bytes), equalToObject("FILE1_CONTENTS\n\n"));
    buffer.position(0);
    buffer.put("NEW_CONTENTS!!!!".getBytes(Charsets.UTF_8));
    archive.close();
    String updatedContents = new String(Files.readAllBytes(path));
    assertThat(updatedContents, not(containsString("FILE1_CONTENTS")));
    assertThat(updatedContents, containsString("NEW_CONTENTS!!!!"));
}
Also used : Path(java.nio.file.Path) MappedByteBuffer(java.nio.MappedByteBuffer) Matchers.containsString(org.hamcrest.Matchers.containsString) NulTerminatedCharsetDecoder(com.facebook.buck.charset.NulTerminatedCharsetDecoder) Test(org.junit.Test)

Example 88 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project buck by facebook.

the class ObjectPathsAbsolutifier method fixCompDirInStaticLibrary.

private void fixCompDirInStaticLibrary(Path destination) throws IOException {
    FileChannel channel = FileChannel.open(destination, StandardOpenOption.READ, StandardOpenOption.WRITE);
    ByteBuffer buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, channel.size());
    if (!UnixArchive.checkHeader(buffer)) {
        LOG.warn("Static library at %s has wrong header, skipping", destination);
        return;
    }
    UnixArchive archive = new UnixArchive(channel, nulTerminatedCharsetDecoder);
    for (UnixArchiveEntry archiveEntry : archive.getEntries()) {
        if (archiveEntry.getFileName().endsWith(".o")) {
            MappedByteBuffer map = archive.getMapForEntry(archiveEntry);
            CompDirReplacer replacer = new CompDirReplacer(map, nulTerminatedCharsetDecoder);
            replacer.replaceCompDir(oldCompDir, newCompDir);
        }
    }
    archive.close();
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) UnixArchive(com.facebook.buck.bsd.UnixArchive) UnixArchiveEntry(com.facebook.buck.bsd.UnixArchiveEntry) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer)

Example 89 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project buck by facebook.

the class ElfClearProgramHeadersStepTest method test.

@Test
public void test() throws IOException {
    ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "elf_shared_lib", tmp);
    workspace.setUp();
    ElfClearProgramHeadersStep step = ElfClearProgramHeadersStep.of(new ProjectFilesystem(tmp.getRoot()), tmp.getRoot().getFileSystem().getPath("libfoo.so"));
    step.execute(TestExecutionContext.newInstance());
    // Verify that the program table section is empty.
    try (FileChannel channel = FileChannel.open(step.getFilesystem().resolve(step.getPath()), StandardOpenOption.READ)) {
        MappedByteBuffer buffer = channel.map(READ_ONLY, 0, channel.size());
        Elf elf = new Elf(buffer);
        // Program headers are at the beginning of the file, so truncating to `int` should be ok.
        buffer.position((int) elf.header.e_phoff);
        for (int index = 0; index < elf.header.e_phnum * elf.header.e_phentsize; index++) {
            assertThat((int) buffer.get(), Matchers.equalTo(0));
        }
    }
}
Also used : ProjectWorkspace(com.facebook.buck.testutil.integration.ProjectWorkspace) MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) Elf(com.facebook.buck.cxx.elf.Elf) Test(org.junit.Test)

Example 90 with MappedByteBuffer

use of java.nio.MappedByteBuffer in project buck by facebook.

the class ElfExtractSectionsStepTest method test.

@Test
public void test() throws IOException, InterruptedException {
    // Only run if `objcopy` is available on the system.
    Path objcopy = assumeObjcopy();
    // Run the step.
    ProjectWorkspace workspace = TestDataHelper.createProjectWorkspaceForScenario(this, "elf_shared_lib", tmp);
    workspace.setUp();
    ElfExtractSectionsStep step = ElfExtractSectionsStep.of(new ProjectFilesystem(tmp.getRoot()), ImmutableList.of(objcopy.toString()), tmp.getRoot().getFileSystem().getPath("libfoo.so"), tmp.getRoot().getFileSystem().getPath("libfoo.extracted.so"), ImmutableSet.of(".dynamic"));
    step.execute(TestExecutionContext.newInstance());
    // Verify that the program table section is empty.
    try (FileChannel channel = FileChannel.open(step.getFilesystem().resolve(step.getOutput()), StandardOpenOption.READ)) {
        MappedByteBuffer buffer = channel.map(READ_ONLY, 0, channel.size());
        Elf elf = new Elf(buffer);
        List<String> sections = new ArrayList<>();
        for (int index = 0; index < elf.getNumberOfSections(); index++) {
            ElfSection section = elf.getSectionByIndex(index);
            if (section.header.sh_flags != 0) {
                String name = elf.getSectionName(section.header);
                sections.add(name);
            }
        }
        assertThat(sections, Matchers.equalTo(ImmutableList.of(".dynamic")));
    }
}
Also used : Path(java.nio.file.Path) ProjectWorkspace(com.facebook.buck.testutil.integration.ProjectWorkspace) MappedByteBuffer(java.nio.MappedByteBuffer) FileChannel(java.nio.channels.FileChannel) ArrayList(java.util.ArrayList) ProjectFilesystem(com.facebook.buck.io.ProjectFilesystem) ElfSection(com.facebook.buck.cxx.elf.ElfSection) Elf(com.facebook.buck.cxx.elf.Elf) Test(org.junit.Test)

Aggregations

MappedByteBuffer (java.nio.MappedByteBuffer)147 FileChannel (java.nio.channels.FileChannel)71 IOException (java.io.IOException)40 File (java.io.File)34 RandomAccessFile (java.io.RandomAccessFile)33 FileInputStream (java.io.FileInputStream)27 ByteBuffer (java.nio.ByteBuffer)24 Test (org.junit.Test)18 Path (java.nio.file.Path)11 ProjectWorkspace (com.facebook.buck.testutil.integration.ProjectWorkspace)9 Elf (com.facebook.buck.cxx.elf.Elf)8 FileOutputStream (java.io.FileOutputStream)7 ElfSection (com.facebook.buck.cxx.elf.ElfSection)6 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)4 FileNotFoundException (java.io.FileNotFoundException)4 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)4 Pair (com.facebook.buck.model.Pair)3 Date (java.util.Date)3 NulTerminatedCharsetDecoder (com.facebook.buck.charset.NulTerminatedCharsetDecoder)2 ElfDynamicSection (com.facebook.buck.cxx.elf.ElfDynamicSection)2