1*ec9afb42Spho /* $NetBSD: refuse_lowlevel.c,v 1.4 2022/01/22 08:09:39 pho Exp $ */
20e4f248fSpho
30e4f248fSpho /*
40e4f248fSpho * Copyright (c) 2016 The NetBSD Foundation, Inc.
50e4f248fSpho * All rights reserved.
60e4f248fSpho *
70e4f248fSpho * Redistribution and use in source and binary forms, with or without
80e4f248fSpho * modification, are permitted provided that the following conditions
90e4f248fSpho * are met:
100e4f248fSpho * 1. Redistributions of source code must retain the above copyright
110e4f248fSpho * notice, this list of conditions and the following disclaimer.
120e4f248fSpho * 2. Redistributions in binary form must reproduce the above copyright
130e4f248fSpho * notice, this list of conditions and the following disclaimer in the
140e4f248fSpho * documentation and/or other materials provided with the distribution.
150e4f248fSpho * 3. The name of the author may not be used to endorse or promote
160e4f248fSpho * products derived from this software without specific prior written
170e4f248fSpho * permission.
180e4f248fSpho *
190e4f248fSpho * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
200e4f248fSpho * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
210e4f248fSpho * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
220e4f248fSpho * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
230e4f248fSpho * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
240e4f248fSpho * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
250e4f248fSpho * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
260e4f248fSpho * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
270e4f248fSpho * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
280e4f248fSpho * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
290e4f248fSpho * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
300e4f248fSpho */
310e4f248fSpho
320e4f248fSpho #include <sys/cdefs.h>
330e4f248fSpho #if !defined(lint)
34*ec9afb42Spho __RCSID("$NetBSD: refuse_lowlevel.c,v 1.4 2022/01/22 08:09:39 pho Exp $");
350e4f248fSpho #endif /* !lint */
360e4f248fSpho
371f6f9b83Spho #include <fuse_internal.h>
380e4f248fSpho #include <fuse_opt.h>
390e4f248fSpho #include <stddef.h>
400e4f248fSpho #include <stdio.h>
410e4f248fSpho #include <stdlib.h>
420e4f248fSpho #include <string.h>
430e4f248fSpho
440e4f248fSpho #define REFUSE_LOWLEVEL_OPT(t, p, v) \
450e4f248fSpho { t, offsetof(struct fuse_cmdline_opts, p), v }
460e4f248fSpho
470e4f248fSpho static struct fuse_opt fuse_lowlevel_opts[] = {
48d61ee2ebSpho REFUSE_LOWLEVEL_OPT("-h" , show_help , REFUSE_SHOW_HELP_FULL),
49d61ee2ebSpho REFUSE_LOWLEVEL_OPT("--help" , show_help , REFUSE_SHOW_HELP_FULL),
50d61ee2ebSpho REFUSE_LOWLEVEL_OPT("-ho" , show_help , REFUSE_SHOW_HELP_NO_HEADER),
510e4f248fSpho REFUSE_LOWLEVEL_OPT("-V" , show_version , 1),
520e4f248fSpho REFUSE_LOWLEVEL_OPT("--version", show_version , 1),
530e4f248fSpho REFUSE_LOWLEVEL_OPT("-d" , debug , 1),
540e4f248fSpho REFUSE_LOWLEVEL_OPT("debug" , debug , 1),
550e4f248fSpho REFUSE_LOWLEVEL_OPT("-d" , foreground , 1),
560e4f248fSpho REFUSE_LOWLEVEL_OPT("debug" , foreground , 1),
570e4f248fSpho REFUSE_LOWLEVEL_OPT("-f" , foreground , 1),
580e4f248fSpho REFUSE_LOWLEVEL_OPT("-s" , singlethread , 1),
590e4f248fSpho REFUSE_LOWLEVEL_OPT("fsname=" , nodefault_fsname, 1),
600e4f248fSpho FUSE_OPT_KEY ("fsname=" , FUSE_OPT_KEY_KEEP ),
610e4f248fSpho FUSE_OPT_END
620e4f248fSpho };
630e4f248fSpho
fuse_lowlevel_version(void)640e4f248fSpho void fuse_lowlevel_version(void)
650e4f248fSpho {
660e4f248fSpho /* XXX: Print something */
670e4f248fSpho }
680e4f248fSpho
fuse_cmdline_help(void)690e4f248fSpho void fuse_cmdline_help(void)
700e4f248fSpho {
710e4f248fSpho printf("refuse options:\n"
720e4f248fSpho " -d, -o debug enable debug output, implies -f\n"
730e4f248fSpho " -f foreground mode\n"
740e4f248fSpho " -s single threaded mode (always enabled for now)\n"
750e4f248fSpho " -o fsname=NAME explicitly set the name of the file system\n");
760e4f248fSpho }
770e4f248fSpho
refuse_lowlevel_opt_proc(void * data,const char * arg,int key,struct fuse_args * outargs)780e4f248fSpho static int refuse_lowlevel_opt_proc(void* data, const char *arg, int key,
790e4f248fSpho struct fuse_args *outargs)
800e4f248fSpho {
810e4f248fSpho struct fuse_cmdline_opts *opts = data;
820e4f248fSpho
830e4f248fSpho switch (key) {
840e4f248fSpho case FUSE_OPT_KEY_NONOPT:
850e4f248fSpho if (opts->mountpoint == NULL) {
860e4f248fSpho return fuse_opt_add_opt(&opts->mountpoint, arg);
870e4f248fSpho }
880e4f248fSpho else {
890e4f248fSpho (void)fprintf(stderr, "fuse: invalid argument: %s\n", arg);
900e4f248fSpho return -1;
910e4f248fSpho }
920e4f248fSpho
930e4f248fSpho default:
940e4f248fSpho return 1; /* keep the argument */
950e4f248fSpho }
960e4f248fSpho }
970e4f248fSpho
add_default_fsname(struct fuse_args * args)980e4f248fSpho static int add_default_fsname(struct fuse_args *args)
990e4f248fSpho {
1000e4f248fSpho const char *arg0 = args->argv[0];
1010e4f248fSpho const char *slash;
1020e4f248fSpho
1030e4f248fSpho if (arg0 == NULL || arg0[0] == '\0') {
1040e4f248fSpho return fuse_opt_add_arg(args, "-ofsname=refuse");
1050e4f248fSpho } else {
1060e4f248fSpho char *arg;
1070e4f248fSpho int rv;
1080e4f248fSpho
1090e4f248fSpho if ((slash = strrchr(arg0, '/')) == NULL) {
1100e4f248fSpho slash = arg0;
1110e4f248fSpho } else {
1120e4f248fSpho slash += 1;
1130e4f248fSpho }
1140e4f248fSpho
1150e4f248fSpho if (asprintf(&arg, "-ofsname=refuse:%s", slash) == -1)
1160e4f248fSpho return -1;
1170e4f248fSpho
1180e4f248fSpho rv = fuse_opt_add_arg(args, arg);
1190e4f248fSpho free(arg);
1200e4f248fSpho return rv;
1210e4f248fSpho }
1220e4f248fSpho }
1230e4f248fSpho
124*ec9afb42Spho int
__fuse_parse_cmdline(struct fuse_args * args,struct fuse_cmdline_opts * opts)125*ec9afb42Spho __fuse_parse_cmdline(struct fuse_args *args, struct fuse_cmdline_opts *opts)
1260e4f248fSpho {
1270e4f248fSpho memset(opts, 0, sizeof(*opts));
1280e4f248fSpho
1290e4f248fSpho /*
1300e4f248fSpho * XXX: The single threaded mode is always enabled and cannot be
1310e4f248fSpho * disabled. This is because puffs currently does not support
1320e4f248fSpho * multithreaded operation.
1330e4f248fSpho */
1340e4f248fSpho opts->singlethread = 1;
1350e4f248fSpho
1360e4f248fSpho if (fuse_opt_parse(args, opts, fuse_lowlevel_opts,
1370e4f248fSpho refuse_lowlevel_opt_proc) == -1)
1380e4f248fSpho return -1;
1390e4f248fSpho
1400e4f248fSpho if (!opts->nodefault_fsname) {
1410e4f248fSpho /* -o fsname=%s is not specified so add a default fsname
1420e4f248fSpho * generated from the program basename.
1430e4f248fSpho */
1440e4f248fSpho if (add_default_fsname(args) == -1)
1450e4f248fSpho return -1;
1460e4f248fSpho }
1470e4f248fSpho
1480e4f248fSpho return 0;
1490e4f248fSpho }
150