1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2024-09-28 21:20:45 +00:00
falk-werner_webfuse-provider/src/app/main.c

163 lines
3.7 KiB
C
Raw Normal View History

2019-01-27 02:45:03 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <stdbool.h>
#include <unistd.h>
#include <getopt.h>
2019-01-27 02:45:03 +00:00
#include "wsfs/fuse_wrapper.h"
2019-01-27 02:45:03 +00:00
#include "wsfs/operations.h"
#include "wsfs/server.h"
struct args
2019-01-27 02:45:03 +00:00
{
struct wsfs_server_config config;
char * mount_point;
2019-01-27 02:45:03 +00:00
int show_help;
};
static void show_help(void)
{
printf(
"wsfs, Copyright (c) 2019, Falk Werner\n"
"Websocket file system daemon\n"
2019-01-27 02:45:03 +00:00
"\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"
2019-01-27 02:45:03 +00:00
"\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;
}
2019-01-27 02:45:03 +00:00
int main(int argc, char * argv[])
{
struct args args =
2019-01-27 02:45:03 +00:00
{
.config =
{
.document_root = NULL,
.cert_path = NULL,
.key_path = NULL,
.vhost_name = strdup("localhost"),
.port = 8080,
},
.mount_point = NULL,
2019-01-27 02:45:03 +00:00
.show_help = 0
};
int result = parse_arguments(argc, argv, &args);
2019-01-27 02:45:03 +00:00
struct fuse_operations operations;
wsfs_operations_init(&operations);
if (!args.show_help)
2019-01-27 02:45:03 +00:00
{
struct wsfs_server * server = wsfs_server_create(&args.config);
2019-01-27 02:45:03 +00:00
if (NULL != server)
{
wsfs_server_start(server);
struct wsfs_jsonrpc * const rpc = wsfs_server_get_jsonrpc_service(server);
char * fuse_args[] = { "-s", "-f", args.mount_point, NULL };
result = fuse_main(3, fuse_args, &operations, rpc);
wsfs_server_dispose(server);
2019-01-27 02:45:03 +00:00
}
else
{
fprintf(stderr, "fatal: unable start server\n");
result = EXIT_FAILURE;
}
}
else
{
show_help();
2019-01-27 02:45:03 +00:00
}
wsfs_server_config_clear(&args.config);
free(args.mount_point);
2019-01-27 02:45:03 +00:00
return result;
}