2019-04-21 00:46:08 +02:00
|
|
|
#include "util.h"
|
|
|
|
|
2019-10-21 04:28:57 +02:00
|
|
|
#include <openssl/md5.h>
|
|
|
|
#include <uuid/uuid.h>
|
|
|
|
|
2019-09-03 15:47:12 +02:00
|
|
|
#include <execinfo.h>
|
|
|
|
#include <unistd.h>
|
2019-04-21 00:46:08 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2019-10-21 04:28:57 +02:00
|
|
|
#include <errno.h>
|
2019-04-21 00:46:08 +02:00
|
|
|
|
2019-10-23 22:04:25 +02:00
|
|
|
/**
|
|
|
|
* \brief Backtrace buffer size
|
|
|
|
*/
|
2019-09-03 20:29:37 +02:00
|
|
|
#define BT_BUF_SIZE 100
|
2019-10-23 22:04:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief The length of a MD5SUM string
|
|
|
|
*/
|
2019-10-21 04:28:57 +02:00
|
|
|
#define MD5_HASH_LEN 32
|
2019-10-23 22:04:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief The length of the salt
|
|
|
|
* \details This is basically the length of a UUID
|
|
|
|
*/
|
2019-10-21 04:28:57 +02:00
|
|
|
#define SALT_LEN 36
|
2019-09-03 15:47:12 +02:00
|
|
|
|
2019-10-23 22:04:25 +02:00
|
|
|
/**
|
|
|
|
* \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;
|
|
|
|
|
|
|
|
/*--------------- Cache related ---------------*/
|
|
|
|
CONFIG.cache_enabled = 0;
|
|
|
|
|
|
|
|
CONFIG.cache_dir = NULL;
|
|
|
|
|
|
|
|
CONFIG.data_blksz = DEFAULT_DATA_BLKSZ;
|
|
|
|
|
|
|
|
CONFIG.max_segbc = DEFAULT_MAX_SEGBC;
|
|
|
|
|
2019-10-23 22:36:08 +02:00
|
|
|
/*-------------- Sonic related -------------*/
|
2019-10-23 22:04:25 +02:00
|
|
|
CONFIG.sonic_mode = 0;
|
|
|
|
|
|
|
|
CONFIG.sonic_username = NULL;
|
|
|
|
|
|
|
|
CONFIG.sonic_password = NULL;
|
2019-10-25 18:06:09 +02:00
|
|
|
|
|
|
|
CONFIG.sonic_id3 = 0;
|
2019-10-23 22:04:25 +02:00
|
|
|
}
|
|
|
|
|
2019-04-26 08:39:45 +02:00
|
|
|
char *path_append(const char *path, const char *filename)
|
2019-04-21 00:46:08 +02:00
|
|
|
{
|
2019-04-26 08:39:45 +02:00
|
|
|
int needs_separator = 0;
|
2019-04-30 00:40:21 +02:00
|
|
|
if ((path[strnlen(path, MAX_PATH_LEN)-1] != '/') && (filename[0] != '/')) {
|
2019-04-26 08:39:45 +02:00
|
|
|
needs_separator = 1;
|
2019-04-21 12:10:18 +02:00
|
|
|
}
|
2019-04-26 08:39:45 +02:00
|
|
|
|
|
|
|
char *str;
|
|
|
|
size_t ul = strnlen(path, MAX_PATH_LEN);
|
|
|
|
size_t sl = strnlen(filename, MAX_FILENAME_LEN);
|
2019-10-21 04:28:57 +02:00
|
|
|
str = CALLOC(ul + sl + needs_separator + 1, sizeof(char));
|
2019-04-26 08:39:45 +02:00
|
|
|
strncpy(str, path, ul);
|
|
|
|
if (needs_separator) {
|
|
|
|
str[ul] = '/';
|
|
|
|
}
|
|
|
|
strncat(str, filename, sl);
|
|
|
|
return str;
|
2019-04-21 00:46:08 +02:00
|
|
|
}
|
2019-04-25 00:58:26 +02:00
|
|
|
|
|
|
|
int64_t round_div(int64_t a, int64_t b)
|
|
|
|
{
|
|
|
|
return (a + (b / 2)) / b;
|
|
|
|
}
|
2019-09-01 22:36:58 +02:00
|
|
|
|
|
|
|
void PTHREAD_MUTEX_UNLOCK(pthread_mutex_t *x)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
i = pthread_mutex_unlock(x);
|
|
|
|
if (i) {
|
2019-09-03 15:59:30 +02:00
|
|
|
fprintf(stderr, "thread %lu: pthread_mutex_unlock() failed, %d, %s\n",
|
|
|
|
pthread_self(), i, strerror(i));
|
2019-09-03 15:47:12 +02:00
|
|
|
exit_failure();
|
2019-09-01 22:36:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PTHREAD_MUTEX_LOCK(pthread_mutex_t *x)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
i = pthread_mutex_lock(x);
|
|
|
|
if (i) {
|
2019-09-03 15:59:30 +02:00
|
|
|
fprintf(stderr, "thread %lu: pthread_mutex_lock() failed, %d, %s\n",
|
|
|
|
pthread_self(), i, strerror(i));
|
2019-09-03 15:47:12 +02:00
|
|
|
exit_failure();
|
2019-09-01 22:36:58 +02:00
|
|
|
}
|
|
|
|
}
|
2019-09-03 15:47:12 +02:00
|
|
|
|
2019-09-03 23:37:27 +02:00
|
|
|
void exit_failure(void)
|
2019-09-03 15:47:12 +02:00
|
|
|
{
|
|
|
|
int nptrs;
|
|
|
|
void *buffer[BT_BUF_SIZE];
|
|
|
|
|
|
|
|
nptrs = backtrace(buffer, BT_BUF_SIZE);
|
|
|
|
fprintf(stderr, "\nOops! HTTPDirFS crashed! :(\n");
|
|
|
|
fprintf(stderr, "backtrace() returned the following %d addresses:\n",
|
|
|
|
nptrs);
|
|
|
|
backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO);
|
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2019-09-03 23:11:23 +02:00
|
|
|
|
|
|
|
void erase_string(FILE *file, size_t max_len, char *s)
|
|
|
|
{
|
|
|
|
size_t l = strnlen(s, max_len);
|
|
|
|
for (size_t k = 0; k < l; k++) {
|
|
|
|
fprintf(file, "\b");
|
|
|
|
}
|
|
|
|
for (size_t k = 0; k < l; k++) {
|
|
|
|
fprintf(file, " ");
|
|
|
|
}
|
|
|
|
for (size_t k = 0; k < l; k++) {
|
|
|
|
fprintf(file, "\b");
|
|
|
|
}
|
|
|
|
}
|
2019-10-21 04:28:57 +02:00
|
|
|
|
2019-10-24 04:15:30 +02:00
|
|
|
char *generate_salt(void)
|
2019-10-21 04:28:57 +02:00
|
|
|
{
|
|
|
|
char *out;
|
|
|
|
out = CALLOC(SALT_LEN + 1, sizeof(char));
|
|
|
|
uuid_t uu;
|
|
|
|
uuid_generate(uu);
|
|
|
|
uuid_unparse(uu, out);
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *generate_md5sum(const char *str)
|
|
|
|
{
|
|
|
|
MD5_CTX c;
|
|
|
|
unsigned char md5[MD5_DIGEST_LENGTH];
|
|
|
|
size_t len = strnlen(str, MAX_PATH_LEN);
|
|
|
|
char *out = CALLOC(MD5_HASH_LEN + 1, sizeof(char));
|
|
|
|
|
|
|
|
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 *ptr = calloc(nmemb, size);
|
|
|
|
if (!ptr) {
|
|
|
|
fprintf(stderr, "calloc() failed, %s!\n", strerror(errno));
|
|
|
|
exit_failure();
|
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
2019-10-23 22:04:25 +02:00
|
|
|
|