具有 libfuse 和文件所有权的自定义 fs

Custom fs with libfuse and file ownership

当用户创建文件时,将调用创建回调:

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

详见linkhttps://github.com/libfuse/libfuse/blob/74596e2929c9b9b065f90d04ab7a2232652c64c4/include/fuse.h#L609

但是没有关于用户想要创建文件的信息,这意味着文件将始终由融合进程 运行 来自的所有者用户创建。

例如,我以 root 身份启动 fuse 进程,但从我的用户创建文件:

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

您可以使用全局熔断器上下文获取调用用户的 uid 和 gid:

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

来自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;
};