style: format code with ClangFormat

This commit fixes the style issues introduced in efa7376 according to the output
from ClangFormat.

Details: None
This commit is contained in:
deepsource-autofix[bot] 2024-02-01 02:26:24 +00:00 committed by GitHub
parent efa7376ca0
commit 7336aff6ae
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
17 changed files with 3009 additions and 3226 deletions

File diff suppressed because it is too large Load Diff

View File

@ -14,9 +14,9 @@ typedef struct Cache Cache;
#include "link.h" #include "link.h"
#include "network.h" #include "network.h"
#include <stdio.h>
#include <stdint.h>
#include <pthread.h> #include <pthread.h>
#include <stdint.h>
#include <stdio.h>
/** /**
* \brief Type definition for a cache segment * \brief Type definition for a cache segment
@ -27,48 +27,48 @@ typedef uint8_t Seg;
* \brief cache data type in-memory data structure * \brief cache data type in-memory data structure
*/ */
struct Cache { struct Cache {
/** \brief How many times the cache has been opened */ /** \brief How many times the cache has been opened */
int cache_opened; int cache_opened;
/** \brief the FILE pointer for the data file*/ /** \brief the FILE pointer for the data file*/
FILE *dfp; FILE *dfp;
/** \brief the FILE pointer for the metadata */ /** \brief the FILE pointer for the metadata */
FILE *mfp; FILE *mfp;
/** \brief the path to the local cache file */ /** \brief the path to the local cache file */
char *path; char *path;
/** \brief the Link associated with this cache data set */ /** \brief the Link associated with this cache data set */
Link *link; Link *link;
/** \brief the modified time of the file */ /** \brief the modified time of the file */
long time; long time;
/** \brief the size of the file */ /** \brief the size of the file */
off_t content_length; off_t content_length;
/** \brief the block size of the data file */ /** \brief the block size of the data file */
int blksz; int blksz;
/** \brief segment array byte count */ /** \brief segment array byte count */
long segbc; long segbc;
/** \brief the detail of each segment */ /** \brief the detail of each segment */
Seg *seg; Seg *seg;
/** \brief mutex lock for seek operation */ /** \brief mutex lock for seek operation */
pthread_mutex_t seek_lock; pthread_mutex_t seek_lock;
/** \brief mutex lock for write operation */ /** \brief mutex lock for write operation */
pthread_mutex_t w_lock; pthread_mutex_t w_lock;
/** \brief background download pthread */ /** \brief background download pthread */
pthread_t bgt; pthread_t bgt;
/** /**
* \brief mutex lock for the background download thread * \brief mutex lock for the background download thread
* \note This lock is locked by the foreground thread, but unlocked by the * \note This lock is locked by the foreground thread, but unlocked by the
* background thread! * background thread!
*/ */
pthread_mutex_t bgt_lock; pthread_mutex_t bgt_lock;
/** \brief mutex attributes for bgt_lock */ /** \brief mutex attributes for bgt_lock */
pthread_mutexattr_t bgt_lock_attr; pthread_mutexattr_t bgt_lock_attr;
/** \brief the offset of the next segment to be downloaded in background*/ /** \brief the offset of the next segment to be downloaded in background*/
off_t next_dl_offset; off_t next_dl_offset;
/** \brief the FUSE filesystem path to the remote file*/ /** \brief the FUSE filesystem path to the remote file*/
char *fs_path; char *fs_path;
}; };
/** /**

View File

@ -6,19 +6,19 @@
/** /**
* \brief The default HTTP 429 (too many requests) wait time * \brief The default HTTP 429 (too many requests) wait time
*/ */
#define DEFAULT_HTTP_WAIT_SEC 5 #define DEFAULT_HTTP_WAIT_SEC 5
/** /**
* \brief Data file block size * \brief Data file block size
* \details We set it to 1024*1024*8 = 8MiB * \details We set it to 1024*1024*8 = 8MiB
*/ */
#define DEFAULT_DATA_BLKSZ 8*1024*1024 #define DEFAULT_DATA_BLKSZ 8 * 1024 * 1024
/** /**
* \brief Maximum segment block count * \brief Maximum segment block count
* \details This is set to 128*1024 blocks, which uses 128KB. By default, * \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 * this allows the user to store (128*1024)*(8*1024*1024) = 1TB of data
*/ */
#define DEFAULT_MAX_SEGBC 128*1024 #define DEFAULT_MAX_SEGBC 128 * 1024
ConfigStruct CONFIG; ConfigStruct CONFIG;
@ -26,50 +26,49 @@ ConfigStruct CONFIG;
* \note The opening curly bracket should be at line 39, so the code lines up * \note The opening curly bracket should be at line 39, so the code lines up
* with the definition code in util.h. * with the definition code in util.h.
*/ */
void Config_init(void) void Config_init(void) {
{ CONFIG.mode = NORMAL;
CONFIG.mode = NORMAL;
CONFIG.log_type = log_level_init(); CONFIG.log_type = log_level_init();
/*---------------- Network related --------------*/ /*---------------- Network related --------------*/
CONFIG.http_username = NULL; CONFIG.http_username = NULL;
CONFIG.http_password = NULL; CONFIG.http_password = NULL;
CONFIG.proxy = NULL; CONFIG.proxy = NULL;
CONFIG.proxy_username = NULL; CONFIG.proxy_username = NULL;
CONFIG.proxy_password = NULL; CONFIG.proxy_password = NULL;
CONFIG.max_conns = DEFAULT_NETWORK_MAX_CONNS; CONFIG.max_conns = DEFAULT_NETWORK_MAX_CONNS;
CONFIG.user_agent = DEFAULT_USER_AGENT; CONFIG.user_agent = DEFAULT_USER_AGENT;
CONFIG.http_wait_sec = DEFAULT_HTTP_WAIT_SEC; CONFIG.http_wait_sec = DEFAULT_HTTP_WAIT_SEC;
CONFIG.no_range_check = 0; CONFIG.no_range_check = 0;
CONFIG.insecure_tls = 0; CONFIG.insecure_tls = 0;
CONFIG.refresh_timeout = DEFAULT_REFRESH_TIMEOUT; CONFIG.refresh_timeout = DEFAULT_REFRESH_TIMEOUT;
/*--------------- Cache related ---------------*/ /*--------------- Cache related ---------------*/
CONFIG.cache_enabled = 0; CONFIG.cache_enabled = 0;
CONFIG.cache_dir = NULL; CONFIG.cache_dir = NULL;
CONFIG.data_blksz = DEFAULT_DATA_BLKSZ; CONFIG.data_blksz = DEFAULT_DATA_BLKSZ;
CONFIG.max_segbc = DEFAULT_MAX_SEGBC; CONFIG.max_segbc = DEFAULT_MAX_SEGBC;
/*-------------- Sonic related -------------*/ /*-------------- Sonic related -------------*/
CONFIG.sonic_username = NULL; CONFIG.sonic_username = NULL;
CONFIG.sonic_password = NULL; CONFIG.sonic_password = NULL;
CONFIG.sonic_id3 = 0; CONFIG.sonic_id3 = 0;
CONFIG.sonic_insecure = 0; CONFIG.sonic_insecure = 0;
} }

View File

