half way writing sonic_LinkTable_new

- now need to write the parser
This commit is contained in:
Fufu Fang 2019-10-22 00:42:46 +01:00
parent de2e5c457f
commit 65a9e7f908
No known key found for this signature in database
GPG Key ID: 0F6BB5EF6F8BB729
5 changed files with 96 additions and 49 deletions

View File

@ -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

View File

@ -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);

View File

@ -7,7 +7,6 @@
*/
#include "util.h"
#include <curl/curl.h>
/** \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

View File

@ -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;

View File

@ -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);
// }