more refactoring

This commit is contained in:
Fufu Fang 2021-09-03 14:56:11 +01:00
parent bc88a681e3
commit cd6bb5bee8
12 changed files with 134 additions and 129 deletions

View File

@ -63,11 +63,12 @@ doc:
doxygen Doxyfile
format:
indent -kr -nut src/*.c src/*.h
astyle --style=kr --align-pointer=name src/*.c src/*.h
clean:
-rm -f src/*.h~
-rm -f src/*.c~
-rm -f src/*orig
-rm -f *.o
-rm -f httpdirfs

View File

@ -148,7 +148,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);
@ -236,7 +236,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);
@ -279,7 +279,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;
@ -326,7 +326,7 @@ 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");
@ -379,7 +379,7 @@ static long Data_read(Cache * cf, uint8_t * buf, off_t len, off_t offset)
}
}
end:
end:
lprintf(cache_lock_debug,
"thread %x: unlocking seek_lock;\n", pthread_self());
@ -397,7 +397,7 @@ static long Data_read(Cache * cf, uint8_t * buf, off_t len, off_t offset)
* - -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,
static long Data_write(Cache *cf, const uint8_t *buf, off_t len,
off_t offset)
{
if (len == 0) {
@ -437,7 +437,7 @@ static long Data_write(Cache * cf, const uint8_t * buf, off_t len,
lprintf(error, "fwrite(): encountered error!\n");
}
end:
end:
lprintf(cache_lock_debug,
"thread %x: unlocking seek_lock;\n", pthread_self());
PTHREAD_MUTEX_UNLOCK(&cf->seek_lock);
@ -499,7 +499,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");
@ -605,7 +605,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+");
@ -627,7 +627,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+");
@ -647,7 +647,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");
@ -869,7 +869,7 @@ cf->content_length: %ld, Data_size(fn): %ld.\n", fn, cf->content_length, Data_si
return cf;
}
void Cache_close(Cache * cf)
void Cache_close(Cache *cf)
{
lprintf(cache_lock_debug,
"thread %x: locking cf_lock;\n", pthread_self());
@ -909,7 +909,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];
@ -922,7 +922,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;
@ -976,7 +976,7 @@ error.\n", recv, cf->blksz);
}
long
Cache_read(Cache * cf, char *const output_buf, const off_t len,
Cache_read(Cache *cf, char *const output_buf, const off_t len,
const off_t offset_start)
{
long send;
@ -1053,8 +1053,7 @@ error.\n", recv, cf->blksz);
/*
* ----------- 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)

View File

@ -118,7 +118,7 @@ Cache *Cache_open(const char *fn);
* \brief Close a cache data structure
* \note This function is called by fs_release()
*/
void Cache_close(Cache * cf);
void Cache_close(Cache *cf);
/**
* \brief create a cache file set if it doesn't exist already
@ -146,6 +146,6 @@ void Cache_delete(const char *fn);
* \return the length of the segment the cache system managed to obtain.
* \note Called by fs_read(), verified to be working
*/
long Cache_read(Cache * cf, char *const output_buf, const off_t len,
long Cache_read(Cache *cf, char *const output_buf, const off_t len,
const off_t offset_start);
#endif

View File

@ -52,7 +52,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)
{
CURL *curl = curl_easy_init();
if (!curl) {
@ -155,7 +155,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)
{
CURL *curl = Link_to_curl(this_link);
CURLcode ret = curl_easy_setopt(curl, CURLOPT_NOBODY, 1);
@ -190,7 +190,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];
@ -220,8 +220,7 @@ static void LinkTable_uninitialised_fill(LinkTable * linktbl)
j++;
}
}
}
while (u);
} while (u);
if (CONFIG.log_type & debug) {
erase_string(stderr, STATUS_LEN, s);
fprintf(stderr, "... Done!\n");
@ -297,7 +296,7 @@ LinkTable *LinkSystem_init(const char *raw_url)
return ROOT_LINK_TBL;
}
void LinkTable_add(LinkTable * linktbl, Link * link)
void LinkTable_add(LinkTable *linktbl, Link *link)
{
linktbl->num++;
linktbl->links =
@ -362,7 +361,7 @@ static int linknames_equal(char *linkname, const char *linkname_new)
* Shamelessly copied and pasted from:
* https://github.com/google/gumbo-parser/blob/master/examples/find_links.cc
*/
static void HTML_to_LinkTable(GumboNode * node, LinkTable * linktbl)
static void HTML_to_LinkTable(GumboNode *node, LinkTable *linktbl)
{
if (node->type != GUMBO_NODE_ELEMENT) {
return;
@ -400,7 +399,7 @@ static void HTML_to_LinkTable(GumboNode * node, LinkTable * 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 =
@ -437,7 +436,7 @@ void Link_set_file_stat(Link * this_link, CURL * curl)
}
}
static void LinkTable_fill(LinkTable * linktbl)
static void LinkTable_fill(LinkTable *linktbl)
{
Link *head_link = linktbl->links[0];
for (int i = 1; i < linktbl->num; i++) {
@ -460,7 +459,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++) {
@ -473,7 +472,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)
{
for (int i = 0; i < linktbl->num; i++) {
FREE(linktbl->links[i]);
@ -482,7 +481,7 @@ void LinkTable_free(LinkTable * linktbl)
FREE(linktbl);
}
void LinkTable_print(LinkTable * linktbl)
void LinkTable_print(LinkTable *linktbl)
{
if (CONFIG.log_type & info) {
int j = 0;
@ -612,7 +611,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;
@ -728,7 +727,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
@ -828,7 +827,7 @@ Link *path_to_Link(const char *path)
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);
@ -870,8 +869,7 @@ TransferStruct Link_download_full(Link * link)
curl_easy_cleanup(curl);
return ts;
}
}
while (HTTP_temp_failure(http_resp));
} while (HTTP_temp_failure(http_resp));
ret = curl_easy_getinfo(curl, CURLINFO_FILETIME, &(link->time));
if (ret) {
@ -881,8 +879,10 @@ TransferStruct Link_download_full(Link * link)
return ts;
}
long
Link_download(Link * link, char *output_buf, size_t req_size, off_t offset)
static CURL *Link_download_curl_setup(Link *link, size_t req_size,
off_t offset,
TransferStruct *header,
TransferStruct *ts)
{
if (!link) {
lprintf(fatal, "Invalid supplied\n");
@ -890,10 +890,36 @@ Link_download(Link * link, char *output_buf, size_t req_size, off_t offset)
size_t start = offset;
size_t end = start + req_size;
char range_str[64];
snprintf(range_str, sizeof(range_str), "%lu-%lu", start, end);
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);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
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);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
ret = curl_easy_setopt(curl, CURLOPT_RANGE, range_str);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
return curl;
}
long
Link_download(Link *link, char *output_buf, size_t req_size, off_t offset)
{
TransferStruct ts;
ts.size = 0;
ts.data = NULL;
@ -904,24 +930,8 @@ Link_download(Link * link, char *output_buf, size_t req_size, off_t offset)
header.size = 0;
header.data = NULL;
CURL *curl = Link_to_curl(link);
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);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
ret = curl_easy_setopt(curl, CURLOPT_PRIVATE, (void *) &ts);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
ret = curl_easy_setopt(curl, CURLOPT_RANGE, range_str);
if (ret) {
lprintf(error, "%s", curl_easy_strerror(ret));
}
CURL *curl = Link_download_curl_setup(link, req_size, offset, &header, &ts);
transfer_blocking(curl);
/*
@ -938,7 +948,7 @@ range requests\n");
FREE(header.data);
long http_resp;
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));
}

View File

@ -101,7 +101,7 @@ LinkTable *LinkSystem_init(const char *raw_url);
/**
* \brief Set the stats of a link, after curl multi handle finished querying
*/
void Link_set_file_stat(Link * this_link, CURL * curl);
void Link_set_file_stat(Link *this_link, CURL *curl);
/**
* \brief create a new LinkTable
@ -119,7 +119,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,
long Link_download(Link *link, char *output_buf, size_t req_size,
off_t offset);
/**
@ -135,7 +135,7 @@ LinkTable *path_to_Link_LinkTable_new(const char *path);
/**
* \brief dump a link table to the disk.
*/
int LinkTable_disk_save(LinkTable * linktbl, const char *dirn);
int LinkTable_disk_save(LinkTable *linktbl, const char *dirn);
/**
* \brief load a link table from the disk.
@ -146,7 +146,7 @@ LinkTable *LinkTable_disk_open(const char *dirn);
* \brief Download a link's content to the memory
* \warning You MUST free the memory field in TransferStruct after use!
*/
TransferStruct Link_download_full(Link * head_link);
TransferStruct Link_download_full(Link *head_link);
/**
* \brief Allocate a LinkTable
@ -157,15 +157,15 @@ LinkTable *LinkTable_alloc(const char *url);
/**
* \brief free a LinkTable
*/
void LinkTable_free(LinkTable * linktbl);
void LinkTable_free(LinkTable *linktbl);
/**
* \brief print a LinkTable
*/
void LinkTable_print(LinkTable * linktbl);
void LinkTable_print(LinkTable *linktbl);
/**
* \brief add a Link to a LinkTable
*/
void LinkTable_add(LinkTable * linktbl, Link * link);
void LinkTable_add(LinkTable *linktbl, Link *link);
#endif

View File

@ -42,8 +42,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;

View File

@ -110,7 +110,7 @@ activate Sonic mode.\n");
}
}
fuse_start:
fuse_start:
fuse_local_init(fuse_argc, fuse_argv);
return 0;

