1 /* $NetBSD: refuse_lowlevel.c,v 1.1 2016/11/20 13:26:28 pho Exp $ */ 2 3 /* 4 * Copyright (c) 2016 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote 16 * products derived from this software without specific prior written 17 * permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS 20 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 25 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #if !defined(lint) 34 __RCSID("$NetBSD: refuse_lowlevel.c,v 1.1 2016/11/20 13:26:28 pho Exp $"); 35 #endif /* !lint */ 36 37 #include <fuse_lowlevel.h> 38 #include <fuse_opt.h> 39 #include <stddef.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 44 #define REFUSE_LOWLEVEL_OPT(t, p, v) \ 45 { t, offsetof(struct fuse_cmdline_opts, p), v } 46 47 static struct fuse_opt fuse_lowlevel_opts[] = { 48 REFUSE_LOWLEVEL_OPT("-h" , show_help , 1), 49 REFUSE_LOWLEVEL_OPT("--help" , show_help , 1), 50 REFUSE_LOWLEVEL_OPT("-V" , show_version , 1), 51 REFUSE_LOWLEVEL_OPT("--version", show_version , 1), 52 REFUSE_LOWLEVEL_OPT("-d" , debug , 1), 53 REFUSE_LOWLEVEL_OPT("debug" , debug , 1), 54 REFUSE_LOWLEVEL_OPT("-d" , foreground , 1), 55 REFUSE_LOWLEVEL_OPT("debug" , foreground , 1), 56 REFUSE_LOWLEVEL_OPT("-f" , foreground , 1), 57 REFUSE_LOWLEVEL_OPT("-s" , singlethread , 1), 58 REFUSE_LOWLEVEL_OPT("fsname=" , nodefault_fsname, 1), 59 FUSE_OPT_KEY ("fsname=" , FUSE_OPT_KEY_KEEP ), 60 FUSE_OPT_END 61 }; 62 63 void fuse_lowlevel_version(void) 64 { 65 /* XXX: Print something */ 66 } 67 68 void fuse_cmdline_help(void) 69 { 70 printf("refuse options:\n" 71 " -d, -o debug enable debug output, implies -f\n" 72 " -f foreground mode\n" 73 " -s single threaded mode (always enabled for now)\n" 74 " -o fsname=NAME explicitly set the name of the file system\n"); 75 } 76 77 static int refuse_lowlevel_opt_proc(void* data, const char *arg, int key, 78 struct fuse_args *outargs) 79 { 80 struct fuse_cmdline_opts *opts = data; 81 82 switch (key) { 83 case FUSE_OPT_KEY_NONOPT: 84 if (opts->mountpoint == NULL) { 85 return fuse_opt_add_opt(&opts->mountpoint, arg); 86 } 87 else { 88 (void)fprintf(stderr, "fuse: invalid argument: %s\n", arg); 89 return -1; 90 } 91 92 default: 93 return 1; /* keep the argument */ 94 } 95 } 96 97 static int add_default_fsname(struct fuse_args *args) 98 { 99 const char *arg0 = args->argv[0]; 100 const char *slash; 101 102 if (arg0 == NULL || arg0[0] == '\0') { 103 return fuse_opt_add_arg(args, "-ofsname=refuse"); 104 } else { 105 char *arg; 106 int rv; 107 108 if ((slash = strrchr(arg0, '/')) == NULL) { 109 slash = arg0; 110 } else { 111 slash += 1; 112 } 113 114 if (asprintf(&arg, "-ofsname=refuse:%s", slash) == -1) 115 return -1; 116 117 rv = fuse_opt_add_arg(args, arg); 118 free(arg); 119 return rv; 120 } 121 } 122 123 int fuse_parse_cmdline(struct fuse_args *args, struct fuse_cmdline_opts *opts) 124 { 125 memset(opts, 0, sizeof(*opts)); 126 127 /* 128 * XXX: The single threaded mode is always enabled and cannot be 129 * disabled. This is because puffs currently does not support 130 * multithreaded operation. 131 */ 132 opts->singlethread = 1; 133 134 if (fuse_opt_parse(args, opts, fuse_lowlevel_opts, 135 refuse_lowlevel_opt_proc) == -1) 136 return -1; 137 138 if (!opts->nodefault_fsname) { 139 /* -o fsname=%s is not specified so add a default fsname 140 * generated from the program basename. 141 */ 142 if (add_default_fsname(args) == -1) 143 return -1; 144 } 145 146 return 0; 147 } 148