Skip to content
This repository has been archived by the owner on Apr 24, 2021. It is now read-only.

Commit

Permalink
Using FD where available to avoid using reflection on Android VM
Browse files Browse the repository at this point in the history
  • Loading branch information
desyncr committed Aug 7, 2020
1 parent 448961b commit d1fe521
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
16 changes: 14 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ repositories {
mavenLocal() // TODO: use lib/ instead?
maven { url 'https://mvn.freenetproject.org' }
jcenter()
maven { url 'https://jitpack.io' }
}

sourceSets {
Expand Down Expand Up @@ -220,7 +221,19 @@ dependencies {
compile "org.bouncycastle:bcprov-jdk15on:1.59"
compile "net.java.dev.jna:jna:4.5.2"
compile "net.java.dev.jna:jna-platform:4.5.2"
compile "org.freenetproject:freenet-ext:29"
compile "org.apache.commons:commons-compress:1.4.1"
compile "tanukisoft:wrapper:3.2.3"

compile 'com.github.desyncr:onion-common:0.0.2'
compile 'com.github.desyncr:onion-fec:0.0.2'
compile 'com.github.desyncr:mantissa:0.0.2'

compile "org.b1.pack:lzma-sdk-4j:9.22.0"
compile "com.github.desyncr:lzmajio:0.95.2"

compile "com.github.desyncr:bitcollider-core:0.0.2"

compile 'com.github.desyncr:freenet-ext:0.60.2'

testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:1.9.5"
Expand All @@ -234,7 +247,6 @@ dependencyVerification {
'org.bouncycastle:bcprov-jdk15on:1c31e44e331d25e46d293b3e8ee2d07028a67db011e74cb2443285aed1d59c85',
'net.java.dev.jna:jna-platform:f1d00c167d8921c6e23c626ef9f1c3ae0be473c95c68ffa012bc7ae55a87e2d6',
'net.java.dev.jna:jna:0c8eb7acf67261656d79005191debaba3b6bf5dd60a43735a245429381dbecff',
'org.freenetproject:freenet-ext:32f2b3d6beedf54137ea2f9a3ebef67666d769f0966b08cd17fd7db59ba4d79f',
'junit:junit:59721f0805e223d84b90677887d9ff567dc534d7c502ca903c0c2b17f05c116a',
'org.mockito:mockito-core:f97483ba0944b9fa133aa29638764ddbeadb51ec3dbc02074c58fa2caecd07fa',
'org.hamcrest:hamcrest-library:711d64522f9ec410983bd310934296da134be4254a125080a0416ec178dfad1c',
Expand Down
5 changes: 3 additions & 2 deletions src/freenet/store/saltedhash/SaltedHashFreenetStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -1070,10 +1070,11 @@ private void setStoreFileSize(long storeMaxEntries) {
{
wrapperKeepalive.start();
if (oldMetaLen < newMetaLen) {
Fallocate.forChannel(metaFC, newMetaLen).fromOffset(oldMetaLen).execute();
// freenet-mobile-changed: Passing file descriptor to avoid using reflection
Fallocate.forChannel(metaFC, metaRAF.getFD() ,newMetaLen).fromOffset(oldMetaLen).execute();
}
if (currentHdLen < newHdLen) {
Fallocate.forChannel(hdFC, newHdLen).fromOffset(currentHdLen).execute();
Fallocate.forChannel(hdFC, hdRAF.getFD(), newHdLen).fromOffset(currentHdLen).execute();
}
}
}
Expand Down
11 changes: 8 additions & 3 deletions src/freenet/support/io/Fallocate.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public final class Fallocate {

private static final boolean IS_LINUX = Platform.isLinux();
private static final boolean IS_POSIX = !Platform.isWindows() && !Platform.isMac();
private static final boolean IS_ANDROID = Platform.isAndroid();

private static final int FALLOC_FL_KEEP_SIZE = 0x01;

Expand All @@ -40,6 +41,10 @@ public static Fallocate forChannel(FileChannel channel, long final_filesize) {
return new Fallocate(channel, getDescriptor(channel), final_filesize);
}

public static Fallocate forChannel(FileChannel channel, FileDescriptor fd, long final_filesize) {
return new Fallocate(channel, getDescriptor(fd), final_filesize);
}

public Fallocate fromOffset(long offset) {
if(offset < 0 || offset > final_filesize) throw new IllegalArgumentException();
this.offset = offset;
Expand Down Expand Up @@ -82,15 +87,15 @@ public void execute() throws IOException {

private static class FallocateHolder {
static {
Native.register(Platform.C_LIBRARY_NAME);
Native.register(FallocateHolder.class, Platform.C_LIBRARY_NAME);
}

private static native int fallocate(int fd, int mode, long offset, long length);
}

private static class FallocateHolderPOSIX {
static {
Native.register(Platform.C_LIBRARY_NAME);
Native.register(FallocateHolderPOSIX.class, Platform.C_LIBRARY_NAME);
}

private static native int posix_fallocate(int fd, long offset, long length);
Expand All @@ -110,7 +115,7 @@ private static int getDescriptor(FileChannel channel) {
private static int getDescriptor(FileDescriptor descriptor) {
try {
// Oracle java.io.FileDescriptor declares private int fd
final Field field = descriptor.getClass().getDeclaredField("fd");
final Field field = descriptor.getClass().getDeclaredField(IS_ANDROID ? "descriptor" : "fd");
field.setAccessible(true);
return (int) field.get(descriptor);
} catch (final Exception e) {
Expand Down
3 changes: 2 additions & 1 deletion src/freenet/support/io/PooledFileRandomAccessBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ public PooledFileRandomAccessBuffer(File file, boolean readOnly, long forceLengt
// Preallocate space. We want predictable disk usage, not minimal disk usage, especially for downloads.
try (WrapperKeepalive wrapperKeepalive = new WrapperKeepalive()) {
wrapperKeepalive.start();
Fallocate.forChannel(raf.getChannel(), forceLength).fromOffset(currentLength).execute();
// freenet-mobile-changed: Passing file descriptor to avoid using reflection
Fallocate.forChannel(raf.getChannel(), raf.getFD(), forceLength).fromOffset(currentLength).execute();
}
raf.setLength(forceLength);
currentLength = forceLength;
Expand Down

0 comments on commit d1fe521

Please # to comment.