@ -5,13 +5,13 @@
* \brief the maximum length of a path and a URL. * \brief the maximum length of a path and a URL.
* \details This corresponds the maximum path length under Ext4. * \details This corresponds the maximum path length under Ext4.
*/ */
#define MAX_PATH_LEN 4096 #define MAX_PATH_LEN 4096
/** /**
* \brief the maximum length of a filename. * \brief the maximum length of a filename.
* \details This corresponds the filename length under Ext4. * \details This corresponds the filename length under Ext4.
*/ */
#define MAX_FILENAME_LEN 255 #define MAX_FILENAME_LEN 255
/** /**
* \brief the default user agent string * \brief the default user agent string
@ -21,20 +21,20 @@
/** /**
* \brief The default maximum number of network connections * \brief The default maximum number of network connections
*/ */
#define DEFAULT_NETWORK_MAX_CONNS 10 #define DEFAULT_NETWORK_MAX_CONNS 10
/** /**
* \brief The default refresh_timeout * \brief The default refresh_timeout
*/ */
#define DEFAULT_REFRESH_TIMEOUT 86400 #define DEFAULT_REFRESH_TIMEOUT 86400
/** /**
* \brief Operation modes * \brief Operation modes
*/ */
typedef enum { typedef enum {
NORMAL = 1, NORMAL = 1,
SONIC = 2, SONIC = 2,
SINGLE = 3, SINGLE = 3,
} OperationMode; } OperationMode;
/** /**
@ -43,55 +43,55 @@ typedef enum {
* lines up with the initialisation code in util.c * lines up with the initialisation code in util.c
*/ */
typedef struct { typedef struct {
/** \brief Operation Mode */ /** \brief Operation Mode */
OperationMode mode; OperationMode mode;
/** \brief Current log level */ /** \brief Current log level */
int log_type; int log_type;
/*---------------- Network related --------------*/ /*---------------- Network related --------------*/
/** \brief HTTP username */ /** \brief HTTP username */
char *http_username; char *http_username;
/** \brief HTTP password */ /** \brief HTTP password */
char *http_password; char *http_password;
/** \brief HTTP proxy URL */ /** \brief HTTP proxy URL */
char *proxy; char *proxy;
/** \brief HTTP proxy username */ /** \brief HTTP proxy username */
char *proxy_username; char *proxy_username;
/** \brief HTTP proxy password */ /** \brief HTTP proxy password */
char *proxy_password; char *proxy_password;
/** \brief HTTP proxy certificate file */ /** \brief HTTP proxy certificate file */
char *proxy_cafile; char *proxy_cafile;
/** \brief HTTP maximum connection count */ /** \brief HTTP maximum connection count */
long max_conns; long max_conns;
/** \brief HTTP user agent*/ /** \brief HTTP user agent*/
char *user_agent; char *user_agent;
/** \brief The waiting time after getting HTTP 429 (too many requests) */ /** \brief The waiting time after getting HTTP 429 (too many requests) */
int http_wait_sec; int http_wait_sec;
/** \brief Disable check for the server's support of HTTP range request */ /** \brief Disable check for the server's support of HTTP range request */
int no_range_check; int no_range_check;
/** \brief Disable TLS certificate verification */ /** \brief Disable TLS certificate verification */
int insecure_tls; int insecure_tls;
/** \brief Server certificate file */ /** \brief Server certificate file */
char *cafile; char *cafile;
/** \brief Refresh directory listing after refresh_timeout seconds*/ /** \brief Refresh directory listing after refresh_timeout seconds*/
int refresh_timeout; int refresh_timeout;
/*--------------- Cache related ---------------*/ /*--------------- Cache related ---------------*/
/** \brief Whether cache mode is enabled */ /** \brief Whether cache mode is enabled */
int cache_enabled; int cache_enabled;
/** \brief The cache location*/ /** \brief The cache location*/
char *cache_dir; char *cache_dir;
/** \brief The size of each download segment for cache mode */ /** \brief The size of each download segment for cache mode */
int data_blksz; int data_blksz;
/** \brief The maximum segment count for a single cache file */ /** \brief The maximum segment count for a single cache file */
int max_segbc; int max_segbc;
/*-------------- Sonic related -------------*/ /*-------------- Sonic related -------------*/
/** \brief The Sonic server username */ /** \brief The Sonic server username */
char *sonic_username; char *sonic_username;
/** \brief The Sonic server password */ /** \brief The Sonic server password */
char *sonic_password; char *sonic_password;
/** \brief Whether we are using sonic mode ID3 extension */ /** \brief Whether we are using sonic mode ID3 extension */
int sonic_id3; int sonic_id3;
/** \brief Whether we use the legacy sonic authentication mode */ /** \brief Whether we use the legacy sonic authentication mode */
int sonic_insecure; int sonic_insecure;
} ConfigStruct; } ConfigStruct;
/** /**

View File

@ -13,110 +13,104 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
static void *fs_init(struct fuse_conn_info *conn) static void *fs_init(struct fuse_conn_info *conn) {
{ (void)conn;
(void) conn; return NULL;
return NULL;
} }
/** \brief release an opened file */ /** \brief release an opened file */
static int fs_release(const char *path, struct fuse_file_info *fi) static int fs_release(const char *path, struct fuse_file_info *fi) {
{ lprintf(info, "%s\n", path);
lprintf(info, "%s\n", path); (void)path;
(void) path; if (CACHE_SYSTEM_INIT) {
if (CACHE_SYSTEM_INIT) { Cache_close((Cache *)fi->fh);
Cache_close((Cache *) fi->fh); }
} return 0;
return 0;
} }
/** \brief return the attributes for a single file indicated by path */ /** \brief return the attributes for a single file indicated by path */
static int fs_getattr(const char *path, struct stat *stbuf) static int fs_getattr(const char *path, struct stat *stbuf) {
{ int res = 0;
int res = 0; memset(stbuf, 0, sizeof(struct stat));
memset(stbuf, 0, sizeof(struct stat));
if (!strcmp(path, "/")) { if (!strcmp(path, "/")) {
stbuf->st_mode = S_IFDIR | 0755; stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 1; stbuf->st_nlink = 1;
} else { } else {
Link *link = path_to_Link(path); Link *link = path_to_Link(path);
if (!link) { if (!link) {
return -ENOENT; return -ENOENT;
}
struct timespec spec = { 0 };
spec.tv_sec = link->time;
#if defined(__APPLE__) && defined(__MACH__)
stbuf->st_mtimespec = spec;
#else
stbuf->st_mtim = spec;
#endif
switch (link->type) {
case LINK_DIR:
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 1;
break;
case LINK_FILE:
stbuf->st_mode = S_IFREG | 0444;
stbuf->st_nlink = 1;
stbuf->st_size = link->content_length;
stbuf->st_blksize = 128 * 1024;
stbuf->st_blocks = (link->content_length) / 512;
break;
default:
return -ENOENT;
}
} }
stbuf->st_uid = getuid(); struct timespec spec = {0};
stbuf->st_gid = getgid(); spec.tv_sec = link->time;
#if defined(__APPLE__) && defined(__MACH__)
stbuf->st_mtimespec = spec;
#else
stbuf->st_mtim = spec;
#endif
switch (link->type) {
case LINK_DIR:
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 1;
break;
case LINK_FILE:
stbuf->st_mode = S_IFREG | 0444;
stbuf->st_nlink = 1;
stbuf->st_size = link->content_length;
stbuf->st_blksize = 128 * 1024;
stbuf->st_blocks = (link->content_length) / 512;
break;
default:
return -ENOENT;
}
}
stbuf->st_uid = getuid();
stbuf->st_gid = getgid();
return res; return res;
} }
/** \brief read a file */ /** \brief read a file */
static int static int fs_read(const char *path, char *buf, size_t size, off_t offset,
fs_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
struct fuse_file_info *fi) long received;
{ if (CACHE_SYSTEM_INIT) {
long received; received = Cache_read((Cache *)fi->fh, buf, size, offset);
if (CACHE_SYSTEM_INIT) { } else {
received = Cache_read((Cache *) fi->fh, buf, size, offset); received = path_download(path, buf, size, offset);
} else { }
received = path_download(path, buf, size, offset); return received;
}
return received;
} }
/** \brief open a file indicated by the path */ /** \brief open a file indicated by the path */
static int fs_open(const char *path, struct fuse_file_info *fi) static int fs_open(const char *path, struct fuse_file_info *fi) {
{ lprintf(info, "%s\n", path);
lprintf(info, "%s\n", path); Link *link = path_to_Link(path);
Link *link = path_to_Link(path); if (!link) {
if (!link) { return -ENOENT;
}
if ((fi->flags & O_RDWR) != O_RDONLY) {
return -EROFS;
}
if (CACHE_SYSTEM_INIT) {
fi->fh = (uint64_t)Cache_open(path);
if (!fi->fh) {
/*
* The link clearly exists, the cache cannot be opened, attempt
* cache creation
*/
Cache_delete(path);
Cache_create(path);
fi->fh = (uint64_t)Cache_open(path);
/*
* The cache definitely cannot be opened for some reason.
*/
if (!fi->fh) {
return -ENOENT; return -ENOENT;
}
} }
if ((fi->flags & O_RDWR) != O_RDONLY) { }
return -EROFS; return 0;
}
if (CACHE_SYSTEM_INIT) {
fi->fh = (uint64_t) Cache_open(path);
if (!fi->fh) {
/*
* The link clearly exists, the cache cannot be opened, attempt
* cache creation
*/
Cache_delete(path);
Cache_create(path);
fi->fh = (uint64_t) Cache_open(path);
/*
* The cache definitely cannot be opened for some reason.
*/
if (!fi->fh) {
return -ENOENT;
}
}
}
return 0;
} }
/** /**
@ -130,46 +124,40 @@ static int fs_open(const char *path, struct fuse_file_info *fi)
* generate the LinkTables for previous level directories. We might * generate the LinkTables for previous level directories. We might
* as well maintain our own tree structure. * as well maintain our own tree structure.
*/ */
static int static int fs_readdir(const char *path, void *buf, fuse_fill_dir_t dir_add,
fs_readdir(const char *path, void *buf, fuse_fill_dir_t dir_add, off_t offset, struct fuse_file_info *fi) {
off_t offset, struct fuse_file_info *fi) (void)offset;
{ (void)fi;
(void) offset; LinkTable *linktbl;
(void) fi;
LinkTable *linktbl;
linktbl = path_to_Link_LinkTable_new(path); linktbl = path_to_Link_LinkTable_new(path);
if (!linktbl) { if (!linktbl) {
return -ENOENT; return -ENOENT;
}
/*
* start adding the links
*/
dir_add(buf, ".", NULL, 0);
dir_add(buf, "..", NULL, 0);
/* We skip the head link */
for (int i = 1; i < linktbl->num; i++) {
Link *link = linktbl->links[i];
if (link->type != LINK_INVALID) {
dir_add(buf, link->linkname, NULL, 0);
} }
}
return 0;
/*
* start adding the links
*/
dir_add(buf, ".", NULL, 0);
dir_add(buf, "..", NULL, 0);
/* We skip the head link */
for (int i = 1; i < linktbl->num; i++) {
Link *link = linktbl->links[i];
if (link->type != LINK_INVALID) {
dir_add(buf, link->linkname, NULL, 0);
}
}
return 0;
} }
static struct fuse_operations fs_oper = { static struct fuse_operations fs_oper = {.getattr = fs_getattr,
.getattr = fs_getattr, .readdir = fs_readdir,
.readdir = fs_readdir, .open = fs_open,
.open = fs_open, .read = fs_read,
.read = fs_read, .init = fs_init,
.init = fs_init, .release = fs_release};
.release = fs_release
};
int fuse_local_init(int argc, char **argv) int fuse_local_init(int argc, char **argv) {
{ return fuse_main(argc, argv, &fs_oper, NULL);
return fuse_main(argc, argv, &fs_oper, NULL);
} }

1902
src/link.c

File diff suppressed because it is too large Load Diff

View File

@ -20,11 +20,11 @@ typedef struct LinkTable LinkTable;
* \brief the link type * \brief the link type
*/ */
typedef enum { typedef enum {
LINK_HEAD = 'H', LINK_HEAD = 'H',
LINK_DIR = 'D', LINK_DIR = 'D',
LINK_FILE = 'F', LINK_FILE = 'F',
LINK_INVALID = 'I', LINK_INVALID = 'I',
LINK_UNINITIALISED_FILE = 'U' LINK_UNINITIALISED_FILE = 'U'
} LinkType; } LinkType;
/** /**
@ -32,32 +32,32 @@ typedef enum {
* \details index 0 contains the Link for the base URL * \details index 0 contains the Link for the base URL
*/ */
struct LinkTable { struct LinkTable {
int num; int num;
time_t index_time; time_t index_time;
Link **links; Link **links;
}; };
/** /**
* \brief Link type data structure * \brief Link type data structure
*/ */
struct Link { struct Link {
/** \brief The link name in the last level of the URL */ /** \brief The link name in the last level of the URL */
char linkname[MAX_FILENAME_LEN + 1]; char linkname[MAX_FILENAME_LEN + 1];
char linkpath[MAX_FILENAME_LEN + 1]; char linkpath[MAX_FILENAME_LEN + 1];
/** \brief The full URL of the file */ /** \brief The full URL of the file */
char f_url[MAX_PATH_LEN + 1]; char f_url[MAX_PATH_LEN + 1];
/** \brief The type of the link */ /** \brief The type of the link */
LinkType type; LinkType type;
/** \brief CURLINFO_CONTENT_LENGTH_DOWNLOAD of the file */ /** \brief CURLINFO_CONTENT_LENGTH_DOWNLOAD of the file */
size_t content_length; size_t content_length;
/** \brief The next LinkTable level, if it is a LINK_DIR */ /** \brief The next LinkTable level, if it is a LINK_DIR */
LinkTable *next_table; LinkTable *next_table;
/** \brief CURLINFO_FILETIME obtained from the server */ /** \brief CURLINFO_FILETIME obtained from the server */
long time; long time;
/** \brief The pointer associated with the cache file */ /** \brief The pointer associated with the cache file */
Cache *cache_ptr; Cache *cache_ptr;
/** \brief Stores *sonic related data */ /** \brief Stores *sonic related data */
Sonic sonic; Sonic sonic;
}; };
/** /**
@ -96,8 +96,7 @@ long path_download(const char *path, char *output_buf, size_t size,
* \brief Download a Link * \brief Download a Link
* \return the number of bytes downloaded * \return the number of bytes downloaded
*/ */
long Link_download(Link *link, char *output_buf, size_t req_size, long Link_download(Link *link, char *output_buf, size_t req_size, off_t offset);
off_t offset);
/** /**
* \brief find the link associated with a path * \brief find the link associated with a path

View File

@ -9,60 +9,55 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
int log_level_init() int log_level_init() {
{ char *env = getenv("HTTPDIRFS_LOG_LEVEL");
char *env = getenv("HTTPDIRFS_LOG_LEVEL"); if (env) {
if (env) { return atoi(env);
return atoi(env); }
return DEFAULT_LOG_LEVEL;
}
void log_printf(LogType type, const char *file, const char *func, int line,
const char *format, ...) {
if (type & CONFIG.log_type) {
switch (type) {
case fatal:
fprintf(stderr, "Fatal:");
break;
case error:
fprintf(stderr, "Error:");
break;
case warning:
fprintf(stderr, "Warning:");
break;
case info:
goto print_actual_message;
default:
fprintf(stderr, "Debug(%x):", type);
break;
} }
return DEFAULT_LOG_LEVEL;
}
void fprintf(stderr, "%s:%d:", file, line);
log_printf(LogType type, const char *file, const char *func, int line,
const char *format, ...)
{
if (type & CONFIG.log_type) {
switch (type) {
case fatal:
fprintf(stderr, "Fatal:");
break;
case error:
fprintf(stderr, "Error:");
break;
case warning:
fprintf(stderr, "Warning:");
break;
case info:
goto print_actual_message;
default:
fprintf(stderr, "Debug(%x):", type);
break;
}
fprintf(stderr, "%s:%d:", file, line); print_actual_message: {}
fprintf(stderr, "%s: ", func);
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
print_actual_message: { if (type == fatal) {
} exit_failure();
fprintf(stderr, "%s: ", func);
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
if (type == fatal) {
exit_failure();
}
} }
}
} }
void print_version() void print_version() {
{ /* FUSE prints its help to stderr */
/* FUSE prints its help to stderr */ fprintf(stderr, "HTTPDirFS version " VERSION "\n");
fprintf(stderr, "HTTPDirFS version " VERSION "\n"); /*
/* * --------- Print off SSL engine version ---------
* --------- Print off SSL engine version --------- */
*/ curl_version_info_data *data = curl_version_info(CURLVERSION_NOW);
curl_version_info_data *data = curl_version_info(CURLVERSION_NOW); fprintf(stderr, "libcurl SSL engine: %s\n", data->ssl_version);
fprintf(stderr, "libcurl SSL engine: %s\n", data->ssl_version);
} }

View File

