fixed memory leak in link table creation

This commit is contained in:
Fufu Fang 2019-04-22 12:06:34 +01:00
parent 5af3def3f8
commit 77bb715590
4 changed files with 63 additions and 40 deletions

View File

@ -29,6 +29,11 @@ typedef struct {
Seg *seg; /**< the detail of each segment */
} Cache;
/**
* \brief whether the cache system is enabled
*/
extern int CACHE_SYSTEM_INIT;
/**
* \brief initialise the cache system directories
* \details This function basically sets up the following variables:

View File

@ -40,7 +40,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)
{
Cache_close((Cache *)fi->fh);
if (CACHE_SYSTEM_INIT) {
Cache_close((Cache *)fi->fh);
}
fprintf(stderr, "fs_release(): %s\n", path);
return 0;
}
@ -105,9 +107,11 @@ static int fs_open(const char *path, struct fuse_file_info *fi)
return -EACCES;
}
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);
@ -128,17 +132,12 @@ static int fs_readdir(const char *path, void *buf, fuse_fill_dir_t dir_add,
if (!strcmp(path, "/")) {
linktbl = ROOT_LINK_TBL;
} else {
link = path_to_Link(path);
if (!link) {
linktbl = path_to_Link_LinkTable_new(path);
fprintf(stderr, "Created new link table for %s\n", path);
LinkTable_print(linktbl);
if(!linktbl) {
return -ENOENT;
}
linktbl = link->next_table;
if (!linktbl) {
linktbl = LinkTable_new(link->f_url);
if(!linktbl) {
return -ENOENT;
}
}
}
/* start adding the links */

View File

@ -1,5 +1,6 @@
#include "link.h"
#include "cache.h"
#include "network.h"
#include <gumbo.h>
@ -23,7 +24,6 @@ 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 void LinkTable_print(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);
@ -221,7 +221,7 @@ static void LinkTable_free(LinkTable *linktbl)
LinkTable *LinkTable_new(const char *url)
{
fprintf(stderr, "LinkTable_new(%s);\n", url);
fprintf(stderr, "%d: LinkTable_new(%s);\n",i, url);
LinkTable *linktbl = calloc(1, sizeof(LinkTable));
if (!linktbl) {
@ -266,11 +266,11 @@ 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);
return linktbl;
}
/** \brief print a LinkTable */
static void LinkTable_print(LinkTable *linktbl)
void LinkTable_print(LinkTable *linktbl)
{
fprintf(stderr, "--------------------------------------------\n");
fprintf(stderr, " LinkTable %p for %s\n", linktbl,
@ -302,6 +302,13 @@ Link *path_to_Link(const char *path)
return link;
}
LinkTable *path_to_Link_LinkTable_new(const char *path)
{
Link *link = path_to_Link(path);
link->next_table = LinkTable_new(link->f_url);
return link->next_table;
}
static Link *path_to_Link_recursive(char *path, LinkTable *linktbl)
{
/* skip the leading '/' if it exists */
@ -340,16 +347,8 @@ static Link *path_to_Link_recursive(char *path, LinkTable *linktbl)
for (int i = 1; i < linktbl->num; i++) {
if (!strncmp(path, linktbl->links[i]->p_url, P_URL_LEN_MAX)) {
/* The next sub-directory exists */
if (!linktbl->links[i]->next_table) {
linktbl->links[i]->next_table = LinkTable_new(
linktbl->links[i]->f_url);
fprintf(stderr, "Created new link table for %s\n",
linktbl->links[i]->f_url);
LinkTable_print(linktbl->links[i]->next_table);
}
return path_to_Link_recursive(next_path,
linktbl->links[i]->next_table);
return path_to_Link_recursive(
next_path, linktbl->links[i]->next_table);
}
}
}

View File

@ -27,14 +27,16 @@ typedef struct LinkTable LinkTable;
/** \brief link data type */
typedef struct Link Link;
/**
* \brief Link data structure
*/
struct Link {
char p_url[P_URL_LEN_MAX];
char f_url[URL_LEN_MAX];
LinkType type;
size_t content_length;
LinkTable *next_table;
long time;
char p_url[P_URL_LEN_MAX]; /**< */
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 */
LinkTable *next_table; /**< The next LinkTable level, if it is a LINK_DIR */
long time; /**< CURLINFO_FILETIME obtained from the server*/
};
struct LinkTable {
@ -42,24 +44,42 @@ struct LinkTable {
Link **links;
};
/** \brief root link table */
/**
* \brief root link table
*/
extern LinkTable *ROOT_LINK_TBL;
/** \brief set the stats for a file */
/**
* \brief set the stats for a file
*/
void Link_set_stat(Link* this_link, CURL *curl);
/** \brief create a new LinkTable */
/**
* \brief create a new LinkTable
*/
LinkTable *LinkTable_new(const char *url);
/**
* \brief download a link */
/* \return the number of bytes downloaded
* \brief print a LinkTable
*/
void LinkTable_print(LinkTable *linktbl);
/**
* \brief download a link
* \return the number of bytes downloaded
*/
long path_download(const char *path, char *output_buf, size_t size,
off_t offset);
/** \brief find the link associated with a path */
/**
* \brief find the link associated with a path
*/
Link *path_to_Link(const char *path);
/**
* \brief return the link table for the associated path
*/
LinkTable *path_to_Link_LinkTable_new(const char *path);
#endif