From 67ec1ad7e509c4681312377338bf63d2cf601f14 Mon Sep 17 00:00:00 2001 From: Fufu Fang Date: Sun, 22 Aug 2021 00:51:37 +0100 Subject: [PATCH] Separated out config.c and config.h --- Makefile | 3 +- src/cache.c | 1 + src/config.c | 72 ++++++++++++++++++++++++++++++++++++++++++++ src/config.h | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/link.h | 2 +- src/log.c | 22 ++++++++++++++ src/log.h | 30 +++++++++++++++++++ src/main.c | 1 + src/network.c | 1 + src/sonic.c | 1 + src/util.c | 74 ++------------------------------------------- src/util.h | 70 ------------------------------------------- 12 files changed, 217 insertions(+), 143 deletions(-) create mode 100644 src/config.c create mode 100644 src/config.h create mode 100644 src/log.c create mode 100644 src/log.h diff --git a/Makefile b/Makefile index 67934bd..4505590 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,8 @@ CFLAGS += -O2 -Wall -Wextra -Wshadow -rdynamic -D_GNU_SOURCE\ `pkg-config --cflags-only-I gumbo libcurl fuse uuid expat` LDFLAGS += `pkg-config --libs-only-L gumbo libcurl fuse uuid expat` LIBS = -pthread -lgumbo -lcurl -lfuse -lcrypto -lexpat -COBJS = main.o network.o fuse_local.o link.o cache.o util.o sonic.o +COBJS = main.o network.o fuse_local.o link.o cache.o util.o sonic.o log.o\ + config.o OS := $(shell uname) ifeq ($(OS),Darwin) diff --git a/src/cache.c b/src/cache.c index b56f7bb..799ea03 100644 --- a/src/cache.c +++ b/src/cache.c @@ -1,4 +1,5 @@ #include "cache.h" +#include "config.h" #include diff --git a/src/config.c b/src/config.c new file mode 100644 index 0000000..919748d --- /dev/null +++ b/src/config.c @@ -0,0 +1,72 @@ +#include "config.h" + +#include "log.h" +#include + +/** + * \brief The default HTTP 429 (too many requests) wait time + */ +#define DEFAULT_HTTP_WAIT_SEC 5 +/** + * \brief Data file block size + * \details We set it to 1024*1024*8 = 8MiB + */ +#define DEFAULT_DATA_BLKSZ 8*1024*1024 + +/** + * \brief Maximum segment block count + * \details This is set to 128*1024 blocks, which uses 128KB. By default, + * this allows the user to store (128*1024)*(8*1024*1024) = 1TB of data + */ +#define DEFAULT_MAX_SEGBC 128*1024 + +ConfigStruct CONFIG; + +/** + * \note The opening curly bracket should be at line 39, so the code lines up + * with the definition code in util.h. + */ +void Config_init(void) +{ + /*---------------- Network related --------------*/ + CONFIG.http_username = NULL; + + CONFIG.http_password = NULL; + + CONFIG.proxy = NULL; + + CONFIG.proxy_username = NULL; + + CONFIG.proxy_password = NULL; + + CONFIG.max_conns = DEFAULT_NETWORK_MAX_CONNS; + + CONFIG.user_agent = DEFAULT_USER_AGENT; + + CONFIG.http_wait_sec = DEFAULT_HTTP_WAIT_SEC; + + CONFIG.no_range_check = 0; + + CONFIG.insecure_tls = 0; + + /*--------------- Cache related ---------------*/ + CONFIG.cache_enabled = 0; + + CONFIG.cache_dir = NULL; + + CONFIG.data_blksz = DEFAULT_DATA_BLKSZ; + + CONFIG.max_segbc = DEFAULT_MAX_SEGBC; + + /*-------------- Sonic related -------------*/ + CONFIG.sonic_username = NULL; + + CONFIG.sonic_password = NULL; + + CONFIG.sonic_id3 = 0; + + CONFIG.sonic_insecure = 0; + + /*--------------- Log related -------------*/ + CONFIG.log_level = log_init(); +} \ No newline at end of file diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..b92a087 --- /dev/null +++ b/src/config.h @@ -0,0 +1,83 @@ +#ifndef CONFIG_H +#define CONFIG_H + +/** + * \brief the maximum length of a path and a URL. + * \details This corresponds the maximum path length under Ext4. + */ +#define MAX_PATH_LEN 4096 + +/** + * \brief the maximum length of a filename. + * \details This corresponds the filename length under Ext4. + */ +#define MAX_FILENAME_LEN 255 + +/** + * \brief the default user agent string + */ +#define DEFAULT_USER_AGENT "HTTPDirFS-" VERSION + +/** + * \brief The default maximum number of network connections + */ +#define DEFAULT_NETWORK_MAX_CONNS 10 + +/** + * \brief configuration data structure + * \note The opening curly bracket should be at line 39, so the code belong + * lines up with the initialisation code in util.c + */ +typedef struct { + /*---------------- Network related --------------*/ + /** \brief HTTP username */ + char *http_username; + /** \brief HTTP password */ + char *http_password; + /** \brief HTTP proxy URL */ + char *proxy; + /** \brief HTTP proxy username */ + char *proxy_username; + /** \brief HTTP proxy password */ + char *proxy_password; + /** \brief HTTP maximum connection count */ + long max_conns; + /** \brief HTTP user agent*/ + char *user_agent; + /** \brief The waiting time after getting HTTP 429 (too many requests) */ + int http_wait_sec; + /** \brief Disable check for the server's support of HTTP range request */ + int no_range_check; + /** \brief Disable TLS certificate verification */ + int insecure_tls; + /*--------------- Cache related ---------------*/ + /** \brief Whether cache mode is enabled */ + int cache_enabled; + /** \brief The cache location*/ + char *cache_dir; + /** \brief The size of each download segment for cache mode */ + int data_blksz; + /** \brief The maximum segment count for a single cache file */ + int max_segbc; + /*-------------- Sonic related -------------*/ + /** \brief Whether we are using the Sonic mode */ + int sonic_mode; + /** \brief The Sonic server username */ + char *sonic_username; + /** \brief The Sonic server password */ + char *sonic_password; + /** \brief Whether we are using sonic mode ID3 extension */ + int sonic_id3; + /** \brief Whether we use the legacy sonic authentication mode */ + int sonic_insecure; + /*--------------- Log related -------------*/ + /** \brief Current log level */ + int log_level; +} ConfigStruct; + +/** + * \brief The Configuration data structure + */ +extern ConfigStruct CONFIG; + +#endif \ No newline at end of file diff --git a/src/link.h b/src/link.h index b300409..570fabb 100644 --- a/src/link.h +++ b/src/link.h @@ -5,7 +5,7 @@ * \file link.h * \brief link related structures and functions */ - +#include "config.h" #include "util.h" #include diff --git a/src/log.c b/src/log.c new file mode 100644 index 0000000..0dbe254 --- /dev/null +++ b/src/log.c @@ -0,0 +1,22 @@ +#include "log.h" + +#include +#include + +int log_init() +{ + char *env = getenv("HTTPDIRFS_DEBUG_LEVEL"); + if (env) { + return atoi(env); + } + return DEFAULT_LOG_LEVEL; +} + +void LOG_PRINTF(int level, const char *file, int line, const char *format, ...) +{ + fprintf(stderr, "(%s:%d): ", file, line); + va_list args; + va_start(args, format); + vfprintf(stderr, format, args); + va_end(args); +} \ No newline at end of file diff --git a/src/log.h b/src/log.h new file mode 100644 index 0000000..085bd9c --- /dev/null +++ b/src/log.h @@ -0,0 +1,30 @@ +#ifndef LOG_H +#define LOG_H +/** + * \brief log level: notice + */ +#define LOG_LEVEL_NOTICE 1 + +/** + * \brief the default log level + */ +#define DEFAULT_LOG_LEVEL LOG_LEVEL_NOTICE + +/** + * \brief Get the log level from the environment. + */ +int log_init(); + +/** + * \brief log printf + * \details This is for printing nice log messages + */ +void LOG_PRINTF(int level, const char *file, int line, const char *format, ...); + +/** + * \brief log printf + * \details This macro automatically prints out the filename and line number + */ +#define log_printf(level, ...) \ + LOG_PRINTF (level, __FILE__, __LINE__, __VA_ARGS__); +#endif \ No newline at end of file diff --git a/src/main.c b/src/main.c index c0b9888..1754949 100644 --- a/src/main.c +++ b/src/main.c @@ -1,3 +1,4 @@ +#include "config.h" #include "cache.h" #include "fuse_local.h" #include "network.h" diff --git a/src/network.c b/src/network.c index 51762d0..947aef2 100644 --- a/src/network.c +++ b/src/network.c @@ -1,6 +1,7 @@ #include "network.h" #include "cache.h" +#include "config.h" #include diff --git a/src/sonic.c b/src/sonic.c index 4e4168b..936e8e9 100644 --- a/src/sonic.c +++ b/src/sonic.c @@ -1,5 +1,6 @@ #include "sonic.h" +#include "config.h" #include "util.h" #include "link.h" #include "network.h" diff --git a/src/util.c b/src/util.c index 2ee2046..080e150 100644 --- a/src/util.c +++ b/src/util.c @@ -1,4 +1,6 @@ +#include "config.h" #include "util.h" +#include "log.h" #include #include @@ -25,76 +27,6 @@ */ #define SALT_LEN 36 -/** - * \brief The default maximum number of network connections - */ -#define DEFAULT_NETWORK_MAX_CONNS 10 - -/** - * \brief The default HTTP 429 (too many requests) wait time - */ -#define DEFAULT_HTTP_WAIT_SEC 5 -/** - * \brief Data file block size - * \details We set it to 1024*1024*8 = 8MiB - */ -#define DEFAULT_DATA_BLKSZ 8*1024*1024 - -/** - * \brief Maximum segment block count - * \details This is set to 128*1024 blocks, which uses 128KB. By default, - * this allows the user to store (128*1024)*(8*1024*1024) = 1TB of data - */ -#define DEFAULT_MAX_SEGBC 128*1024 - -ConfigStruct CONFIG; - -/** - * \note The opening curly bracket should be at line 39, so the code lines up - * with the definition code in util.h. - */ -void Config_init(void) -{ - /*---------------- Network related --------------*/ - CONFIG.http_username = NULL; - - CONFIG.http_password = NULL; - - CONFIG.proxy = NULL; - - CONFIG.proxy_username = NULL; - - CONFIG.proxy_password = NULL; - - CONFIG.max_conns = DEFAULT_NETWORK_MAX_CONNS; - - CONFIG.user_agent = DEFAULT_USER_AGENT; - - CONFIG.http_wait_sec = DEFAULT_HTTP_WAIT_SEC; - - CONFIG.no_range_check = 0; - - CONFIG.insecure_tls = 0; - - /*--------------- Cache related ---------------*/ - CONFIG.cache_enabled = 0; - - CONFIG.cache_dir = NULL; - - CONFIG.data_blksz = DEFAULT_DATA_BLKSZ; - - CONFIG.max_segbc = DEFAULT_MAX_SEGBC; - - /*-------------- Sonic related -------------*/ - CONFIG.sonic_username = NULL; - - CONFIG.sonic_password = NULL; - - CONFIG.sonic_id3 = 0; - - CONFIG.sonic_insecure = 0; -} - char *path_append(const char *path, const char *filename) { int needs_separator = 0; @@ -213,4 +145,4 @@ char *str_to_hex(char *s) sprintf(h, "%x", *c); } return hex; -} +} \ No newline at end of file diff --git a/src/util.h b/src/util.h index 9b15cc3..6f0a530 100644 --- a/src/util.h +++ b/src/util.h @@ -9,76 +9,6 @@ #include #include -/** - * \brief the maximum length of a path and a URL. - * \details This corresponds the maximum path length under Ext4. - */ -#define MAX_PATH_LEN 4096 - -/** - * \brief the maximum length of a filename. - * \details This corresponds the filename length under Ext4. - */ -#define MAX_FILENAME_LEN 255 - -/** - * \brief the default user agent string - */ -#define DEFAULT_USER_AGENT "HTTPDirFS-" VERSION - -/** - * \brief configuration data structure - * \note The opening curly bracket should be at line 39, so the code belong - * lines up with the initialisation code in util.c - */ -typedef struct { - /** \brief HTTP username */ - char *http_username; - /** \brief HTTP password */ - char *http_password; - /** \brief HTTP proxy URL */ - char *proxy; - /** \brief HTTP proxy username */ - char *proxy_username; - /** \brief HTTP proxy password */ - char *proxy_password; - /** \brief HTTP maximum connection count */ - long max_conns; - /** \brief HTTP user agent*/ - char *user_agent; - /** \brief The waiting time after getting HTTP 429 (too many requests) */ - int http_wait_sec; - /** \brief Disable check for the server's support of HTTP range request */ - int no_range_check; - /** \brief Disable TLS certificate verification */ - int insecure_tls; - - /** \brief Whether cache mode is enabled */ - int cache_enabled; - /** \brief The cache location*/ - char *cache_dir; - /** \brief The size of each download segment for cache mode */ - int data_blksz; - /** \brief The maximum segment count for a single cache file */ - int max_segbc; - - /** \brief Whether we are using the Sonic mode */ - int sonic_mode; - /** \brief The Sonic server username */ - char *sonic_username; - /** \brief The Sonic server password */ - char *sonic_password; - /** \brief Whether we are using sonic mode ID3 extension */ - int sonic_id3; - /** \brief Whether we use the legacy sonic authentication mode */ - int sonic_insecure; -} ConfigStruct; - -/** - * \brief The Configuration data structure - */ -extern ConfigStruct CONFIG; - /** * \brief append a path * \details This function appends a path with the next level, while taking the