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

问题描述 投票:0回答:1

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

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

请参见链接https://github.com/libfuse/libfuse/blob/74596e2929c9b9b065f90d04ab7a2232652c64c4/include/fuse.h#L609

但是哪里没有有关要创建文件的用户的信息,这意味着将始终与运行融合过程的所有者用户一起创建文件。

例如,我以root用户身份启动了融合过程,但从我的用户创建文件:

$ echo $USERNAME
 myuser
$ echo 'f' > f
$ ll
$ ll
total 4,0K
-rw-r--r-- 1 root root
fuse libfuse
1个回答
0
投票

您可以使用全局熔丝上下文获取主叫用户的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;
};
© www.soinside.com 2019 - 2024. All rights reserved.