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

View File

@ -37,23 +37,20 @@ static char *DATA_DIR;
/**
* \brief Calculate cache system directory path
*/
static char *CacheSystem_calc_dir(const char *url)
{
static char *CacheSystem_calc_dir(const char *url) {
char *xdg_cache_home = getenv("XDG_CACHE_HOME");
if (!xdg_cache_home) {
char *home = getenv("HOME");
char *xdg_cache_home_default = "/.cache";
xdg_cache_home = path_append(home, xdg_cache_home_default);
}
if (mkdir
(xdg_cache_home, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
&& (errno != EEXIST)) {
if (mkdir(xdg_cache_home, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) &&
(errno != EEXIST)) {
lprintf(fatal, "mkdir(): %s\n", strerror(errno));
}
char *cache_dir_root = path_append(xdg_cache_home, "/httpdirfs/");
if (mkdir
(cache_dir_root, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
&& (errno != EEXIST)) {
if (mkdir(cache_dir_root, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) &&
(errno != EEXIST)) {
lprintf(fatal, "mkdir(): %s\n", strerror(errno));
}
@ -76,8 +73,8 @@ static char *CacheSystem_calc_dir(const char *url)
CURL *c = curl_easy_init();
char *escaped_url = curl_easy_escape(c, url, 0);
char *full_path = path_append(cache_dir_root, escaped_url);
if (mkdir(full_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
&& (errno != EEXIST)) {
if (mkdir(full_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) &&
(errno != EEXIST)) {
lprintf(fatal, "mkdir(): %s\n", strerror(errno));
}
FREE(fn);
@ -87,10 +84,8 @@ static char *CacheSystem_calc_dir(const char *url)
return full_path;
}
void CacheSystem_init(const char *path, int url_supplied)
{
lprintf(cache_lock_debug,
"thread %x: initialise cf_lock;\n", pthread_self());
void CacheSystem_init(const char *path, int url_supplied) {
lprintf(cache_lock_debug, "thread %x: initialise cf_lock;\n", pthread_self());
if (pthread_mutex_init(&cf_lock, NULL)) {
lprintf(fatal, "cf_lock initialisation failed!\n");
}
@ -106,13 +101,13 @@ void CacheSystem_init(const char *path, int url_supplied)
/*
* Check if directories exist, if not, create them
*/
if (mkdir(META_DIR, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
&& (errno != EEXIST)) {
if (mkdir(META_DIR, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) &&
(errno != EEXIST)) {
lprintf(fatal, "mkdir(): %s\n", strerror(errno));
}
if (mkdir(DATA_DIR, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
&& (errno != EEXIST)) {
if (mkdir(DATA_DIR, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) &&
(errno != EEXIST)) {
lprintf(fatal, "mkdir(): %s\n", strerror(errno));
}
@ -122,9 +117,8 @@ void CacheSystem_init(const char *path, int url_supplied)
* Create "rest" sub-directory for META_DIR
*/
sonic_path = path_append(META_DIR, "rest/");
if (mkdir
(sonic_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
&& (errno != EEXIST)) {
if (mkdir(sonic_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) &&
(errno != EEXIST)) {
lprintf(fatal, "mkdir(): %s\n", strerror(errno));
}
FREE(sonic_path);
@ -133,9 +127,8 @@ void CacheSystem_init(const char *path, int url_supplied)
* Create "rest" sub-directory for DATA_DIR
*/
sonic_path = path_append(DATA_DIR, "rest/");
if (mkdir
(sonic_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH)
&& (errno != EEXIST)) {
if (mkdir(sonic_path, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) &&
(errno != EEXIST)) {
lprintf(fatal, "mkdir(): %s\n", strerror(errno));
}
FREE(sonic_path);
@ -148,8 +141,7 @@ void CacheSystem_init(const char *path, int url_supplied)
* \brief read a metadata file
* \return 0 on success, errno on error.
*/
static int Meta_read(Cache *cf)
{
static int Meta_read(Cache *cf) {
FILE *fp = cf->mfp;
rewind(fp);
@ -166,16 +158,14 @@ static int Meta_read(Cache *cf)
if (sizeof(long) != fread(&cf->time, sizeof(long), 1, fp) ||
sizeof(off_t) != fread(&cf->content_length, sizeof(off_t), 1, fp) ||
sizeof(int) != fread(&cf->blksz, sizeof(int), 1, fp) ||
sizeof(long) != fread(&cf->segbc, sizeof(long), 1, fp) ||
ferror(fp)) {
sizeof(long) != fread(&cf->segbc, sizeof(long), 1, fp) || ferror(fp)) {
lprintf(error, "error reading core metadata!\n");
return EIO;
}
/* These things really should not be zero!!! */
if (!cf->content_length || !cf->blksz || !cf->segbc) {
lprintf(error,
"corruption: content_length: %ld, blksz: %d, segbc: %ld\n",
lprintf(error, "corruption: content_length: %ld, blksz: %d, segbc: %ld\n",
cf->content_length, cf->blksz, cf->segbc);
return EBADMSG;
}
@ -232,8 +222,7 @@ file!\n");
* - -1 on error,
* - 0 on success
*/
static int Meta_write(Cache *cf)
{
static int Meta_write(Cache *cf) {
FILE *fp = cf->mfp;
rewind(fp);
@ -275,8 +264,7 @@ static int Meta_write(Cache *cf)
* \details We use sparse creation here
* \return exit on failure
*/
static void Data_create(Cache *cf)
{
static void Data_create(Cache *cf) {
int fd;
int mode;
@ -299,8 +287,7 @@ static void Data_create(Cache *cf)
* \brief obtain the data file size
* \return file size on success, -1 on error
*/
static long Data_size(const char *fn)
{
static long Data_size(const char *fn) {
char *datafn = path_append(DATA_DIR, fn);
struct stat st;
int s = stat(datafn, &st);
@ -322,15 +309,13 @@ static long Data_size(const char *fn)
* - negative values on error,
* - otherwise, the number of bytes read.
*/
static long Data_read(Cache *cf, uint8_t *buf, off_t len, off_t offset)
{
static long Data_read(Cache *cf, uint8_t *buf, off_t len, off_t offset) {
if (len == 0) {
lprintf(error, "requested to read 0 byte!\n");
return -EINVAL;
}
lprintf(cache_lock_debug,
"thread %x: locking seek_lock;\n", pthread_self());
lprintf(cache_lock_debug, "thread %x: locking seek_lock;\n", pthread_self());
PTHREAD_MUTEX_LOCK(&cf->seek_lock);
long byte_read = 0;
@ -359,8 +344,7 @@ static long Data_read(Cache *cf, uint8_t *buf, off_t len, off_t offset)
byte_read = fread(buf, sizeof(uint8_t), len, cf->dfp);
if (byte_read != len) {
lprintf(debug,
"fread(): requested %ld, returned %ld!\n", len, byte_read);
lprintf(debug, "fread(): requested %ld, returned %ld!\n", len, byte_read);
if (feof(cf->dfp)) {
/*
* reached EOF
@ -377,8 +361,8 @@ static long Data_read(Cache *cf, uint8_t *buf, off_t len, off_t offset)
end:
lprintf(cache_lock_debug,
"thread %x: unlocking seek_lock;\n", pthread_self());
lprintf(cache_lock_debug, "thread %x: unlocking seek_lock;\n",
pthread_self());
PTHREAD_MUTEX_UNLOCK(&cf->seek_lock);
return byte_read;
}
@ -393,9 +377,7 @@ end:
* - -1 when the data file does not exist
* - otherwise, the number of bytes written.
*/
static long Data_write(Cache *cf, const uint8_t *buf, off_t len,
off_t offset)
{
static long Data_write(Cache *cf, const uint8_t *buf, off_t len, off_t offset) {
if (len == 0) {
/*
* We should permit empty files
@ -403,8 +385,7 @@ static long Data_write(Cache *cf, const uint8_t *buf, off_t len,
return 0;
}
lprintf(cache_lock_debug,
"thread %x: locking seek_lock;\n", pthread_self());
lprintf(cache_lock_debug, "thread %x: locking seek_lock;\n", pthread_self());
PTHREAD_MUTEX_LOCK(&cf->seek_lock);
long byte_written = 0;
@ -421,9 +402,8 @@ static long Data_write(Cache *cf, const uint8_t *buf, off_t len,
byte_written = fwrite(buf, sizeof(uint8_t), len, cf->dfp);
if (byte_written != len) {
lprintf(error,
"fwrite(): requested %ld, returned %ld!\n",
len, byte_written);
lprintf(error, "fwrite(): requested %ld, returned %ld!\n", len,
byte_written);
}
if (ferror(cf->dfp)) {
@ -434,14 +414,13 @@ static long Data_write(Cache *cf, const uint8_t *buf, off_t len,
}
end:
lprintf(cache_lock_debug,
"thread %x: unlocking seek_lock;\n", pthread_self());
lprintf(cache_lock_debug, "thread %x: unlocking seek_lock;\n",
pthread_self());
PTHREAD_MUTEX_UNLOCK(&cf->seek_lock);
return byte_written;
}
int CacheDir_create(const char *dirn)
{
int CacheDir_create(const char *dirn) {
char *metadirn = path_append(META_DIR, dirn);
char *datadirn = path_append(DATA_DIR, dirn);
int i;
@ -451,8 +430,7 @@ int CacheDir_create(const char *dirn)
lprintf(fatal, "mkdir(): %s\n", strerror(errno));
}
i |= -mkdir(datadirn,
S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) << 1;
i |= -mkdir(datadirn, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) << 1;
if (i && (errno != EEXIST)) {
lprintf(fatal, "mkdir(): %s\n", strerror(errno));
}
@ -464,8 +442,7 @@ int CacheDir_create(const char *dirn)
/**
* \brief Allocate a new cache data structure
*/
static Cache *Cache_alloc()
{
static Cache *Cache_alloc() {
Cache *cf = CALLOC(1, sizeof(Cache));
if (pthread_mutex_init(&cf->seek_lock, NULL)) {
@ -495,8 +472,7 @@ static Cache *Cache_alloc()
/**
* \brief free a cache data structure
*/
static void Cache_free(Cache *cf)
{
static void Cache_free(Cache *cf) {
if (pthread_mutex_destroy(&cf->seek_lock)) {
lprintf(fatal, "could not destroy seek_lock!\n");
}
@ -538,8 +514,7 @@ static void Cache_free(Cache *cf)
* - 0, if both metadata and cache file exist
* - -1, otherwise
*/
static int Cache_exist(const char *fn)
{
static int Cache_exist(const char *fn) {
char *metafn = path_append(META_DIR, fn);
char *datafn = path_append(DATA_DIR, fn);
/*
@ -571,8 +546,7 @@ static int Cache_exist(const char *fn)
/**
* \brief delete a cache file set
*/
void Cache_delete(const char *fn)
{
void Cache_delete(const char *fn) {
if (CONFIG.mode == SONIC) {
Link *link = path_to_Link(fn);
fn = link->sonic.id;
@ -601,8 +575,7 @@ void Cache_delete(const char *fn)
* - 0 on success
* - -1 on failure, with appropriate errno set.
*/
static int Data_open(Cache *cf)
{
static int Data_open(Cache *cf) {
char *datafn = path_append(DATA_DIR, cf->path);
cf->dfp = fopen(datafn, "r+");
if (!cf->dfp) {
@ -623,8 +596,7 @@ static int Data_open(Cache *cf)
* - 0 on success
* - -1 on failure, with appropriate errno set.
*/
static int Meta_open(Cache *cf)
{
static int Meta_open(Cache *cf) {
char *metafn = path_append(META_DIR, cf->path);
cf->mfp = fopen(metafn, "r+");
if (!cf->mfp) {
@ -643,8 +615,7 @@ static int Meta_open(Cache *cf)
* \brief Create a metafile
* \return exit on error
*/
static void Meta_create(Cache *cf)
{
static void Meta_create(Cache *cf) {
char *metafn = path_append(META_DIR, cf->path);
cf->mfp = fopen(metafn, "w");
if (!cf->mfp) {
@ -654,22 +625,18 @@ static void Meta_create(Cache *cf)
lprintf(fatal, "fopen(%s): %s\n", metafn, strerror(errno));
}
if (fclose(cf->mfp)) {
lprintf(error,
"cannot close metadata after creation: %s.\n",
lprintf(error, "cannot close metadata after creation: %s.\n",
strerror(errno));
}
FREE(metafn);
}
int Cache_create(const char *path)
{
int Cache_create(const char *path) {
Link *this_link = path_to_Link(path);
char *fn = "__UNINITIALISED__";
if (CONFIG.mode == NORMAL) {
fn = curl_easy_unescape(NULL,
this_link->f_url + ROOT_LINK_OFFSET, 0,
NULL);
fn = curl_easy_unescape(NULL, this_link->f_url + ROOT_LINK_OFFSET, 0, NULL);
} else if (CONFIG.mode == SINGLE) {
fn = curl_easy_unescape(NULL, this_link->linkname, 0, NULL);
} else if (CONFIG.mode == SONIC) {
@ -699,9 +666,7 @@ int Cache_create(const char *path)
}
if (fclose(cf->mfp)) {
lprintf(error,
"cannot close metadata after write, %s.\n",
strerror(errno));
lprintf(error, "cannot close metadata after write, %s.\n", strerror(errno));
}
Data_create(cf);
@ -717,8 +682,7 @@ int Cache_create(const char *path)
return res;
}
Cache *Cache_open(const char *fn)
{
Cache *Cache_open(const char *fn) {
/*
* Obtain the link structure memory pointer
*/
@ -730,15 +694,14 @@ Cache *Cache_open(const char *fn)
return NULL;
}
lprintf(cache_lock_debug,
"thread %x: locking cf_lock;\n", pthread_self());
lprintf(cache_lock_debug, "thread %x: locking cf_lock;\n", pthread_self());
PTHREAD_MUTEX_LOCK(&cf_lock);
if (link->cache_ptr) {
link->cache_ptr->cache_opened++;
lprintf(cache_lock_debug,
"thread %x: unlocking cf_lock;\n", pthread_self());
lprintf(cache_lock_debug, "thread %x: unlocking cf_lock;\n",
pthread_self());
PTHREAD_MUTEX_UNLOCK(&cf_lock);
return link->cache_ptr;
}
@ -749,16 +712,16 @@ Cache *Cache_open(const char *fn)
if (CONFIG.mode == NORMAL || CONFIG.mode == SINGLE) {
if (Cache_exist(fn)) {
lprintf(cache_lock_debug,
"thread %x: unlocking cf_lock;\n", pthread_self());
lprintf(cache_lock_debug, "thread %x: unlocking cf_lock;\n",
pthread_self());
PTHREAD_MUTEX_UNLOCK(&cf_lock);
return NULL;
}
} else if (CONFIG.mode == SONIC) {
if (Cache_exist(link->sonic.id)) {
lprintf(cache_lock_debug,
"thread %x: unlocking cf_lock;\n", pthread_self());
lprintf(cache_lock_debug, "thread %x: unlocking cf_lock;\n",
pthread_self());
PTHREAD_MUTEX_UNLOCK(&cf_lock);
return NULL;
}
@ -795,8 +758,8 @@ Cache *Cache_open(const char *fn)
Cache_free(cf);
lprintf(error, "cannot open metadata file %s.\n", fn);
lprintf(cache_lock_debug,
"thread %x: unlocking cf_lock;\n", pthread_self());
lprintf(cache_lock_debug, "thread %x: unlocking cf_lock;\n",
pthread_self());
PTHREAD_MUTEX_UNLOCK(&cf_lock);
return NULL;
}
@ -808,8 +771,8 @@ Cache *Cache_open(const char *fn)
Cache_free(cf);
lprintf(error, "metadata error: %s.\n", fn);
lprintf(cache_lock_debug,
"thread %x: unlocking cf_lock;\n", pthread_self());
lprintf(cache_lock_debug, "thread %x: unlocking cf_lock;\n",
pthread_self());
PTHREAD_MUTEX_UNLOCK(&cf_lock);
return NULL;
}
@ -821,12 +784,12 @@ Cache *Cache_open(const char *fn)
*/
if (cf->content_length > Data_size(fn)) {
lprintf(error, "metadata inconsistency %s, \
cf->content_length: %ld, Data_size(fn): %ld.\n", fn, cf->content_length,
Data_size(fn));
cf->content_length: %ld, Data_size(fn): %ld.\n",
fn, cf->content_length, Data_size(fn));
Cache_free(cf);
lprintf(cache_lock_debug,
"thread %x: unlocking cf_lock;\n", pthread_self());
lprintf(cache_lock_debug, "thread %x: unlocking cf_lock;\n",
pthread_self());
PTHREAD_MUTEX_UNLOCK(&cf_lock);
return NULL;
}
@ -838,8 +801,8 @@ cf->content_length: %ld, Data_size(fn): %ld.\n", fn, cf->content_length,
lprintf(warning, "outdated cache file: %s.\n", fn);
Cache_free(cf);
lprintf(cache_lock_debug,
"thread %x: unlocking cf_lock;\n", pthread_self());
lprintf(cache_lock_debug, "thread %x: unlocking cf_lock;\n",
pthread_self());
PTHREAD_MUTEX_UNLOCK(&cf_lock);
return NULL;
}
@ -848,8 +811,8 @@ cf->content_length: %ld, Data_size(fn): %ld.\n", fn, cf->content_length,
Cache_free(cf);
lprintf(error, "cannot open data file %s.\n", fn);
lprintf(cache_lock_debug,
"thread %x: unlocking cf_lock;\n", pthread_self());
lprintf(cache_lock_debug, "thread %x: unlocking cf_lock;\n",
pthread_self());
PTHREAD_MUTEX_UNLOCK(&cf_lock);
return NULL;
}
@ -860,24 +823,21 @@ cf->content_length: %ld, Data_size(fn): %ld.\n", fn, cf->content_length,
*/
cf->link->cache_ptr = cf;
lprintf(cache_lock_debug,
"thread %x: unlocking cf_lock;\n", pthread_self());
lprintf(cache_lock_debug, "thread %x: unlocking cf_lock;\n", pthread_self());
PTHREAD_MUTEX_UNLOCK(&cf_lock);
return cf;
}
void Cache_close(Cache *cf)
{
lprintf(cache_lock_debug,
"thread %x: locking cf_lock;\n", pthread_self());
void Cache_close(Cache *cf) {
lprintf(cache_lock_debug, "thread %x: locking cf_lock;\n", pthread_self());
PTHREAD_MUTEX_LOCK(&cf_lock);
cf->cache_opened--;
if (cf->cache_opened > 0) {
lprintf(cache_lock_debug,
"thread %x: unlocking cf_lock;\n", pthread_self());
lprintf(cache_lock_debug, "thread %x: unlocking cf_lock;\n",
pthread_self());
PTHREAD_MUTEX_UNLOCK(&cf_lock);
return;
}
@ -896,8 +856,7 @@ void Cache_close(Cache *cf)
cf->link->cache_ptr = NULL;
lprintf(cache_lock_debug,
"thread %x: unlocking cf_lock;\n", pthread_self());
lprintf(cache_lock_debug, "thread %x: unlocking cf_lock;\n", pthread_self());
PTHREAD_MUTEX_UNLOCK(&cf_lock);
return Cache_free(cf);
}
@ -906,8 +865,7 @@ void Cache_close(Cache *cf)
* \brief Check if a segment exists.
* \return 1 if the segment exists
*/
static int Seg_exist(Cache *cf, off_t offset)
{
static int Seg_exist(Cache *cf, off_t offset) {
off_t byte = offset / cf->blksz;
return cf->seg[byte];
}
@ -919,8 +877,7 @@ static int Seg_exist(Cache *cf, off_t offset)
* \param[in] i 1 for exist, 0 for doesn't exist
* \note Call this after downloading a segment.
*/
static void Seg_set(Cache *cf, off_t offset, int i)
{
static void Seg_set(Cache *cf, off_t offset, int i) {
off_t byte = offset / cf->blksz;
cf->seg[byte] = i;
}
@ -931,41 +888,38 @@ static void Seg_set(Cache *cf, off_t offset, int i)
* segment, we can spawn a pthread using this function to download the next
* segment.
*/
static void *Cache_bgdl(void *arg)
{
Cache *cf = (Cache *) arg;
static void *Cache_bgdl(void *arg) {
Cache *cf = (Cache *)arg;
lprintf(cache_lock_debug, "thread %x: locking w_lock;\n",
pthread_self());
lprintf(cache_lock_debug, "thread %x: locking w_lock;\n", pthread_self());
PTHREAD_MUTEX_LOCK(&cf->w_lock);
uint8_t *recv_buf = CALLOC(cf->blksz, sizeof(uint8_t));
lprintf(debug, "thread %x spawned.\n ", pthread_self());
long recv = Link_download(cf->link, (char *) recv_buf, cf->blksz,
cf->next_dl_offset);
long recv =
Link_download(cf->link, (char *)recv_buf, cf->blksz, cf->next_dl_offset);
if (recv < 0) {
lprintf(error, "thread %x received %ld bytes, \
which doesn't make sense\n", pthread_self(), recv);
which doesn't make sense\n",
pthread_self(), recv);
}
if ((recv == cf->blksz) ||
(cf->next_dl_offset ==
(cf->content_length / cf->blksz * cf->blksz))) {
(cf->next_dl_offset == (cf->content_length / cf->blksz * cf->blksz))) {
Data_write(cf, recv_buf, recv, cf->next_dl_offset);
Seg_set(cf, cf->next_dl_offset, 1);
} else {
lprintf(error, "received %ld rather than %ld, possible network \
error.\n", recv, cf->blksz);
error.\n",
recv, cf->blksz);
}
FREE(recv_buf);
lprintf(cache_lock_debug,
"thread %x: unlocking bgt_lock;\n", pthread_self());
lprintf(cache_lock_debug, "thread %x: unlocking bgt_lock;\n", pthread_self());
PTHREAD_MUTEX_UNLOCK(&cf->bgt_lock);
lprintf(cache_lock_debug,
"thread %x: unlocking w_lock;\n", pthread_self());
lprintf(cache_lock_debug, "thread %x: unlocking w_lock;\n", pthread_self());
PTHREAD_MUTEX_UNLOCK(&cf->w_lock);
if (pthread_detach(pthread_self())) {
@ -974,10 +928,8 @@ error.\n", recv, cf->blksz);
pthread_exit(NULL);
}
long
Cache_read(Cache *cf, char *const output_buf, const off_t len,
const off_t offset_start)
{
long Cache_read(Cache *cf, char *const output_buf, const off_t len,
const off_t offset_start) {
long send;
/*
@ -989,15 +941,14 @@ Cache_read(Cache *cf, char *const output_buf, const off_t len,
* ------------- Check if the segment already exists --------------
*/
if (Seg_exist(cf, dl_offset)) {
send = Data_read(cf, (uint8_t *) output_buf, len, offset_start);
send = Data_read(cf, (uint8_t *)output_buf, len, offset_start);
goto bgdl;
} else {
/*
* Wait for any other download thread to finish
*/
lprintf(cache_lock_debug,
"thread %ld: locking w_lock;\n", pthread_self());
lprintf(cache_lock_debug, "thread %ld: locking w_lock;\n", pthread_self());
PTHREAD_MUTEX_LOCK(&cf->w_lock);
if (Seg_exist(cf, dl_offset)) {
@ -1005,11 +956,10 @@ Cache_read(Cache *cf, char *const output_buf, const off_t len,
* The segment now exists - it was downloaded by another
* download thread. Send it off and unlock the I/O
*/
send =
Data_read(cf, (uint8_t *) output_buf, len, offset_start);
send = Data_read(cf, (uint8_t *)output_buf, len, offset_start);
lprintf(cache_lock_debug,
"thread %x: unlocking w_lock;\n", pthread_self());
lprintf(cache_lock_debug, "thread %x: unlocking w_lock;\n",
pthread_self());
PTHREAD_MUTEX_UNLOCK(&cf->w_lock);
goto bgdl;
@ -1022,11 +972,11 @@ Cache_read(Cache *cf, char *const output_buf, const off_t len,
uint8_t *recv_buf = CALLOC(cf->blksz, sizeof(uint8_t));
lprintf(debug, "thread %x: spawned.\n ", pthread_self());
long recv = Link_download(cf->link, (char *) recv_buf, cf->blksz,
dl_offset);
long recv = Link_download(cf->link, (char *)recv_buf, cf->blksz, dl_offset);
if (recv < 0) {
lprintf(error, "thread %x received %ld bytes, \
which doesn't make sense\n", pthread_self(), recv);
which doesn't make sense\n",
pthread_self(), recv);
}
/*
* check if we have received enough data, write it to the disk
@ -1040,33 +990,31 @@ which doesn't make sense\n", pthread_self(), recv);
Seg_set(cf, dl_offset, 1);
} else {
lprintf(error, "received %ld rather than %ld, possible network \
error.\n", recv, cf->blksz);
error.\n",
recv, cf->blksz);
}
FREE(recv_buf);
send = Data_read(cf, (uint8_t *) output_buf, len, offset_start);
send = Data_read(cf, (uint8_t *)output_buf, len, offset_start);
lprintf(cache_lock_debug,
"thread %x: unlocking w_lock;\n", pthread_self());
lprintf(cache_lock_debug, "thread %x: unlocking w_lock;\n", pthread_self());
PTHREAD_MUTEX_UNLOCK(&cf->w_lock);
/*
* ----------- Download the next segment in background -----------------
*/
bgdl: {
}
bgdl: {}
off_t next_dl_offset = round_div(offset_start, cf->blksz) * cf->blksz;
if ((next_dl_offset > dl_offset) && !Seg_exist(cf, next_dl_offset)
&& next_dl_offset < cf->content_length) {
if ((next_dl_offset > dl_offset) && !Seg_exist(cf, next_dl_offset) &&
next_dl_offset < cf->content_length) {
/*
* Stop the spawning of multiple background pthreads
*/
if (!pthread_mutex_trylock(&cf->bgt_lock)) {
lprintf(cache_lock_debug,
"thread %x: trylocked bgt_lock;\n", pthread_self());
lprintf(cache_lock_debug, "thread %x: trylocked bgt_lock;\n",
pthread_self());
cf->next_dl_offset = next_dl_offset;
if (pthread_create(&cf->bgt, NULL, Cache_bgdl, cf)) {
lprintf(error,
"Error creating background download thread\n");
lprintf(error, "Error creating background download thread\n");
}
}
}

View File

@ -14,9 +14,9 @@ typedef struct Cache Cache;
#include "link.h"
#include "network.h"
#include <stdio.h>
#include <stdint.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
/**
* \brief Type definition for a cache segment

View File

@ -11,14 +11,14 @@
* \brief Data file block size
* \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
* \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
#define DEFAULT_MAX_SEGBC 128 * 1024
ConfigStruct CONFIG;
@ -26,8 +26,7 @@ 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)
{
void Config_init(void) {
CONFIG.mode = NORMAL;
CONFIG.log_type = log_level_init();

View File

@ -13,26 +13,23 @@
#include <string.h>
#include <unistd.h>
static void *fs_init(struct fuse_conn_info *conn)
{
(void) conn;
static void *fs_init(struct fuse_conn_info *conn) {
(void)conn;
return NULL;
}
/** \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);
(void) path;
(void)path;
if (CACHE_SYSTEM_INIT) {
Cache_close((Cache *) fi->fh);
Cache_close((Cache *)fi->fh);
}
return 0;
}
/** \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;
memset(stbuf, 0, sizeof(struct stat));
@ -44,7 +41,7 @@ static int fs_getattr(const char *path, struct stat *stbuf)
if (!link) {
return -ENOENT;
}
struct timespec spec = { 0 };
struct timespec spec = {0};
spec.tv_sec = link->time;
#if defined(__APPLE__) && defined(__MACH__)
stbuf->st_mtimespec = spec;
@ -74,13 +71,11 @@ static int fs_getattr(const char *path, struct stat *stbuf)
}
/** \brief read a file */
static int
fs_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
static int fs_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi) {
long received;
if (CACHE_SYSTEM_INIT) {
received = Cache_read((Cache *) fi->fh, buf, size, offset);
received = Cache_read((Cache *)fi->fh, buf, size, offset);
} else {
received = path_download(path, buf, size, offset);
}
@ -88,8 +83,7 @@ fs_read(const char *path, char *buf, size_t size, off_t offset,
}
/** \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);
Link *link = path_to_Link(path);
if (!link) {
@ -99,7 +93,7 @@ static int fs_open(const char *path, struct fuse_file_info *fi)
return -EROFS;
}
if (CACHE_SYSTEM_INIT) {
fi->fh = (uint64_t) Cache_open(path);
fi->fh = (uint64_t)Cache_open(path);
if (!fi->fh) {
/*
* The link clearly exists, the cache cannot be opened, attempt
@ -107,7 +101,7 @@ static int fs_open(const char *path, struct fuse_file_info *fi)
*/
Cache_delete(path);
Cache_create(path);
fi->fh = (uint64_t) Cache_open(path);
fi->fh = (uint64_t)Cache_open(path);
/*
* The cache definitely cannot be opened for some reason.
*/
@ -130,12 +124,10 @@ static int fs_open(const char *path, struct fuse_file_info *fi)
* generate the LinkTables for previous level directories. We might
* as well maintain our own tree structure.
*/
static int
fs_readdir(const char *path, void *buf, fuse_fill_dir_t dir_add,
off_t offset, struct fuse_file_info *fi)
{
(void) offset;
(void) fi;
static int fs_readdir(const char *path, void *buf, fuse_fill_dir_t dir_add,
off_t offset, struct fuse_file_info *fi) {
(void)offset;
(void)fi;
LinkTable *linktbl;
linktbl = path_to_Link_LinkTable_new(path);
@ -143,7 +135,6 @@ fs_readdir(const char *path, void *buf, fuse_fill_dir_t dir_add,
return -ENOENT;
}
/*
* start adding the links
*/
@ -160,16 +151,13 @@ fs_readdir(const char *path, void *buf, fuse_fill_dir_t dir_add,
return 0;
}
static struct fuse_operations fs_oper = {
.getattr = fs_getattr,
static struct fuse_operations fs_oper = {.getattr = fs_getattr,
.readdir = fs_readdir,
.open = fs_open,
.read = fs_read,
.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);
}

View File

@ -32,8 +32,7 @@ static void make_link_relative(const char *page_url, char *link_url);
/**
* \brief create a new Link
*/
static Link *Link_new(const char *linkname, LinkType type)
{
static Link *Link_new(const char *linkname, LinkType type) {
Link *link = CALLOC(1, sizeof(Link));
strncpy(link->linkname, linkname, MAX_FILENAME_LEN);
@ -51,8 +50,7 @@ static Link *Link_new(const char *linkname, LinkType type)
return link;
}
static CURL *Link_to_curl(Link *link)
{
static CURL *Link_to_curl(Link *link) {
lprintf(debug, "%s\n", link->f_url);
CURL *curl = curl_easy_init();
if (!curl) {
@ -61,8 +59,7 @@ static CURL *Link_to_curl(Link *link)
/*
* set up some basic curl stuff
*/
CURLcode ret =
curl_easy_setopt(curl, CURLOPT_USERAGENT, CONFIG.user_agent);
CURLcode ret = curl_easy_setopt(curl, CURLOPT_USERAGENT, CONFIG.user_agent);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
@ -93,8 +90,7 @@ static CURL *Link_to_curl(Link *link)
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
ret =
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_memory_callback);
ret = curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_memory_callback);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
@ -153,16 +149,14 @@ static CURL *Link_to_curl(Link *link)
}
if (CONFIG.proxy_username) {
ret = curl_easy_setopt(curl, CURLOPT_PROXYUSERNAME,
CONFIG.proxy_username);
ret = curl_easy_setopt(curl, CURLOPT_PROXYUSERNAME, CONFIG.proxy_username);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
}
if (CONFIG.proxy_password) {
ret = curl_easy_setopt(curl, CURLOPT_PROXYPASSWORD,
CONFIG.proxy_password);
ret = curl_easy_setopt(curl, CURLOPT_PROXYPASSWORD, CONFIG.proxy_password);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
@ -175,8 +169,7 @@ static CURL *Link_to_curl(Link *link)
lprintf(error, "%s", curl_easy_strerror(ret));
}
ret = curl_easy_setopt(curl, CURLOPT_PROXY_CAINFO,
CONFIG.proxy_cafile);
ret = curl_easy_setopt(curl, CURLOPT_PROXY_CAINFO, CONFIG.proxy_cafile);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
@ -185,8 +178,7 @@ static CURL *Link_to_curl(Link *link)
return curl;
}
static void Link_req_file_stat(Link *this_link)
{
static void Link_req_file_stat(Link *this_link) {
lprintf(debug, "%s\n", this_link->f_url);
CURL *curl = Link_to_curl(this_link);
CURLcode ret = curl_easy_setopt(curl, CURLOPT_NOBODY, 1);
@ -221,8 +213,7 @@ static void Link_req_file_stat(Link *this_link)
* \details Try and get the stats for each link in the link table. This will get
* repeated until the uninitialised entry count drop to zero.
*/
static void LinkTable_uninitialised_fill(LinkTable *linktbl)
{
static void LinkTable_uninitialised_fill(LinkTable *linktbl) {
int u;
char s[STATUS_LEN];
lprintf(debug, " ... ");
@ -261,8 +252,7 @@ static void LinkTable_uninitialised_fill(LinkTable *linktbl)
/**
* \brief Create the root linktable for single file mode
*/
static LinkTable *single_LinkTable_new(const char *url)
{
static LinkTable *single_LinkTable_new(const char *url) {
char *ptr = strrchr(url, '/') + 1;
LinkTable *linktbl = LinkTable_alloc(url);
Link *link = Link_new(ptr, LINK_UNINITIALISED_FILE);
@ -273,8 +263,7 @@ static LinkTable *single_LinkTable_new(const char *url)
return linktbl;
}
LinkTable *LinkSystem_init(const char *url)
{
LinkTable *LinkSystem_init(const char *url) {
if (pthread_mutex_init(&link_lock, NULL)) {
lprintf(error, "link_lock initialisation failed!\n");
}
@ -285,8 +274,8 @@ LinkTable *LinkSystem_init(const char *url)
/*
* This is where the '/' should be
*/
ROOT_LINK_OFFSET = strnlen(url, MAX_PATH_LEN) -
((url[url_len] == '/') ? 1 : 0);
ROOT_LINK_OFFSET =
strnlen(url, MAX_PATH_LEN) - ((url[url_len] == '/') ? 1 : 0);
/*
* --------------------- Enable cache system --------------------
@ -307,8 +296,7 @@ LinkTable *LinkSystem_init(const char *url)
} else if (CONFIG.mode == SINGLE) {
ROOT_LINK_TBL = single_LinkTable_new(url);
} else if (CONFIG.mode == SONIC) {
sonic_config_init(url, CONFIG.sonic_username,
CONFIG.sonic_password);
sonic_config_init(url, CONFIG.sonic_username, CONFIG.sonic_password);
if (!CONFIG.sonic_id3) {
ROOT_LINK_TBL = sonic_LinkTable_new_index("0");
} else {
@ -320,19 +308,16 @@ LinkTable *LinkSystem_init(const char *url)
return ROOT_LINK_TBL;
}
void LinkTable_add(LinkTable *linktbl, Link *link)
{
void LinkTable_add(LinkTable *linktbl, Link *link) {
linktbl->num++;
linktbl->links =
realloc(linktbl->links, linktbl->num * sizeof(Link *));
linktbl->links = realloc(linktbl->links, linktbl->num * sizeof(Link *));
if (!linktbl->links) {
lprintf(fatal, "realloc() failure!\n");
}
linktbl->links[linktbl->num - 1] = link;
}
static LinkType linkname_to_LinkType(const char *linkname)
{
static LinkType linkname_to_LinkType(const char *linkname) {
/*
* The link name has to start with alphanumerical character
*/
@ -361,8 +346,7 @@ static LinkType linkname_to_LinkType(const char *linkname)
/**
* \brief check if two link names are equal, after taking the '/' into account.
*/
static int linknames_equal(char *linkname, const char *linkname_new)
{
static int linknames_equal(char *linkname, const char *linkname_new) {
if (!strncmp(linkname, linkname_new, MAX_FILENAME_LEN)) {
return 1;
}
@ -370,11 +354,10 @@ static int linknames_equal(char *linkname, const char *linkname_new)
/*
* check if the link names differ by a single '/'
*/
if (!strncmp
(linkname, linkname_new, strnlen(linkname, MAX_FILENAME_LEN))) {
if (!strncmp(linkname, linkname_new, strnlen(linkname, MAX_FILENAME_LEN))) {
size_t linkname_new_len = strnlen(linkname_new, MAX_FILENAME_LEN);
if ((linkname_new_len - strnlen(linkname, MAX_FILENAME_LEN) == 1)
&& (linkname_new[linkname_new_len - 1] == '/')) {
if ((linkname_new_len - strnlen(linkname, MAX_FILENAME_LEN) == 1) &&
(linkname_new[linkname_new_len - 1] == '/')) {
return 1;
}
}
@ -386,16 +369,14 @@ static int linknames_equal(char *linkname, const char *linkname_new)
* https://github.com/google/gumbo-parser/blob/master/examples/find_links.cc
*/
static void HTML_to_LinkTable(const char *url, GumboNode *node,
LinkTable *linktbl)
{
LinkTable *linktbl) {
if (node->type != GUMBO_NODE_ELEMENT) {
return;
}
GumboAttribute *href;
if (node->v.element.tag == GUMBO_TAG_A &&
(href =
gumbo_get_attribute(&node->v.element.attributes, "href"))) {
char *link_url = (char *) href->value;
(href = gumbo_get_attribute(&node->v.element.attributes, "href"))) {
char *link_url = (char *)href->value;
make_link_relative(url, link_url);
/*
* if it is valid, copy the link onto the heap
@ -417,28 +398,24 @@ static void HTML_to_LinkTable(const char *url, GumboNode *node,
*/
GumboVector *children = &node->v.element.children;
for (size_t i = 0; i < children->length; ++i) {
HTML_to_LinkTable(url, (GumboNode *) children->data[i], linktbl);
HTML_to_LinkTable(url, (GumboNode *)children->data[i], linktbl);
}
return;
}
void Link_set_file_stat(Link *this_link, CURL *curl)
{
void Link_set_file_stat(Link *this_link, CURL *curl) {
long http_resp;
CURLcode ret =
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_resp);
CURLcode ret = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_resp);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
if (http_resp == HTTP_OK) {
curl_off_t cl = 0;
ret =
curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &cl);
ret = curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD_T, &cl);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
ret =
curl_easy_getinfo(curl, CURLINFO_FILETIME, &(this_link->time));
ret = curl_easy_getinfo(curl, CURLINFO_FILETIME, &(this_link->time));
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
@ -459,8 +436,7 @@ void Link_set_file_stat(Link *this_link, CURL *curl)
}
}
static void LinkTable_fill(LinkTable *linktbl)
{
static void LinkTable_fill(LinkTable *linktbl) {
CURL *c = curl_easy_init();
Link *head_link = linktbl->links[0];
lprintf(debug, "Filling %s\n", head_link->f_url);
@ -473,8 +449,7 @@ static void LinkTable_fill(LinkTable *linktbl)
will definitely be happy with it (e.g., curl won't accept URLs with
spaces in them!). If we only escaped it, and there were already
encoded characters in it, then that would break the link. */
char *unescaped_path = curl_easy_unescape(c, this_link->linkpath, 0,
NULL);
char *unescaped_path = curl_easy_unescape(c, this_link->linkpath, 0, NULL);
char *escaped_path = curl_easy_escape(c, unescaped_path, 0);
curl_free(unescaped_path);
/* Our code does the wrong thing if there's a trailing slash that's been
@ -488,8 +463,7 @@ static void LinkTable_fill(LinkTable *linktbl)
strncpy(this_link->f_url, url, MAX_PATH_LEN);
FREE(url);
char *unescaped_linkname;
unescaped_linkname = curl_easy_unescape(c, this_link->linkname,
0, NULL);
unescaped_linkname = curl_easy_unescape(c, this_link->linkname, 0, NULL);
strncpy(this_link->linkname, unescaped_linkname, MAX_FILENAME_LEN);
curl_free(unescaped_linkname);
}
@ -500,8 +474,7 @@ static void LinkTable_fill(LinkTable *linktbl)
/**
* \brief Reset invalid links in the link table
*/
static void LinkTable_invalid_reset(LinkTable *linktbl)
{
static void LinkTable_invalid_reset(LinkTable *linktbl) {
int j = 0;
for (int i = 0; i < linktbl->num; i++) {
Link *this_link = linktbl->links[i];
@ -513,8 +486,7 @@ static void LinkTable_invalid_reset(LinkTable *linktbl)
lprintf(debug, "%d invalid links\n", j);
}
void LinkTable_free(LinkTable *linktbl)
{
void LinkTable_free(LinkTable *linktbl) {
if (linktbl) {
for (int i = 0; i < linktbl->num; i++) {
LinkTable_free(linktbl->links[i]->next_table);
@ -525,24 +497,18 @@ void LinkTable_free(LinkTable *linktbl)
}
}
void LinkTable_print(LinkTable *linktbl)
{
void LinkTable_print(LinkTable *linktbl) {
if (CONFIG.log_type & info) {
int j = 0;
lprintf(info, "--------------------------------------------\n");
lprintf(info, " LinkTable %p for %s\n", linktbl,
linktbl->links[0]->f_url);
lprintf(info, " LinkTable %p for %s\n", linktbl, linktbl->links[0]->f_url);
lprintf(info, "--------------------------------------------\n");
for (int i = 0; i < linktbl->num; i++) {
Link *this_link = linktbl->links[i];
lprintf(info, "%d %c %lu %s %s\n",
i,
this_link->type,
this_link->content_length,
this_link->linkname, this_link->f_url);
if ((this_link->type != LINK_FILE)
&& (this_link->type != LINK_DIR)
&& (this_link->type != LINK_HEAD)) {
lprintf(info, "%d %c %lu %s %s\n", i, this_link->type,
this_link->content_length, this_link->linkname, this_link->f_url);
if ((this_link->type != LINK_FILE) && (this_link->type != LINK_DIR) &&
(this_link->type != LINK_HEAD)) {
j++;
}
}
@ -552,14 +518,12 @@ void LinkTable_print(LinkTable *linktbl)
}
}
LinkTable *LinkTable_alloc(const char *url)
{
LinkTable *LinkTable_alloc(const char *url) {
LinkTable *linktbl = CALLOC(1, sizeof(LinkTable));
linktbl->num = 0;
linktbl->index_time = 0;
linktbl->links = NULL;
/*
* populate the base URL
*/
@ -570,8 +534,7 @@ LinkTable *LinkTable_alloc(const char *url)
return linktbl;
}
LinkTable *LinkTable_new(const char *url)
{
LinkTable *LinkTable_new(const char *url) {
LinkTable *linktbl = LinkTable_alloc(url);
linktbl->index_time = time(NULL);
@ -595,8 +558,7 @@ LinkTable *LinkTable_new(const char *url)
int skip_fill = 0;
char *unescaped_path;
CURL *c = curl_easy_init();
unescaped_path =
curl_easy_unescape(c, url + ROOT_LINK_OFFSET, 0, NULL);
unescaped_path = curl_easy_unescape(c, url + ROOT_LINK_OFFSET, 0, NULL);
if (CACHE_SYSTEM_INIT) {
CacheDir_create(unescaped_path);
LinkTable *disk_linktbl;
@ -605,8 +567,7 @@ LinkTable *LinkTable_new(const char *url)
/*
* Check if we need to update the link table
*/
lprintf(debug,
"disk_linktbl->num: %d, linktbl->num: %d\n",
lprintf(debug, "disk_linktbl->num: %d, linktbl->num: %d\n",
disk_linktbl->num, linktbl->num);
if (disk_linktbl->num == linktbl->num) {
LinkTable_free(linktbl);
@ -648,8 +609,7 @@ LinkTable *LinkTable_new(const char *url)
return linktbl;
}
static void LinkTable_disk_delete(const char *dirn)
{
static void LinkTable_disk_delete(const char *dirn) {
char *metadirn = path_append(META_DIR, dirn);
char *path;
path = path_append(metadirn, "/.LinkTable");
@ -660,8 +620,7 @@ static void LinkTable_disk_delete(const char *dirn)
FREE(metadirn);
}
int LinkTable_disk_save(LinkTable *linktbl, const char *dirn)
{
int LinkTable_disk_save(LinkTable *linktbl, const char *dirn) {
char *metadirn = path_append(META_DIR, dirn);
char *path;
path = path_append(metadirn, "/.LinkTable");
@ -677,8 +636,7 @@ int LinkTable_disk_save(LinkTable *linktbl, const char *dirn)
fwrite(&linktbl->num, sizeof(int), 1, fp);
for (int i = 0; i < linktbl->num; i++) {
fwrite(linktbl->links[i]->linkname, sizeof(char),
MAX_FILENAME_LEN, fp);
fwrite(linktbl->links[i]->linkname, sizeof(char), MAX_FILENAME_LEN, fp);
fwrite(linktbl->links[i]->f_url, sizeof(char), MAX_PATH_LEN, fp);
fwrite(&linktbl->links[i]->type, sizeof(LinkType), 1, fp);
fwrite(&linktbl->links[i]->content_length, sizeof(size_t), 1, fp);
@ -693,8 +651,7 @@ int LinkTable_disk_save(LinkTable *linktbl, const char *dirn)
}
if (fclose(fp)) {
lprintf(error,
"cannot close the file pointer, %s\n", strerror(errno));
lprintf(error, "cannot close the file pointer, %s\n", strerror(errno));
res = -1;
}
@ -704,13 +661,9 @@ int LinkTable_disk_save(LinkTable *linktbl, const char *dirn)
/* This is necessary to get the compiler on some platforms to stop
complaining about the fact that we're not using the return value of
fread, when we know we aren't and that's fine. */
static inline void ignore_value(int i)
{
(void) i;
}
static inline void ignore_value(int i) { (void)i; }
LinkTable *LinkTable_disk_open(const char *dirn)
{
LinkTable *LinkTable_disk_open(const char *dirn) {
char *metadirn = path_append(META_DIR, dirn);
char *path;
if (metadirn[strnlen(metadirn, MAX_PATH_LEN)] == '/') {
@ -742,13 +695,13 @@ LinkTable *LinkTable_disk_open(const char *dirn)
linktbl->links[i] = CALLOC(1, sizeof(Link));
/* The return values are safe to ignore here since we check them
immediately afterwards with feof() and ferror(). */
ignore_value(fread(linktbl->links[i]->linkname, sizeof(char),
MAX_FILENAME_LEN, fp));
ignore_value(fread(linktbl->links[i]->f_url, sizeof(char),
MAX_PATH_LEN, fp));
ignore_value(
fread(linktbl->links[i]->linkname, sizeof(char), MAX_FILENAME_LEN, fp));
ignore_value(
fread(linktbl->links[i]->f_url, sizeof(char), MAX_PATH_LEN, fp));
ignore_value(fread(&linktbl->links[i]->type, sizeof(LinkType), 1, fp));
ignore_value(fread(&linktbl->links[i]->content_length,
sizeof(size_t), 1, fp));
ignore_value(
fread(&linktbl->links[i]->content_length, sizeof(size_t), 1, fp));
ignore_value(fread(&linktbl->links[i]->time, sizeof(long), 1, fp));
if (feof(fp)) {
/*
@ -767,17 +720,15 @@ LinkTable *LinkTable_disk_open(const char *dirn)
}
}
if (fclose(fp)) {
lprintf(error,
"cannot close the file pointer, %s\n", strerror(errno));
lprintf(error, "cannot close the file pointer, %s\n", strerror(errno));
}
return linktbl;
}
LinkTable *path_to_Link_LinkTable_new(const char *path)
{
LinkTable *path_to_Link_LinkTable_new(const char *path) {
Link *link = NULL;
Link *tmp_link = NULL;
Link link_cpy = { 0 };
Link link_cpy = {0};
LinkTable *next_table = NULL;
if (!strcmp(path, "/")) {
next_table = ROOT_LINK_TBL;
@ -809,8 +760,7 @@ LinkTable *path_to_Link_LinkTable_new(const char *path)
next_table = sonic_LinkTable_new_index(tmp_link->sonic.id);
} else {
next_table =
sonic_LinkTable_new_id3(tmp_link->sonic.depth,
tmp_link->sonic.id);
sonic_LinkTable_new_id3(tmp_link->sonic.depth, tmp_link->sonic.id);
}
} else {
lprintf(fatal, "Invalid CONFIG.mode: %d\n", CONFIG.mode);
@ -824,8 +774,7 @@ LinkTable *path_to_Link_LinkTable_new(const char *path)
return next_table;
}
static Link *path_to_Link_recursive(char *path, LinkTable *linktbl)
{
static Link *path_to_Link_recursive(char *path, LinkTable *linktbl) {
/*
* skip the leading '/' if it exists
*/
@ -847,8 +796,7 @@ static Link *path_to_Link_recursive(char *path, LinkTable *linktbl)
* We cannot find another '/', we have reached the last level
*/
for (int i = 1; i < linktbl->num; i++) {
if (!strncmp
(path, linktbl->links[i]->linkname, MAX_FILENAME_LEN)) {
if (!strncmp(path, linktbl->links[i]->linkname, MAX_FILENAME_LEN)) {
/*
* We found our link
*/
@ -871,27 +819,21 @@ static Link *path_to_Link_recursive(char *path, LinkTable *linktbl)
*/
char *next_path = slash + 1;
for (int i = 1; i < linktbl->num; i++) {
if (!strncmp
(path, linktbl->links[i]->linkname, MAX_FILENAME_LEN)) {
if (!strncmp(path, linktbl->links[i]->linkname, MAX_FILENAME_LEN)) {
/*
* The next sub-directory exists
*/
LinkTable *next_table = linktbl->links[i]->next_table;
if (!next_table) {
if (CONFIG.mode == NORMAL) {
next_table =
LinkTable_new(linktbl->links[i]->f_url);
next_table = LinkTable_new(linktbl->links[i]->f_url);
} else if (CONFIG.mode == SONIC) {
if (!CONFIG.sonic_id3) {
next_table =
sonic_LinkTable_new_index
(linktbl->links[i]->sonic.id);
sonic_LinkTable_new_index(linktbl->links[i]->sonic.id);
} else {
next_table =
sonic_LinkTable_new_id3
(linktbl->links
[i]->sonic.depth,
linktbl->links[i]->sonic.id);
next_table = sonic_LinkTable_new_id3(
linktbl->links[i]->sonic.depth, linktbl->links[i]->sonic.id);
}
} else {
lprintf(fatal, "Invalid CONFIG.mode\n");
@ -905,10 +847,8 @@ static Link *path_to_Link_recursive(char *path, LinkTable *linktbl)
return NULL;
}
Link *path_to_Link(const char *path)
{
lprintf(link_lock_debug,
"thread %x: locking link_lock;\n", pthread_self());
Link *path_to_Link(const char *path) {
lprintf(link_lock_debug, "thread %x: locking link_lock;\n", pthread_self());
PTHREAD_MUTEX_LOCK(&link_lock);
char *new_path = strndup(path, MAX_PATH_LEN);
@ -918,14 +858,12 @@ Link *path_to_Link(const char *path)
Link *link = path_to_Link_recursive(new_path, ROOT_LINK_TBL);
FREE(new_path);
lprintf(link_lock_debug,
"thread %x: unlocking link_lock;\n", pthread_self());
lprintf(link_lock_debug, "thread %x: unlocking link_lock;\n", pthread_self());
PTHREAD_MUTEX_UNLOCK(&link_lock);
return link;
}
TransferStruct Link_download_full(Link *link)
{
TransferStruct Link_download_full(Link *link) {
char *url = link->f_url;
CURL *curl = Link_to_curl(link);
@ -935,11 +873,11 @@ TransferStruct Link_download_full(Link *link)
ts.type = DATA;
ts.transferring = 1;
CURLcode ret = curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) &ts);
CURLcode ret = curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&ts);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
ret = curl_easy_setopt(curl, CURLOPT_PRIVATE, (void *) &ts);
ret = curl_easy_setopt(curl, CURLOPT_PRIVATE, (void *)&ts);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
@ -955,13 +893,10 @@ TransferStruct Link_download_full(Link *link)
lprintf(error, "%s", curl_easy_strerror(ret));
}
if (HTTP_temp_failure(http_resp)) {
lprintf(warning,
"URL: %s, HTTP %ld, retrying later.\n",
url, http_resp);
lprintf(warning, "URL: %s, HTTP %ld, retrying later.\n", url, http_resp);
sleep(CONFIG.http_wait_sec);
} else if (http_resp != HTTP_OK) {
lprintf(warning,
"cannot retrieve URL: %s, HTTP %ld\n", url, http_resp);
lprintf(warning, "cannot retrieve URL: %s, HTTP %ld\n", url, http_resp);
ts.curr_size = 0;
free(ts.data); /* not FREE(); can be NULL on error path! */
curl_easy_cleanup(curl);
@ -979,8 +914,7 @@ TransferStruct Link_download_full(Link *link)
static CURL *Link_download_curl_setup(Link *link, size_t req_size, off_t offset,
TransferStruct *header,
TransferStruct *ts)
{
TransferStruct *ts) {
if (!link) {
lprintf(fatal, "Invalid supplied\n");
}
@ -993,16 +927,15 @@ static CURL *Link_download_curl_setup(Link *link, size_t req_size, off_t offset,
lprintf(debug, "%s: %s\n", link->linkname, range_str);
CURL *curl = Link_to_curl(link);
CURLcode ret =
curl_easy_setopt(curl, CURLOPT_HEADERDATA, (void *) header);
CURLcode ret = curl_easy_setopt(curl, CURLOPT_HEADERDATA, (void *)header);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
ret = curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *) ts);
ret = curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)ts);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
ret = curl_easy_setopt(curl, CURLOPT_PRIVATE, (void *) ts);
ret = curl_easy_setopt(curl, CURLOPT_PRIVATE, (void *)ts);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
@ -1014,8 +947,7 @@ static CURL *Link_download_curl_setup(Link *link, size_t req_size, off_t offset,
return curl;
}
static curl_off_t Link_download_cleanup(CURL *curl, TransferStruct *header)
{
static curl_off_t Link_download_cleanup(CURL *curl, TransferStruct *header) {
/*
* Check for range seek support
*/
@ -1034,8 +966,7 @@ range requests\n");
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
if ((http_resp != HTTP_OK) &&
(http_resp != HTTP_PARTIAL_CONTENT) &&
if ((http_resp != HTTP_OK) && (http_resp != HTTP_PARTIAL_CONTENT) &&
(http_resp != HTTP_RANGE_NOT_SATISFIABLE)) {
char *url;
curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
@ -1054,8 +985,8 @@ range requests\n");
return recv;
}
long Link_download(Link *link, char *output_buf, size_t req_size, off_t offset)
{
long Link_download(Link *link, char *output_buf, size_t req_size,
off_t offset) {
TransferStruct ts;
ts.curr_size = 0;
ts.data = NULL;
@ -1068,7 +999,8 @@ long Link_download(Link *link, char *output_buf, size_t req_size, off_t offset)
if (offset + req_size > link->content_length) {
lprintf(error,
"requested size too large, req_size: %lu, recv: %ld, content-length: %ld\n",
"requested size too large, req_size: %lu, recv: %ld, "
"content-length: %ld\n",
req_size, recv, link->content_length);
req_size = link->content_length - offset;
}
@ -1080,7 +1012,7 @@ long Link_download(Link *link, char *output_buf, size_t req_size, off_t offset)
curl_off_t recv = Link_download_cleanup(curl, &header);
/* The extra 1 byte is probably for '\0' */
if (recv - 1 == (long int) req_size) {
if (recv - 1 == (long int)req_size) {
recv--;
} else if (offset + req_size < link->content_length) {
lprintf(error, "req_size: %lu, recv: %ld\n", req_size, recv);
@ -1093,8 +1025,7 @@ long Link_download(Link *link, char *output_buf, size_t req_size, off_t offset)
}
long path_download(const char *path, char *output_buf, size_t req_size,
off_t offset)
{
off_t offset) {
if (!path) {
lprintf(fatal, "NULL path supplied\n");
}
@ -1108,8 +1039,7 @@ long path_download(const char *path, char *output_buf, size_t req_size,
return Link_download(link, output_buf, req_size, offset);
}
static void make_link_relative(const char *page_url, char *link_url)
{
static void make_link_relative(const char *page_url, char *link_url) {
/*
Some servers make the links to subdirectories absolute (in URI terms:
path-absolute), but our code expects them to be relative (in URI terms:
@ -1144,14 +1074,14 @@ static void make_link_relative(const char *page_url, char *link_url)
/* Find the slash after the host name. */
int slashes_left_to_find = 3;
while (*page_url) {
if (*page_url == '/' && ! --slashes_left_to_find)
if (*page_url == '/' && !--slashes_left_to_find)
break;
/* N.B. This is here, rather than doing `while (*page_url++)`, because
when we're done we want the pointer to point at the final slash. */
page_url++;
}
if (slashes_left_to_find) {
if (slashes_left_to_find == 1 && ! *page_url)
if (slashes_left_to_find == 1 && !*page_url)
/* We're at the top level of the web site and the user entered the URL
without a trailing slash. */
page_url = "/";
@ -1166,7 +1096,7 @@ static void make_link_relative(const char *page_url, char *link_url)
if (strstr(link_url, page_url) != link_url)
return;
int skip_len = strlen(page_url);
if (page_url[skip_len-1] != '/') {
if (page_url[skip_len - 1] != '/') {
if (page_url[skip_len] != '/')
/* Um, I'm not sure what to do here, so give up. */
return;

View File

@ -96,8 +96,7 @@ long path_download(const char *path, char *output_buf, size_t size,
* \brief Download a Link
* \return the number of bytes downloaded
*/
long Link_download(Link *link, char *output_buf, size_t req_size,
off_t offset);
long Link_download(Link *link, char *output_buf, size_t req_size, off_t offset);
/**
* \brief find the link associated with a path

View File

@ -9,8 +9,7 @@
#include <stdio.h>
#include <stdlib.h>
int log_level_init()
{
int log_level_init() {
char *env = getenv("HTTPDIRFS_LOG_LEVEL");
if (env) {
return atoi(env);
@ -18,10 +17,8 @@ int log_level_init()
return DEFAULT_LOG_LEVEL;
}
void
log_printf(LogType type, const char *file, const char *func, int line,
const char *format, ...)
{
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:
@ -42,8 +39,7 @@ log_printf(LogType type, const char *file, const char *func, int line,
fprintf(stderr, "%s:%d:", file, line);
print_actual_message: {
}
print_actual_message: {}
fprintf(stderr, "%s: ", func);
va_list args;
va_start(args, format);
@ -56,8 +52,7 @@ print_actual_message: {
}
}
void print_version()
{
void print_version() {
/* FUSE prints its help to stderr */
fprintf(stderr, "HTTPDirFS version " VERSION "\n");
/*

View File

@ -10,21 +10,19 @@
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_long_help();
static int
parse_arg_list(int argc, char **argv, char ***fuse_argv, int *fuse_argc);
static int parse_arg_list(int argc, char **argv, char ***fuse_argv,
int *fuse_argc);
void parse_config_file(char ***argv, int *argc);
static char *config_path = NULL;
int main(int argc, char **argv)
{
int main(int argc, char **argv) {
/*
* Automatically print help if not enough arguments are supplied
*/
if (argc < 2) {
print_help(argv[0], 0);
fprintf(stderr, "For more information, run \"%s --help.\"\n",
argv[0]);
fprintf(stderr, "For more information, run \"%s --help.\"\n", argv[0]);
exit(EXIT_FAILURE);
}
@ -88,8 +86,7 @@ int main(int argc, char **argv)
* 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)) {
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);
@ -97,8 +94,7 @@ int main(int argc, char **argv)
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 \
fprintf(stderr, "Error: You have to supply both username and password to \
activate Sonic mode.\n");
exit(EXIT_FAILURE);
}
@ -114,8 +110,7 @@ fuse_start:
return 0;
}
void parse_config_file(char ***argv, int *argc)
{
void parse_config_file(char ***argv, int *argc) {
char *full_path;
if (!config_path) {
char *xdg_config_home = getenv("XDG_CONFIG_HOME");
@ -155,9 +150,7 @@ void parse_config_file(char ***argv, int *argc)
/*
* Starts copying after the space
*/
(*argv)[*argc - 1] = strndup(space + 1,
buf_len -
(space + 1 - buf));
(*argv)[*argc - 1] = strndup(space + 1, buf_len - (space + 1 - buf));
}
}
}
@ -166,9 +159,8 @@ void parse_config_file(char ***argv, int *argc)
FREE(full_path);
}
static int
parse_arg_list(int argc, char **argv, char ***fuse_argv, int *fuse_argc)
{
static int parse_arg_list(int argc, char **argv, char ***fuse_argv,
int *fuse_argc) {
int c;
int long_index = 0;
const char *short_opts = "o:hVdfsp:u:P:";
@ -176,37 +168,35 @@ parse_arg_list(int argc, char **argv, char ***fuse_argv, int *fuse_argc)
/*
* 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) {
{"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");
@ -326,16 +316,14 @@ parse_arg_list(int argc, char **argv, char ***fuse_argv, int *fuse_argc)
* \brief add an argument to an argv array
* \details This is basically how you add a string to an array of string
*/
void add_arg(char ***fuse_argv_ptr, int *fuse_argc, char *opt_string)
{
void add_arg(char ***fuse_argv_ptr, int *fuse_argc, char *opt_string) {
(*fuse_argc)++;
*fuse_argv_ptr = realloc(*fuse_argv_ptr, *fuse_argc * sizeof(char *));
char **fuse_argv = *fuse_argv_ptr;
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 */
fprintf(stderr, "usage: %s [options] URL mountpoint\n", program_name);
if (long_help) {
@ -343,8 +331,7 @@ static void print_help(char *program_name, int long_help)
}
}
static void print_long_help()
{
static void print_long_help() {
/* FUSE prints its help to stderr */
fprintf(stderr, "\n\
general options:\n\

View File

@ -7,10 +7,9 @@
#include <string.h>
size_t write_memory_callback(void *recv_data, size_t size, size_t nmemb,
void *userp)
{
void *userp) {
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);
if (!ts->data) {

View File

@ -5,10 +5,7 @@
/**
* \brief specify the type of data transfer
*/
typedef enum {
FILESTAT = 's',
DATA = 'd'
} TransferType;
typedef enum { FILESTAT = 's', DATA = 'd' } TransferType;
/**
* \brief For storing transfer data and metadata

View File

@ -7,8 +7,8 @@
#include <openssl/crypto.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
/*
@ -37,10 +37,9 @@ static pthread_mutex_t curl_lock;
* \brief OpenSSL 1.02 cryptography callback function
* \details Required for OpenSSL 1.02, but not OpenSSL 1.1
*/
static void crypto_lock_callback(int mode, int type, char *file, int line)
{
(void) file;
(void) line;
static void crypto_lock_callback(int mode, int type, char *file, int line) {
(void)file;
(void)line;
if (mode & CRYPTO_LOCK) {
PTHREAD_MUTEX_LOCK(&(crypto_lockarray[type]));
} else {
@ -52,32 +51,30 @@ static void crypto_lock_callback(int mode, int type, char *file, int line)
* \brief OpenSSL 1.02 thread ID function
* \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;
ret = (unsigned long) pthread_self();
ret = (unsigned long)pthread_self();
return ret;
}
#pragma GCC diagnostic pop
static void crypto_lock_init(void)
{
static void crypto_lock_init(void) {
int i;
crypto_lockarray =
(pthread_mutex_t *) OPENSSL_malloc(CRYPTO_num_locks() *
crypto_lockarray = (pthread_mutex_t *)OPENSSL_malloc(CRYPTO_num_locks() *
sizeof(pthread_mutex_t));
for (i = 0; i < CRYPTO_num_locks(); i++) {
if (pthread_mutex_init(&(crypto_lockarray[i]), NULL)) {
lprintf(fatal, "crypto_lockarray[%d] initialisation \
failed!\n", i);
failed!\n",
i);
};
}
CRYPTO_set_id_callback((unsigned long (*)()) thread_id);
CRYPTO_set_locking_callback((void (*)()) crypto_lock_callback);
CRYPTO_set_id_callback((unsigned long (*)())thread_id);
CRYPTO_set_locking_callback((void (*)())crypto_lock_callback);
}
/**
@ -85,23 +82,20 @@ failed!\n", i);
* \details Adapted from:
* https://curl.haxx.se/libcurl/c/threaded-shared-conn.html
*/
static void
curl_callback_lock(CURL *handle, curl_lock_data data,
curl_lock_access access, void *userptr)
{
(void) access; /* unused */
(void) userptr; /* unused */
(void) handle; /* unused */
(void) data; /* unused */
static void curl_callback_lock(CURL *handle, curl_lock_data data,
curl_lock_access access, void *userptr) {
(void)access; /* unused */
(void)userptr; /* unused */
(void)handle; /* unused */
(void)data; /* unused */
PTHREAD_MUTEX_LOCK(&curl_lock);
}
static void
curl_callback_unlock(CURL *handle, curl_lock_data data, void *userptr)
{
(void) userptr; /* unused */
(void) handle; /* unused */
(void) data; /* unused */
static void curl_callback_unlock(CURL *handle, curl_lock_data data,
void *userptr) {
(void)userptr; /* unused */
(void)handle; /* unused */
(void)data; /* unused */
PTHREAD_MUTEX_UNLOCK(&curl_lock);
}
@ -110,18 +104,16 @@ curl_callback_unlock(CURL *handle, curl_lock_data data, void *userptr)
* \details Adapted from:
* https://curl.haxx.se/libcurl/c/10-at-a-time.html
*/
static void
curl_process_msgs(CURLMsg *curl_msg, int n_running_curl, int n_mesgs)
{
(void) n_running_curl;
(void) n_mesgs;
static void curl_process_msgs(CURLMsg *curl_msg, int n_running_curl,
int n_mesgs) {
(void)n_running_curl;
(void)n_mesgs;
static volatile int slept = 0;
if (curl_msg->msg == CURLMSG_DONE) {
TransferStruct *ts;
CURL *curl = curl_msg->easy_handle;
CURLcode ret =
curl_easy_getinfo(curl_msg->easy_handle, CURLINFO_PRIVATE,
&ts);
curl_easy_getinfo(curl_msg->easy_handle, CURLINFO_PRIVATE, &ts);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
@ -142,9 +134,8 @@ curl_process_msgs(CURLMsg *curl_msg, int n_running_curl, int n_mesgs)
}
if (HTTP_temp_failure(http_resp)) {
if (!slept) {
lprintf(warning,
"HTTP %ld, sleeping for %d sec\n",
http_resp, CONFIG.http_wait_sec);
lprintf(warning, "HTTP %ld, sleeping for %d sec\n", http_resp,
CONFIG.http_wait_sec);
sleep(CONFIG.http_wait_sec);
slept = 1;
}
@ -160,8 +151,7 @@ curl_process_msgs(CURLMsg *curl_msg, int n_running_curl, int n_mesgs)
Link_set_file_stat(ts->link, curl);
}
} else {
lprintf(error, "%d - %s <%s>\n",
curl_msg->data.result,
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);
@ -181,10 +171,9 @@ curl_process_msgs(CURLMsg *curl_msg, int n_running_curl, int n_mesgs)
* \details effectively based on
* https://curl.haxx.se/libcurl/c/multi-double.html
*/
int curl_multi_perform_once(void)
{
lprintf(network_lock_debug,
"thread %x: locking transfer_lock;\n", pthread_self());
int curl_multi_perform_once(void) {
lprintf(network_lock_debug, "thread %x: locking transfer_lock;\n",
pthread_self());
PTHREAD_MUTEX_LOCK(&transfer_lock);
/*
@ -210,15 +199,14 @@ int curl_multi_perform_once(void)
curl_process_msgs(curl_msg, n_running_curl, n_mesgs);
}
lprintf(network_lock_debug,
"thread %x: unlocking transfer_lock;\n", pthread_self());
lprintf(network_lock_debug, "thread %x: unlocking transfer_lock;\n",
pthread_self());
PTHREAD_MUTEX_UNLOCK(&transfer_lock);
return n_running_curl;
}
void NetworkSystem_init(void)
{
void NetworkSystem_init(void) {
/*
* ------- Global related ----------
*/
@ -236,15 +224,13 @@ void NetworkSystem_init(void)
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_SSL_SESSION);
curl_share_setopt(CURL_SHARE, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
if (pthread_mutex_init(&curl_lock, NULL)) {
lprintf(fatal, "curl_lock initialisation failed!\n");
}
curl_share_setopt(CURL_SHARE, CURLSHOPT_LOCKFUNC, curl_callback_lock);
curl_share_setopt(CURL_SHARE, CURLSHOPT_UNLOCKFUNC,
curl_callback_unlock);
curl_share_setopt(CURL_SHARE, CURLSHOPT_UNLOCKFUNC, curl_callback_unlock);
/*
* ------------- Multi related -----------
@ -272,16 +258,15 @@ void NetworkSystem_init(void)
crypto_lock_init();
}
void transfer_blocking(CURL *curl)
{
void transfer_blocking(CURL *curl) {
TransferStruct *ts;
CURLcode ret = curl_easy_getinfo(curl, CURLINFO_PRIVATE, &ts);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
lprintf(network_lock_debug,
"thread %x: locking transfer_lock;\n", pthread_self());
lprintf(network_lock_debug, "thread %x: locking transfer_lock;\n",
pthread_self());
PTHREAD_MUTEX_LOCK(&transfer_lock);
CURLMcode res = curl_multi_add_handle(curl_multi, curl);
@ -289,8 +274,8 @@ void transfer_blocking(CURL *curl)
lprintf(error, "%d, %s\n", res, curl_multi_strerror(res));
}
lprintf(network_lock_debug,
"thread %x: unlocking transfer_lock;\n", pthread_self());
lprintf(network_lock_debug, "thread %x: unlocking transfer_lock;\n",
pthread_self());
PTHREAD_MUTEX_UNLOCK(&transfer_lock);
while (ts->transferring) {
@ -298,10 +283,9 @@ void transfer_blocking(CURL *curl)
}
}
void transfer_nonblocking(CURL *curl)
{
lprintf(network_lock_debug,
"thread %x: locking transfer_lock;\n", pthread_self());
void transfer_nonblocking(CURL *curl) {
lprintf(network_lock_debug, "thread %x: locking transfer_lock;\n",
pthread_self());
PTHREAD_MUTEX_LOCK(&transfer_lock);
CURLMcode res = curl_multi_add_handle(curl_multi, curl);
@ -309,13 +293,12 @@ void transfer_nonblocking(CURL *curl)
lprintf(error, "%s\n", curl_multi_strerror(res));
}
lprintf(network_lock_debug,
"thread %x: unlocking transfer_lock;\n", pthread_self());
lprintf(network_lock_debug, "thread %x: unlocking transfer_lock;\n",
pthread_self());
PTHREAD_MUTEX_UNLOCK(&transfer_lock);
}
int HTTP_temp_failure(HTTPResponseCode http_resp)
{
int HTTP_temp_failure(HTTPResponseCode http_resp) {
switch (http_resp) {
case HTTP_TOO_MANY_REQUESTS:
case HTTP_CLOUDFLARE_UNKNOWN_ERROR:

View File

@ -1,16 +1,16 @@
#include "sonic.h"
#include "config.h"
#include "log.h"
#include "link.h"
#include "log.h"
#include "memcache.h"
#include "util.h"
#include <expat.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
typedef struct {
@ -26,10 +26,8 @@ static SonicConfigStruct SONIC_CONFIG;
/**
* \brief initialise Sonic configuration struct
*/
void
sonic_config_init(const char *server, const char *username,
const char *password)
{
void sonic_config_init(const char *server, const char *username,
const char *password) {
SONIC_CONFIG.server = strndup(server, MAX_PATH_LEN);
/*
* Correct for the extra '/'
@ -59,8 +57,7 @@ sonic_config_init(const char *server, const char *username,
/**
* \brief generate authentication string
*/
static char *sonic_gen_auth_str(void)
{
static char *sonic_gen_auth_str(void) {
if (!CONFIG.sonic_insecure) {
char *salt = generate_salt();
size_t pwd_len = strnlen(SONIC_CONFIG.password, MAX_FILENAME_LEN);
@ -70,20 +67,18 @@ static char *sonic_gen_auth_str(void)
strncat(pwd_salt + pwd_len, salt, MAX_FILENAME_LEN);
char *token = generate_md5sum(pwd_salt);
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",
SONIC_CONFIG.username, token, salt,
SONIC_CONFIG.api_version, SONIC_CONFIG.client);
snprintf(auth_str, MAX_PATH_LEN, ".view?u=%s&t=%s&s=%s&v=%s&c=%s",
SONIC_CONFIG.username, token, salt, SONIC_CONFIG.api_version,
SONIC_CONFIG.client);
FREE(salt);
FREE(token);
return auth_str;
} else {
char *pwd_hex = str_to_hex(SONIC_CONFIG.password);
char *auth_str = CALLOC(MAX_PATH_LEN + 1, sizeof(char));
snprintf(auth_str, MAX_PATH_LEN,
".view?u=%s&p=enc:%s&v=%s&c=%s",
SONIC_CONFIG.username, pwd_hex,
SONIC_CONFIG.api_version, SONIC_CONFIG.client);
snprintf(auth_str, MAX_PATH_LEN, ".view?u=%s&p=enc:%s&v=%s&c=%s",
SONIC_CONFIG.username, pwd_hex, SONIC_CONFIG.api_version,
SONIC_CONFIG.client);
FREE(pwd_hex);
return auth_str;
}
@ -92,12 +87,11 @@ static char *sonic_gen_auth_str(void)
/**
* \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 *url = CALLOC(MAX_PATH_LEN + 1, sizeof(char));
snprintf(url, MAX_PATH_LEN, "%s/rest/%s%s", SONIC_CONFIG.server,
method, auth_str);
snprintf(url, MAX_PATH_LEN, "%s/rest/%s%s", SONIC_CONFIG.server, method,
auth_str);
FREE(auth_str);
return url;
}
@ -105,8 +99,7 @@ static char *sonic_gen_url_first_part(char *method)
/**
* \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 *url = CALLOC(MAX_PATH_LEN + 1, sizeof(char));
snprintf(url, MAX_PATH_LEN, "%s&id=%s", first_part, id);
@ -117,8 +110,7 @@ static char *sonic_getMusicDirectory_link(const char *id)
/**
* \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 *url = CALLOC(MAX_PATH_LEN + 1, sizeof(char));
snprintf(url, MAX_PATH_LEN, "%s&id=%s", first_part, id);
@ -129,8 +121,7 @@ static char *sonic_getArtist_link(const char *id)
/**
* \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 *url = CALLOC(MAX_PATH_LEN + 1, sizeof(char));
snprintf(url, MAX_PATH_LEN, "%s&id=%s", first_part, id);
@ -141,8 +132,7 @@ static char *sonic_getAlbum_link(const char *id)
/**
* \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 *url = CALLOC(MAX_PATH_LEN + 1, sizeof(char));
snprintf(url, MAX_PATH_LEN, "%s&format=raw&id=%s", first_part, id);
@ -164,9 +154,8 @@ static char *sonic_stream_link(const char *id)
* parser terminates the strings properly, which is a fair assumption,
* considering how mature expat is.
*/
static void XMLCALL
XML_parser_general(void *data, const char *elem, const char **attr)
{
static void XMLCALL XML_parser_general(void *data, const char *elem,
const char **attr) {
/*
* Error checking
*/
@ -177,7 +166,7 @@ XML_parser_general(void *data, const char *elem, const char **attr)
}
}
LinkTable *linktbl = (LinkTable *) data;
LinkTable *linktbl = (LinkTable *)data;
Link *link;
/*
@ -190,23 +179,20 @@ XML_parser_general(void *data, const char *elem, const char **attr)
* 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) {
} 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) {
} 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) {
} else if (!strcmp(elem, "song") && linktbl->links[0]->sonic.depth == 4) {
link = CALLOC(1, sizeof(Link));
link->type = LINK_FILE;
} else {
@ -251,8 +237,7 @@ XML_parser_general(void *data, const char *elem, const char **attr)
* N.B. "path" attribute is given the preference
*/
if (!linkname_set) {
if (!strcmp("title", attr[i])
|| !strcmp("name", attr[i])) {
if (!strcmp("title", attr[i]) || !strcmp("name", attr[i])) {
strncpy(link->linkname, attr[i + 1], MAX_FILENAME_LEN);
linkname_set = 1;
continue;
@ -285,20 +270,20 @@ XML_parser_general(void *data, const char *elem, const char **attr)
}
if (!strcmp("title", attr[i])) {
title = (char *) attr[i + 1];
title = (char *)attr[i + 1];
continue;
}
if (!strcmp("suffix", attr[i])) {
suffix = (char *) attr[i + 1];
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);
snprintf(link->linkname, MAX_FILENAME_LEN, "%02d - %s.%s", track, title,
suffix);
linkname_set = 1;
}
@ -319,8 +304,7 @@ XML_parser_general(void *data, const char *elem, const char **attr)
LinkTable_add(linktbl, link);
}
static void sanitise_LinkTable(LinkTable *linktbl)
{
static void sanitise_LinkTable(LinkTable *linktbl) {
for (int i = 0; i < linktbl->num; i++) {
if (!strcmp(linktbl->links[i]->linkname, ".")) {
/* Note the super long sanitised name to avoid collision */
@ -348,8 +332,8 @@ static void sanitise_LinkTable(LinkTable *linktbl)
* \brief parse a XML string in order to fill in the LinkTable
*/
static LinkTable *sonic_url_to_LinkTable(const char *url,
XML_StartElementHandler handler, int depth)
{
XML_StartElementHandler handler,
int depth) {
LinkTable *linktbl = LinkTable_alloc(url);
linktbl->links[0]->sonic.depth = depth;
@ -368,8 +352,7 @@ static LinkTable *sonic_url_to_LinkTable(const char *url,
XML_SetStartElementHandler(parser, handler);
if (XML_Parse(parser, xml.data, xml.curr_size, 1) == XML_STATUS_ERROR) {
lprintf(error,
"Parse error at line %lu: %s\n",
lprintf(error, "Parse error at line %lu: %s\n",
XML_GetCurrentLineNumber(parser),
XML_ErrorString(XML_GetErrorCode(parser)));
}
@ -383,26 +366,22 @@ static LinkTable *sonic_url_to_LinkTable(const char *url,
sanitise_LinkTable(linktbl);
return linktbl;
}
LinkTable *sonic_LinkTable_new_index(const char *id)
{
LinkTable *sonic_LinkTable_new_index(const char *id) {
char *url;
if (strcmp(id, "0")) {
url = sonic_getMusicDirectory_link(id);
} else {
url = sonic_gen_url_first_part("getIndexes");
}
LinkTable *linktbl =
sonic_url_to_LinkTable(url, XML_parser_general, 0);
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)
{
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) {
@ -410,15 +389,14 @@ XML_parser_id3_root(void *data, const char *elem, const char **attr)
}
}
LinkTable *root_linktbl = (LinkTable *) data;
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;
this_linktbl = root_linktbl->links[root_linktbl->num - 1]->next_table;
}
int id_set = 0;
@ -464,8 +442,7 @@ XML_parser_id3_root(void *data, const char *elem, const char **attr)
}
if (!strcmp("id", attr[i])) {
link->sonic.id =
CALLOC(MAX_FILENAME_LEN + 1, sizeof(char));
link->sonic.id = CALLOC(MAX_FILENAME_LEN + 1, sizeof(char));
strncpy(link->sonic.id, attr[i + 1], MAX_FILENAME_LEN);
id_set = 1;
continue;
@ -488,8 +465,7 @@ XML_parser_id3_root(void *data, const char *elem, const char **attr)
*/
}
LinkTable *sonic_LinkTable_new_id3(int depth, const char *id)
{
LinkTable *sonic_LinkTable_new_id3(int depth, const char *id) {
char *url;
LinkTable *linktbl = ROOT_LINK_TBL;
switch (depth) {

View File

@ -12,7 +12,6 @@
#include <string.h>
#include <unistd.h>
/**
* \brief Backtrace buffer size
*/
@ -29,11 +28,9 @@
*/
#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;
if ((path[strnlen(path, MAX_PATH_LEN) - 1] != '/')
&& (filename[0] != '/')) {
if ((path[strnlen(path, MAX_PATH_LEN) - 1] != '/') && (filename[0] != '/')) {
needs_separator = 1;
}
@ -49,47 +46,37 @@ char *path_append(const char *path, const char *filename)
return str;
}
int64_t round_div(int64_t a, int64_t b)
{
return (a + (b / 2)) / b;
}
int64_t round_div(int64_t a, int64_t b) { return (a + (b / 2)) / b; }
void PTHREAD_MUTEX_UNLOCK(pthread_mutex_t *x)
{
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));
lprintf(fatal, "thread %x: %d, %s\n", pthread_self(), i, strerror(i));
}
}
void PTHREAD_MUTEX_LOCK(pthread_mutex_t *x)
{
void PTHREAD_MUTEX_LOCK(pthread_mutex_t *x) {
int i;
i = pthread_mutex_lock(x);
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 exit_failure(void)
{
void exit_failure(void) {
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);
fprintf(stderr, "backtrace() returned the following %d addresses:\n", nptrs);
backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO);
exit(EXIT_FAILURE);
}
void erase_string(FILE *file, size_t max_len, char *s)
{
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");
@ -102,8 +89,7 @@ void erase_string(FILE *file, size_t max_len, char *s)
}
}
char *generate_salt(void)
{
char *generate_salt(void) {
char *out;
out = CALLOC(SALT_LEN + 1, sizeof(char));
uuid_t uu;
@ -112,8 +98,7 @@ char *generate_salt(void)
return out;
}
char *generate_md5sum(const char *str)
{
char *generate_md5sum(const char *str) {
MD5_CTX c;
unsigned char md5[MD5_DIGEST_LENGTH];
size_t len = strnlen(str, MAX_PATH_LEN);
@ -129,8 +114,7 @@ char *generate_md5sum(const char *str)
return out;
}
void *CALLOC(size_t nmemb, size_t size)
{
void *CALLOC(size_t nmemb, size_t size) {
void *ptr = calloc(nmemb, size);
if (!ptr) {
lprintf(fatal, "%s!\n", strerror(errno));
@ -138,8 +122,7 @@ void *CALLOC(size_t nmemb, size_t size)
return ptr;
}
void FREE(void *ptr)
{
void FREE(void *ptr) {
if (ptr) {
free(ptr);
} else {
@ -147,8 +130,7 @@ void FREE(void *ptr)
}
}
char *str_to_hex(char *s)
{
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);