renamed MemoryStruct to DataStruct, removed spurious link type detection logic

This commit is contained in:
Fufu Fang 2019-10-22 20:26:21 +01:00
parent eb27257e47
commit b7c63f4418
No known key found for this signature in database
GPG Key ID: 0F6BB5EF6F8BB729
6 changed files with 82 additions and 32 deletions

View File

@ -2,9 +2,9 @@ VERSION=1.1.10
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 sonic.o
`pkg-config --cflags-only-I gumbo libcurl fuse uuid expat`
LIBS = -pthread -lgumbo -lcurl -lfuse -lcrypto -luuid -lexpat
COBJS = network.o fuse_local.o link.o cache.o util.o main.o
prefix ?= /usr/local

View File

@ -26,7 +26,6 @@ static int fs_release(const char *path, struct fuse_file_info *fi)
return 0;
}
/** \brief return the attributes for a single file indicated by path */
static int fs_getattr(const char *path, struct stat *stbuf)
{

View File

@ -73,6 +73,9 @@ static void LinkTable_add(LinkTable *linktbl, Link *link)
linktbl->links[linktbl->num - 1] = link;
}
/**
* \brief create a new Link
*/
static Link *Link_new(const char *linkname, LinkType type)
{
Link *link = CALLOC(1, sizeof(Link));
@ -96,11 +99,6 @@ static LinkType linkname_to_LinkType(const char *linkname)
return LINK_INVALID;
}
/* check for http:// and https:// */
if ( !strncmp(linkname, "http://", 7) || !strncmp(linkname, "https://", 8) ) {
return LINK_INVALID;
}
if ( linkname[strnlen(linkname, MAX_FILENAME_LEN) - 1] == '/' ) {
return LINK_DIR;
}
@ -339,13 +337,13 @@ void LinkTable_print(LinkTable *linktbl)
fprintf(stderr, "--------------------------------------------\n");
}
MemoryStruct Link_to_MemoryStruct(Link *head_link)
DataStruct Link_to_DataStruct(Link *head_link)
{
char *url = head_link->f_url;
CURL *curl = Link_to_curl(head_link);
MemoryStruct buf;
DataStruct buf;
buf.size = 0;
buf.memory = NULL;
buf.data = NULL;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&buf);
/* If we get temporary HTTP failure, wait for 5 seconds before retry */
@ -397,17 +395,17 @@ LinkTable *LinkTable_new(const char *url)
LinkTable *linktbl = LinkTable_alloc(url);
/* start downloading the base URL */
MemoryStruct buf = Link_to_MemoryStruct(linktbl->links[0]);
DataStruct buf = Link_to_DataStruct(linktbl->links[0]);
if (buf.size == 0) {
LinkTable_free(linktbl);
return NULL;
}
/* Otherwise parsed the received data */
GumboOutput* output = gumbo_parse(buf.memory);
GumboOutput* output = gumbo_parse(buf.data);
HTML_to_LinkTable(output->root, linktbl);
gumbo_destroy_output(&kGumboDefaultOptions, output);
free(buf.memory);
free(buf.data);
int skip_fill = 0;
char *unescaped_path;
@ -662,9 +660,9 @@ long path_download(const char *path, char *output_buf, size_t size,
snprintf(range_str, sizeof(range_str), "%lu-%lu", start, end);
fprintf(stderr, "path_download(%s, %s);\n", path, range_str);
MemoryStruct buf;
DataStruct buf;
buf.size = 0;
buf.memory = NULL;
buf.data = NULL;
CURL *curl = Link_to_curl(link);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&buf);
@ -701,8 +699,8 @@ long path_download(const char *path, char *output_buf, size_t size,
recv = size;
}
memmove(output_buf, buf.memory, recv);
memmove(output_buf, buf.data, recv);
curl_easy_cleanup(curl);
free(buf.memory);
free(buf.data);
return recv;
}

View File

