fuselibfuse

Custom fs with libfuse and file ownership


When user create file, create callback will be called:

int (*create) (const char *, mode_t, struct fuse_file_info *);

see link for details https://github.com/libfuse/libfuse/blob/74596e2929c9b9b065f90d04ab7a2232652c64c4/include/fuse.h#L609

But where are no info about what user want to create a file, means file will be always created with owner user that fuse process run from.

For example I started fuse process as a root, but create file from my user:

$ echo $USERNAME
 myuser
$ echo 'f' > f
$ ll
$ ll
total 4,0K
-rw-r--r-- 1 root root

Solution

  • You can get the uid and gid of the calling user using the global fuse context:

            struct fuse_context *cxt = fuse_get_context();
            if (cxt)
                uid = cxt->uid;
                gid = cxt->gid;
    

    From fuse.h:

    /** Extra context that may be needed by some filesystems
     *
     * The uid, gid and pid fields are not filled in case of a writepage
     * operation.
     */
    struct fuse_context {
        /** Pointer to the fuse object */
        struct fuse *fuse;
    
        /** User ID of the calling process */
        uid_t uid;
    
        /** Group ID of the calling process */
        gid_t gid;
    
        /** Process ID of the calling thread */
        pid_t pid;
    
        /** Private filesystem data */
        void *private_data;
    
        /** Umask of the calling process */
        mode_t umask;
    };