Separated out config.c and config.h

This commit is contained in:
Fufu Fang 2021-08-22 00:51:37 +01:00
parent 89df992053
commit 67ec1ad7e5
No known key found for this signature in database
GPG Key ID: 0F6BB5EF6F8BB729
12 changed files with 217 additions and 143 deletions

View File

@ -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)

View File

@ -1,4 +1,5 @@
#include "cache.h"
#include "config.h"
#include <sys/stat.h>

72
src/config.c Normal file
View File

@ -0,0 +1,72 @@
#include "config.h"
#include "log.h"
#include <stddef.h>
/**
* \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();
}

83
src/config.h Normal file
View File

@ -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

View File

@ -5,7 +5,7 @@
* \file link.h
* \brief link related structures and functions
*/
#include "config.h"
#include "util.h"
#include <curl/curl.h>

22
src/log.c Normal file
View File

@ -0,0 +1,22 @@
#include "log.h"
#include <stdarg.h>
#include <stdio.h>
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);
}

30
src/log.h Normal file
View File

@ -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

View File

@ -1,3 +1,4 @@
#include "config.h"
#include "cache.h"
#include "fuse_local.h"
#include "network.h"

View File

@ -1,6 +1,7 @@
#include "network.h"
#include "cache.h"
#include "config.h"
#include <openssl/crypto.h>

View File

@ -1,5 +1,6 @@
#include "sonic.h"
#include "config.h"
#include "util.h"
#include "link.h"
#include "network.h"

View File

@ -1,4 +1,6 @@
#include "config.h"
#include "util.h"
#include "log.h"
#include <openssl/md5.h>
#include <uuid/uuid.h>
@ -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;
}
}

View File

@ -9,76 +9,6 @@
#include <stdint.h>
#include <stdio.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 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