From 65a9e7f9086fe11a856c9be33eafb63bddebee2f Mon Sep 17 00:00:00 2001 From: Fufu Fang Date: Tue, 22 Oct 2019 00:42:46 +0100 Subject: [PATCH] half way writing sonic_LinkTable_new - now need to write the parser --- Makefile | 2 +- src/link.c | 55 +++++++++++++++++++++++++++++++++------------------ src/link.h | 38 ++++++++++++++++++++++++++++++++++- src/network.h | 16 --------------- src/sonic.c | 34 ++++++++++++++++++++----------- 5 files changed, 96 insertions(+), 49 deletions(-) diff --git a/Makefile b/Makefile index 8ecc903..3e6e6d5 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,7 @@ CFLAGS+= -O2 -Wall -Wextra -Wshadow -rdynamic\ -D_FILE_OFFSET_BITS=64 -DVERSION=\"$(VERSION)\" \ `pkg-config --cflags-only-I gumbo libcurl fuse uuid` LIBS = -pthread -lgumbo -lcurl -lfuse -lcrypto -luuid -COBJS = network.o fuse_local.o link.o cache.o util.o main.o +COBJS = main.o network.o fuse_local.o link.o cache.o util.o sonic.o prefix ?= /usr/local diff --git a/src/link.c b/src/link.c index bf29513..17fb088 100644 --- a/src/link.c +++ b/src/link.c @@ -275,7 +275,7 @@ static void LinkTable_invalid_reset(LinkTable *linktbl) fprintf(stderr, "LinkTable_invalid_reset(): %d invalid links\n", j); } -static void LinkTable_free(LinkTable *linktbl) +void LinkTable_free(LinkTable *linktbl) { for (int i = 0; i < linktbl->num; i++) { free(linktbl->links[i]); @@ -312,23 +312,9 @@ static void LinkTable_print(LinkTable *linktbl) fprintf(stderr, "--------------------------------------------\n"); } -LinkTable *LinkTable_new(const char *url) +MemoryStruct Link_to_MemoryStruct(Link *head_link) { - #ifdef LINK_LOCK_DEBUG - fprintf(stderr, - "LinkTable_new(): thread %lu: locking link_lock;\n", - pthread_self()); - #endif - PTHREAD_MUTEX_LOCK(&link_lock); - LinkTable *linktbl = CALLOC(1, sizeof(LinkTable)); - - /* populate the base URL */ - LinkTable_add(linktbl, Link_new("/", LINK_HEAD)); - Link *head_link = linktbl->links[0]; - head_link->type = LINK_HEAD; - strncpy(head_link->f_url, url, MAX_PATH_LEN); - - /* start downloading the base URL */ + char *url = head_link->f_url; CURL *curl = Link_to_curl(head_link); MemoryStruct buf; buf.size = 0; @@ -349,15 +335,46 @@ LinkTable *LinkTable_new(const char *url) fprintf(stderr, "LinkTable_new(): cannot retrieve URL: %s, HTTP %ld\n", url, http_resp); - LinkTable_free(linktbl); + buf.size = 0; curl_easy_cleanup(curl); - return NULL; + return buf; } } while (HTTP_temp_failure(http_resp)); curl_easy_getinfo(curl, CURLINFO_FILETIME, &(head_link->time)); curl_easy_cleanup(curl); + return buf; +} + +LinkTable *LinkTable_alloc(const char *url) +{ + LinkTable *linktbl = CALLOC(1, sizeof(LinkTable)); + + /* populate the base URL */ + Link *head_link = Link_new("/", LINK_HEAD); + LinkTable_add(linktbl, head_link); + strncpy(head_link->f_url, url, MAX_PATH_LEN); + return linktbl; +} + +LinkTable *LinkTable_new(const char *url) +{ + #ifdef LINK_LOCK_DEBUG + fprintf(stderr, + "LinkTable_new(): thread %lu: locking link_lock;\n", + pthread_self()); + #endif + PTHREAD_MUTEX_LOCK(&link_lock); + + LinkTable *linktbl = LinkTable_alloc(url); + + /* start downloading the base URL */ + MemoryStruct buf = Link_to_MemoryStruct(linktbl->links[0]); + if (buf.size == 0) { + LinkTable_free(linktbl); + return NULL; + } /* Otherwise parsed the received data */ GumboOutput* output = gumbo_parse(buf.memory); diff --git a/src/link.h b/src/link.h index a47523b..c59c0f0 100644 --- a/src/link.h +++ b/src/link.h @@ -7,7 +7,6 @@ */ #include "util.h" - #include /** \brief Link type */ @@ -24,6 +23,25 @@ typedef enum { LINK_UNINITIALISED_FILE = 'U' } LinkType; +/** \brief for storing downloaded data in memory */ +typedef struct { + char *memory; + size_t size; +} MemoryStruct; + +/** \brief specify the type of data transfer */ +typedef enum { + FILESTAT = 's', + DATA = 'd' +} TransferType; + +/** \brief for storing the link being transferred, and metadata */ +typedef struct { + TransferType type; + int transferring; + Link *link; +} TransferStruct; + /** * \brief link table type * \details index 0 contains the Link for the base URL @@ -118,4 +136,22 @@ int LinkTable_disk_save(LinkTable *linktbl, const char *dirn); * \brief load a link table from the disk. */ LinkTable *LinkTable_disk_open(const char *dirn); + +/** + * \brief Download a link's content to the memory + * \warning You MUST free the memory field in MemoryStruct after use! + */ +MemoryStruct Link_to_MemoryStruct(Link *head_link); + +/** + * \brief Allocate a LinkTable + * \note This does not fill in the LinkTable. + */ +LinkTable *LinkTable_alloc(const char *url); + +/** + * \brief free a LinkTable + */ +void LinkTable_free(LinkTable *linktbl); + #endif diff --git a/src/network.h b/src/network.h index 5be2039..9eb1894 100644 --- a/src/network.h +++ b/src/network.h @@ -13,22 +13,6 @@ */ #define DEFAULT_USER_AGENT "HTTPDirFS-" VERSION -typedef enum { - FILESTAT = 's', - DATA = 'd' -} TransferType; - -typedef struct { - char *memory; - size_t size; -} MemoryStruct; - -typedef struct { - TransferType type; - int transferring; - Link *link; -} TransferStruct; - typedef struct { char *username; char *password; diff --git a/src/sonic.c b/src/sonic.c index 4c6013b..ee46618 100644 --- a/src/sonic.c +++ b/src/sonic.c @@ -1,6 +1,7 @@ #include "sonic.h" #include "util.h" +#include "link.h" #include "network.h" @@ -75,7 +76,7 @@ static char *sonic_gen_url_first_part(char *method) return url; } -LinkTable *sonic_LinkTable_new(int id) +LinkTable *sonic_LinkTable_new(const int id) { char *url; if (id > 0) { @@ -87,18 +88,27 @@ LinkTable *sonic_LinkTable_new(int id) url = sonic_gen_url_first_part("getIndexes"); } - printf("%s\n", url); - LinkTable *linktbl = CALLOC(1, sizeof(LinkTable)); + LinkTable *linktbl = LinkTable_alloc(url); + /* start downloading the base URL */ + MemoryStruct buf = Link_to_MemoryStruct(linktbl->links[0]); + if (buf.size == 0) { + LinkTable_free(linktbl); + return NULL; + } + + + + free(url); return NULL; } -int main(int argc, char **argv) -{ - (void) argc; - (void) argv; - - sonic_config_init(argv[1], argv[2], argv[3]); - sonic_LinkTable_new(0); - sonic_LinkTable_new(3); -} +// int main(int argc, char **argv) +// { +// (void) argc; +// (void) argv; +// +// sonic_config_init(argv[1], argv[2], argv[3]); +// sonic_LinkTable_new(0); +// sonic_LinkTable_new(3); +// }