diff --git a/Makefile b/Makefile index b982cf6..b469234 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CC=gcc CFLAGS= -Wall -Wextra -lgumbo -lcurl -g -OBJ = main.o network.o test.o +OBJ = main.o network.o %.o: %.c $(CC) -c -o $@ $< $(CFLAGS) diff --git a/TODO.md b/TODO.md deleted file mode 100644 index 6063c44..0000000 --- a/TODO.md +++ /dev/null @@ -1,3 +0,0 @@ -- implement fseek -- work on linklist again - append it so it includes curl data structure -- implement function which generates the initial file list. diff --git a/data.h b/data.h index c5963a1..0915e87 100644 --- a/data.h +++ b/data.h @@ -20,23 +20,31 @@ typedef enum { LINK_INVALID = 'I' } LinkType; +/** + * \brief link table type + * \details index 0 contains the Link for the base URL + */ +typedef struct LinkTable LinkTable; + /** \brief link data type */ -typedef struct { +typedef struct Link Link; + +struct Link { char p_url[255]; LinkType type; CURL *curl; char *body; size_t body_sz; size_t content_length; -} Link; + LinkTable *nextTable; +}; -/** - * \brief link table type - * \details index 0 contains the Link for the base URL - */ -typedef struct { + +struct LinkTable { int num; Link **links; -} LinkTable; +}; + + #endif diff --git a/fuse_local.c b/fuse_local.c index 0f442f8..899f00a 100644 --- a/fuse_local.c +++ b/fuse_local.c @@ -1,19 +1,32 @@ -#include #include -#include #include #include #include -#include -#include #include "network.h" #include "fuse_local.h" -static struct fuse_lowlevel_ops fs_ops = { - .lookup = fs_lookup, +static struct fuse_operations fs_oper = { .getattr = fs_getattr, .readdir = fs_readdir, .open = fs_open, .read = fs_read, }; + +static int fs_getattr(const char *path, struct stat *stbuf) +{ + int res = 0; + + memset(stbuf, 0, sizeof(struct stat)); + if (strcmp(path, "/") == 0) { + stbuf->st_mode = S_IFDIR | 0755; + stbuf->st_nlink = 2; + } else if (strcmp(path, hello_path) == 0) { + stbuf->st_mode = S_IFREG | 0444; + stbuf->st_nlink = 1; + stbuf->st_size = strlen(hello_str); + } else + res = -ENOENT; + + return res; +} diff --git a/fuse_local.h b/fuse_local.h index 2956c6b..1e08e57 100644 --- a/fuse_local.h +++ b/fuse_local.h @@ -1,4 +1,9 @@ -#ifndef FUSE_H -#define FUSE_H +#ifndef FUSE_LOCAL_H +#define FUSE_LOCAL_H + +/* must be included before including */ +#define FUSE_USE_VERSION 26 + +#include #endif diff --git a/main.c b/main.c index 6c3238b..feaf393 100644 --- a/main.c +++ b/main.c @@ -4,7 +4,19 @@ #include #include "network.h" -#include "test.h" +void link_test() +{ + printf("--- start of link_test ---\n"); + + LinkTable *linktbl = LinkTable_new( + "http://127.0.0.1/~fangfufu/"); + + LinkTable_print(linktbl); + Link_download(linktbl->links[1], 1, 20); + printf(linktbl->links[1]->body); + LinkTable_free(linktbl); + printf("\n--- end of link_test ---\n\n"); +} void init() { diff --git a/network.c b/network.c index 265857e..a504531 100644 --- a/network.c +++ b/network.c @@ -110,6 +110,12 @@ Link *Link_new(const char *p_url) strncpy(link->p_url, p_url, LINK_LEN_MAX); + /* remove the '/' */ + char *c = &(link->p_url[strnlen(link->p_url, LINK_LEN_MAX) - 1]); + if ( *c == '/') { + *c = '\0'; + } + link->type = LINK_UNKNOWN; link->curl = curl_easy_init(); @@ -119,6 +125,12 @@ Link *Link_new(const char *p_url) curl_easy_setopt(link->curl, CURLOPT_WRITEDATA, (void *)link); curl_easy_setopt(link->curl, CURLOPT_USERAGENT, "mount-http-dir/libcurl"); curl_easy_setopt(link->curl, CURLOPT_VERBOSE, 0); + curl_easy_setopt(link->curl, CURLOPT_FOLLOWLOCATION, 1); + /* + * only 1 redirection is really needed + * - for following directories without the '/' + */ + curl_easy_setopt(link->curl, CURLOPT_MAXREDIRS, 3); return link; } @@ -151,7 +163,7 @@ LinkTable *LinkTable_new(const char *url) LinkTable *linktbl = calloc(1, sizeof(LinkTable)); /* populate the base URL */ - LinkTable_add(linktbl, Link_new(url)); + LinkTable_add(linktbl, Link_new("/")); Link *head_link = linktbl->links[0]; head_link->type = LINK_HEAD; curl_easy_setopt(head_link->curl, CURLOPT_URL, url); @@ -200,13 +212,14 @@ void LinkTable_add(LinkTable *linktbl, Link *link) void LinkTable_fill(LinkTable *linktbl) { + Link *head_link = linktbl->links[0]; for (int i = 0; i < linktbl->num; i++) { Link *this_link = linktbl->links[i]; if (this_link->type == LINK_UNKNOWN) { CURL *curl = this_link->curl; char *url; - curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url); - url = url_append(linktbl->links[0]->p_url, this_link->p_url); + curl_easy_getinfo(head_link->curl, CURLINFO_EFFECTIVE_URL, &url); + url = url_append(url, this_link->p_url); curl_easy_setopt(curl, CURLOPT_URL, url); free(url); curl_easy_setopt(curl, CURLOPT_NOBODY, 1); diff --git a/test.c b/test.c deleted file mode 100644 index 867f4fb..0000000 --- a/test.c +++ /dev/null @@ -1,23 +0,0 @@ -#include -#include -#include -#include -#include - -#include "network.h" -#include "test.h" - - -void link_test() -{ - printf("--- start of link_test ---\n"); - - LinkTable *linktbl = LinkTable_new( - "http://127.0.0.1/~fangfufu/"); - - LinkTable_print(linktbl); - Link_download(linktbl->links[1], 1, 20); - printf(linktbl->links[1]->body); - LinkTable_free(linktbl); - printf("\n--- end of link_test ---\n\n"); -} diff --git a/test.h b/test.h deleted file mode 100644 index be716dc..0000000 --- a/test.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef TEST_H -#define TEST_H - -void link_test(); - -#endif