fixed memory leak in link table creation
This commit is contained in:
parent
5af3def3f8
commit
77bb715590
|
@ -29,6 +29,11 @@ typedef struct {
|
||||||
Seg *seg; /**< the detail of each segment */
|
Seg *seg; /**< the detail of each segment */
|
||||||
} Cache;
|
} Cache;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief whether the cache system is enabled
|
||||||
|
*/
|
||||||
|
extern int CACHE_SYSTEM_INIT;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief initialise the cache system directories
|
* \brief initialise the cache system directories
|
||||||
* \details This function basically sets up the following variables:
|
* \details This function basically sets up the following variables:
|
||||||
|
|
|
@ -40,7 +40,9 @@ static void *fs_init(struct fuse_conn_info *conn)
|
||||||
/** \brief release an opened file */
|
/** \brief release an opened file */
|
||||||
static int fs_release(const char *path, struct fuse_file_info *fi)
|
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);
|
fprintf(stderr, "fs_release(): %s\n", path);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -105,9 +107,11 @@ static int fs_open(const char *path, struct fuse_file_info *fi)
|
||||||
return -EACCES;
|
return -EACCES;
|
||||||
}
|
}
|
||||||
|
|
||||||
fi->fh = (uint64_t) Cache_open(path);
|
if (CACHE_SYSTEM_INIT) {
|
||||||
if (!fi->fh) {
|
fi->fh = (uint64_t) Cache_open(path);
|
||||||
return -ENOENT;
|
if (!fi->fh) {
|
||||||
|
return -ENOENT;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fprintf(stderr, "fs_open(): %s\n", path);
|
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, "/")) {
|
if (!strcmp(path, "/")) {
|
||||||
linktbl = ROOT_LINK_TBL;
|
linktbl = ROOT_LINK_TBL;
|
||||||
} else {
|
} else {
|
||||||
link = path_to_Link(path);
|
linktbl = path_to_Link_LinkTable_new(path);
|
||||||
if (!link) {
|
fprintf(stderr, "Created new link table for %s\n", path);
|
||||||
|
LinkTable_print(linktbl);
|
||||||
|
if(!linktbl) {
|
||||||
return -ENOENT;
|
return -ENOENT;
|
||||||
}
|
}
|
||||||
linktbl = link->next_table;
|
|
||||||
if (!linktbl) {
|
|
||||||
linktbl = LinkTable_new(link->f_url);
|
|
||||||
if(!linktbl) {
|
|
||||||
return -ENOENT;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* start adding the links */
|
/* start adding the links */
|
||||||
|
|
27
src/link.c
27
src/link.c
|
@ -1,5 +1,6 @@
|
||||||
#include "link.h"
|
#include "link.h"
|
||||||
|
|
||||||
|
#include "cache.h"
|
||||||
#include "network.h"
|
#include "network.h"
|
||||||
|
|
||||||
#include <gumbo.h>
|
#include <gumbo.h>
|
||||||
|
@ -23,7 +24,6 @@ static void Link_get_stat(Link *this_link);
|
||||||
static void LinkTable_add(LinkTable *linktbl, Link *link);
|
static void LinkTable_add(LinkTable *linktbl, Link *link);
|
||||||
void LinkTable_fill(LinkTable *linktbl);
|
void LinkTable_fill(LinkTable *linktbl);
|
||||||
static void LinkTable_free(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 Link *path_to_Link_recursive(char *path, LinkTable *linktbl);
|
||||||
static LinkType p_url_type(const char *p_url);
|
static LinkType p_url_type(const char *p_url);
|
||||||
static char *url_append(const char *url, const char *sublink);
|
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)
|
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));
|
LinkTable *linktbl = calloc(1, sizeof(LinkTable));
|
||||||
if (!linktbl) {
|
if (!linktbl) {
|
||||||
|
@ -266,11 +266,11 @@ URL: %s, HTTP %ld\n", url, http_resp);
|
||||||
|
|
||||||
/* Fill in the link table */
|
/* Fill in the link table */
|
||||||
LinkTable_fill(linktbl);
|
LinkTable_fill(linktbl);
|
||||||
|
fprintf(stderr, "%d: LinkTable_new(): returning LinkTable %p\n", i, linktbl);
|
||||||
return linktbl;
|
return linktbl;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** \brief print a LinkTable */
|
void LinkTable_print(LinkTable *linktbl)
|
||||||
static void LinkTable_print(LinkTable *linktbl)
|
|
||||||
{
|
{
|
||||||
fprintf(stderr, "--------------------------------------------\n");
|
fprintf(stderr, "--------------------------------------------\n");
|
||||||
fprintf(stderr, " LinkTable %p for %s\n", linktbl,
|
fprintf(stderr, " LinkTable %p for %s\n", linktbl,
|
||||||
|
@ -302,6 +302,13 @@ Link *path_to_Link(const char *path)
|
||||||
return link;
|
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)
|
static Link *path_to_Link_recursive(char *path, LinkTable *linktbl)
|
||||||
{
|
{
|
||||||
/* skip the leading '/' if it exists */
|
/* 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++) {
|
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]->p_url, P_URL_LEN_MAX)) {
|
||||||
/* The next sub-directory exists */
|
/* The next sub-directory exists */
|
||||||
if (!linktbl->links[i]->next_table) {
|
return path_to_Link_recursive(
|
||||||
linktbl->links[i]->next_table = LinkTable_new(
|
next_path, linktbl->links[i]->next_table);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
46
src/link.h
46
src/link.h
|
@ -27,14 +27,16 @@ typedef struct LinkTable LinkTable;
|
||||||
/** \brief link data type */
|
/** \brief link data type */
|
||||||
typedef struct Link Link;
|
typedef struct Link Link;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Link data structure
|
||||||
|
*/
|
||||||
struct Link {
|
struct Link {
|
||||||
char p_url[P_URL_LEN_MAX];
|
char p_url[P_URL_LEN_MAX]; /**< */
|
||||||
char f_url[URL_LEN_MAX];
|
char f_url[URL_LEN_MAX]; /**< The full URL of the file*/
|
||||||
LinkType type;
|
LinkType type; /**< The type of the link */
|
||||||
size_t content_length;
|
size_t content_length; /**< CURLINFO_CONTENT_LENGTH_DOWNLOAD of the file */
|
||||||
LinkTable *next_table;
|
LinkTable *next_table; /**< The next LinkTable level, if it is a LINK_DIR */
|
||||||
long time;
|
long time; /**< CURLINFO_FILETIME obtained from the server*/
|
||||||
};
|
};
|
||||||
|
|
||||||
struct LinkTable {
|
struct LinkTable {
|
||||||
|
@ -42,24 +44,42 @@ struct LinkTable {
|
||||||
Link **links;
|
Link **links;
|
||||||
};
|
};
|
||||||
|
|
||||||
/** \brief root link table */
|
/**
|
||||||
|
* \brief root link table
|
||||||
|
*/
|
||||||
extern LinkTable *ROOT_LINK_TBL;
|
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);
|
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);
|
LinkTable *LinkTable_new(const char *url);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief download a link */
|
* \brief print a LinkTable
|
||||||
/* \return the number of bytes downloaded
|
*/
|
||||||
|
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,
|
long path_download(const char *path, char *output_buf, size_t size,
|
||||||
off_t offset);
|
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);
|
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
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue