Commits

Etienne Perot  committed 9dab949

Add support for getting the fuse_context structure and some convenience accessors for some of its fields

  • Participants
  • Parent commits 7c38614

Comments (0)

Files changed (3)

File src/net/fusejna/FuseFilesystem.java

 import net.fusejna.types.TypeMode.ModeWrapper;
 import net.fusejna.types.TypeMode.NodeType;
 import net.fusejna.types.TypeOff;
+import net.fusejna.types.TypePid;
 import net.fusejna.types.TypeSize;
 import net.fusejna.types.TypeUInt32;
 import net.fusejna.types.TypeUid;
 	@UserMethod
 	public abstract int getattr(final String path, final StatWrapper stat);
 
+	/**
+	 * Returns the raw fuse_context structure. Only valid when called while a filesystem operation is taking place.
+	 * 
+	 * @return The fuse_context structure by reference.
+	 */
+	protected final StructFuseContext getFuseContext()
+	{
+		if (!isMounted()) {
+			throw new IllegalStateException("Cannot get FUSE context if the filesystem is not mounted.");
+		}
+		return FuseJna.getFuseContext();
+	}
+
+	/**
+	 * Returns the gid field of the fuse context. Only valid when called while a filesystem operation is taking place.
+	 * 
+	 * @return The group ID of the process executing an operation on this filesystem.
+	 */
+	protected TypeGid getFuseContextGid()
+	{
+		return getFuseContext().gid;
+	}
+
+	/**
+	 * Returns the pid field of the fuse context. Only valid when called while a filesystem operation is taking place.
+	 * 
+	 * @return The process ID of the process executing an operation on this filesystem.
+	 */
+	protected TypePid getFuseContextPid()
+	{
+		return getFuseContext().pid;
+	}
+
+	/**
+	 * Returns the uid field of the fuse context. Only valid when called while a filesystem operation is taking place.
+	 * 
+	 * @return The user ID of the user running the process executing an operation of this filesystem.
+	 */
+	protected TypeUid getFuseContextUid()
+	{
+		return getFuseContext().uid;
+	}
+
 	final String getFuseName()
 	{
 		String name = getName();

File src/net/fusejna/FuseJna.java

 
 import net.fusejna.types.TypeSize;
 
-final class FuseJna
+public final class FuseJna
 {
 	private static final class MountThread extends Thread
 	{
 	private static int currentUid = 0;
 	private static int currentGid = 0;
 
+	static StructFuseContext getFuseContext()
+	{
+		return init().fuse_get_context();
+	}
+
 	private static final String getFilesystemName(final File mountPoint, final String fuseName)
 	{
 		filesystemNameLock.lock();

File src/net/fusejna/examples/FuseContextExampleFS.java

+package net.fusejna.examples;
+
+import java.nio.ByteBuffer;
+
+import net.fusejna.FuseException;
+import net.fusejna.StructFuseFileInfo.FileInfoWrapper;
+
+public final class FuseContextExampleFS
+{
+	private static class FuseContextFS extends HelloFS
+	{
+		@Override
+		public int read(final String path, final ByteBuffer buffer, final long size, final long offset,
+				final FileInfoWrapper info)
+		{
+			System.out.println(path + " is being read by pid=" + getFuseContextPid() + " uid=" + getFuseContextUid() + " gid="
+					+ getFuseContextGid());
+			return super.read(path, buffer, size, offset, info);
+		}
+	}
+
+	public static void main(final String[] args) throws FuseException
+	{
+		if (args.length != 1) {
+			System.err.println("Usage: FuseContextExampleFS <mountpoint>");
+			System.exit(1);
+		}
+		new FuseContextFS().mount(args[0]);
+	}
+}