@ -25,9 +25,9 @@ typedef enum {
/** \brief for storing downloaded data in memory */
typedef struct {
char *memory;
char *data;
size_t size;
} MemoryStruct;
} DataStruct;
/** \brief specify the type of data transfer */
typedef enum {
@ -139,9 +139,9 @@ 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!
* \warning You MUST free the memory field in DataStruct after use!
*/
MemoryStruct Link_to_MemoryStruct(Link *head_link);
DataStruct Link_to_DataStruct(Link *head_link);
/**
* \brief Allocate a LinkTable

View File

@ -360,19 +360,19 @@ size_t write_memory_callback(void *contents, size_t size, size_t nmemb,
void *userp)
{
size_t realsize = size * nmemb;
MemoryStruct *mem = (MemoryStruct *)userp;
DataStruct *mem = (DataStruct *)userp;
mem->memory = realloc(mem->memory, mem->size + realsize + 1);
if(!mem->memory) {
mem->data = realloc(mem->data, mem->size + realsize + 1);
if(!mem->data) {
/* out of memory! */
fprintf(stderr, "write_memory_callback(): realloc failure!\n");
exit_failure();
return 0;
}
memmove(&mem->memory[mem->size], contents, realsize);
memmove(&mem->data[mem->size], contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
mem->data[mem->size] = 0;
return realsize;
}

View File

@ -76,6 +76,57 @@ static char *sonic_gen_url_first_part(char *method)
return url;
}
/**
* \brief Process a single element output by the parser
* \details This is the callback function called by the the XML parser.
* \param[in] data user supplied data, in this case it is the pointer to the
* LinkTable.
* \param[in] element the name of this element, it should be either "child" or
* "artist"
* \param[in] attributes Each attribute seen in a start (or empty) tag occupies
* 2 consecutive places in this vector: the attribute name followed by the
* attribute value. These pairs are terminated by a null pointer.
*/
static void XMLCALL XML_process_single_element(void *data, const char *element,
const char **attributes)
{
LinkTable *linktbl = (LinkTable *) data;
Link *link;
if (!strncmp(element, "child", 5)) {
/* Return from getMusicDirectory */
link = CALLOC(1, sizeof(Link));
link->type = LINK_INVALID;
} else if (!strncmp(element, "artist", 6)){
/* Return from getIndexes */
link = CALLOC(1, sizeof(Link));
link->type = LINK_DIR;
} else {
/* The element does not contain directory structural information */
return;
}
for (int i = 0; attributes[i]; i += 2) {
}
}
/**
* \brief parse a XML string in order to fill in the LinkTable
*/
static void sonic_XML_to_LinkTable(DataStruct ds, LinkTable *linktbl)
{
XML_Parser parser = XML_ParserCreate(NULL);
XML_SetUserData(parser, linktbl);
XML_SetStartElementHandler(parser, XML_process_single_element);
if (XML_Parse(parser, ds.data, ds.size, 1) == XML_STATUS_ERROR) {
fprintf(stderr,
"sonic_XML_to_LinkTable(): Parse error at line %lu: %s\n",
XML_GetCurrentLineNumber(parser),
XML_ErrorString(XML_GetErrorCode(parser)));
}
XML_ParserFree(parser);
}
LinkTable *sonic_LinkTable_new(const int id)
{
char *url;
@ -93,15 +144,17 @@ LinkTable *sonic_LinkTable_new(const int id)
LinkTable *linktbl = LinkTable_alloc(url);
/* start downloading the base URL */
MemoryStruct buf = Link_to_MemoryStruct(linktbl->links[0]);
if (buf.size == 0) {
DataStruct xml = Link_to_DataStruct(linktbl->links[0]);
if (xml.size == 0) {
LinkTable_free(linktbl);
return NULL;
}
printf("%s", buf.memory);
sonic_XML_to_LinkTable(xml, linktbl);
free(buf.memory);
LinkTable_print(linktbl);
free(xml.data);
free(url);
return NULL;
}