【问题标题】:fuse parsing custom arguments in C在 C 中熔断解析自定义参数
【发布时间】:2016-09-28 00:09:45
【问题描述】:

在我的 c 项目中使用 libfuse,我正在尝试添加自定义命令行参数并处理它们。

这是我所依赖的一个例子

https://github.com/libfuse/libfuse/wiki/Option-Parsing

首先,我尝试为挂载点配置-с <pathtoconfig>做参数

我尝试了很多方法来描述像-c --config conf= -o conf= 这样的选项,但都没有效果

请帮我找到解决问题的正确方法:(

#include <stdio.h>
#include <unistd.h>

#include <string.h>

#include <stdlib.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <fuse.h>

#include "fuu_walk.h"
#include "jsmnload.h"

#define _JSMN_TOKEN_SIZE_   256
#define _JSMN_BUFFER_SIZE_  4096
#define MYFS_OPT(t, p, v) { t, offsetof(struct myfs_config, p), v }

struct myfs_config {
    char *mystring;
} conf;

static struct fuse_opt myfs_opts[] = {
    MYFS_OPT("-c %s", mystring, 1),

    FUSE_OPT_END
};

jsmntok_t   t[_JSMN_TOKEN_SIZE_];
char        buf[_JSMN_BUFFER_SIZE_];
#if 0
= ""
"{\"root\": ["
    "{\"path\":\"/\", \"mode\":\"drw-------\"},"
    "{\"path\":\"/12ABC345DE67\", \"mode\":\"drw-------\"},"
    "{\"path\":\"/12ABC345DE67/_XQ01\", \"mode\":\"-rw-------\"},"
    "{\"path\":\"/12ABC345DE67/_XQ02\", \"mode\":\"-rw-------\"},"
    "{\"path\":\"/12ABC345DE78\", \"mode\":\"drw-------\"},"
    "{\"path\":\"/12ABC345DE89\", \"mode\":\"drw-------\"}"
"]}";
#endif


static int myfs_opt_proc(void *data, const char *arg, int key, struct fuse_args *outargs)
{
    struct myfs_config *ptr = (struct myfs_config *)data;
    FILE *conf;
    int rc = 0; 

//I wanna check the argument on the each iteration of fuse_opt_parse. It's just the debug printf
    printf("arg = %s\t string %s\t key = %i\n", arg, ptr->mystring, key);
    switch (key) {
        case 1:
            conf = fopen(ptr->mystring, "r");
            rc = read(fileno(conf), buf, _JSMN_BUFFER_SIZE_);

            if ( jsmnload(buf, t, _JSMN_TOKEN_SIZE_, fuu_mkfstree) < 0 ) {
                printf("Error load configuration\n");
                exit(-1);
            }

    }

    return 1;
}

int main(int argc, char *argv[])
{
    struct fuse_args args = FUSE_ARGS_INIT(argc, argv);

    memset(&conf, 0, sizeof(conf));

    fuse_opt_parse(&args, &conf, myfs_opts, myfs_opt_proc);

    return fuu_main(args.argc, args.argv);
}

启动示例

./appendix/fuu /mnt/cdrom/ -c /mnt/fs.json

因此,myfs_opt_proc 函数中的 printf 只工作一次并输出

arg = /mnt/cdrom/        string (null)   key = -2

为什么 myfs_opt_proc 不适用于选项 -c?

【问题讨论】:

    标签: c command-line-arguments fuse


    【解决方案1】:

    我无法发表评论作为答案...查看您提供的参考资料,在我看来,没有以-c 开头的选项。所以结果似乎是正确的,因为 fuse 无法解析它。从您的链接中查看这个 sn-p:

    fuse_opt_add_arg(&args, "-omodules=subdir,subdir=/foo");
    

    您可以尝试使用-o 宣布融合选项。


    编辑:您的示例与链接的示例不同,请尝试将以下行添加到定义的struct

    FUSE_OPT_KEY("-c", "KEY_CONFIG");
    

    前几行

    emum {
        KEY_CONFIG
    };
    

    并在 myfs_opt_proc 函数中像这样解析它

    switch (key) {
        case KEY_CONFIG:
            /* ... */
    

    。总结一下,您错过了声明 -c 键。

    【讨论】:

    • 我认为你是对的。 MYFS_OPT 属于 -o。也许FUSE_OPT_KEY 可能会有所帮助
    • 如果我使用FUSE_OPT_KEY,输出是arg = /mnt/cdrom/ string (null) key = -2 arg = -c string (null) key = -1 arg = /media/sf_work_134/fuseuse/appendix/fs.json string (null) key = -2。看来我需要使用fuse_opt_add_arg,但我不明白它是如何工作的
    • 我认为FUSE_OPT_KEY 只是重载了现有选项,但我想创建我的自定义选项
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-04
    • 2023-01-17
    • 2013-08-03
    相关资源
    最近更新 更多