mirror of
https://github.com/falk-werner/webfuse-provider
synced 2024-10-27 20:44:10 +00:00
use getopt to parse arguments (instead of libfuse)
This commit is contained in:
parent
09560fc9fe
commit
0c0c94242a
143
src/app/main.c
143
src/app/main.c
@ -2,45 +2,117 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <getopt.h>
|
||||
|
||||
#include "wsfs/fuse_wrapper.h"
|
||||
#include "wsfs/operations.h"
|
||||
#include "wsfs/server.h"
|
||||
|
||||
struct options
|
||||
struct args
|
||||
{
|
||||
struct wsfs_server_config config;
|
||||
char * mount_point;
|
||||
int show_help;
|
||||
};
|
||||
|
||||
static struct fuse_opt const option_spec[] =
|
||||
{
|
||||
{"--document_root=%s" , offsetof(struct options, config.document_root), 1},
|
||||
{"--server_cert_path=%s", offsetof(struct options, config.cert_path), 1},
|
||||
{"--server_key_path=%s" , offsetof(struct options, config.key_path), 1},
|
||||
{"--vhost_name=%s" , offsetof(struct options, config.vhost_name), 1},
|
||||
{"--port=%d" , offsetof(struct options, config.port), 1},
|
||||
{"-h" , offsetof(struct options, show_help), 1},
|
||||
{"--help" , offsetof(struct options, show_help), 1},
|
||||
FUSE_OPT_END
|
||||
};
|
||||
|
||||
static void show_help(void)
|
||||
{
|
||||
printf(
|
||||
"wsfs, Copyright (c) 2019, Falk Werner\n"
|
||||
"Websocket file system daemon\n"
|
||||
"\n"
|
||||
"File-system specific options:\n"
|
||||
" --document_root=<s> Path of www directory (default: not set, www disabled)\n"
|
||||
" --server_cert_path=<s> Path of servers own certificate (default: not set, TLS disabled)\n"
|
||||
" --server_key_path=<s> Path of servers private key (default: not set, TLS disabled)\n"
|
||||
" --vhost_name=<s> Name of virtual host (default: \"localhost\")\n"
|
||||
" --port=<d> Number of servers port (default: 8080)\n"
|
||||
"Usage: wsfs [m <mount_point>] [-d <document_root] [-n <vhost_name>] [-p <port>]\n"
|
||||
" [-c <server_cert_path> -k] [<server_key_path>]\n"
|
||||
"\n"
|
||||
"Options:\n"
|
||||
"\t-m, --mount_point Path of mount point (required)\n"
|
||||
"\t-d, --document_root Path of www directory (default: not set, www disabled)\n"
|
||||
"\t-c, --server_cert_path Path of servers own certificate (default: not set, TLS disabled)\n"
|
||||
"\t-k, --server_key_path Path of servers private key (default: not set, TLS disabled)\n"
|
||||
"\t-n, --vhost_name Name of virtual host (default: \"localhost\")\n"
|
||||
"\t-p, --port Number of servers port (default: 8080)\n"
|
||||
"\n");
|
||||
}
|
||||
|
||||
static int parse_arguments(int argc, char * argv[], struct args * args)
|
||||
{
|
||||
static struct option const options[] =
|
||||
{
|
||||
{"mount_point", required_argument, NULL, 'm'},
|
||||
{"document_root", required_argument, NULL, 'd'},
|
||||
{"server_cert_path", required_argument, NULL, 'c'},
|
||||
{"server_key_path", required_argument, NULL, 'k'},
|
||||
{"vhost_name", required_argument, NULL, 'n'},
|
||||
{"port", required_argument, NULL, 'p'},
|
||||
{"help", no_argument, NULL, 'h'},
|
||||
{NULL, 0, NULL, 0}
|
||||
};
|
||||
|
||||
bool result = EXIT_SUCCESS;
|
||||
bool finished = false;
|
||||
while ((!finished) && (EXIT_SUCCESS == result))
|
||||
{
|
||||
int option_index = 0;
|
||||
int const c = getopt_long(argc, argv, "m:d:c:k:n:p:h", options, &option_index);
|
||||
|
||||
switch (c)
|
||||
{
|
||||
case -1:
|
||||
finished = true;
|
||||
break;
|
||||
case 'h':
|
||||
args->show_help = true;
|
||||
finished = true;
|
||||
break;
|
||||
case 'm':
|
||||
free(args->mount_point);
|
||||
args->mount_point = strdup(optarg);
|
||||
break;
|
||||
case 'd':
|
||||
free(args->config.document_root);
|
||||
args->config.document_root = strdup(optarg);
|
||||
break;
|
||||
case 'c':
|
||||
free(args->config.cert_path);
|
||||
args->config.cert_path = strdup(optarg);
|
||||
break;
|
||||
case 'k':
|
||||
free(args->config.key_path);
|
||||
args->config.key_path = strdup(optarg);
|
||||
break;
|
||||
case 'n':
|
||||
free(args->config.vhost_name);
|
||||
args->config.vhost_name = strdup(optarg);
|
||||
break;
|
||||
case 'p':
|
||||
args->config.port = atoi(optarg);
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr, "error: unknown argument\n");
|
||||
result = EXIT_FAILURE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (EXIT_SUCCESS != result)
|
||||
{
|
||||
args->show_help = true;
|
||||
}
|
||||
|
||||
if ((!args->show_help) && (NULL == args->mount_point))
|
||||
{
|
||||
fprintf(stderr, "error: missing mount point\n");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
struct options options =
|
||||
struct args args =
|
||||
{
|
||||
.config =
|
||||
{
|
||||
@ -50,28 +122,25 @@ int main(int argc, char * argv[])
|
||||
.vhost_name = strdup("localhost"),
|
||||
.port = 8080,
|
||||
},
|
||||
.mount_point = NULL,
|
||||
.show_help = 0
|
||||
};
|
||||
|
||||
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
|
||||
if (-1 == fuse_opt_parse(&args, &options, option_spec, NULL))
|
||||
{
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
int result = parse_arguments(argc, argv, &args);
|
||||
|
||||
struct fuse_operations operations;
|
||||
wsfs_operations_init(&operations);
|
||||
|
||||
int result;
|
||||
if (!options.show_help)
|
||||
if (!args.show_help)
|
||||
{
|
||||
struct wsfs_server * server = wsfs_server_create(&options.config);
|
||||
struct wsfs_server * server = wsfs_server_create(&args.config);
|
||||
if (NULL != server)
|
||||
{
|
||||
wsfs_server_start(server);
|
||||
struct wsfs_jsonrpc * const rpc = wsfs_server_get_jsonrpc_service(server);
|
||||
|
||||
result = fuse_main(args.argc, args.argv, &operations, rpc);
|
||||
char * fuse_args[] = { "-s", "-f", args.mount_point, NULL };
|
||||
result = fuse_main(3, fuse_args, &operations, rpc);
|
||||
wsfs_server_dispose(server);
|
||||
}
|
||||
else
|
||||
@ -82,21 +151,11 @@ int main(int argc, char * argv[])
|
||||
}
|
||||
else
|
||||
{
|
||||
if (0 == fuse_opt_add_arg(&args, "--help"))
|
||||
{
|
||||
fuse_main(args.argc, args.argv, &operations, NULL);
|
||||
show_help();
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(stderr, "fatal: unable to show help\n");
|
||||
}
|
||||
|
||||
result = EXIT_FAILURE;
|
||||
show_help();
|
||||
}
|
||||
|
||||
fuse_opt_free_args(&args);
|
||||
wsfs_server_config_clear(&options.config);
|
||||
wsfs_server_config_clear(&args.config);
|
||||
free(args.mount_point);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user