use of java.nio.MappedByteBuffer in project buck by facebook.
the class AbstractElfClearProgramHeadersStep method execute.
@Override
public StepExecutionResult execute(ExecutionContext context) throws IOException {
try (FileChannel channel = FileChannel.open(getFilesystem().resolve(getPath()), StandardOpenOption.READ, StandardOpenOption.WRITE)) {
MappedByteBuffer buffer = channel.map(READ_WRITE, 0, channel.size());
Elf elf = new Elf(buffer);
Preconditions.checkState(elf.header.e_phoff == (int) elf.header.e_phoff, "program headers are expected to be within 4GB of beginning of file");
buffer.position((int) elf.header.e_phoff);
for (int index = 0; index < elf.header.e_phnum * elf.header.e_phentsize; index++) {
buffer.put((byte) 0);
}
}
return StepExecutionResult.SUCCESS;
}
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();
}
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!!!!"));
}
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();
}
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));
}
}
}
Aggregations