#include <sys/cdefs.h>
#if !defined(lint)
__RCSID("$NetBSD: refuse_lowlevel.c,v 1.4 2022/01/22 08:09:39 pho Exp $");
#endif
#include <fuse_internal.h>
#include <fuse_opt.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define REFUSE_LOWLEVEL_OPT(t, p, v) \
{ t, offsetof(struct fuse_cmdline_opts, p), v }
static struct fuse_opt fuse_lowlevel_opts[] = {
REFUSE_LOWLEVEL_OPT("-h" , show_help , REFUSE_SHOW_HELP_FULL),
REFUSE_LOWLEVEL_OPT("--help" , show_help , REFUSE_SHOW_HELP_FULL),
REFUSE_LOWLEVEL_OPT("-ho" , show_help , REFUSE_SHOW_HELP_NO_HEADER),
REFUSE_LOWLEVEL_OPT("-V" , show_version , 1),
REFUSE_LOWLEVEL_OPT("--version", show_version , 1),
REFUSE_LOWLEVEL_OPT("-d" , debug , 1),
REFUSE_LOWLEVEL_OPT("debug" , debug , 1),
REFUSE_LOWLEVEL_OPT("-d" , foreground , 1),
REFUSE_LOWLEVEL_OPT("debug" , foreground , 1),
REFUSE_LOWLEVEL_OPT("-f" , foreground , 1),
REFUSE_LOWLEVEL_OPT("-s" , singlethread , 1),
REFUSE_LOWLEVEL_OPT("fsname=" , nodefault_fsname, 1),
FUSE_OPT_KEY ("fsname=" , FUSE_OPT_KEY_KEEP ),
FUSE_OPT_END
};
void fuse_lowlevel_version(void)
{
}
void fuse_cmdline_help(void)
{
printf("refuse options:\n"
" -d, -o debug enable debug output, implies -f\n"
" -f foreground mode\n"
" -s single threaded mode (always enabled for now)\n"
" -o fsname=NAME explicitly set the name of the file system\n");
}
static int refuse_lowlevel_opt_proc(void* data, const char *arg, int key,
struct fuse_args *outargs)
{
struct fuse_cmdline_opts *opts = data;
switch (key) {
case FUSE_OPT_KEY_NONOPT:
if (opts->mountpoint == NULL) {
return fuse_opt_add_opt(&opts->mountpoint, arg);
}
else {
(void)fprintf(stderr, "fuse: invalid argument: %s\n", arg);
return -1;
}
default:
return 1;
}
}
static int add_default_fsname(struct fuse_args *args)
{
const char *arg0 = args->argv[0];
const char *slash;
if (arg0 == NULL || arg0[0] == '\0') {
return fuse_opt_add_arg(args, "-ofsname=refuse");
} else {
char *arg;
int rv;
if ((slash = strrchr(arg0, '/')) == NULL) {
slash = arg0;
} else {
slash += 1;
}
if (asprintf(&arg, "-ofsname=refuse:%s", slash) == -1)
return -1;
rv = fuse_opt_add_arg(args, arg);
free(arg);
return rv;
}
}
int
__fuse_parse_cmdline(struct fuse_args *args, struct fuse_cmdline_opts *opts)
{
memset(opts, 0, sizeof(*opts));
opts->singlethread = 1;
if (fuse_opt_parse(args, opts, fuse_lowlevel_opts,
refuse_lowlevel_opt_proc) == -1)
return -1;
if (!opts->nodefault_fsname) {
if (add_default_fsname(args) == -1)
return -1;
}
return 0;
}