From 2d42313e8f80c2c067ea00045a16f9b205043f8c Mon Sep 17 00:00:00 2001 From: Fufu Fang Date: Thu, 2 Sep 2021 15:36:53 +0100 Subject: [PATCH] compiles, but not running properly --- Makefile | 2 +- src/cache.c | 6 ++--- src/link.c | 41 ++++++++++++++++++++-------------- src/link.h | 64 +++++++++++++++++++++++++++++++---------------------- src/log.h | 2 +- src/main.c | 2 +- src/sonic.c | 2 +- 7 files changed, 68 insertions(+), 51 deletions(-) diff --git a/Makefile b/Makefile index 630ecad..ae38cfb 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ VERSION = 1.2.3 -CFLAGS += -O2 -Wall -Wextra -Wshadow -fanalyzer -fsanitize=undefined \ +CFLAGS += -O2 -g -Wall -Wextra -Wshadow -fanalyzer -fsanitize=undefined \ -Wno-analyzer-file-leak\ -rdynamic -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -DVERSION=\"$(VERSION)\"\ `pkg-config --cflags-only-I gumbo libcurl fuse uuid expat` diff --git a/src/cache.c b/src/cache.c index 5439ef5..f4f257b 100644 --- a/src/cache.c +++ b/src/cache.c @@ -89,7 +89,7 @@ static char *CacheSystem_calc_dir(const char *url) void CacheSystem_init(const char *path, int url_supplied) { - if (pthread_mutex_init(&cf_lock, NULL)) { + if (pthread_mutex_lock(&cf_lock, NULL)) { lprintf(fatal, "cf_lock initialisation failed!\n"); } @@ -940,7 +940,7 @@ static void *Cache_bgdl(void *arg) uint8_t *recv_buf = CALLOC(cf->blksz, sizeof(uint8_t)); lprintf(debug, "thread %x spawned.\n ", pthread_self()); - long recv = path_download(cf->fs_path, (char *) recv_buf, cf->blksz, + 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, \ @@ -1019,7 +1019,7 @@ 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 = path_download(cf->fs_path, (char *) recv_buf, cf->blksz, + long recv = Link_download(cf->link, (char *) recv_buf, cf->blksz, dl_offset); if (recv < 0) { lprintf(error, "thread %x received %ld bytes, \ diff --git a/src/link.c b/src/link.c index bff61ae..4ac457d 100644 --- a/src/link.c +++ b/src/link.c @@ -112,7 +112,7 @@ static void Link_req_file_stat(Link * this_link) * We need to put the variable on the heap, because otherwise the * variable gets popped from the stack as the function returns. * - * It gets freed in curl_multi_perform_once(); + * It gets freed in curl_process_msgs(); */ TransferStruct *transfer = CALLOC(1, sizeof(TransferStruct)); @@ -435,10 +435,10 @@ void LinkTable_print(LinkTable * linktbl) } } -TransferStruct Link_to_TransferStruct(Link * head_link) +TransferStruct Link_download_full(Link * link) { - char *url = head_link->f_url; - CURL *curl = Link_to_curl(head_link); + char *url = link->f_url; + CURL *curl = Link_to_curl(link); TransferStruct buf; buf.size = 0; @@ -468,7 +468,7 @@ TransferStruct Link_to_TransferStruct(Link * head_link) } while (HTTP_temp_failure(http_resp)); - curl_easy_getinfo(curl, CURLINFO_FILETIME, &(head_link->time)); + curl_easy_getinfo(curl, CURLINFO_FILETIME, &(link->time)); curl_easy_cleanup(curl); return buf; } @@ -494,7 +494,7 @@ LinkTable *LinkTable_new(const char *url) /* * start downloading the base URL */ - TransferStruct buf = Link_to_TransferStruct(linktbl->links[0]); + TransferStruct buf = Link_download_full(linktbl->links[0]); if (buf.size == 0) { LinkTable_free(linktbl); return NULL; @@ -793,23 +793,14 @@ Link *path_to_Link(const char *path) } long -path_download(const char *path, char *output_buf, size_t req_size, +Link_download(Link *link, char *output_buf, size_t req_size, off_t offset) { - if (!path) { - lprintf(fatal, "NULL path supplied\n"); - } - Link *link; - link = path_to_Link(path); - if (!link) { - return -ENOENT; - } - 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", path, range_str); + lprintf(debug, "%s: %s\n", link->linkname, range_str); TransferStruct buf; buf.size = 0; @@ -866,3 +857,19 @@ range requests\n"); return recv; } + +long +path_download(const char *path, char *output_buf, size_t req_size, + off_t offset) +{ + if (!path) { + lprintf(fatal, "NULL path supplied\n"); + } + Link *link; + link = path_to_Link(path); + if (!link) { + return -ENOENT; + } + + return Link_download(link, output_buf, req_size, offset); +} diff --git a/src/link.h b/src/link.h index 0bbada9..c18126f 100644 --- a/src/link.h +++ b/src/link.h @@ -33,6 +33,33 @@ typedef enum { DATA = 'd' } TransferType; +/** + * \brief For storing transfer data and metadata + */ +typedef struct { + /** \brief The array to store the data */ + char *data; + /** \brief The size of the array */ + size_t size; + /** \brief The minium requested size */ + size_t min_req_size; + /** \brief The type of transfer being done */ + TransferType type; + /** \brief Whether transfer is in progress */ + int transferring; + /** \brief The link associated with the transfer */ + Link *link; +} TransferStruct; + +/** + * \brief link table type + * \details index 0 contains the Link for the base URL + */ +struct LinkTable { + int num; + Link **links; +}; + /** * \brief Link type data structure */ @@ -51,35 +78,12 @@ struct Link { long time; /** \brief The pointer associated with the cache file */ Cache *cache_ptr; + /** \brief The pointer associated with the transfer struct */ + TransferStruct *ts_ptr; /** \brief Stores *sonic related data */ Sonic sonic; }; -/** - * \brief link table type - * \details index 0 contains the Link for the base URL - */ -struct LinkTable { - int num; - Link **links; -}; - -/** \brief For storing transfer data */ -typedef struct { - /** \brief The array to store the data */ - char *data; - /** \brief The size of the array */ - size_t size; - /** \brief The minium requested size */ - size_t min_req_size; - /** \brief The type of transfer being done */ - TransferType type; - /** \brief Whether transfer is in progress */ - int transferring; - /** \brief The link associated with the transfer */ - Link *link; -} TransferStruct; - /** * \brief root link table */ @@ -106,12 +110,18 @@ void Link_set_file_stat(Link * this_link, CURL * curl); LinkTable *LinkTable_new(const char *url); /** - * \brief download a link + * \brief download a path * \return the number of bytes downloaded */ long path_download(const char *path, char *output_buf, size_t size, off_t offset); +/** + * \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); + /** * \brief find the link associated with a path */ @@ -136,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_to_TransferStruct(Link * head_link); +TransferStruct Link_download_full(Link * head_link); /** * \brief Allocate a LinkTable diff --git a/src/log.h b/src/log.h index 7fa04b2..15adde9 100644 --- a/src/log.h +++ b/src/log.h @@ -18,7 +18,7 @@ typedef enum { /** * \brief The default log level */ -#define DEFAULT_LOG_LEVEL fatal | error | warning | info | debug +#define DEFAULT_LOG_LEVEL fatal | error | warning | info | debug | cache_lock_debug /** * \brief Get the log level from the environment. diff --git a/src/main.c b/src/main.c index d3c58e0..6ea8edc 100644 --- a/src/main.c +++ b/src/main.c @@ -163,9 +163,9 @@ void parse_config_file(char ***argv, int *argc) } } } + fclose(config); } FREE(full_path); - fclose(config); } static int diff --git a/src/sonic.c b/src/sonic.c index 9383e9e..0995022 100644 --- a/src/sonic.c +++ b/src/sonic.c @@ -332,7 +332,7 @@ static LinkTable *sonic_url_to_LinkTable(const char *url, /* * start downloading the base URL */ - TransferStruct xml = Link_to_TransferStruct(linktbl->links[0]); + TransferStruct xml = Link_download_full(linktbl->links[0]); if (xml.size == 0) { LinkTable_free(linktbl); return NULL;