View File

@ -87,7 +87,7 @@ failed!\n", i);
* https://curl.haxx.se/libcurl/c/threaded-shared-conn.html
*/
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)
{
(void) access; /* unused */
@ -98,7 +98,7 @@ curl_callback_lock(CURL * handle, curl_lock_data data,
}
static void
curl_callback_unlock(CURL * handle, curl_lock_data data, void *userptr)
curl_callback_unlock(CURL *handle, curl_lock_data data, void *userptr)
{
(void) userptr; /* unused */
(void) handle; /* unused */
@ -112,7 +112,7 @@ curl_callback_unlock(CURL * handle, curl_lock_data data, void *userptr)
* 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)
curl_process_msgs(CURLMsg *curl_msg, int n_running_curl, int n_mesgs)
{
(void) n_running_curl;
(void) n_mesgs;
@ -316,7 +316,7 @@ 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);
@ -343,11 +343,7 @@ void transfer_blocking(CURL * curl)
}
}
// void transfer_semiblocking(CURL *curl) {
// }
void transfer_nonblocking(CURL * curl)
void transfer_nonblocking(CURL *curl)
{
lprintf(network_lock_debug,
"thread %x: locking transfer_lock;\n", pthread_self());

View File

@ -28,10 +28,10 @@ int curl_multi_perform_once(void);
void NetworkSystem_init(void);
/** \brief blocking file transfer */
void transfer_blocking(CURL * curl);
void transfer_blocking(CURL *curl);
/** \brief non blocking file transfer */
void transfer_nonblocking(CURL * curl);
void transfer_nonblocking(CURL *curl);
/** \brief callback function for file transfer */
size_t

View File

@ -52,7 +52,7 @@ 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);
@ -62,7 +62,7 @@ void PTHREAD_MUTEX_UNLOCK(pthread_mutex_t * x)
}
}
void PTHREAD_MUTEX_LOCK(pthread_mutex_t * x)
void PTHREAD_MUTEX_LOCK(pthread_mutex_t *x)
{
int i;
i = pthread_mutex_lock(x);
@ -86,7 +86,7 @@ void exit_failure(void)
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++) {

View File

@ -25,12 +25,12 @@ int64_t round_div(int64_t a, int64_t b);
/**
* \brief wrapper for pthread_mutex_lock(), with error handling
*/
void PTHREAD_MUTEX_LOCK(pthread_mutex_t * x);
void PTHREAD_MUTEX_LOCK(pthread_mutex_t *x);
/**
* \brief wrapper for pthread_mutex_unlock(), with error handling
*/
void PTHREAD_MUTEX_UNLOCK(pthread_mutex_t * x);
void PTHREAD_MUTEX_UNLOCK(pthread_mutex_t *x);
/**
* \brief wrapper for exit(EXIT_FAILURE), with error handling
@ -40,7 +40,7 @@ void exit_failure(void);
/**
* \brief erase a string from the terminal
*/
void erase_string(FILE * file, size_t max_len, char *s);
void erase_string(FILE *file, size_t max_len, char *s);
/**
* \brief generate the salt for authentication string