|
| 1 | +package de.skymatic.fusepanama; |
| 2 | + |
| 3 | +import java.foreign.Scope; |
| 4 | +import java.foreign.memory.Callback; |
| 5 | +import java.foreign.memory.Pointer; |
| 6 | +import java.lang.reflect.Method; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.Set; |
| 9 | +import java.util.stream.Collectors; |
| 10 | + |
| 11 | +import com.github.libfuse.fuse_common_h; |
| 12 | +import com.github.libfuse.fuse_h; |
| 13 | +import com.github.libfuse.fuse_lib; |
| 14 | +import org.slf4j.Logger; |
| 15 | +import org.slf4j.LoggerFactory; |
| 16 | + |
| 17 | +public abstract class AbstractFuseFileSystem implements AutoCloseable, FuseOperations { |
| 18 | + |
| 19 | + private static final Logger LOG = LoggerFactory.getLogger(AbstractFuseFileSystem.class); |
| 20 | + |
| 21 | + protected final Scope scope; |
| 22 | + protected final fuse_h.fuse_operations fuseOperations; |
| 23 | + private final Set<String> notImplementedMethods; |
| 24 | + private Pointer<fuse_h.fuse> fuseHandle; |
| 25 | + |
| 26 | + public AbstractFuseFileSystem() { |
| 27 | + this.scope = Scope.globalScope().fork(); |
| 28 | + this.fuseOperations = scope.allocateStruct(fuse_h.fuse_operations.class); |
| 29 | + this.notImplementedMethods = Arrays.stream(getClass().getMethods()) |
| 30 | + .filter(method -> method.getAnnotation(NotImplemented.class) != null) |
| 31 | + .map(Method::getName) |
| 32 | + .collect(Collectors.toSet()); |
| 33 | + fuseOperations.getattr$set(allocateIfImplemented("getattr", this::getattr)); |
| 34 | + fuseOperations.readlink$set(allocateIfImplemented("readlink", this::readlink)); |
| 35 | + fuseOperations.getdir$set(allocateIfImplemented("getdir", this::getdir)); |
| 36 | + fuseOperations.mknod$set(allocateIfImplemented("mknod", this::mknod)); |
| 37 | + fuseOperations.mkdir$set(allocateIfImplemented("mkdir", this::mkdir)); |
| 38 | + fuseOperations.unlink$set(allocateIfImplemented("unlink", this::unlink)); |
| 39 | + fuseOperations.rmdir$set(allocateIfImplemented("rmdir", this::rmdir)); |
| 40 | + fuseOperations.symlink$set(allocateIfImplemented("symlink", this::symlink)); |
| 41 | + fuseOperations.rename$set(allocateIfImplemented("rename", this::rename)); |
| 42 | + fuseOperations.link$set(allocateIfImplemented("link", this::link)); |
| 43 | + fuseOperations.chmod$set(allocateIfImplemented("chmod", this::chmod)); |
| 44 | + fuseOperations.chown$set(allocateIfImplemented("chown", this::chown)); |
| 45 | + fuseOperations.truncate$set(allocateIfImplemented("truncate", this::truncate)); |
| 46 | + fuseOperations.utime$set(allocateIfImplemented("utime", this::utime)); |
| 47 | + fuseOperations.open$set(allocateIfImplemented("open", this::open)); |
| 48 | + fuseOperations.read$set(allocateIfImplemented("read", this::read)); |
| 49 | + fuseOperations.write$set(allocateIfImplemented("write", this::write)); |
| 50 | + fuseOperations.statfs$set(allocateIfImplemented("statfs", this::statfs)); |
| 51 | + fuseOperations.flush$set(allocateIfImplemented("flush", this::flush)); |
| 52 | + fuseOperations.release$set(allocateIfImplemented("release", this::release)); |
| 53 | + fuseOperations.fsync$set(allocateIfImplemented("fsync", this::fsync)); |
| 54 | + fuseOperations.setxattr$set(allocateIfImplemented("setxattr", this::setxattr)); |
| 55 | + fuseOperations.getxattr$set(allocateIfImplemented("getxattr", this::getxattr)); |
| 56 | + fuseOperations.listxattr$set(allocateIfImplemented("listxattr", this::listxattr)); |
| 57 | + fuseOperations.removexattr$set(allocateIfImplemented("removexattr", this::removexattr)); |
| 58 | + fuseOperations.opendir$set(allocateIfImplemented("opendir", this::opendir)); |
| 59 | + fuseOperations.readdir$set(allocateIfImplemented("readdir", this::readdir)); |
| 60 | + fuseOperations.releasedir$set(allocateIfImplemented("releasedir", this::releasedir)); |
| 61 | + fuseOperations.fsyncdir$set(allocateIfImplemented("fsyncdir", this::fsyncdir)); |
| 62 | + fuseOperations.init$set(allocateIfImplemented("init", this::init)); |
| 63 | + fuseOperations.destroy$set(allocateIfImplemented("destroy", this::destroy)); |
| 64 | + fuseOperations.access$set(allocateIfImplemented("access", this::access)); |
| 65 | + fuseOperations.create$set(allocateIfImplemented("create", this::create)); |
| 66 | + fuseOperations.ftruncate$set(allocateIfImplemented("ftruncate", this::ftruncate)); |
| 67 | + fuseOperations.fgetattr$set(allocateIfImplemented("fgetattr", this::fgetattr)); |
| 68 | + fuseOperations.lock$set(allocateIfImplemented("lock", this::lock)); |
| 69 | + fuseOperations.utimens$set(allocateIfImplemented("utimens", this::utimens)); |
| 70 | + fuseOperations.bmap$set(allocateIfImplemented("bmap", this::bmap)); |
| 71 | + fuseOperations.ioctl$set(allocateIfImplemented("ioctl", this::ioctl)); |
| 72 | + fuseOperations.poll$set(allocateIfImplemented("poll", this::poll)); |
| 73 | + fuseOperations.write_buf$set(allocateIfImplemented("writeBuf", this::writeBuf)); |
| 74 | + fuseOperations.read_buf$set(allocateIfImplemented("readBuf", this::readBuf)); |
| 75 | + fuseOperations.flock$set(allocateIfImplemented("flock", this::flock)); |
| 76 | + fuseOperations.fallocate$set(allocateIfImplemented("fallocate", this::fallocate)); |
| 77 | + } |
| 78 | + |
| 79 | + private <T> Callback<T> allocateIfImplemented(String methodName, T callback) { |
| 80 | + if (notImplementedMethods.contains(methodName)) { |
| 81 | + return Callback.ofNull(); |
| 82 | + } else { |
| 83 | + return scope.allocateCallback(callback); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public Pointer<Void> init(Pointer<fuse_common_h.fuse_conn_info> conn) { |
| 89 | + this.fuseHandle = fuse_lib.fuse_get_context().get().fuse$get(); |
| 90 | + LOG.debug("init()"); |
| 91 | + return Pointer.ofNull(); |
| 92 | + } |
| 93 | + |
| 94 | + protected Pointer<fuse_h.fuse> getFuseHandle() { |
| 95 | + return fuseHandle; |
| 96 | + } |
| 97 | + |
| 98 | + public fuse_h.fuse_operations getFuseOperations() { |
| 99 | + return fuseOperations; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void destroy(Pointer<?> pointer) { |
| 104 | + this.fuseHandle = Pointer.ofNull(); |
| 105 | + LOG.debug("destroy()"); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public void close() { |
| 110 | + scope.close(); |
| 111 | + } |
| 112 | +} |
0 commit comments