@ -5,16 +5,16 @@
* \brief Log types * \brief Log types
*/ */
typedef enum { typedef enum {
fatal = 1 << 0, fatal = 1 << 0,
error = 1 << 1, error = 1 << 1,
warning = 1 << 2, warning = 1 << 2,
info = 1 << 3, info = 1 << 3,
debug = 1 << 4, debug = 1 << 4,
link_lock_debug = 1 << 5, link_lock_debug = 1 << 5,
network_lock_debug = 1 << 6, network_lock_debug = 1 << 6,
cache_lock_debug = 1 << 7, cache_lock_debug = 1 << 7,
memcache_debug = 1 << 8, memcache_debug = 1 << 8,
libcurl_debug = 1 << 9, libcurl_debug = 1 << 9,
} LogType; } LogType;
/** /**
@ -38,8 +38,8 @@ void log_printf(LogType type, const char *file, const char *func, int line,
* \brief Log type printf * \brief Log type printf
* \details This macro automatically prints out the filename and line number * \details This macro automatically prints out the filename and line number
*/ */
#define lprintf(type, ...) \ #define lprintf(type, ...) \
log_printf(type, __FILE__, __func__, __LINE__, __VA_ARGS__); log_printf(type, __FILE__, __func__, __LINE__, __VA_ARGS__);
#endif #endif
/** /**

View File

@ -10,343 +10,330 @@
void add_arg(char ***fuse_argv_ptr, int *fuse_argc, char *opt_string); 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_help(char *program_name, int long_help);
static void print_long_help(); static void print_long_help();
static int static int parse_arg_list(int argc, char **argv, char ***fuse_argv,
parse_arg_list(int argc, char **argv, char ***fuse_argv, int *fuse_argc); int *fuse_argc);
void parse_config_file(char ***argv, int *argc); void parse_config_file(char ***argv, int *argc);
static char *config_path = NULL; static char *config_path = NULL;
int main(int argc, char **argv) int main(int argc, char **argv) {
{ /*
/* * Automatically print help if not enough arguments are supplied
* Automatically print help if not enough arguments are supplied */
*/ if (argc < 2) {
if (argc < 2) { print_help(argv[0], 0);
print_help(argv[0], 0); fprintf(stderr, "For more information, run \"%s --help.\"\n", argv[0]);
fprintf(stderr, "For more information, run \"%s --help.\"\n", exit(EXIT_FAILURE);
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)) {
/* /*
* These are passed into fuse initialiser * The user basically didn't supply enough arguments, if we reach here
* The point is to print some error messages
*/ */
char **fuse_argv = NULL; goto fuse_start;
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 the last remaining argument, which is the mountpoint ---*/
add_arg(&all_argv, &all_argc, argv[0]); add_arg(&fuse_argv, &fuse_argc, argv[argc - 1]);
/*--- FUSE expects the first initialisation to be the program's name ---*/
add_arg(&fuse_argv, &fuse_argc, argv[0]);
/* /*
* initialise network configuration struct * The second last remaining argument is the URL
*/ */
Config_init(); 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");
* initialise network subsystem print_help(argv[0], 0);
*/ exit(EXIT_FAILURE);
NetworkSystem_init(); } else {
if (CONFIG.sonic_username && CONFIG.sonic_password) {
/* CONFIG.mode = SONIC;
* Copy the command line argument list to the combined argument list } else if (CONFIG.sonic_username || CONFIG.sonic_password) {
*/ fprintf(stderr, "Error: You have to supply both username and password to \
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.mode = SONIC;
} else if (CONFIG.sonic_username || CONFIG.sonic_password) {
fprintf(stderr,
"Error: You have to supply both username and password to \
activate Sonic mode.\n"); activate Sonic mode.\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
}
if (!LinkSystem_init(base_url)) {
fprintf(stderr, "Network initialisation failed.\n");
exit(EXIT_FAILURE);
}
} }
if (!LinkSystem_init(base_url)) {
fprintf(stderr, "Network initialisation failed.\n");
exit(EXIT_FAILURE);
}
}
fuse_start: fuse_start:
fuse_local_init(fuse_argc, fuse_argv); fuse_local_init(fuse_argc, fuse_argv);
return 0; return 0;
} }
void parse_config_file(char ***argv, int *argc) void parse_config_file(char ***argv, int *argc) {
{ char *full_path;
char *full_path; if (!config_path) {
if (!config_path) { char *xdg_config_home = getenv("XDG_CONFIG_HOME");
char *xdg_config_home = getenv("XDG_CONFIG_HOME"); if (!xdg_config_home) {
if (!xdg_config_home) { char *home = getenv("HOME");
char *home = getenv("HOME"); char *xdg_config_home_default = "/.config";
char *xdg_config_home_default = "/.config"; xdg_config_home = path_append(home, xdg_config_home_default);
xdg_config_home = path_append(home, xdg_config_home_default);
}
full_path = path_append(xdg_config_home, "/httpdirfs/config");
} else {
full_path = config_path;
} }
full_path = path_append(xdg_config_home, "/httpdirfs/config");
} else {
full_path = config_path;
}
/* /*
* The buffer has to be able to fit a URL * The buffer has to be able to fit a URL
*/ */
int buf_len = MAX_PATH_LEN; int buf_len = MAX_PATH_LEN;
char buf[buf_len]; char buf[buf_len];
FILE *config = fopen(full_path, "r"); FILE *config = fopen(full_path, "r");
if (config) { if (config) {
while (fgets(buf, buf_len, config)) { while (fgets(buf, buf_len, config)) {
if (buf[0] == '-') { if (buf[0] == '-') {
(*argc)++; (*argc)++;
buf[strnlen(buf, buf_len) - 1] = '\0'; buf[strnlen(buf, buf_len) - 1] = '\0';
char *space; char *space;
space = strchr(buf, ' '); space = strchr(buf, ' ');
if (!space) { if (!space) {
*argv = realloc(*argv, *argc * sizeof(char *)); *argv = realloc(*argv, *argc * sizeof(char *));
(*argv)[*argc - 1] = strndup(buf, buf_len); (*argv)[*argc - 1] = strndup(buf, buf_len);
} else { } else {
(*argc)++; (*argc)++;
*argv = realloc(*argv, *argc * sizeof(char *)); *argv = realloc(*argv, *argc * sizeof(char *));
/* /*
* Only copy up to the space character * Only copy up to the space character
*/ */
(*argv)[*argc - 2] = strndup(buf, space - buf); (*argv)[*argc - 2] = strndup(buf, space - buf);
/* /*
* Starts copying after the space * Starts copying after the space
*/ */
(*argv)[*argc - 1] = strndup(space + 1, (*argv)[*argc - 1] = strndup(space + 1, buf_len - (space + 1 - buf));
buf_len -
(space + 1 - buf));
}
}
} }
fclose(config); }
} }
FREE(full_path); fclose(config);
}
FREE(full_path);
} }
static int static int parse_arg_list(int argc, char **argv, char ***fuse_argv,
parse_arg_list(int argc, char **argv, char ***fuse_argv, int *fuse_argc) int *fuse_argc) {
{ int c;
int c; int long_index = 0;
int long_index = 0; const char *short_opts = "o:hVdfsp:u:P:";
const char *short_opts = "o:hVdfsp:u:P:"; const struct option long_opts[] = {
const struct option long_opts[] = { /*
* Note that 'L' is returned for long options
*/
{"help", no_argument, NULL, 'h'}, /* 0 */
{"version", no_argument, NULL, 'V'}, /* 1 */
{"debug", no_argument, NULL, 'd'}, /* 2 */
{"username", required_argument, NULL, 'u'}, /* 3 */
{"password", required_argument, NULL, 'p'}, /* 4 */
{"proxy", required_argument, NULL, 'P'}, /* 5 */
{"proxy-username", required_argument, NULL, 'L'}, /* 6 */
{"proxy-password", required_argument, NULL, 'L'}, /* 7 */
{"cache", no_argument, NULL, 'L'}, /* 8 */
{"dl-seg-size", required_argument, NULL, 'L'}, /* 9 */
{"max-seg-count", required_argument, NULL, 'L'}, /* 10 */
{"max-conns", required_argument, NULL, 'L'}, /* 11 */
{"user-agent", required_argument, NULL, 'L'}, /* 12 */
{"retry-wait", required_argument, NULL, 'L'}, /* 13 */
{"cache-location", required_argument, NULL, 'L'}, /* 14 */
{"sonic-username", required_argument, NULL, 'L'}, /* 15 */
{"sonic-password", required_argument, NULL, 'L'}, /* 16 */
{"sonic-id3", no_argument, NULL, 'L'}, /* 17 */
{"no-range-check", no_argument, NULL, 'L'}, /* 18 */
{"sonic-insecure", no_argument, NULL, 'L'}, /* 19 */
{"insecure-tls", no_argument, NULL, 'L'}, /* 20 */
{"config", required_argument, NULL, 'L'}, /* 21 */
{"single-file-mode", required_argument, NULL, 'L'}, /* 22 */
{"cacert", required_argument, NULL, 'L'}, /* 23 */
{"proxy-cacert", required_argument, NULL, 'L'}, /* 24 */
{"refresh-timeout", required_argument, NULL, 'L'}, /* 25 */
{0, 0, 0, 0}};
while ((c = getopt_long(argc, argv, short_opts, long_opts, &long_index)) !=
-1) {
switch (c) {
case 'o':
add_arg(fuse_argv, fuse_argc, "-o");
add_arg(fuse_argv, fuse_argc, optarg);
break;
case 'h':
print_help(argv[0], 1);
add_arg(fuse_argv, fuse_argc, "-ho");
/*
* skip everything else to print the help
*/
return 1;
case 'V':
print_version();
add_arg(fuse_argv, fuse_argc, "-V");
return 1;
case 'd':
add_arg(fuse_argv, fuse_argc, "-d");
CONFIG.log_type |= debug;
break;
case 'f':
add_arg(fuse_argv, fuse_argc, "-f");
break;
case 's':
add_arg(fuse_argv, fuse_argc, "-s");
break;
case 'u':
CONFIG.http_username = strdup(optarg);
break;
case 'p':
CONFIG.http_password = strdup(optarg);
break;
case 'P':
CONFIG.proxy = strdup(optarg);
break;
case 'L':
/*
* Long options
*/
switch (long_index) {
case 6:
CONFIG.proxy_username = strdup(optarg);
break;
case 7:
CONFIG.proxy_password = strdup(optarg);
break;
case 8:
CONFIG.cache_enabled = 1;
break;
case 9:
CONFIG.data_blksz = atoi(optarg) * 1024 * 1024;
break;
case 10:
CONFIG.max_segbc = atoi(optarg);
break;
case 11:
CONFIG.max_conns = atoi(optarg);
break;
case 12:
CONFIG.user_agent = strdup(optarg);
break;
case 13:
CONFIG.http_wait_sec = atoi(optarg);
break;
case 14:
CONFIG.cache_dir = strdup(optarg);
break;
case 15:
CONFIG.sonic_username = strdup(optarg);
break;
case 16:
CONFIG.sonic_password = strdup(optarg);
break;
case 17:
CONFIG.sonic_id3 = 1;
break;
case 18:
CONFIG.no_range_check = 1;
break;
case 19:
CONFIG.sonic_insecure = 1;
break;
case 20:
CONFIG.insecure_tls = 1;
break;
case 21:
/* /*
* Note that 'L' is returned for long options * This is for --config, we don't need to do anything
*/ */
{ "help", no_argument, NULL, 'h' }, /* 0 */ break;
{ "version", no_argument, NULL, 'V' }, /* 1 */ case 22:
{ "debug", no_argument, NULL, 'd' }, /* 2 */ CONFIG.mode = SINGLE;
{ "username", required_argument, NULL, 'u' }, /* 3 */ break;
{ "password", required_argument, NULL, 'p' }, /* 4 */ case 23:
{ "proxy", required_argument, NULL, 'P' }, /* 5 */ CONFIG.cafile = strdup(optarg);
{ "proxy-username", required_argument, NULL, 'L' }, /* 6 */ break;
{ "proxy-password", required_argument, NULL, 'L' }, /* 7 */ case 24:
{ "cache", no_argument, NULL, 'L' }, /* 8 */ CONFIG.proxy_cafile = strdup(optarg);
{ "dl-seg-size", required_argument, NULL, 'L' }, /* 9 */ break;
{ "max-seg-count", required_argument, NULL, 'L' }, /* 10 */ case 25:
{ "max-conns", required_argument, NULL, 'L' }, /* 11 */ CONFIG.refresh_timeout = atoi(optarg);
{ "user-agent", required_argument, NULL, 'L' }, /* 12 */ break;
{ "retry-wait", required_argument, NULL, 'L' }, /* 13 */ default:
{ "cache-location", required_argument, NULL, 'L' }, /* 14 */ fprintf(stderr, "see httpdirfs -h for usage\n");
{ "sonic-username", required_argument, NULL, 'L' }, /* 15 */ return 1;
{ "sonic-password", required_argument, NULL, 'L' }, /* 16 */ }
{ "sonic-id3", no_argument, NULL, 'L' }, /* 17 */ break;
{ "no-range-check", no_argument, NULL, 'L' }, /* 18 */ default:
{ "sonic-insecure", no_argument, NULL, 'L' }, /* 19 */ fprintf(stderr, "see httpdirfs -h for usage\n");
{ "insecure-tls", no_argument, NULL, 'L' }, /* 20 */ return 1;
{ "config", required_argument, NULL, 'L' }, /* 21 */ }
{ "single-file-mode", required_argument, NULL, 'L' }, /* 22 */ };
{ "cacert", required_argument, NULL, 'L' }, /* 23 */ return 0;
{ "proxy-cacert", required_argument, NULL, 'L' }, /* 24 */
{ "refresh-timeout", required_argument, NULL, 'L' }, /* 25 */
{ 0, 0, 0, 0 }
};
while ((c =
getopt_long(argc, argv, short_opts, long_opts,
&long_index)) != -1) {
switch (c) {
case 'o':
add_arg(fuse_argv, fuse_argc, "-o");
add_arg(fuse_argv, fuse_argc, optarg);
break;
case 'h':
print_help(argv[0], 1);
add_arg(fuse_argv, fuse_argc, "-ho");
/*
* skip everything else to print the help
*/
return 1;
case 'V':
print_version();
add_arg(fuse_argv, fuse_argc, "-V");
return 1;
case 'd':
add_arg(fuse_argv, fuse_argc, "-d");
CONFIG.log_type |= debug;
break;
case 'f':
add_arg(fuse_argv, fuse_argc, "-f");
break;
case 's':
add_arg(fuse_argv, fuse_argc, "-s");
break;
case 'u':
CONFIG.http_username = strdup(optarg);
break;
case 'p':
CONFIG.http_password = strdup(optarg);
break;
case 'P':
CONFIG.proxy = strdup(optarg);
break;
case 'L':
/*
* Long options
*/
switch (long_index) {
case 6:
CONFIG.proxy_username = strdup(optarg);
break;
case 7:
CONFIG.proxy_password = strdup(optarg);
break;
case 8:
CONFIG.cache_enabled = 1;
break;
case 9:
CONFIG.data_blksz = atoi(optarg) * 1024 * 1024;
break;
case 10:
CONFIG.max_segbc = atoi(optarg);
break;
case 11:
CONFIG.max_conns = atoi(optarg);
break;
case 12:
CONFIG.user_agent = strdup(optarg);
break;
case 13:
CONFIG.http_wait_sec = atoi(optarg);
break;
case 14:
CONFIG.cache_dir = strdup(optarg);
break;
case 15:
CONFIG.sonic_username = strdup(optarg);
break;
case 16:
CONFIG.sonic_password = strdup(optarg);
break;
case 17:
CONFIG.sonic_id3 = 1;
break;
case 18:
CONFIG.no_range_check = 1;
break;
case 19:
CONFIG.sonic_insecure = 1;
break;
case 20:
CONFIG.insecure_tls = 1;
break;
case 21:
/*
* This is for --config, we don't need to do anything
*/
break;
case 22:
CONFIG.mode = SINGLE;
break;
case 23:
CONFIG.cafile = strdup(optarg);
break;
case 24:
CONFIG.proxy_cafile = strdup(optarg);
break;
case 25:
CONFIG.refresh_timeout = atoi(optarg);
break;
default:
fprintf(stderr, "see httpdirfs -h for usage\n");
return 1;
}
break;
default:
fprintf(stderr, "see httpdirfs -h for usage\n");
return 1;
}
};
return 0;
} }
/** /**
* \brief add an argument to an argv array * \brief add an argument to an argv array
* \details This is basically how you add a string to an array of string * \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) void add_arg(char ***fuse_argv_ptr, int *fuse_argc, char *opt_string) {
{ (*fuse_argc)++;
(*fuse_argc)++; *fuse_argv_ptr = realloc(*fuse_argv_ptr, *fuse_argc * sizeof(char *));
*fuse_argv_ptr = realloc(*fuse_argv_ptr, *fuse_argc * sizeof(char *)); char **fuse_argv = *fuse_argv_ptr;
char **fuse_argv = *fuse_argv_ptr; fuse_argv[*fuse_argc - 1] = strdup(opt_string);
fuse_argv[*fuse_argc - 1] = strdup(opt_string);
} }
static void print_help(char *program_name, int long_help) static void print_help(char *program_name, int long_help) {
{ /* FUSE prints its help to stderr */
/* FUSE prints its help to stderr */ fprintf(stderr, "usage: %s [options] URL mountpoint\n", program_name);
fprintf(stderr, "usage: %s [options] URL mountpoint\n", program_name); if (long_help) {
if (long_help) { print_long_help();
print_long_help(); }
}
} }
static void print_long_help() static void print_long_help() {
{ /* FUSE prints its help to stderr */
/* FUSE prints its help to stderr */ fprintf(stderr, "\n\
fprintf(stderr, "\n\
general options:\n\ general options:\n\
--config Specify a configuration file \n\ --config Specify a configuration file \n\
-o opt,[opt...] Mount options\n\ -o opt,[opt...] Mount options\n\

View File

@ -7,22 +7,21 @@
#include <string.h> #include <string.h>
size_t write_memory_callback(void *recv_data, size_t size, size_t nmemb, size_t write_memory_callback(void *recv_data, size_t size, size_t nmemb,
void *userp) void *userp) {
{ size_t recv_size = size * nmemb;
size_t recv_size = size * nmemb; TransferStruct *ts = (TransferStruct *)userp;
TransferStruct *ts = (TransferStruct *) userp;
ts->data = realloc(ts->data, ts->curr_size + recv_size + 1); ts->data = realloc(ts->data, ts->curr_size + recv_size + 1);
if (!ts->data) { if (!ts->data) {
/* /*
* out of memory! * out of memory!
*/ */
lprintf(fatal, "realloc failure!\n"); lprintf(fatal, "realloc failure!\n");
} }
memmove(&ts->data[ts->curr_size], recv_data, recv_size); memmove(&ts->data[ts->curr_size], recv_data, recv_size);
ts->curr_size += recv_size; ts->curr_size += recv_size;
ts->data[ts->curr_size] = '\0'; ts->data[ts->curr_size] = '\0';
return recv_size; return recv_size;
} }

View File

@ -5,25 +5,22 @@
/** /**
* \brief specify the type of data transfer * \brief specify the type of data transfer
*/ */
typedef enum { typedef enum { FILESTAT = 's', DATA = 'd' } TransferType;
FILESTAT = 's',
DATA = 'd'
} TransferType;
/** /**
* \brief For storing transfer data and metadata * \brief For storing transfer data and metadata
*/ */
struct TransferStruct { struct TransferStruct {
/** \brief The array to store the data */ /** \brief The array to store the data */
char *data; char *data;
/** \brief The current size of the array */ /** \brief The current size of the array */
size_t curr_size; size_t curr_size;
/** \brief The type of transfer being done */ /** \brief The type of transfer being done */
TransferType type; TransferType type;
/** \brief Whether transfer is in progress */ /** \brief Whether transfer is in progress */
volatile int transferring; volatile int transferring;
/** \brief The link associated with the transfer */ /** \brief The link associated with the transfer */
Link *link; Link *link;
}; };
/** /**

View File

@ -7,8 +7,8 @@
#include <openssl/crypto.h> #include <openssl/crypto.h>
#include <errno.h> #include <errno.h>
#include <string.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
/* /*
@ -37,47 +37,44 @@ static pthread_mutex_t curl_lock;
* \brief OpenSSL 1.02 cryptography callback function * \brief OpenSSL 1.02 cryptography callback function
* \details Required for OpenSSL 1.02, but not OpenSSL 1.1 * \details Required for OpenSSL 1.02, but not OpenSSL 1.1
*/ */
static void crypto_lock_callback(int mode, int type, char *file, int line) static void crypto_lock_callback(int mode, int type, char *file, int line) {
{ (void)file;
(void) file; (void)line;
(void) line; if (mode & CRYPTO_LOCK) {
if (mode & CRYPTO_LOCK) { PTHREAD_MUTEX_LOCK(&(crypto_lockarray[type]));
PTHREAD_MUTEX_LOCK(&(crypto_lockarray[type])); } else {
} else { PTHREAD_MUTEX_UNLOCK(&(crypto_lockarray[type]));
PTHREAD_MUTEX_UNLOCK(&(crypto_lockarray[type])); }
}
} }
/** /**
* \brief OpenSSL 1.02 thread ID function * \brief OpenSSL 1.02 thread ID function
* \details Required for OpenSSL 1.02, but not OpenSSL 1.1 * \details Required for OpenSSL 1.02, but not OpenSSL 1.1
*/ */
static unsigned long thread_id(void) static unsigned long thread_id(void) {
{ unsigned long ret;
unsigned long ret;
ret = (unsigned long) pthread_self(); ret = (unsigned long)pthread_self();
return ret; return ret;
} }
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
static void crypto_lock_init(void) static void crypto_lock_init(void) {
{ int i;
int i;
crypto_lockarray = crypto_lockarray = (pthread_mutex_t *)OPENSSL_malloc(CRYPTO_num_locks() *
(pthread_mutex_t *) OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
sizeof(pthread_mutex_t)); for (i = 0; i < CRYPTO_num_locks(); i++) {
for (i = 0; i < CRYPTO_num_locks(); i++) { if (pthread_mutex_init(&(crypto_lockarray[i]), NULL)) {
if (pthread_mutex_init(&(crypto_lockarray[i]), NULL)) { lprintf(fatal, "crypto_lockarray[%d] initialisation \
lprintf(fatal, "crypto_lockarray[%d] initialisation \ failed!\n",
failed!\n", i); i);
}; };
} }
CRYPTO_set_id_callback((unsigned long (*)()) thread_id); CRYPTO_set_id_callback((unsigned long (*)())thread_id);
CRYPTO_set_locking_callback((void (*)()) crypto_lock_callback); CRYPTO_set_locking_callback((void (*)())crypto_lock_callback);
} }
/** /**
@ -85,24 +82,21 @@ failed!\n", i);
* \details Adapted from: * \details Adapted from:
* https://curl.haxx.se/libcurl/c/threaded-shared-conn.html * https://curl.haxx.se/libcurl/c/threaded-shared-conn.html
*/ */
static void static void curl_callback_lock(CURL *handle, curl_lock_data data,
curl_callback_lock(CURL *handle, curl_lock_data data, curl_lock_access access, void *userptr) {
curl_lock_access access, void *userptr) (void)access; /* unused */
{ (void)userptr; /* unused */
(void) access; /* unused */ (void)handle; /* unused */
(void) userptr; /* unused */ (void)data; /* unused */
(void) handle; /* unused */ PTHREAD_MUTEX_LOCK(&curl_lock);
(void) data; /* unused */
PTHREAD_MUTEX_LOCK(&curl_lock);
} }
static void static void curl_callback_unlock(CURL *handle, curl_lock_data data,
curl_callback_unlock(CURL *handle, curl_lock_data data, void *userptr) void *userptr) {
{ (void)userptr; /* unused */
(void) userptr; /* unused */ (void)handle; /* unused */
(void) handle; /* unused */ (void)data; /* unused */
(void) data; /* unused */ PTHREAD_MUTEX_UNLOCK(&curl_lock);
PTHREAD_MUTEX_UNLOCK(&curl_lock);
} }
/** /**
@ -110,218 +104,207 @@ curl_callback_unlock(CURL *handle, curl_lock_data data, void *userptr)
* \details Adapted from: * \details Adapted from:
* https://curl.haxx.se/libcurl/c/10-at-a-time.html * https://curl.haxx.se/libcurl/c/10-at-a-time.html
*/ */
static void static void curl_process_msgs(CURLMsg *curl_msg, int n_running_curl,
curl_process_msgs(CURLMsg *curl_msg, int n_running_curl, int n_mesgs) int n_mesgs) {
{ (void)n_running_curl;
(void) n_running_curl; (void)n_mesgs;
(void) n_mesgs; static volatile int slept = 0;
static volatile int slept = 0; if (curl_msg->msg == CURLMSG_DONE) {
if (curl_msg->msg == CURLMSG_DONE) { TransferStruct *ts;
TransferStruct *ts; CURL *curl = curl_msg->easy_handle;
CURL *curl = curl_msg->easy_handle; CURLcode ret =
CURLcode ret = curl_easy_getinfo(curl_msg->easy_handle, CURLINFO_PRIVATE, &ts);
curl_easy_getinfo(curl_msg->easy_handle, CURLINFO_PRIVATE, if (ret) {
&ts); lprintf(error, "%s", curl_easy_strerror(ret));
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
ts->transferring = 0;
char *url = NULL;
ret = curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
/*
* Wait for 5 seconds if we get HTTP 429
*/
long http_resp = 0;
ret = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_resp);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
if (HTTP_temp_failure(http_resp)) {
if (!slept) {
lprintf(warning,
"HTTP %ld, sleeping for %d sec\n",
http_resp, CONFIG.http_wait_sec);
sleep(CONFIG.http_wait_sec);
slept = 1;
}
} else {
slept = 0;
}
if (!curl_msg->data.result) {
/*
* Transfer successful, set the file size
*/
if (ts->type == FILESTAT) {
Link_set_file_stat(ts->link, curl);
}
} else {
lprintf(error, "%d - %s <%s>\n",
curl_msg->data.result,
curl_easy_strerror(curl_msg->data.result), url);
}
curl_multi_remove_handle(curl_multi, curl);
/*
* clean up the handle, if we are querying the file size
*/
if (ts->type == FILESTAT) {
curl_easy_cleanup(curl);
FREE(ts);
}
} else {
lprintf(warning, "curl_msg->msg: %d\n", curl_msg->msg);
} }
ts->transferring = 0;
char *url = NULL;
ret = curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
/*
* Wait for 5 seconds if we get HTTP 429
*/
long http_resp = 0;
ret = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_resp);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
if (HTTP_temp_failure(http_resp)) {
if (!slept) {
lprintf(warning, "HTTP %ld, sleeping for %d sec\n", http_resp,
CONFIG.http_wait_sec);
sleep(CONFIG.http_wait_sec);
slept = 1;
}
} else {
slept = 0;
}
if (!curl_msg->data.result) {
/*
* Transfer successful, set the file size
*/
if (ts->type == FILESTAT) {
Link_set_file_stat(ts->link, curl);
}
} else {
lprintf(error, "%d - %s <%s>\n", curl_msg->data.result,
curl_easy_strerror(curl_msg->data.result), url);
}
curl_multi_remove_handle(curl_multi, curl);
/*
* clean up the handle, if we are querying the file size
*/
if (ts->type == FILESTAT) {
curl_easy_cleanup(curl);
FREE(ts);
}
} else {
lprintf(warning, "curl_msg->msg: %d\n", curl_msg->msg);
}
} }
/** /**
* \details effectively based on * \details effectively based on
* https://curl.haxx.se/libcurl/c/multi-double.html * https://curl.haxx.se/libcurl/c/multi-double.html
*/ */
int curl_multi_perform_once(void) int curl_multi_perform_once(void) {
{ lprintf(network_lock_debug, "thread %x: locking transfer_lock;\n",
lprintf(network_lock_debug, pthread_self());
"thread %x: locking transfer_lock;\n", pthread_self()); PTHREAD_MUTEX_LOCK(&transfer_lock);
PTHREAD_MUTEX_LOCK(&transfer_lock);
/* /*
* Get curl multi interface to perform pending tasks * Get curl multi interface to perform pending tasks
*/ */
int n_running_curl; int n_running_curl;
CURLMcode mc = curl_multi_perform(curl_multi, &n_running_curl); CURLMcode mc = curl_multi_perform(curl_multi, &n_running_curl);
if (mc) { if (mc) {
lprintf(error, "%s\n", curl_multi_strerror(mc)); lprintf(error, "%s\n", curl_multi_strerror(mc));
} }
mc = curl_multi_poll(curl_multi, NULL, 0, 100, NULL); mc = curl_multi_poll(curl_multi, NULL, 0, 100, NULL);
if (mc) { if (mc) {
lprintf(error, "%s\n", curl_multi_strerror(mc)); lprintf(error, "%s\n", curl_multi_strerror(mc));
} }
/* /*
* Process the message queue * Process the message queue
*/ */
int n_mesgs; int n_mesgs;
CURLMsg *curl_msg; CURLMsg *curl_msg;
while ((curl_msg = curl_multi_info_read(curl_multi, &n_mesgs))) { while ((curl_msg = curl_multi_info_read(curl_multi, &n_mesgs))) {
curl_process_msgs(curl_msg, n_running_curl, n_mesgs); curl_process_msgs(curl_msg, n_running_curl, n_mesgs);
} }
lprintf(network_lock_debug, lprintf(network_lock_debug, "thread %x: unlocking transfer_lock;\n",
"thread %x: unlocking transfer_lock;\n", pthread_self()); pthread_self());
PTHREAD_MUTEX_UNLOCK(&transfer_lock); PTHREAD_MUTEX_UNLOCK(&transfer_lock);
return n_running_curl; return n_running_curl;
} }
void NetworkSystem_init(void) void NetworkSystem_init(void) {
{ /*
/* * ------- Global related ----------
* ------- Global related ---------- */
*/ if (curl_global_init(CURL_GLOBAL_ALL)) {
if (curl_global_init(CURL_GLOBAL_ALL)) { lprintf(fatal, "curl_global_init() failed!\n");
lprintf(fatal, "curl_global_init() failed!\n"); }
}
/* /*
* -------- Share related ---------- * -------- Share related ----------
*/ */
CURL_SHARE = curl_share_init(); CURL_SHARE = curl_share_init();
if (!(CURL_SHARE)) { if (!(CURL_SHARE)) {
lprintf(fatal, "curl_share_init() failed!\n"); lprintf(fatal, "curl_share_init() failed!\n");
} }
curl_share_setopt(CURL_SHARE, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE); curl_share_setopt(CURL_SHARE, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
curl_share_setopt(CURL_SHARE, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS); curl_share_setopt(CURL_SHARE, CURLSHOPT_SHARE, CURL_LOCK_DATA_DNS);
curl_share_setopt(CURL_SHARE, CURLSHOPT_SHARE, curl_share_setopt(CURL_SHARE, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
CURL_LOCK_DATA_SSL_SESSION);
if (pthread_mutex_init(&curl_lock, NULL)) { if (pthread_mutex_init(&curl_lock, NULL)) {
lprintf(fatal, "curl_lock initialisation failed!\n"); lprintf(fatal, "curl_lock initialisation failed!\n");
} }
curl_share_setopt(CURL_SHARE, CURLSHOPT_LOCKFUNC, curl_callback_lock); curl_share_setopt(CURL_SHARE, CURLSHOPT_LOCKFUNC, curl_callback_lock);
curl_share_setopt(CURL_SHARE, CURLSHOPT_UNLOCKFUNC, curl_share_setopt(CURL_SHARE, CURLSHOPT_UNLOCKFUNC, curl_callback_unlock);
curl_callback_unlock);
/* /*
* ------------- Multi related ----------- * ------------- Multi related -----------
*/ */
curl_multi = curl_multi_init(); curl_multi = curl_multi_init();
if (!curl_multi) { if (!curl_multi) {
lprintf(fatal, "curl_multi_init() failed!\n"); lprintf(fatal, "curl_multi_init() failed!\n");
} }
curl_multi_setopt(curl_multi, CURLMOPT_MAX_TOTAL_CONNECTIONS, curl_multi_setopt(curl_multi, CURLMOPT_MAX_TOTAL_CONNECTIONS,
CONFIG.max_conns); CONFIG.max_conns);
curl_multi_setopt(curl_multi, CURLMOPT_MAX_HOST_CONNECTIONS, curl_multi_setopt(curl_multi, CURLMOPT_MAX_HOST_CONNECTIONS,
CONFIG.max_conns); CONFIG.max_conns);
/* /*
* ------------ Initialise locks --------- * ------------ Initialise locks ---------
*/ */
if (pthread_mutex_init(&transfer_lock, NULL)) { if (pthread_mutex_init(&transfer_lock, NULL)) {
lprintf(fatal, "transfer_lock initialisation failed!\n"); lprintf(fatal, "transfer_lock initialisation failed!\n");
} }
/* /*
* cryptographic lock functions were shamelessly copied from * cryptographic lock functions were shamelessly copied from
* https://curl.haxx.se/libcurl/c/threaded-ssl.html * https://curl.haxx.se/libcurl/c/threaded-ssl.html
*/ */
crypto_lock_init(); crypto_lock_init();
} }
void transfer_blocking(CURL *curl) void transfer_blocking(CURL *curl) {
{ TransferStruct *ts;
TransferStruct *ts; CURLcode ret = curl_easy_getinfo(curl, CURLINFO_PRIVATE, &ts);
CURLcode ret = curl_easy_getinfo(curl, CURLINFO_PRIVATE, &ts); if (ret) {
if (ret) { lprintf(error, "%s", curl_easy_strerror(ret));
lprintf(error, "%s", curl_easy_strerror(ret)); }
}
lprintf(network_lock_debug, lprintf(network_lock_debug, "thread %x: locking transfer_lock;\n",
"thread %x: locking transfer_lock;\n", pthread_self()); pthread_self());
PTHREAD_MUTEX_LOCK(&transfer_lock); PTHREAD_MUTEX_LOCK(&transfer_lock);
CURLMcode res = curl_multi_add_handle(curl_multi, curl); CURLMcode res = curl_multi_add_handle(curl_multi, curl);
if (res > 0) { if (res > 0) {
lprintf(error, "%d, %s\n", res, curl_multi_strerror(res)); lprintf(error, "%d, %s\n", res, curl_multi_strerror(res));
} }
lprintf(network_lock_debug, lprintf(network_lock_debug, "thread %x: unlocking transfer_lock;\n",
"thread %x: unlocking transfer_lock;\n", pthread_self()); pthread_self());
PTHREAD_MUTEX_UNLOCK(&transfer_lock); PTHREAD_MUTEX_UNLOCK(&transfer_lock);
while (ts->transferring) { while (ts->transferring) {
curl_multi_perform_once(); curl_multi_perform_once();
} }
} }
void transfer_nonblocking(CURL *curl) void transfer_nonblocking(CURL *curl) {
{ lprintf(network_lock_debug, "thread %x: locking transfer_lock;\n",
lprintf(network_lock_debug, pthread_self());
"thread %x: locking transfer_lock;\n", pthread_self()); PTHREAD_MUTEX_LOCK(&transfer_lock);
PTHREAD_MUTEX_LOCK(&transfer_lock);
CURLMcode res = curl_multi_add_handle(curl_multi, curl); CURLMcode res = curl_multi_add_handle(curl_multi, curl);
if (res > 0) { if (res > 0) {
lprintf(error, "%s\n", curl_multi_strerror(res)); lprintf(error, "%s\n", curl_multi_strerror(res));
} }
lprintf(network_lock_debug, lprintf(network_lock_debug, "thread %x: unlocking transfer_lock;\n",
"thread %x: unlocking transfer_lock;\n", pthread_self()); pthread_self());
PTHREAD_MUTEX_UNLOCK(&transfer_lock); PTHREAD_MUTEX_UNLOCK(&transfer_lock);
} }
int HTTP_temp_failure(HTTPResponseCode http_resp) int HTTP_temp_failure(HTTPResponseCode http_resp) {
{ switch (http_resp) {
switch (http_resp) { case HTTP_TOO_MANY_REQUESTS:
case HTTP_TOO_MANY_REQUESTS: case HTTP_CLOUDFLARE_UNKNOWN_ERROR:
case HTTP_CLOUDFLARE_UNKNOWN_ERROR: case HTTP_CLOUDFLARE_TIMEOUT:
case HTTP_CLOUDFLARE_TIMEOUT: return 1;
return 1; default:
default: return 0;
return 0; }
}
} }

View File

@ -14,12 +14,12 @@ typedef struct TransferStruct TransferStruct;
/** \brief HTTP response codes */ /** \brief HTTP response codes */
typedef enum { typedef enum {
HTTP_OK = 200, HTTP_OK = 200,
HTTP_PARTIAL_CONTENT = 206, HTTP_PARTIAL_CONTENT = 206,
HTTP_RANGE_NOT_SATISFIABLE = 416, HTTP_RANGE_NOT_SATISFIABLE = 416,
HTTP_TOO_MANY_REQUESTS = 429, HTTP_TOO_MANY_REQUESTS = 429,
HTTP_CLOUDFLARE_UNKNOWN_ERROR = 520, HTTP_CLOUDFLARE_UNKNOWN_ERROR = 520,
HTTP_CLOUDFLARE_TIMEOUT = 524 HTTP_CLOUDFLARE_TIMEOUT = 524
} HTTPResponseCode; } HTTPResponseCode;
/** \brief curl shared interface */ /** \brief curl shared interface */

View File

@ -1,24 +1,24 @@
#include "sonic.h" #include "sonic.h"
#include "config.h" #include "config.h"
#include "log.h"
#include "link.h" #include "link.h"
#include "log.h"
#include "memcache.h" #include "memcache.h"
#include "util.h" #include "util.h"
#include <expat.h> #include <expat.h>
#include <assert.h> #include <assert.h>
#include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
typedef struct { typedef struct {
char *server; char *server;
char *username; char *username;
char *password; char *password;
char *client; char *client;
char *api_version; char *api_version;
} SonicConfigStruct; } SonicConfigStruct;
static SonicConfigStruct SONIC_CONFIG; static SonicConfigStruct SONIC_CONFIG;
@ -26,128 +26,118 @@ static SonicConfigStruct SONIC_CONFIG;
/** /**
* \brief initialise Sonic configuration struct * \brief initialise Sonic configuration struct
*/ */
void void sonic_config_init(const char *server, const char *username,
sonic_config_init(const char *server, const char *username, const char *password) {
const char *password) SONIC_CONFIG.server = strndup(server, MAX_PATH_LEN);
{ /*
SONIC_CONFIG.server = strndup(server, MAX_PATH_LEN); * Correct for the extra '/'
/* */
* Correct for the extra '/' size_t server_url_len = strnlen(SONIC_CONFIG.server, MAX_PATH_LEN) - 1;
*/ if (SONIC_CONFIG.server[server_url_len] == '/') {
size_t server_url_len = strnlen(SONIC_CONFIG.server, MAX_PATH_LEN) - 1; SONIC_CONFIG.server[server_url_len] = '\0';
if (SONIC_CONFIG.server[server_url_len] == '/') { }
SONIC_CONFIG.server[server_url_len] = '\0'; SONIC_CONFIG.username = strndup(username, MAX_FILENAME_LEN);
} SONIC_CONFIG.password = strndup(password, MAX_FILENAME_LEN);
SONIC_CONFIG.username = strndup(username, MAX_FILENAME_LEN); SONIC_CONFIG.client = DEFAULT_USER_AGENT;
SONIC_CONFIG.password = strndup(password, MAX_FILENAME_LEN);
SONIC_CONFIG.client = DEFAULT_USER_AGENT;
if (!CONFIG.sonic_insecure) { if (!CONFIG.sonic_insecure) {
/* /*
* API 1.13.0 is the minimum version that supports * API 1.13.0 is the minimum version that supports
* salt authentication scheme * salt authentication scheme
*/ */
SONIC_CONFIG.api_version = "1.13.0"; SONIC_CONFIG.api_version = "1.13.0";
} else { } else {
/* /*
* API 1.8.0 is the minimum version that supports ID3 mode * API 1.8.0 is the minimum version that supports ID3 mode
*/ */
SONIC_CONFIG.api_version = "1.8.0"; SONIC_CONFIG.api_version = "1.8.0";
} }
} }
/** /**
* \brief generate authentication string * \brief generate authentication string
*/ */
static char *sonic_gen_auth_str(void) static char *sonic_gen_auth_str(void) {
{ if (!CONFIG.sonic_insecure) {
if (!CONFIG.sonic_insecure) { char *salt = generate_salt();
char *salt = generate_salt(); size_t pwd_len = strnlen(SONIC_CONFIG.password, MAX_FILENAME_LEN);
size_t pwd_len = strnlen(SONIC_CONFIG.password, MAX_FILENAME_LEN); size_t pwd_salt_len = pwd_len + strnlen(salt, MAX_FILENAME_LEN);
size_t pwd_salt_len = pwd_len + strnlen(salt, MAX_FILENAME_LEN); char *pwd_salt = CALLOC(pwd_salt_len + 1, sizeof(char));
char *pwd_salt = CALLOC(pwd_salt_len + 1, sizeof(char)); strncat(pwd_salt, SONIC_CONFIG.password, MAX_FILENAME_LEN);
strncat(pwd_salt, SONIC_CONFIG.password, MAX_FILENAME_LEN); strncat(pwd_salt + pwd_len, salt, MAX_FILENAME_LEN);
strncat(pwd_salt + pwd_len, salt, MAX_FILENAME_LEN); char *token = generate_md5sum(pwd_salt);
char *token = generate_md5sum(pwd_salt); char *auth_str = CALLOC(MAX_PATH_LEN + 1, sizeof(char));
char *auth_str = CALLOC(MAX_PATH_LEN + 1, sizeof(char)); snprintf(auth_str, MAX_PATH_LEN, ".view?u=%s&t=%s&s=%s&v=%s&c=%s",
snprintf(auth_str, MAX_PATH_LEN, SONIC_CONFIG.username, token, salt, SONIC_CONFIG.api_version,
".view?u=%s&t=%s&s=%s&v=%s&c=%s", SONIC_CONFIG.client);
SONIC_CONFIG.username, token, salt, FREE(salt);
SONIC_CONFIG.api_version, SONIC_CONFIG.client); FREE(token);
FREE(salt); return auth_str;
FREE(token); } else {
return auth_str; char *pwd_hex = str_to_hex(SONIC_CONFIG.password);
} else { char *auth_str = CALLOC(MAX_PATH_LEN + 1, sizeof(char));
char *pwd_hex = str_to_hex(SONIC_CONFIG.password); snprintf(auth_str, MAX_PATH_LEN, ".view?u=%s&p=enc:%s&v=%s&c=%s",
char *auth_str = CALLOC(MAX_PATH_LEN + 1, sizeof(char)); SONIC_CONFIG.username, pwd_hex, SONIC_CONFIG.api_version,
snprintf(auth_str, MAX_PATH_LEN, SONIC_CONFIG.client);
".view?u=%s&p=enc:%s&v=%s&c=%s", FREE(pwd_hex);
SONIC_CONFIG.username, pwd_hex, return auth_str;
SONIC_CONFIG.api_version, SONIC_CONFIG.client); }
FREE(pwd_hex);
return auth_str;
}
} }
/** /**
* \brief generate the first half of the request URL * \brief generate the first half of the request URL
*/ */
static char *sonic_gen_url_first_part(char *method) static char *sonic_gen_url_first_part(char *method) {
{ char *auth_str = sonic_gen_auth_str();
char *auth_str = sonic_gen_auth_str(); char *url = CALLOC(MAX_PATH_LEN + 1, sizeof(char));
char *url = CALLOC(MAX_PATH_LEN + 1, sizeof(char)); snprintf(url, MAX_PATH_LEN, "%s/rest/%s%s", SONIC_CONFIG.server, method,
snprintf(url, MAX_PATH_LEN, "%s/rest/%s%s", SONIC_CONFIG.server, auth_str);
method, auth_str); FREE(auth_str);
FREE(auth_str); return url;
return url;
} }
/** /**
* \brief generate a getMusicDirectory request URL * \brief generate a getMusicDirectory request URL
*/ */
static char *sonic_getMusicDirectory_link(const char *id) static char *sonic_getMusicDirectory_link(const char *id) {
{ char *first_part = sonic_gen_url_first_part("getMusicDirectory");
char *first_part = sonic_gen_url_first_part("getMusicDirectory"); char *url = CALLOC(MAX_PATH_LEN + 1, sizeof(char));
char *url = CALLOC(MAX_PATH_LEN + 1, sizeof(char)); snprintf(url, MAX_PATH_LEN, "%s&id=%s", first_part, id);
snprintf(url, MAX_PATH_LEN, "%s&id=%s", first_part, id); FREE(first_part);
FREE(first_part); return url;
return url;
} }
/** /**
* \brief generate a getArtist request URL * \brief generate a getArtist request URL
*/ */
static char *sonic_getArtist_link(const char *id) static char *sonic_getArtist_link(const char *id) {
{ char *first_part = sonic_gen_url_first_part("getArtist");
char *first_part = sonic_gen_url_first_part("getArtist"); char *url = CALLOC(MAX_PATH_LEN + 1, sizeof(char));
char *url = CALLOC(MAX_PATH_LEN + 1, sizeof(char)); snprintf(url, MAX_PATH_LEN, "%s&id=%s", first_part, id);
snprintf(url, MAX_PATH_LEN, "%s&id=%s", first_part, id); FREE(first_part);
FREE(first_part); return url;
return url;
} }
/** /**
* \brief generate a getAlbum request URL * \brief generate a getAlbum request URL
*/ */
static char *sonic_getAlbum_link(const char *id) static char *sonic_getAlbum_link(const char *id) {
{ char *first_part = sonic_gen_url_first_part("getAlbum");
char *first_part = sonic_gen_url_first_part("getAlbum"); char *url = CALLOC(MAX_PATH_LEN + 1, sizeof(char));
char *url = CALLOC(MAX_PATH_LEN + 1, sizeof(char)); snprintf(url, MAX_PATH_LEN, "%s&id=%s", first_part, id);
snprintf(url, MAX_PATH_LEN, "%s&id=%s", first_part, id); FREE(first_part);
FREE(first_part); return url;
return url;
} }
/** /**
* \brief generate a download request URL * \brief generate a download request URL
*/ */
static char *sonic_stream_link(const char *id) static char *sonic_stream_link(const char *id) {
{ char *first_part = sonic_gen_url_first_part("stream");
char *first_part = sonic_gen_url_first_part("stream"); char *url = CALLOC(MAX_PATH_LEN + 1, sizeof(char));
char *url = CALLOC(MAX_PATH_LEN + 1, sizeof(char)); snprintf(url, MAX_PATH_LEN, "%s&format=raw&id=%s", first_part, id);
snprintf(url, MAX_PATH_LEN, "%s&format=raw&id=%s", first_part, id); FREE(first_part);
FREE(first_part); return url;
return url;
} }
/** /**
@ -164,366 +154,352 @@ static char *sonic_stream_link(const char *id)
* parser terminates the strings properly, which is a fair assumption, * parser terminates the strings properly, which is a fair assumption,
* considering how mature expat is. * considering how mature expat is.
*/ */
static void XMLCALL static void XMLCALL XML_parser_general(void *data, const char *elem,
XML_parser_general(void *data, const char *elem, const char **attr) const char **attr) {
{ /*
/* * Error checking
* Error checking */
*/ if (!strcmp(elem, "error")) {
if (!strcmp(elem, "error")) { lprintf(error, "error:\n");
lprintf(error, "error:\n");
for (int i = 0; attr[i]; i += 2) {
lprintf(error, "%s: %s\n", attr[i], attr[i + 1]);
}
}
LinkTable *linktbl = (LinkTable *) data;
Link *link;
/*
* Please refer to the documentation at the function prototype of
* sonic_LinkTable_new_id3()
*/
if (!strcmp(elem, "child")) {
link = CALLOC(1, sizeof(Link));
/*
* Initialise to LINK_DIR, as the LINK_FILE is set later.
*/
link->type = LINK_DIR;
} else if (!strcmp(elem, "artist")
&& linktbl->links[0]->sonic.depth != 3) {
/*
* We want to skip the first "artist" element in the album table
*/
link = CALLOC(1, sizeof(Link));
link->type = LINK_DIR;
} else if (!strcmp(elem, "album")
&& linktbl->links[0]->sonic.depth == 3) {
link = CALLOC(1, sizeof(Link));
link->type = LINK_DIR;
/*
* The new table should be a level 4 song table
*/
link->sonic.depth = 4;
} else if (!strcmp(elem, "song")
&& linktbl->links[0]->sonic.depth == 4) {
link = CALLOC(1, sizeof(Link));
link->type = LINK_FILE;
} else {
/*
* The element does not contain directory structural information
*/
return;
}
int id_set = 0;
int linkname_set = 0;
int track = 0;
char *title = "";
char *suffix = "";
for (int i = 0; attr[i]; i += 2) { for (int i = 0; attr[i]; i += 2) {
if (!strcmp("id", attr[i])) { lprintf(error, "%s: %s\n", attr[i], attr[i + 1]);
link->sonic.id = CALLOC(MAX_FILENAME_LEN + 1, sizeof(char)); }
strncpy(link->sonic.id, attr[i + 1], MAX_FILENAME_LEN); }
id_set = 1;
continue;
}
if (!strcmp("path", attr[i])) { LinkTable *linktbl = (LinkTable *)data;
memset(link->linkname, 0, MAX_FILENAME_LEN); Link *link;
/*
* Skip to the last '/' if it exists
*/
char *s = strrchr(attr[i + 1], '/');
if (s) {
strncpy(link->linkname, s + 1, MAX_FILENAME_LEN);
} else {
strncpy(link->linkname, attr[i + 1], MAX_FILENAME_LEN);
}
linkname_set = 1;
continue;
}
/* /*
* "title" is used for directory name, * Please refer to the documentation at the function prototype of
* "name" is for top level directories * sonic_LinkTable_new_id3()
* N.B. "path" attribute is given the preference */
*/ if (!strcmp(elem, "child")) {
if (!linkname_set) { link = CALLOC(1, sizeof(Link));
if (!strcmp("title", attr[i]) /*
|| !strcmp("name", attr[i])) { * Initialise to LINK_DIR, as the LINK_FILE is set later.
strncpy(link->linkname, attr[i + 1], MAX_FILENAME_LEN); */
linkname_set = 1; link->type = LINK_DIR;
continue; } else if (!strcmp(elem, "artist") && linktbl->links[0]->sonic.depth != 3) {
} /*
} * We want to skip the first "artist" element in the album table
*/
link = CALLOC(1, sizeof(Link));
link->type = LINK_DIR;
} else if (!strcmp(elem, "album") && linktbl->links[0]->sonic.depth == 3) {
link = CALLOC(1, sizeof(Link));
link->type = LINK_DIR;
/*
* The new table should be a level 4 song table
*/
link->sonic.depth = 4;
} else if (!strcmp(elem, "song") && linktbl->links[0]->sonic.depth == 4) {
link = CALLOC(1, sizeof(Link));
link->type = LINK_FILE;
} else {
/*
* The element does not contain directory structural information
*/
return;
}
if (!strcmp("isDir", attr[i])) { int id_set = 0;
if (!strcmp("false", attr[i + 1])) { int linkname_set = 0;
link->type = LINK_FILE;
}
continue;
}
if (!strcmp("created", attr[i])) { int track = 0;
struct tm *tm = CALLOC(1, sizeof(struct tm)); char *title = "";
strptime(attr[i + 1], "%Y-%m-%dT%H:%M:%S.000Z", tm); char *suffix = "";
link->time = mktime(tm); for (int i = 0; attr[i]; i += 2) {
FREE(tm); if (!strcmp("id", attr[i])) {
continue; link->sonic.id = CALLOC(MAX_FILENAME_LEN + 1, sizeof(char));
} strncpy(link->sonic.id, attr[i + 1], MAX_FILENAME_LEN);
id_set = 1;
if (!strcmp("size", attr[i])) { continue;
link->content_length = atoll(attr[i + 1]);
continue;
}
if (!strcmp("track", attr[i])) {
track = atoi(attr[i + 1]);
continue;
}
if (!strcmp("title", attr[i])) {
title = (char *) attr[i + 1];
continue;
}
if (!strcmp("suffix", attr[i])) {
suffix = (char *) attr[i + 1];
continue;
}
} }
if (!linkname_set && strnlen(title, MAX_PATH_LEN) > 0 && if (!strcmp("path", attr[i])) {
strnlen(suffix, MAX_PATH_LEN) > 0) { memset(link->linkname, 0, MAX_FILENAME_LEN);
snprintf(link->linkname, MAX_FILENAME_LEN, "%02d - %s.%s", /*
track, title, suffix); * Skip to the last '/' if it exists
linkname_set = 1; */
char *s = strrchr(attr[i + 1], '/');
if (s) {
strncpy(link->linkname, s + 1, MAX_FILENAME_LEN);
} else {
strncpy(link->linkname, attr[i + 1], MAX_FILENAME_LEN);
}
linkname_set = 1;
continue;
} }
/* /*
* Clean up if linkname or id is not set * "title" is used for directory name,
* "name" is for top level directories
* N.B. "path" attribute is given the preference
*/ */
if (!linkname_set || !id_set) { if (!linkname_set) {
FREE(link); if (!strcmp("title", attr[i]) || !strcmp("name", attr[i])) {
return; strncpy(link->linkname, attr[i + 1], MAX_FILENAME_LEN);
linkname_set = 1;
continue;
}
} }
if (link->type == LINK_FILE) { if (!strcmp("isDir", attr[i])) {
char *url = sonic_stream_link(link->sonic.id); if (!strcmp("false", attr[i + 1])) {
strncpy(link->f_url, url, MAX_PATH_LEN); link->type = LINK_FILE;
FREE(url); }
continue;
} }
LinkTable_add(linktbl, link); if (!strcmp("created", attr[i])) {
struct tm *tm = CALLOC(1, sizeof(struct tm));
strptime(attr[i + 1], "%Y-%m-%dT%H:%M:%S.000Z", tm);
link->time = mktime(tm);
FREE(tm);
continue;
}
if (!strcmp("size", attr[i])) {
link->content_length = atoll(attr[i + 1]);
continue;
}
if (!strcmp("track", attr[i])) {
track = atoi(attr[i + 1]);
continue;
}
if (!strcmp("title", attr[i])) {
title = (char *)attr[i + 1];
continue;
}
if (!strcmp("suffix", attr[i])) {
suffix = (char *)attr[i + 1];
continue;
}
}
if (!linkname_set && strnlen(title, MAX_PATH_LEN) > 0 &&
strnlen(suffix, MAX_PATH_LEN) > 0) {
snprintf(link->linkname, MAX_FILENAME_LEN, "%02d - %s.%s", track, title,
suffix);
linkname_set = 1;
}
/*
* Clean up if linkname or id is not set
*/
if (!linkname_set || !id_set) {
FREE(link);
return;
}
if (link->type == LINK_FILE) {
char *url = sonic_stream_link(link->sonic.id);
strncpy(link->f_url, url, MAX_PATH_LEN);
FREE(url);
}
LinkTable_add(linktbl, link);
} }
static void sanitise_LinkTable(LinkTable *linktbl) static void sanitise_LinkTable(LinkTable *linktbl) {
{ for (int i = 0; i < linktbl->num; i++) {
for (int i = 0; i < linktbl->num; i++) { if (!strcmp(linktbl->links[i]->linkname, ".")) {
if (!strcmp(linktbl->links[i]->linkname, ".")) { /* Note the super long sanitised name to avoid collision */
/* Note the super long sanitised name to avoid collision */ strcpy(linktbl->links[i]->linkname, "__DOT__");
strcpy(linktbl->links[i]->linkname, "__DOT__");
}
if (!strcmp(linktbl->links[i]->linkname, "/")) {
/* Ditto */
strcpy(linktbl->links[i]->linkname, "__FORWARD-SLASH__");
}
for (size_t j = 0; j < strlen(linktbl->links[i]->linkname); j++) {
if (linktbl->links[i]->linkname[j] == '/') {
linktbl->links[i]->linkname[j] = '-';
}
}
if (linktbl->links[i]->next_table != NULL) {
sanitise_LinkTable(linktbl->links[i]->next_table);
}
} }
if (!strcmp(linktbl->links[i]->linkname, "/")) {
/* Ditto */
strcpy(linktbl->links[i]->linkname, "__FORWARD-SLASH__");
}
for (size_t j = 0; j < strlen(linktbl->links[i]->linkname); j++) {
if (linktbl->links[i]->linkname[j] == '/') {
linktbl->links[i]->linkname[j] = '-';
}
}
if (linktbl->links[i]->next_table != NULL) {
sanitise_LinkTable(linktbl->links[i]->next_table);
}
}
} }
/** /**
* \brief parse a XML string in order to fill in the LinkTable * \brief parse a XML string in order to fill in the LinkTable
*/ */
static LinkTable *sonic_url_to_LinkTable(const char *url, static LinkTable *sonic_url_to_LinkTable(const char *url,
XML_StartElementHandler handler, int depth) XML_StartElementHandler handler,
{ int depth) {
LinkTable *linktbl = LinkTable_alloc(url); LinkTable *linktbl = LinkTable_alloc(url);
linktbl->links[0]->sonic.depth = depth; linktbl->links[0]->sonic.depth = depth;
/* /*
* start downloading the base URL * start downloading the base URL
*/ */
TransferStruct xml = Link_download_full(linktbl->links[0]); TransferStruct xml = Link_download_full(linktbl->links[0]);
if (xml.curr_size == 0) { if (xml.curr_size == 0) {
LinkTable_free(linktbl); LinkTable_free(linktbl);
return NULL; return NULL;
} }
XML_Parser parser = XML_ParserCreate(NULL); XML_Parser parser = XML_ParserCreate(NULL);
XML_SetUserData(parser, linktbl); XML_SetUserData(parser, linktbl);
XML_SetStartElementHandler(parser, handler); XML_SetStartElementHandler(parser, handler);
if (XML_Parse(parser, xml.data, xml.curr_size, 1) == XML_STATUS_ERROR) { if (XML_Parse(parser, xml.data, xml.curr_size, 1) == XML_STATUS_ERROR) {
lprintf(error, lprintf(error, "Parse error at line %lu: %s\n",
"Parse error at line %lu: %s\n", XML_GetCurrentLineNumber(parser),
XML_GetCurrentLineNumber(parser), XML_ErrorString(XML_GetErrorCode(parser)));
XML_ErrorString(XML_GetErrorCode(parser))); }
}
XML_ParserFree(parser); XML_ParserFree(parser);
FREE(xml.data); FREE(xml.data);
LinkTable_print(linktbl); LinkTable_print(linktbl);
sanitise_LinkTable(linktbl); sanitise_LinkTable(linktbl);
return linktbl;
return linktbl;
} }
LinkTable *sonic_LinkTable_new_index(const char *id) LinkTable *sonic_LinkTable_new_index(const char *id) {
{ char *url;
char *url; if (strcmp(id, "0")) {
if (strcmp(id, "0")) { url = sonic_getMusicDirectory_link(id);
url = sonic_getMusicDirectory_link(id); } else {
url = sonic_gen_url_first_part("getIndexes");
}
LinkTable *linktbl = sonic_url_to_LinkTable(url, XML_parser_general, 0);
FREE(url);
return linktbl;
}
static void XMLCALL XML_parser_id3_root(void *data, const char *elem,
const char **attr) {
if (!strcmp(elem, "error")) {
lprintf(error, "\n");
for (int i = 0; attr[i]; i += 2) {
lprintf(error, "%s: %s\n", attr[i], attr[i + 1]);
}
}
LinkTable *root_linktbl = (LinkTable *)data;
LinkTable *this_linktbl = NULL;
/*
* Set the current linktbl, if we have more than head link.
*/
if (root_linktbl->num > 1) {
this_linktbl = root_linktbl->links[root_linktbl->num - 1]->next_table;
}
int id_set = 0;
int linkname_set = 0;
Link *link;
if (!strcmp(elem, "index")) {
/*
* Add a subdirectory
*/
link = CALLOC(1, sizeof(Link));
link->type = LINK_DIR;
for (int i = 0; attr[i]; i += 2) {
if (!strcmp("name", attr[i])) {
strncpy(link->linkname, attr[i + 1], MAX_FILENAME_LEN);
linkname_set = 1;
/*
* Allocate a new LinkTable
*/
link->next_table = LinkTable_alloc("/");
}
}
/*
* Make sure we don't add an empty directory
*/
if (linkname_set) {
LinkTable_add(root_linktbl, link);
} else { } else {
url = sonic_gen_url_first_part("getIndexes"); FREE(link);
} }
LinkTable *linktbl = return;
sonic_url_to_LinkTable(url, XML_parser_general, 0); } else if (!strcmp(elem, "artist")) {
link = CALLOC(1, sizeof(Link));
link->type = LINK_DIR;
/*
* The new table should be a level 3 album table
*/
link->sonic.depth = 3;
for (int i = 0; attr[i]; i += 2) {
if (!strcmp("name", attr[i])) {
strncpy(link->linkname, attr[i + 1], MAX_FILENAME_LEN);
linkname_set = 1;
continue;
}
if (!strcmp("id", attr[i])) {
link->sonic.id = CALLOC(MAX_FILENAME_LEN + 1, sizeof(char));
strncpy(link->sonic.id, attr[i + 1], MAX_FILENAME_LEN);
id_set = 1;
continue;
}
}
/*
* Clean up if linkname is not set
*/
if (!linkname_set || !id_set) {
FREE(link);
return;
}
LinkTable_add(this_linktbl, link);
}
/*
* If we reach here, then this element does not contain directory structural
* information
*/
}
LinkTable *sonic_LinkTable_new_id3(int depth, const char *id) {
char *url;
LinkTable *linktbl = ROOT_LINK_TBL;
switch (depth) {
/*
* Root table
*/
case 0:
url = sonic_gen_url_first_part("getArtists");
linktbl = sonic_url_to_LinkTable(url, XML_parser_id3_root, 0);
FREE(url); FREE(url);
return linktbl; break;
} /*
* Album table - get all the albums of an artist
static void XMLCALL */
XML_parser_id3_root(void *data, const char *elem, const char **attr) case 3:
{ url = sonic_getArtist_link(id);
if (!strcmp(elem, "error")) { linktbl = sonic_url_to_LinkTable(url, XML_parser_general, depth);
lprintf(error, "\n"); FREE(url);
for (int i = 0; attr[i]; i += 2) { break;
lprintf(error, "%s: %s\n", attr[i], attr[i + 1]); /*
} * Song table - get all the songs of an album
} */
case 4:
LinkTable *root_linktbl = (LinkTable *) data; url = sonic_getAlbum_link(id);
LinkTable *this_linktbl = NULL; linktbl = sonic_url_to_LinkTable(url, XML_parser_general, depth);
FREE(url);
/* break;
* Set the current linktbl, if we have more than head link. default:
*/ /*
if (root_linktbl->num > 1) { * We shouldn't reach here.
this_linktbl = */
root_linktbl->links[root_linktbl->num - 1]->next_table; lprintf(fatal, "case %d.\n", depth);
} break;
}
int id_set = 0;
int linkname_set = 0; return linktbl;
Link *link;
if (!strcmp(elem, "index")) {
/*
* Add a subdirectory
*/
link = CALLOC(1, sizeof(Link));
link->type = LINK_DIR;
for (int i = 0; attr[i]; i += 2) {
if (!strcmp("name", attr[i])) {
strncpy(link->linkname, attr[i + 1], MAX_FILENAME_LEN);
linkname_set = 1;
/*
* Allocate a new LinkTable
*/
link->next_table = LinkTable_alloc("/");
}
}
/*
* Make sure we don't add an empty directory
*/
if (linkname_set) {
LinkTable_add(root_linktbl, link);
} else {
FREE(link);
}
return;
} else if (!strcmp(elem, "artist")) {
link = CALLOC(1, sizeof(Link));
link->type = LINK_DIR;
/*
* The new table should be a level 3 album table
*/
link->sonic.depth = 3;
for (int i = 0; attr[i]; i += 2) {
if (!strcmp("name", attr[i])) {
strncpy(link->linkname, attr[i + 1], MAX_FILENAME_LEN);
linkname_set = 1;
continue;
}
if (!strcmp("id", attr[i])) {
link->sonic.id =
CALLOC(MAX_FILENAME_LEN + 1, sizeof(char));
strncpy(link->sonic.id, attr[i + 1], MAX_FILENAME_LEN);
id_set = 1;
continue;
}
}
/*
* Clean up if linkname is not set
*/
if (!linkname_set || !id_set) {
FREE(link);
return;
}
LinkTable_add(this_linktbl, link);
}
/*
* If we reach here, then this element does not contain directory structural
* information
*/
}
LinkTable *sonic_LinkTable_new_id3(int depth, const char *id)
{
char *url;
LinkTable *linktbl = ROOT_LINK_TBL;
switch (depth) {
/*
* Root table
*/
case 0:
url = sonic_gen_url_first_part("getArtists");
linktbl = sonic_url_to_LinkTable(url, XML_parser_id3_root, 0);
FREE(url);
break;
/*
* Album table - get all the albums of an artist
*/
case 3:
url = sonic_getArtist_link(id);
linktbl = sonic_url_to_LinkTable(url, XML_parser_general, depth);
FREE(url);
break;
/*
* Song table - get all the songs of an album
*/
case 4:
url = sonic_getAlbum_link(id);
linktbl = sonic_url_to_LinkTable(url, XML_parser_general, depth);
FREE(url);
break;
default:
/*
* We shouldn't reach here.
*/
lprintf(fatal, "case %d.\n", depth);
break;
}
return linktbl;
} }

View File

@ -6,22 +6,22 @@
*/ */
typedef struct { typedef struct {
/** /**
* \brief Sonic id field * \brief Sonic id field
* \details This is used to store the following: * \details This is used to store the following:
* - Arist ID * - Arist ID
* - Album ID * - Album ID
* - Song ID * - Song ID
* - Sub-directory ID (in the XML response, this is the ID on the "child" * - Sub-directory ID (in the XML response, this is the ID on the "child"
* element) * element)
*/ */
char *id; char *id;
/** /**
* \brief Sonic directory depth * \brief Sonic directory depth
* \details This is used exclusively in ID3 mode to store the depth of the * \details This is used exclusively in ID3 mode to store the depth of the
* current directory. * current directory.
*/ */
int depth; int depth;
} Sonic; } Sonic;
#include "link.h" #include "link.h"

View File

@ -12,7 +12,6 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
/** /**
* \brief Backtrace buffer size * \brief Backtrace buffer size
*/ */
@ -29,129 +28,112 @@
*/ */
#define SALT_LEN 36 #define SALT_LEN 36
char *path_append(const char *path, const char *filename) char *path_append(const char *path, const char *filename) {
{ int needs_separator = 0;
int needs_separator = 0; if ((path[strnlen(path, MAX_PATH_LEN) - 1] != '/') && (filename[0] != '/')) {
if ((path[strnlen(path, MAX_PATH_LEN) - 1] != '/') needs_separator = 1;
&& (filename[0] != '/')) { }
needs_separator = 1;
}
char *str; char *str;
size_t ul = strnlen(path, MAX_PATH_LEN); size_t ul = strnlen(path, MAX_PATH_LEN);
size_t sl = strnlen(filename, MAX_FILENAME_LEN); size_t sl = strnlen(filename, MAX_FILENAME_LEN);
str = CALLOC(ul + sl + needs_separator + 1, sizeof(char)); str = CALLOC(ul + sl + needs_separator + 1, sizeof(char));
strncpy(str, path, ul); strncpy(str, path, ul);
if (needs_separator) { if (needs_separator) {
str[ul] = '/'; str[ul] = '/';
} }
strncat(str, filename, sl); strncat(str, filename, sl);
return str; return str;
} }
int64_t round_div(int64_t a, int64_t b) int64_t round_div(int64_t a, int64_t b) { return (a + (b / 2)) / b; }
{
return (a + (b / 2)) / b; void PTHREAD_MUTEX_UNLOCK(pthread_mutex_t *x) {
int i;
i = pthread_mutex_unlock(x);
if (i) {
lprintf(fatal, "thread %x: %d, %s\n", pthread_self(), i, strerror(i));
}
} }
void PTHREAD_MUTEX_UNLOCK(pthread_mutex_t *x) void PTHREAD_MUTEX_LOCK(pthread_mutex_t *x) {
{ int i;
int i; i = pthread_mutex_lock(x);
i = pthread_mutex_unlock(x); if (i) {
if (i) { lprintf(fatal, "thread %x: %d, %s\n", pthread_self(), i, strerror(i));
lprintf(fatal, }
"thread %x: %d, %s\n", pthread_self(), i, strerror(i));
}
} }
void PTHREAD_MUTEX_LOCK(pthread_mutex_t *x) void exit_failure(void) {
{ int nptrs;
int i; void *buffer[BT_BUF_SIZE];
i = pthread_mutex_lock(x);
if (i) { nptrs = backtrace(buffer, BT_BUF_SIZE);
lprintf(fatal, fprintf(stderr, "\nOops! HTTPDirFS crashed! :(\n");
"thread %x: %d, %s\n", pthread_self(), i, strerror(i)); fprintf(stderr, "backtrace() returned the following %d addresses:\n", nptrs);
} backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO);
exit(EXIT_FAILURE);
} }
void exit_failure(void) void erase_string(FILE *file, size_t max_len, char *s) {
{ size_t l = strnlen(s, max_len);
int nptrs; for (size_t k = 0; k < l; k++) {
void *buffer[BT_BUF_SIZE]; fprintf(file, "\b");
}
nptrs = backtrace(buffer, BT_BUF_SIZE); for (size_t k = 0; k < l; k++) {
fprintf(stderr, "\nOops! HTTPDirFS crashed! :(\n"); fprintf(file, " ");
fprintf(stderr, "backtrace() returned the following %d addresses:\n", }
nptrs); for (size_t k = 0; k < l; k++) {
backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO); fprintf(file, "\b");
}
exit(EXIT_FAILURE);
} }
void erase_string(FILE *file, size_t max_len, char *s) char *generate_salt(void) {
{ char *out;
size_t l = strnlen(s, max_len); out = CALLOC(SALT_LEN + 1, sizeof(char));
for (size_t k = 0; k < l; k++) { uuid_t uu;
fprintf(file, "\b"); uuid_generate(uu);
} uuid_unparse(uu, out);
for (size_t k = 0; k < l; k++) { return out;
fprintf(file, " ");
}
for (size_t k = 0; k < l; k++) {
fprintf(file, "\b");
}
} }
char *generate_salt(void) char *generate_md5sum(const char *str) {
{ MD5_CTX c;
char *out; unsigned char md5[MD5_DIGEST_LENGTH];
out = CALLOC(SALT_LEN + 1, sizeof(char)); size_t len = strnlen(str, MAX_PATH_LEN);
uuid_t uu; char *out = CALLOC(MD5_HASH_LEN + 1, sizeof(char));
uuid_generate(uu);
uuid_unparse(uu, out); MD5_Init(&c);
return out; MD5_Update(&c, str, len);
MD5_Final(md5, &c);
for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
sprintf(out + 2 * i, "%02x", md5[i]);
}
return out;
} }
char *generate_md5sum(const char *str) void *CALLOC(size_t nmemb, size_t size) {
{ void *ptr = calloc(nmemb, size);
MD5_CTX c; if (!ptr) {
unsigned char md5[MD5_DIGEST_LENGTH]; lprintf(fatal, "%s!\n", strerror(errno));
size_t len = strnlen(str, MAX_PATH_LEN); }
char *out = CALLOC(MD5_HASH_LEN + 1, sizeof(char)); return ptr;
MD5_Init(&c);
MD5_Update(&c, str, len);
MD5_Final(md5, &c);
for (int i = 0; i < MD5_DIGEST_LENGTH; i++) {
sprintf(out + 2 * i, "%02x", md5[i]);
}
return out;
} }
void *CALLOC(size_t nmemb, size_t size) void FREE(void *ptr) {
{ if (ptr) {
void *ptr = calloc(nmemb, size); free(ptr);
if (!ptr) { } else {
lprintf(fatal, "%s!\n", strerror(errno)); lprintf(fatal, "attempted to free NULL ptr!\n");
} }
return ptr;
} }
void FREE(void *ptr) char *str_to_hex(char *s) {
{ char *hex = CALLOC(strnlen(s, MAX_PATH_LEN) * 2 + 1, sizeof(char));
if (ptr) { for (char *c = s, *h = hex; *c; c++, h += 2) {
free(ptr); sprintf(h, "%x", *c);
} else { }
lprintf(fatal, "attempted to free NULL ptr!\n"); return hex;
}
}
char *str_to_hex(char *s)
{
char *hex = CALLOC(strnlen(s, MAX_PATH_LEN) * 2 + 1, sizeof(char));
for (char *c = s, *h = hex; *c; c++, h += 2) {
sprintf(h, "%x", *c);
}
return hex;
} }