removed a bunch of function prototypes

This commit is contained in:
Fufu Fang 2019-04-22 13:32:15 +01:00
parent e515a2a197
commit b6bdf15ad1
7 changed files with 202 additions and 215 deletions

View File

@ -160,17 +160,13 @@ void CacheSystem_init(const char *path)
}
/* Check if directories exist, if not, create them */
dir = opendir(META_DIR);
if (dir) {
closedir(dir);
} else if (mkdir(META_DIR, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
fprintf(stderr, "CacheSystem_init(): mkdir(): %s\n",
if (mkdir(META_DIR, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
&& (errno != EEXIST)) {
fprintf(stderr, "CacheSystem_init(): mkdir(): %s\n",
strerror(errno));
}
dir = opendir(DATA_DIR);
if (dir) {
closedir(dir);
} else if (mkdir(META_DIR, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
if (mkdir(DATA_DIR, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
&& (errno != EEXIST)) {
fprintf(stderr, "CacheSystem_init(): mkdir(): %s\n",
strerror(errno));
}
@ -459,6 +455,8 @@ static Cache *Cache_alloc()
void Cache_close(Cache *cf)
{
fprintf(stderr, "Cache_close(): Creating cache files for %p.\n", cf);
return Cache_free(cf);
}
@ -513,6 +511,8 @@ static int Cache_exist(const char *fn)
int Cache_create(const char *fn, long len, long time)
{
fprintf(stderr, "Cache_create(): Creating cache files for %s.\n", fn);
Cache *cf = Cache_alloc();
cf->p_url = strndup(fn, MAX_PATH_LEN);

View File

@ -21,7 +21,8 @@ typedef uint8_t Seg;
* \brief cache in-memory data structure
*/
typedef struct {
char *p_url; /**< the filename from the http server */
char *p_url; /**< the partial URL -- this is the link relative
to the root link */
long time; /**<the modified time of the file */
off_t content_length; /**<the size of the file */
int blksz; /**<the block size of the data file */
@ -41,10 +42,21 @@ extern int CACHE_SYSTEM_INIT;
* - DATA_DIR
*
* If these directories do not exist, they will be created.
* \note Called by parse_arg_list()
* \note Called by parse_arg_list(), verified to be working
*/
void CacheSystem_init(const char *dir);
/**
* \brief Create directories under the cache directory structure, if they do
* not already exist
* \return
* - -1 failed to create metadata directory.
* - -2 failed to create data directory.
* - -3 failed to create both metadata and data directory.
* \note Called by fs_readdir(), verified to be working
*/
int CacheDir_create(const char *fn);
/**
* \brief open a cache file set
* \note This function is called by fs_open().
@ -82,17 +94,6 @@ void Seg_set(Cache *cf, long start, int i);
*/
int Cache_create(const char *fn, long len, long time);
/**
* \brief Create directories under the cache directory structure, if they do
* not already exist
* \return
* - -1 failed to create metadata directory.
* - -2 failed to create data directory.
* - -3 failed to create both metadata and data directory.
* \note Call this when creating a new LinkTable
*/
int CacheDir_create(const char *fn);
/***************************** To be completed ******************************/

View File

@ -7,30 +7,6 @@
#include <string.h>
#include <unistd.h>
static void *fs_init(struct fuse_conn_info *conn);
static int fs_getattr(const char *path, struct stat *stbuf);
static int fs_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi);
static int fs_open(const char *path, struct fuse_file_info *fi);
static int fs_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi);
static int fs_release(const char *path, struct fuse_file_info *fi);
static struct fuse_operations fs_oper = {
.getattr = fs_getattr,
.readdir = fs_readdir,
.open = fs_open,
.read = fs_read,
.init = fs_init,
.release = fs_release
};
int fuse_local_init(int argc, char **argv)
{
return fuse_main(argc, argv, &fs_oper, NULL);
}
static void *fs_init(struct fuse_conn_info *conn)
{
(void) conn;
@ -40,9 +16,9 @@ static void *fs_init(struct fuse_conn_info *conn)
/** \brief release an opened file */
static int fs_release(const char *path, struct fuse_file_info *fi)
{
if (CACHE_SYSTEM_INIT) {
Cache_close((Cache *)fi->fh);
}
// if (CACHE_SYSTEM_INIT) {
// Cache_close((Cache *)fi->fh);
// }
fprintf(stderr, "fs_release(): %s\n", path);
return 0;
}
@ -107,12 +83,12 @@ static int fs_open(const char *path, struct fuse_file_info *fi)
return -EACCES;
}
if (CACHE_SYSTEM_INIT) {
fi->fh = (uint64_t) Cache_open(path);
if (!fi->fh) {
return -ENOENT;
}
}
// if (CACHE_SYSTEM_INIT) {
// fi->fh = (uint64_t) Cache_open(path);
// if (!fi->fh) {
// return -ENOENT;
// }
// }
fprintf(stderr, "fs_open(): %s\n", path);
@ -133,7 +109,9 @@ static int fs_readdir(const char *path, void *buf, fuse_fill_dir_t dir_add,
linktbl = ROOT_LINK_TBL;
} else {
linktbl = path_to_Link_LinkTable_new(path);
fprintf(stderr, "Created new link table for %s\n", path);
if (CACHE_SYSTEM_INIT) {
CacheDir_create(path);
}
LinkTable_print(linktbl);
if(!linktbl) {
return -ENOENT;
@ -146,9 +124,23 @@ static int fs_readdir(const char *path, void *buf, fuse_fill_dir_t dir_add,
for (int i = 1; i < linktbl->num; i++) {
link = linktbl->links[i];
if (link->type != LINK_INVALID) {
dir_add(buf, link->p_url, NULL, 0);
dir_add(buf, link->linkname, NULL, 0);
}
}
return 0;
}
static struct fuse_operations fs_oper = {
.getattr = fs_getattr,
.readdir = fs_readdir,
.open = fs_open,
.read = fs_read,
.init = fs_init,
.release = fs_release
};
int fuse_local_init(int argc, char **argv)
{
return fuse_main(argc, argv, &fs_oper, NULL);
}

View File

@ -16,17 +16,56 @@
/* ---------------- External variables -----------------------*/
LinkTable *ROOT_LINK_TBL = NULL;
int ROOT_LINK_LEN = 0;
static void HTML_to_LinkTable(GumboNode *node, LinkTable *linktbl);
static Link *Link_new(const char *p_url, LinkType type);
static CURL *Link_to_curl(Link *link);
static void Link_get_stat(Link *this_link);
static void LinkTable_add(LinkTable *linktbl, Link *link);
void LinkTable_fill(LinkTable *linktbl);
static void LinkTable_free(LinkTable *linktbl);
static Link *path_to_Link_recursive(char *path, LinkTable *linktbl);
static LinkType p_url_type(const char *p_url);
static char *url_append(const char *url, const char *sublink);
static void LinkTable_add(LinkTable *linktbl, Link *link)
{
linktbl->num++;
linktbl->links = realloc(linktbl->links, linktbl->num * sizeof(Link *));
if (!linktbl->links) {
fprintf(stderr, "LinkTable_add(): realloc failure!\n");
exit(EXIT_FAILURE);
}
linktbl->links[linktbl->num - 1] = link;
}
static Link *Link_new(const char *linkname, LinkType type)
{
Link *link = calloc(1, sizeof(Link));
if (!link) {
fprintf(stderr, "Link_new(): calloc failure!\n");
exit(EXIT_FAILURE);
}
strncpy(link->linkname, linkname, LINKNAME_LEN_MAX);
link->type = type;
/* remove the '/' from linkname if it exists */
char *c = &(link->linkname[strnlen(link->linkname, LINKNAME_LEN_MAX) - 1]);
if ( *c == '/') {
*c = '\0';
}
return link;
}
static LinkType linkname_type(const char *linkname)
{
/* The link name has to start with alphanumerical character */
if (!isalnum(linkname[0])) {
return LINK_INVALID;
}
/* check for http:// and https:// */
if ( !strncmp(linkname, "http://", 7) || !strncmp(linkname, "https://", 8) ) {
return LINK_INVALID;
}
if ( linkname[strnlen(linkname, LINKNAME_LEN_MAX) - 1] == '/' ) {
return LINK_DIR;
}
return LINK_FILE;
}
/**
* Shamelessly copied and pasted from:
@ -42,7 +81,7 @@ static void HTML_to_LinkTable(GumboNode *node, LinkTable *linktbl)
if (node->v.element.tag == GUMBO_TAG_A &&
(href = gumbo_get_attribute(&node->v.element.attributes, "href"))) {
/* if it is valid, copy the link onto the heap */
LinkType type = p_url_type(href->value);
LinkType type = linkname_type(href->value);
if (type) {
LinkTable_add(linktbl, Link_new(href->value, type));
}
@ -55,25 +94,6 @@ static void HTML_to_LinkTable(GumboNode *node, LinkTable *linktbl)
return;
}
static Link *Link_new(const char *p_url, LinkType type)
{
Link *link = calloc(1, sizeof(Link));
if (!link) {
fprintf(stderr, "Link_new(): calloc failure!\n");
exit(EXIT_FAILURE);
}
strncpy(link->p_url, p_url, P_URL_LEN_MAX);
link->type = type;
/* remove the '/' from p_url if it exists */
char *c = &(link->p_url[strnlen(link->p_url, P_URL_LEN_MAX) - 1]);
if ( *c == '/') {
*c = '\0';
}
return link;
}
static CURL *Link_to_curl(Link *link)
{
CURL *curl = curl_easy_init();
@ -157,7 +177,6 @@ void Link_set_stat(Link* this_link, CURL *curl)
curl_easy_getinfo(curl, CURLINFO_FILETIME, &(this_link->time));
if (cl == -1) {
/* Turns out not to be a file after all */
this_link->content_length = 0;
this_link->type = LINK_DIR;
} else {
@ -169,33 +188,45 @@ void Link_set_stat(Link* this_link, CURL *curl)
}
}
static void LinkTable_add(LinkTable *linktbl, Link *link)
static char *url_append(const char *url, const char *sublink)
{
linktbl->num++;
linktbl->links = realloc(linktbl->links, linktbl->num * sizeof(Link *));
if (!linktbl->links) {
fprintf(stderr, "LinkTable_add(): realloc failure!\n");
int needs_separator = 0;
if (url[strnlen(url, URL_LEN_MAX)-1] != '/') {
needs_separator = 1;
}
char *str;
size_t ul = strnlen(url, URL_LEN_MAX);
size_t sl = strnlen(sublink, LINKNAME_LEN_MAX);
str = calloc(ul + sl + needs_separator + 1, sizeof(char));
if (!str) {
fprintf(stderr, "url_append(): calloc failure!\n");
exit(EXIT_FAILURE);
}
linktbl->links[linktbl->num - 1] = link;
strncpy(str, url, ul);
if (needs_separator) {
str[ul] = '/';
}
strncat(str, sublink, sl);
return str;
}
void LinkTable_fill(LinkTable *linktbl)
static 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) {
char *url;
url = url_append(head_link->f_url, this_link->p_url);
url = url_append(head_link->f_url, this_link->linkname);
strncpy(this_link->f_url, url, URL_LEN_MAX);
free(url);
char *unescaped_p_url;
unescaped_p_url = curl_easy_unescape(NULL, this_link->p_url, 0,
char *unescaped_linkname;
unescaped_linkname = curl_easy_unescape(NULL, this_link->linkname, 0,
NULL);
strncpy(this_link->p_url, unescaped_p_url, P_URL_LEN_MAX);
curl_free(unescaped_p_url);
strncpy(this_link->linkname, unescaped_linkname, LINKNAME_LEN_MAX);
curl_free(unescaped_linkname);
if (this_link->type == LINK_FILE && !(this_link->content_length)) {
Link_get_stat(this_link);
@ -221,7 +252,7 @@ static void LinkTable_free(LinkTable *linktbl)
LinkTable *LinkTable_new(const char *url)
{
fprintf(stderr, "%d: LinkTable_new(%s);\n",i, url);
fprintf(stderr, "LinkTable_new(%s);\n", url);
LinkTable *linktbl = calloc(1, sizeof(LinkTable));
if (!linktbl) {
@ -266,7 +297,7 @@ URL: %s, HTTP %ld\n", url, http_resp);
/* Fill in the link table */
LinkTable_fill(linktbl);
fprintf(stderr, "%d: LinkTable_new(): returning LinkTable %p\n", i, linktbl);
fprintf(stderr, "LinkTable_new(): returning LinkTable %p\n", linktbl);
return linktbl;
}
@ -282,7 +313,7 @@ void LinkTable_print(LinkTable *linktbl)
i,
this_link->type,
this_link->content_length,
this_link->p_url,
this_link->linkname,
this_link->f_url
);
@ -290,18 +321,6 @@ void LinkTable_print(LinkTable *linktbl)
fprintf(stderr, "--------------------------------------------\n");
}
Link *path_to_Link(const char *path)
{
char *new_path = strndup(path, URL_LEN_MAX);
if (!new_path) {
fprintf(stderr, "path_to_Link(): cannot allocate memory\n");
exit(EXIT_FAILURE);
}
Link *link = path_to_Link_recursive(new_path, ROOT_LINK_TBL);
free(new_path);
return link;
}
LinkTable *path_to_Link_LinkTable_new(const char *path)
{
Link *link = path_to_Link(path);
@ -326,7 +345,7 @@ static Link *path_to_Link_recursive(char *path, LinkTable *linktbl)
if ( slash == NULL ) {
/* We cannot find another '/', we have reached the last level */
for (int i = 1; i < linktbl->num; i++) {
if (!strncmp(path, linktbl->links[i]->p_url, P_URL_LEN_MAX)) {
if (!strncmp(path, linktbl->links[i]->linkname, LINKNAME_LEN_MAX)) {
/* We found our link */
return linktbl->links[i];
}
@ -345,7 +364,7 @@ static Link *path_to_Link_recursive(char *path, LinkTable *linktbl)
/* move the pointer past the '/' */
char *next_path = slash + 1;
for (int i = 1; i < linktbl->num; i++) {
if (!strncmp(path, linktbl->links[i]->p_url, P_URL_LEN_MAX)) {
if (!strncmp(path, linktbl->links[i]->linkname, LINKNAME_LEN_MAX)) {
/* The next sub-directory exists */
return path_to_Link_recursive(
next_path, linktbl->links[i]->next_table);
@ -355,6 +374,18 @@ static Link *path_to_Link_recursive(char *path, LinkTable *linktbl)
return NULL;
}
Link *path_to_Link(const char *path)
{
char *new_path = strndup(path, URL_LEN_MAX);
if (!new_path) {
fprintf(stderr, "path_to_Link(): cannot allocate memory\n");
exit(EXIT_FAILURE);
}
Link *link = path_to_Link_recursive(new_path, ROOT_LINK_TBL);
free(new_path);
return link;
}
long path_download(const char *path, char *output_buf, size_t size,
off_t offset)
{
@ -407,45 +438,3 @@ long path_download(const char *path, char *output_buf, size_t size,
free(buf.memory);
return recv;
}
static LinkType p_url_type(const char *p_url)
{
/* The link name has to start with alphanumerical character */
if (!isalnum(p_url[0])) {
return LINK_INVALID;
}
/* check for http:// and https:// */
if ( !strncmp(p_url, "http://", 7) || !strncmp(p_url, "https://", 8) ) {
return LINK_INVALID;
}
if ( p_url[strnlen(p_url, P_URL_LEN_MAX) - 1] == '/' ) {
return LINK_DIR;
}
return LINK_FILE;
}
static char *url_append(const char *url, const char *sublink)
{
int needs_separator = 0;
if (url[strnlen(url, URL_LEN_MAX)-1] != '/') {
needs_separator = 1;
}
char *str;
size_t ul = strnlen(url, URL_LEN_MAX);
size_t sl = strnlen(sublink, P_URL_LEN_MAX);
str = calloc(ul + sl + needs_separator + 1, sizeof(char));
if (!str) {
fprintf(stderr, "url_append(): calloc failure!\n");
exit(EXIT_FAILURE);
}
strncpy(str, url, ul);
if (needs_separator) {
str[ul] = '/';
}
strncat(str, sublink, sl);
return str;
}

View File

@ -8,7 +8,7 @@
/** \brief the maximum length of the URL */
#define URL_LEN_MAX 2048
/** \brief the maximum length of a partial URL (a link) */
#define P_URL_LEN_MAX 255
#define LINKNAME_LEN_MAX 255
/** \brief the link type */
typedef enum {
@ -31,7 +31,8 @@ typedef struct Link Link;
* \brief Link data structure
*/
struct Link {
char p_url[P_URL_LEN_MAX]; /**< */
char linkname[LINKNAME_LEN_MAX]; /**< The link name in the last level of
the URL*/
char f_url[URL_LEN_MAX]; /**< The full URL of the file*/
LinkType type; /**< The type of the link */
size_t content_length; /**< CURLINFO_CONTENT_LENGTH_DOWNLOAD of the file */
@ -49,6 +50,11 @@ struct LinkTable {
*/
extern LinkTable *ROOT_LINK_TBL;
/**
* \brief the length of the root link
*/
extern int ROOT_LINK_LEN;
/**
* \brief set the stats for a file
*/

View File

@ -192,6 +192,7 @@ parse_arg_list(int argc, char **argv, char ***fuse_argv, int *fuse_argc)
break;
case 8:
CacheSystem_init(optarg);
break;
default:
fprintf(stderr, "Error: Invalid option\n");
add_arg(fuse_argv, fuse_argc, "--help");

View File

@ -25,16 +25,6 @@ static pthread_mutex_t *crypto_lockarray;
static pthread_mutex_t curl_lock;
/** \brief network configuration */
/* ---------------- Static function prototype ---------------*/
static void crypto_lock_callback(int mode, int type, char *file, int line);
static void crypto_lock_init(void);
static void curl_callback_lock(CURL *handle, curl_lock_data data,
curl_lock_access access, void *userptr);
static void curl_callback_unlock(CURL *handle, curl_lock_data data,
void *userptr);
void curl_process_msgs(CURLMsg *curl_msg, int n_running_curl, int n_mesgs);
static unsigned long thread_id(void);
/* -------------------- Functions -------------------------- */
static void crypto_lock_callback(int mode, int type, char *file, int line)
{
@ -47,6 +37,14 @@ static void crypto_lock_callback(int mode, int type, char *file, int line)
}
}
static unsigned long thread_id(void)
{
unsigned long ret;
ret = (unsigned long)pthread_self();
return ret;
}
static void crypto_lock_init(void)
{
int i;
@ -84,6 +82,42 @@ static void curl_callback_unlock(CURL *handle, curl_lock_data data,
pthread_mutex_unlock(&curl_lock);
}
static void curl_process_msgs(CURLMsg *curl_msg, int n_running_curl, int n_mesgs)
{
if (curl_msg->msg == CURLMSG_DONE) {
TransferStruct *transfer;
CURL *curl = curl_msg->easy_handle;
curl_easy_getinfo(curl_msg->easy_handle, CURLINFO_PRIVATE,
&transfer);
transfer->transferring = 0;
char *url = NULL;
curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
if (curl_msg->data.result) {
fprintf(stderr, "curl_process_msgs(): %d - %s <%s>\n",
curl_msg->data.result,
curl_easy_strerror(curl_msg->data.result),
url);
usleep(1000);
} else {
/* Transfer successful, query the file size */
if (transfer->type == FILESTAT) {
fprintf(stderr, "Link_set_stat(): %d, %d, %s\n",
n_running_curl, n_mesgs, url);
Link_set_stat(transfer->link, curl);
}
}
curl_multi_remove_handle(curl_multi, curl);
/* clean up the handle, if we are querying the file size */
if (transfer->type == FILESTAT) {
curl_easy_cleanup(curl);
free(transfer);
}
} else {
fprintf(stderr, "curl_process_msgs(): curl_msg->msg: %d\n",
curl_msg->msg);
}
}
int curl_multi_perform_once()
{
pthread_mutex_lock(&transfer_lock);
@ -156,41 +190,7 @@ int curl_multi_perform_once()
return n_running_curl;
}
void curl_process_msgs(CURLMsg *curl_msg, int n_running_curl, int n_mesgs)
{
if (curl_msg->msg == CURLMSG_DONE) {
TransferStruct *transfer;
CURL *curl = curl_msg->easy_handle;
curl_easy_getinfo(curl_msg->easy_handle, CURLINFO_PRIVATE,
&transfer);
transfer->transferring = 0;
char *url = NULL;
curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
if (curl_msg->data.result) {
fprintf(stderr, "curl_process_msgs(): %d - %s <%s>\n",
curl_msg->data.result,
curl_easy_strerror(curl_msg->data.result),
url);
usleep(1000);
} else {
/* Transfer successful, query the file size */
if (transfer->type == FILESTAT) {
fprintf(stderr, "Link_set_stat(): %d, %d, %s\n",
n_running_curl, n_mesgs, url);
Link_set_stat(transfer->link, curl);
}
}
curl_multi_remove_handle(curl_multi, curl);
/* clean up the handle, if we are querying the file size */
if (transfer->type == FILESTAT) {
curl_easy_cleanup(curl);
free(transfer);
}
} else {
fprintf(stderr, "curl_process_msgs(): curl_msg->msg: %d\n",
curl_msg->msg);
}
}
void network_config_init()
{
@ -255,7 +255,13 @@ LinkTable *network_init(const char *url)
curl_version_info_data *data = curl_version_info(CURLVERSION_NOW);
printf("libcurl SSL engine: %s\n", data->ssl_version);
/* ----------- create the root link table --------------*/
/* --------- Set the length of the root link ----------- */
ROOT_LINK_LEN = strnlen(url, URL_LEN_MAX);
if (url[ROOT_LINK_LEN - 1] == '/') {
ROOT_LINK_LEN++;
}
/* ----------- Create the root link table --------------*/
ROOT_LINK_TBL = LinkTable_new(url);
return ROOT_LINK_TBL;
}
@ -300,14 +306,6 @@ void transfer_nonblocking(CURL *curl)
}
}
static unsigned long thread_id(void)
{
unsigned long ret;
ret = (unsigned long)pthread_self();
return ret;
}
size_t
write_memory_callback(void *contents, size_t size, size_t nmemb, void *userp)
{