httpdirfs/main.c

161 lines
5.3 KiB
C
Raw Normal View History

#include "network.h"
2018-07-26 11:01:47 +02:00
#include "fuse_local.h"
2018-07-29 15:44:51 +02:00
#include <getopt.h>
#include <string.h>
2018-07-30 15:20:04 +02:00
#define ARG_LEN_MAX 64
2018-07-29 15:44:51 +02:00
2018-07-30 15:20:04 +02:00
void add_arg(char ***fuse_argv_ptr, int *fuse_argc, char *opt_string);
static void print_help(char *program_name, int long_help);
static void print_http_options();
2018-07-25 01:54:30 +02:00
int main(int argc, char **argv)
{
2018-07-29 18:05:49 +02:00
char **fuse_argv = NULL;
int fuse_argc = 0;
2018-07-29 18:05:49 +02:00
/* Add the program's name to the fuse argument */
add_arg(&fuse_argv, &fuse_argc, argv[0]);
/* Automatically print help if not enough arguments are supplied */
if (argc < 2) {
2018-07-30 15:20:04 +02:00
print_help(argv[0], 0);
fprintf(stderr, "For more information, run \"%s --help.\"\n", argv[0]);
exit(EXIT_FAILURE);
2018-07-29 18:05:49 +02:00
}
2018-07-29 15:44:51 +02:00
2018-07-30 15:20:04 +02:00
/* initialise network configuration struct */
network_config_init();
2018-07-29 15:44:51 +02:00
char c;
2018-07-30 15:55:38 +02:00
int long_index = 0;
const char *short_opts = "o:hVdfsp:u:P:";
2018-07-29 15:44:51 +02:00
const struct option long_opts[] = {
2018-07-30 15:55:38 +02:00
{"help", no_argument, NULL, 'h'}, /* 0 */
{"version", no_argument, NULL, 'V'}, /* 1 */
{"debug", no_argument, NULL, 'd'}, /* 2 */
{"user", required_argument, NULL, 'u'}, /* 3 */
{"password", required_argument, NULL, 'p'}, /* 4 */
{"proxy", required_argument, NULL, 'P'}, /* 5 */
{"proxy-user", required_argument, NULL, 'L'}, /* 6 */
{"proxy-pass", required_argument, NULL, 'L'}, /* 7 */
2018-07-29 15:44:51 +02:00
{0, 0, 0, 0}
};
while ((c =
getopt_long(argc, argv, short_opts, long_opts,
2018-07-30 15:55:38 +02:00
&long_index)) != -1) {
2018-07-29 15:44:51 +02:00
switch (c) {
case 'o':
2018-07-29 18:05:49 +02:00
add_arg(&fuse_argv, &fuse_argc, "-o");
add_arg(&fuse_argv, &fuse_argc, optarg);
2018-07-29 15:44:51 +02:00
break;
case 'h':
2018-07-30 15:20:04 +02:00
print_help(argv[0], 1);
2018-07-29 18:05:49 +02:00
add_arg(&fuse_argv, &fuse_argc, "-h");
2018-07-29 15:44:51 +02:00
goto fuse_start;
break;
case 'V':
2018-07-29 18:05:49 +02:00
add_arg(&fuse_argv, &fuse_argc, "-V");
2018-07-29 15:44:51 +02:00
break;
case 'd':
2018-07-29 18:05:49 +02:00
add_arg(&fuse_argv, &fuse_argc, "-d");
2018-07-29 15:44:51 +02:00
break;
case 'f':
2018-07-29 18:05:49 +02:00
add_arg(&fuse_argv, &fuse_argc, "-f");
2018-07-29 15:44:51 +02:00
break;
case 's':
2018-07-29 18:05:49 +02:00
add_arg(&fuse_argv, &fuse_argc, "-s");
2018-07-29 15:44:51 +02:00
break;
2018-07-30 15:20:04 +02:00
case 'p':
NETWORK_CONFIG.username = strndup(optarg, ARG_LEN_MAX);
break;
case 'u':
NETWORK_CONFIG.password = strndup(optarg, ARG_LEN_MAX);
break;
2018-07-30 15:55:38 +02:00
case 'P':
NETWORK_CONFIG.proxy = strndup(optarg, URL_LEN_MAX);
break;
case 'L':
switch (long_index) {
case 6:
NETWORK_CONFIG.proxy_user = strndup(optarg,
ARG_LEN_MAX);
printf("proxy_user: %s\n", optarg);
break;
case 7:
NETWORK_CONFIG.proxy_pass = strndup(optarg,
ARG_LEN_MAX);
printf("proxy_pass: %s\n", optarg);
break;
default:
fprintf(stderr, "Error: Invalid option\n");
add_arg(&fuse_argv, &fuse_argc, "--help");
goto fuse_start;
break;
}
break;
default:
2018-07-30 15:20:04 +02:00
fprintf(stderr, "Error: Invalid option\n");
add_arg(&fuse_argv, &fuse_argc, "--help");
goto fuse_start;
2018-07-30 15:55:38 +02:00
break;
2018-07-29 15:44:51 +02:00
}
};
2018-07-30 15:20:04 +02:00
/* Add the last remaining argument, which is the mountpoint */
2018-07-29 18:05:49 +02:00
add_arg(&fuse_argv, &fuse_argc, argv[argc-1]);
2018-07-29 15:44:51 +02:00
/* The second last remaining argument is the URL */
char *base_url = argv[argc-2];
if (strncmp(base_url, "http://", 7) && strncmp(base_url, "https://", 8)) {
2018-07-29 18:05:49 +02:00
fprintf(stderr, "Error: Please supply a valid URL.\n");
2018-07-30 15:20:04 +02:00
print_help(argv[0], 0);
2018-07-29 15:44:51 +02:00
exit(EXIT_FAILURE);
2018-07-29 18:05:49 +02:00
} else {
2018-07-30 15:20:04 +02:00
if(!network_init(base_url)) {
fprintf(stderr, "Error: Network initialisation failed.\n");
exit(EXIT_FAILURE);
}
2018-07-29 15:44:51 +02:00
}
2018-07-25 01:54:30 +02:00
2018-07-29 15:44:51 +02:00
fuse_start:
2018-07-29 18:05:49 +02:00
fuse_local_init(fuse_argc, fuse_argv);
2018-07-25 01:54:30 +02:00
return 0;
}
2018-07-30 15:20:04 +02:00
/**
* \brief add an argument to an argv array
* \details This is basically how you add a string to an array of string
*/
void add_arg(char ***fuse_argv_ptr, int *fuse_argc, char *opt_string)
{
(*fuse_argc)++;
*fuse_argv_ptr = realloc(*fuse_argv_ptr, *fuse_argc * sizeof(char *));
char **fuse_argv = *fuse_argv_ptr;
fuse_argv[*fuse_argc - 1] = strndup(opt_string, ARG_LEN_MAX);
}
static void print_help(char *program_name, int long_help)
{
2018-07-26 11:01:47 +02:00
fprintf(stderr,
2018-07-30 15:20:04 +02:00
"Usage: %s [options] URL mountpoint\n", program_name);
if (long_help) {
print_http_options();
}
2018-07-18 17:26:26 +02:00
}
2018-07-26 11:01:47 +02:00
2018-07-30 15:20:04 +02:00
static void print_http_options()
{
fprintf(stderr,
"HTTP options:\n\
2018-07-30 15:55:38 +02:00
-u --user HTTP authentication username\n\
-p --password HTTP authentication password\n\
-P --proxy Proxy for libcurl, for details refer to\n\
https://curl.haxx.se/libcurl/c/CURLOPT_PROXY.html\n\
--proxy-user Username for the proxy\n\
--proxy-pass Password for the proxy\n\
\n\
2018-07-30 15:20:04 +02:00
libfuse options:\n");
}