Skip to content

Instantly share code, notes, and snippets.

@etehtsea
Created May 11, 2017 16:20
Show Gist options
  • Save etehtsea/12584ea05174f2f41a8cd850c9e03135 to your computer and use it in GitHub Desktop.
Save etehtsea/12584ea05174f2f41a8cd850c9e03135 to your computer and use it in GitHub Desktop.
import java.io.File;
import jnr.ffi.Runtime;
import jnr.ffi.Memory;
import jnr.ffi.StructLayout;
import jnr.ffi.LibraryLoader;
import jnr.ffi.Pointer;
import jnr.ffi.annotations.Out;
import jnr.ffi.annotations.Transient;
public class HelloWorld {
public static interface LibC {
int puts(String s);
int __xstat64(int statVersion, CharSequence path, @Out @Transient SB stat);
}
public static final class SB extends StructLayout {
public SB(jnr.ffi.Runtime runtime) {
super(runtime);
}
public final dev_t st_dev = new dev_t();
public final ino_t st_ino = new ino_t();
public final mode_t st_mode = new mode_t();
public final nlink_t st_nlink = new nlink_t();
public final uid_t st_uid = new uid_t();
public final gid_t st_gid = new gid_t();
public final dev_t st_rdev = new dev_t();
public final size_t st_size = new size_t();
public final blksize_t st_blksize = new blksize_t();
public final blkcnt_t st_blocks = new blkcnt_t();
public final time_t st_atime = new time_t(); // Time of last access
public final SignedLong st_atimensec = new SignedLong(); // Time of last access (nanoseconds)
public final time_t st_mtime = new time_t(); // Last data modification time
public final SignedLong st_mtimensec = new SignedLong(); // Last data modification time (nanoseconds)
public final time_t st_ctime = new time_t(); // Time of last status change
public final SignedLong st_ctimensec = new SignedLong(); // Time of last status change (nanoseconds)
public final Signed64 __unused4 = new Signed64();
}
public static void main(String[] args) throws Throwable {
LibC libc = LibraryLoader.create(LibC.class).load("c");
File f = File.createTempFile("stat", null).getParentFile();
//jnr.ffi.Runtime runtime = Runtime.getRuntime(libc);
//SB stat = new SB(runtime);
//Pointer memory = Memory.allocate(runtime, stat.size());
//System.out.println("Struct size: " + stat.size());
try {
System.out.println(f.getAbsolutePath());
// int ret = libc.__xstat64(0, f.getAbsolutePath(), stat);
//System.out.println("Finished with: " + ret);
//System.out.println("\tdev: " + stat.st_dev.get(memory));
//System.out.println("\tinode: " + stat.st_ino.get(memory));
//System.out.println("\tmode: " + stat.st_mode.get(memory));
//System.out.println("\tlinks: " + stat.st_nlink.get(memory));
//System.out.println("\towner: " + stat.st_uid.get(memory));
//System.out.println("\tgroup: " + stat.st_gid.get(memory));
//System.out.println("\trdev: " + stat.st_rdev.get(memory));
//System.out.println("\tsize: " + stat.st_size.get(memory));
//System.out.println("\tblksize: " + stat.st_blksize.get(memory));
//System.out.println("\tblkcnt: " + stat.st_blocks.get(memory));
} finally {
f.delete();
}
libc.puts("Hello, World");
}
}
@etehtsea
Copy link
Author

kes@labs:~$ javac -cp jnr-ffi-2.1.5.jar HelloWorld.java && java -cp asm-4.0_RC2.jar:jffi-1.2.15.jar:jnr-ffi-2.1.5.jar:. HelloWorld
/tmp
Exception in thread "main" java.lang.IllegalArgumentException: unsupported type: class HelloWorld$SB
        at jnr.ffi.provider.jffi.Types.lookupType(Types.java:123)
        at jnr.ffi.provider.jffi.Types.lookupAndCacheType(Types.java:59)
        at jnr.ffi.provider.jffi.Types.getType(Types.java:45)
        at jnr.ffi.provider.jffi.InvokerUtil.getMethodParameterNativeType(InvokerUtil.java:213)
        at jnr.ffi.provider.jffi.InvokerUtil.getParameterType(InvokerUtil.java:151)
        at jnr.ffi.provider.jffi.InvokerUtil.getParameterTypes(InvokerUtil.java:180)
        at jnr.ffi.provider.jffi.AsmLibraryLoader.generateInterfaceImpl(AsmLibraryLoader.java:166)
        at jnr.ffi.provider.jffi.AsmLibraryLoader.loadLibrary(AsmLibraryLoader.java:89)
        at jnr.ffi.provider.jffi.NativeLibraryLoader.loadLibrary(NativeLibraryLoader.java:44)
        at jnr.ffi.LibraryLoader.load(LibraryLoader.java:325)
        at jnr.ffi.LibraryLoader.load(LibraryLoader.java:304)
        at HelloWorld.main(HelloWorld.java:43)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment