Split out the sonicfs feature from httpdirfs

Squashed commit of the following:

commit e96a31b1c4e62e5cde13d4150bb0fa8333542bfe
Author: Fufu Fang <fangfufu2003@gmail.com>
Date:   Sun Aug 8 14:48:00 2021 +0100
This commit is contained in:
Fufu Fang 2021-08-08 16:03:49 +01:00
parent 5f86703f17
commit 0155a6fb1f
No known key found for this signature in database
GPG Key ID: 0F6BB5EF6F8BB729
7 changed files with 212 additions and 107 deletions

View File

@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
### Changed
Now generate separate binaries
- ``HTTDirFS`` for mounting HTTP directory listings.
- ``SonicFS`` for mounting Airsonic / Subsonic server.
## [1.2.2] - 2021-08-08
### Fixed

View File

@ -23,57 +23,81 @@ endif
prefix ?= /usr/local
all: httpdirfs
all: httpdirfs sonicfs
%.o: src/%.c
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -c -o $@ $<
httpdirfs: $(COBJS)
httpdirfs: httpdirfs.o $(COBJS)
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
install:
sonicfs: sonicfs.o $(COBJS)
$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
install: all
ifeq ($(OS),Linux)
install -m 755 -D httpdirfs \
$(DESTDIR)$(prefix)/bin/httpdirfs
install -m 644 -D doc/man/httpdirfs.1 \
$(DESTDIR)$(prefix)/share/man/man1/httpdirfs.1
install -m 755 -D sonicfs \
$(DESTDIR)$(prefix)/bin/sonicfs
install -m 644 -D doc/man/sonicfs.1 \
$(DESTDIR)$(prefix)/share/man/man1/sonicfs.1
endif
ifeq ($(OS),FreeBSD)
install -m 755 httpdirfs \
install -m 755 httpdirfs\
$(DESTDIR)$(prefix)/bin/httpdirfs
gzip -f -k doc/man/httpdirfs.1
install -m 644 doc/man/httpdirfs.1.gz \
$(DESTDIR)$(prefix)/man/man1/httpdirfs.1.gz
install -m 755 sonicfs\
$(DESTDIR)$(prefix)/bin/sonicfs
gzip -f -k doc/man/sonicfs.1
install -m 644 doc/man/sonicfs.1.gz \
$(DESTDIR)$(prefix)/man/man1/sonicfs.1.gz
endif
ifeq ($(OS),Darwin)
install -d $(DESTDIR)$(prefix)/bin
install -m 755 httpdirfs \
install -m 755 httpdirfs\
$(DESTDIR)$(prefix)/bin/httpdirfs
install -m 755 sonicfs\
$(DESTDIR)$(prefix)/bin/sonicfs
install -d $(DESTDIR)$(prefix)/share/man/man1
install -m 644 doc/man/httpdirfs.1 \
$(DESTDIR)$(prefix)/share/man/man1/httpdirfs.1
install -m 644 doc/man/sonicfs.1 \
$(DESTDIR)$(prefix)/share/man/man1/sonicfs.1
endif
doc:
doxygen Doxyfile
man: all
help2man --no-discard-stderr ./httpdirfs > doc/man/httpdirfs.1
help2man --no-discard-stderr ./sonicfs > doc/man/sonicfs.1
clean:
-rm -f *.o
-rm -f httpdirfs
-rm -rf doc/html
-rm -f httpdirfs sonicfs
distclean: clean
-rm -rf doc/html
uninstall:
-rm -f $(DESTDIR)$(prefix)/bin/httpdirfs
-rm -f $(DESTDIR)$(prefix)/bin/sonicfs
ifeq ($(OS),Linux)
-rm -f $(DESTDIR)$(prefix)/share/man/man1/httpdirfs.1
-rm -f $(DESTDIR)$(prefix)/share/man/man1/sonicfs.1
endif
ifeq ($(OS),FreeBSD)
-rm -f $(DESTDIR)$(prefix)/man/man1/httpdirfs.1.gz
-rm -f $(DESTDIR)$(prefix)/man/man1/sonicfs.1.gz
endif
ifeq ($(OS),Darwin)
-rm -f $(DESTDIR)$(prefix)/share/man/man1/httpdirfs.1
-rm -f $(DESTDIR)$(prefix)/share/man/man1/sonicfs.1
endif
depend: .depend
@ -82,4 +106,4 @@ depend: .depend
$(CC) $(CFLAGS) -MM $^ -MF ./.depend;
include .depend
.PHONY: all doc install clean distclean uninstall depend
.PHONY: all doc man install clean distclean uninstall depend

6
TODO.md Normal file
View File

@ -0,0 +1,6 @@
## Todo
- [ ] Improve debug functions
- [ ] Automatic generation of man page with proper description
- [ ] Update change log
- [ ] Create a new release
- [ ] Contact maintainer for a new Debian package

6
src/httpdirfs.c Normal file
View File

@ -0,0 +1,6 @@
#include "main.h"
int main(int argc, char **argv)
{
return common_main(&argc, &argv);
}

View File

@ -1,3 +1,5 @@
#include "main.h"
#include "cache.h"
#include "fuse_local.h"
#include "network.h"
@ -6,92 +8,7 @@
#include <stdlib.h>
#include <string.h>
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_version();
static void print_long_help();
static int
parse_arg_list(int argc, char **argv, char ***fuse_argv, int *fuse_argc);
void parse_config_file(char ***argv, int *argc);
static char *config_path = NULL;
int main(int argc, char **argv)
{
/* Automatically print help if not enough arguments are supplied */
if (argc < 2) {
print_help(argv[0], 0);
fprintf(stderr, "For more information, run \"%s --help.\"\n", argv[0]);
exit(EXIT_FAILURE);
}
/* These are passed into fuse initialiser */
char **fuse_argv = NULL;
int fuse_argc = 0;
/* These are the combined argument with the config file */
char **all_argv = NULL;
int all_argc = 0;
/*--- Add the program's name to the combined argument list ---*/
add_arg(&all_argv, &all_argc, argv[0]);
/*--- FUSE expects the first initialisation to be the program's name ---*/
add_arg(&fuse_argv, &fuse_argc, argv[0]);
/* initialise network configuration struct */
Config_init();
/* initialise network subsystem */
NetworkSystem_init();
/* Copy the command line argument list to the combined argument list */
for (int i = 1; i < argc; i++) {
add_arg(&all_argv, &all_argc, argv[i]);
if (!strcmp(argv[i], "--config")) {
config_path = strdup(argv[i+1]);
}
}
/* parse the config file, if it exists, store it in all_argv and all_argc */
parse_config_file(&all_argv, &all_argc);
/* parse the combined argument list */
if (parse_arg_list(all_argc, all_argv, &fuse_argv, &fuse_argc)) {
/*
* The user basically didn't supply enough arguments, if we reach here
* The point is to print some error messages
*/
goto fuse_start;
}
/*--- Add the last remaining argument, which is the mountpoint ---*/
add_arg(&fuse_argv, &fuse_argc, argv[argc-1]);
/* 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)) {
fprintf(stderr, "Error: Please supply a valid URL.\n");
print_help(argv[0], 0);
exit(EXIT_FAILURE);
} else {
if (CONFIG.sonic_username && CONFIG.sonic_password) {
CONFIG.sonic_mode = 1;
} else if (CONFIG.sonic_username || CONFIG.sonic_password) {
fprintf(stderr,
"Error: You have to supply both username and password to \
activate Sonic mode.\n");
exit(EXIT_FAILURE);
}
if(!LinkSystem_init(base_url)) {
fprintf(stderr, "Error: Network initialisation failed.\n");
exit(EXIT_FAILURE);
}
}
fuse_start:
fuse_local_init(fuse_argc, fuse_argv);
return 0;
}
char *config_path = NULL;
void parse_config_file(char ***argv, int *argc)
{
@ -136,8 +53,7 @@ void parse_config_file(char ***argv, int *argc)
}
}
static int
parse_arg_list(int argc, char **argv, char ***fuse_argv, int *fuse_argc)
int parse_arg_list(int argc, char **argv, char ***fuse_argv, int *fuse_argc)
{
char c;
int long_index = 0;
@ -267,10 +183,6 @@ parse_arg_list(int argc, char **argv, char ***fuse_argv, int *fuse_argc)
return 0;
}
/**
* \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)++;
@ -279,7 +191,7 @@ void add_arg(char ***fuse_argv_ptr, int *fuse_argc, char *opt_string)
fuse_argv[*fuse_argc - 1] = strdup(opt_string);
}
static void print_help(char *program_name, int long_help)
void print_help(char *program_name, int long_help)
{
fprintf(stderr,
"usage: %s [options] URL mountpoint\n", program_name);
@ -288,19 +200,25 @@ static void print_help(char *program_name, int long_help)
}
}
static void print_version()
void print_version()
{
fprintf(stderr, "HTTPDirFS version " VERSION "\n");
char *fs_name;
if (!CONFIG.sonic_mode) {
fs_name = "HTTPDirFS";
} else {
fs_name = "SonicFS";
}
fprintf(stderr, "%s version " VERSION "\n", fs_name);
/* --------- Print off SSL engine version --------- */
curl_version_info_data *data = curl_version_info(CURLVERSION_NOW);
fprintf(stderr, "libcurl SSL engine: %s\n", data->ssl_version);
}
static void print_long_help()
void print_long_help()
{
fprintf(stderr,
"\n\
general options:\n\
General options:\n\
--config Specify a configuration file \n\
-o opt,[opt...] Mount options\n\
-h --help Print help\n\
@ -333,8 +251,17 @@ HTTPDirFS options:\n\
for HTTP range requests\n\
--insecure_tls Disable licurl TLS certificate verification by\n\
setting CURLOPT_SSL_VERIFYHOST to 0\n\
\n\
For mounting a Airsonic / Subsonic server:\n\
\n");
if (CONFIG.sonic_mode) {
print_sonic_help();
}
}
void print_sonic_help()
{
fprintf(stderr,
"\n\
Airsonic / Subsonic server specific options:\n\
--sonic-username The username for your Airsonic / Subsonic server\n\
--sonic-password The password for your Airsonic / Subsonic server\n\
--sonic-id3 Enable ID3 mode - this present the server content in\n\
@ -344,3 +271,83 @@ HTTPDirFS options:\n\
scheme\n\
\n");
}
int common_main(int *argc_in, char ***argv_in)
{
int argc = *argc_in;
char **argv = *argv_in;
/* Automatically print help if not enough arguments are supplied */
if (argc < 2) {
print_help(argv[0], 0);
fprintf(stderr, "For more information, run \"%s --help.\"\n", argv[0]);
exit(EXIT_FAILURE);
}
/* These are passed into fuse initialiser */
char **fuse_argv = NULL;
int fuse_argc = 0;
/* These are the combined argument with the config file */
char **all_argv = NULL;
int all_argc = 0;
/*--- Add the program's name to the combined argument list ---*/
add_arg(&all_argv, &all_argc, argv[0]);
/*--- FUSE expects the first initialisation to be the program's name ---*/
add_arg(&fuse_argv, &fuse_argc, argv[0]);
/* initialise network configuration struct */
Config_init();
/* initialise network subsystem */
NetworkSystem_init();
/* Copy the command line argument list to the combined argument list */
for (int i = 1; i < argc; i++) {
add_arg(&all_argv, &all_argc, argv[i]);
if (!strcmp(argv[i], "--config")) {
config_path = strdup(argv[i+1]);
}
}
/* parse the config file, if it exists, store it in all_argv and all_argc */
parse_config_file(&all_argv, &all_argc);
/* parse the combined argument list */
if (parse_arg_list(all_argc, all_argv, &fuse_argv, &fuse_argc)) {
/*
* The user basically didn't supply enough arguments, if we reach here
* The point is to print some error messages
*/
goto fuse_start;
}
/*--- Add the last remaining argument, which is the mountpoint ---*/
add_arg(&fuse_argv, &fuse_argc, argv[argc-1]);
/* 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)) {
fprintf(stderr, "Error: Please supply a valid URL.\n");
print_help(argv[0], 0);
exit(EXIT_FAILURE);
}
if (CONFIG.sonic_mode &&
!(CONFIG.sonic_username && CONFIG.sonic_password)) {
fprintf(stderr,
"Error: You have to supply both username and password for your\
Sonic server account.\n");
exit(EXIT_FAILURE);
}
if(!LinkSystem_init(base_url)) {
fprintf(stderr, "Error: Network initialisation failed.\n");
exit(EXIT_FAILURE);
}
fuse_start:
fuse_local_init(fuse_argc, fuse_argv);
return 0;
}

49
src/main.h Normal file
View File

@ -0,0 +1,49 @@
#ifndef MAIN_H
#define MAIN_H
/**
* \brief Configuration path
*/
extern char *config_path;
/**
* \brief Parse the configuration file
*/
void parse_config_file(char ***argv, int *argc);
/**
* \brief Parse argument list
*/
int parse_arg_list(int argc, char **argv, char ***fuse_argv, int *fuse_argc);
/**
* \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);
/**
* \brief Print short help information
*/
void print_help(char *program_name, int long_help);
/**
* \brief Print version number
*/
void print_version();
/**
* \brief Print long help information
*/
void print_long_help();
/**
* \brief Print Sonic server related help information
*/
void print_sonic_help();
/**
* @brief The old main function that is common to both HTTPDirFS and SonicFS
*/
int common_main(int *argc_in, char ***argv_in);
#endif

9
src/sonicfs.c Normal file
View File

@ -0,0 +1,9 @@
#include "main.h"
#include "util.h"
int main(int argc, char **argv)
{
CONFIG.sonic_mode = 1;
return common_main(&argc, &argv);
}