only network.c needs to be cleaned up

This commit is contained in:
Fufu Fang 2021-08-30 05:17:15 +01:00
parent 2a4c61477a
commit 0219d7460a
No known key found for this signature in database
GPG Key ID: 0F6BB5EF6F8BB729
5 changed files with 46 additions and 83 deletions

View File

@ -98,14 +98,7 @@ void CacheSystem_init(const char *path, int url_supplied)
} }
lprintf(debug, "CacheSystem_init(): directory: %s\n", path); lprintf(debug, "CacheSystem_init(): directory: %s\n", path);
DIR* dir;
dir = opendir(path);
if (!dir) {
lprintf(fatal,
"CacheSystem_init(): opendir(): %s\n", strerror(errno));
}
closedir(dir);
META_DIR = path_append(path, "meta/"); META_DIR = path_append(path, "meta/");
DATA_DIR = path_append(path, "data/"); DATA_DIR = path_append(path, "data/");
/* Check if directories exist, if not, create them */ /* Check if directories exist, if not, create them */
@ -270,12 +263,9 @@ static int Meta_write(Cache *cf)
/** /**
* \brief create a data file * \brief create a data file
* \details We use sparse creation here * \details We use sparse creation here
* \return * \return exit on failure
* - 0 on successful creation of the data file, note that the result of
* the ftruncate() is ignored.
* - -1 on failure to create the data file.
*/ */
static int Data_create(Cache *cf) static void Data_create(Cache *cf)
{ {
int fd; int fd;
int mode; int mode;
@ -285,17 +275,14 @@ static int Data_create(Cache *cf)
fd = open(datafn, O_WRONLY | O_CREAT, mode); fd = open(datafn, O_WRONLY | O_CREAT, mode);
FREE(datafn); FREE(datafn);
if (fd == -1) { if (fd == -1) {
lprintf(error, "Data_create(): open(): %s\n", strerror(errno)); lprintf(fatal, "Data_create(): open(): %s\n", strerror(errno));
return -1;
} }
if (ftruncate(fd, cf->content_length)) { if (ftruncate(fd, cf->content_length)) {
lprintf(warning, "Data_create(): ftruncate(): %s\n", strerror(errno)); lprintf(warning, "Data_create(): ftruncate(): %s\n", strerror(errno));
} }
if (close(fd)) { if (close(fd)) {
lprintf(error, "Data_create(): close:(): %s\n", strerror(errno)); lprintf(fatal, "Data_create(): close:(): %s\n", strerror(errno));
return -1;
} }
return 0;
} }
/** /**
@ -438,12 +425,12 @@ int CacheDir_create(const char *dirn)
i = -mkdir(metadirn, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); i = -mkdir(metadirn, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
if (i && (errno != EEXIST)) { if (i && (errno != EEXIST)) {
lprintf(error, "CacheDir_create(): mkdir(): %s\n", strerror(errno)); lprintf(fatal, "CacheDir_create(): mkdir(): %s\n", strerror(errno));
} }
i |= -mkdir(datadirn, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) << 1; i |= -mkdir(datadirn, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) << 1;
if (i && (errno != EEXIST)) { if (i && (errno != EEXIST)) {
lprintf(error, "CacheDir_create(): mkdir(): %s\n", strerror(errno)); lprintf(fatal, "CacheDir_create(): mkdir(): %s\n", strerror(errno));
} }
FREE(datadirn); FREE(datadirn);
FREE(metadirn); FREE(metadirn);
@ -458,25 +445,25 @@ static Cache *Cache_alloc()
Cache *cf = CALLOC(1, sizeof(Cache)); Cache *cf = CALLOC(1, sizeof(Cache));
if (pthread_mutex_init(&cf->seek_lock, NULL)) { if (pthread_mutex_init(&cf->seek_lock, NULL)) {
lprintf(error, "Cache_alloc(): seek_lock initialisation failed!\n"); lprintf(fatal, "Cache_alloc(): seek_lock initialisation failed!\n");
} }
if (pthread_mutex_init(&cf->w_lock, NULL)) { if (pthread_mutex_init(&cf->w_lock, NULL)) {
lprintf(error, "Cache_alloc(): w_lock initialisation failed!\n"); lprintf(fatal, "Cache_alloc(): w_lock initialisation failed!\n");
} }
if (pthread_mutexattr_init(&cf->bgt_lock_attr)) { if (pthread_mutexattr_init(&cf->bgt_lock_attr)) {
lprintf(error, lprintf(fatal,
"Cache_alloc(): bgt_lock_attr initialisation failed!\n"); "Cache_alloc(): bgt_lock_attr initialisation failed!\n");
} }
if (pthread_mutexattr_setpshared(&cf->bgt_lock_attr, if (pthread_mutexattr_setpshared(&cf->bgt_lock_attr,
PTHREAD_PROCESS_SHARED)) { PTHREAD_PROCESS_SHARED)) {
lprintf(error, "Cache_alloc(): could not set bgt_lock_attr!\n"); lprintf(fatal, "Cache_alloc(): could not set bgt_lock_attr!\n");
} }
if (pthread_mutex_init(&cf->bgt_lock, &cf->bgt_lock_attr)) { if (pthread_mutex_init(&cf->bgt_lock, &cf->bgt_lock_attr)) {
lprintf(error, "Cache_alloc(): bgt_lock initialisation failed!\n"); lprintf(fatal, "Cache_alloc(): bgt_lock initialisation failed!\n");
} }
return cf; return cf;
@ -488,19 +475,19 @@ static Cache *Cache_alloc()
static void Cache_free(Cache *cf) static void Cache_free(Cache *cf)
{ {
if (pthread_mutex_destroy(&cf->seek_lock)) { if (pthread_mutex_destroy(&cf->seek_lock)) {
lprintf(error, "Cache_free(): could not destroy seek_lock!\n"); lprintf(fatal, "Cache_free(): could not destroy seek_lock!\n");
} }
if (pthread_mutex_destroy(&cf->w_lock)) { if (pthread_mutex_destroy(&cf->w_lock)) {
lprintf(error, "Cache_free(): could not destroy w_lock!\n"); lprintf(fatal, "Cache_free(): could not destroy w_lock!\n");
} }
if (pthread_mutex_destroy(&cf->bgt_lock)) { if (pthread_mutex_destroy(&cf->bgt_lock)) {
lprintf(error, "Cache_free(): could not destroy bgt_lock!\n"); lprintf(fatal, "Cache_free(): could not destroy bgt_lock!\n");
} }
if (pthread_mutexattr_destroy(&cf->bgt_lock_attr)) { if (pthread_mutexattr_destroy(&cf->bgt_lock_attr)) {
lprintf(error, "Cache_alloc(): could not destroy bgt_lock_attr!\n"); lprintf(fatal, "Cache_alloc(): could not destroy bgt_lock_attr!\n");
} }
if (cf->path) { if (cf->path) {
@ -630,23 +617,18 @@ static int Meta_open(Cache *cf)
/** /**
* \brief Create a metafile * \brief Create a metafile
* \return * \return exit on error
* - 0 on success
* - -1 on failure, with appropriate errno set.
*/ */
static int Meta_create(Cache *cf) static void Meta_create(Cache *cf)
{ {
char *metafn = path_append(META_DIR, cf->path); char *metafn = path_append(META_DIR, cf->path);
cf->mfp = fopen(metafn, "w"); cf->mfp = fopen(metafn, "w");
if (!cf->mfp) { if (!cf->mfp) {
/* Failed to open the data file */ /* Failed to open the data file */
lprintf(error, "Meta_create(): fopen(%s): %s\n", metafn, lprintf(fatal, "Meta_create(): fopen(%s): %s\n", metafn,
strerror(errno)); strerror(errno));
FREE(metafn);
return -1;
} }
FREE(metafn); FREE(metafn);
return 0;
} }
int Cache_create(const char *path) int Cache_create(const char *path)
@ -670,9 +652,7 @@ int Cache_create(const char *path)
cf->segbc = (cf->content_length / cf->blksz) + 1; cf->segbc = (cf->content_length / cf->blksz) + 1;
cf->seg = CALLOC(cf->segbc, sizeof(Seg)); cf->seg = CALLOC(cf->segbc, sizeof(Seg));
if (Meta_create(cf)) { Meta_create(cf);
lprintf(error, "Cache_create(): cannot create metadata.\n");
}
if (fclose(cf->mfp)) { if (fclose(cf->mfp)) {
lprintf(error, lprintf(error,
@ -695,9 +675,7 @@ int Cache_create(const char *path)
strerror(errno)); strerror(errno));
} }
if (Data_create(cf)) { Data_create(cf);
lprintf(error, "Cache_create(): Data_create() failed!\n");
}
Cache_free(cf); Cache_free(cf);

View File

@ -39,9 +39,8 @@ LinkTable *LinkSystem_init(const char *raw_url)
} }
if (pthread_mutex_init(&link_lock, NULL) != 0) { if (pthread_mutex_init(&link_lock, NULL) != 0) {
lprintf(fatal, lprintf(error,
"link_system_init(): link_lock initialisation failed!\n"); "link_system_init(): link_lock initialisation failed!\n");
exit_failure();
} }
/* --------- Set the length of the root link ----------- */ /* --------- Set the length of the root link ----------- */
@ -77,8 +76,7 @@ void LinkTable_add(LinkTable *linktbl, Link *link)
linktbl->num++; linktbl->num++;
linktbl->links = realloc(linktbl->links, linktbl->num * sizeof(Link *)); linktbl->links = realloc(linktbl->links, linktbl->num * sizeof(Link *));
if (!linktbl->links) { if (!linktbl->links) {
lprintf(debug, "LinkTable_add(): realloc failure!\n"); lprintf(fatal, "LinkTable_add(): realloc() failure!\n");
exit_failure();
} }
linktbl->links[linktbl->num - 1] = link; linktbl->links[linktbl->num - 1] = link;
} }
@ -186,7 +184,7 @@ static CURL *Link_to_curl(Link *link)
{ {
CURL *curl = curl_easy_init(); CURL *curl = curl_easy_init();
if (!curl) { if (!curl) {
lprintf(debug, "Link_to_curl(): curl_easy_init() failed!\n"); lprintf(fatal, "Link_to_curl(): curl_easy_init() failed!\n");
} }
/* set up some basic curl stuff */ /* set up some basic curl stuff */
curl_easy_setopt(curl, CURLOPT_USERAGENT, CONFIG.user_agent); curl_easy_setopt(curl, CURLOPT_USERAGENT, CONFIG.user_agent);
@ -229,14 +227,8 @@ static CURL *Link_to_curl(Link *link)
return curl; return curl;
} }
void Link_req_file_stat(Link *this_link) static void Link_req_file_stat(Link *this_link)
{ {
if (this_link->type != LINK_UNINITIALISED_FILE) {
lprintf(debug, "Link_req_file_stat(), invalid request, LinkType: %c",
this_link->type);
exit_failure();
}
CURL *curl = Link_to_curl(this_link); CURL *curl = Link_to_curl(this_link);
curl_easy_setopt(curl, CURLOPT_NOBODY, 1); curl_easy_setopt(curl, CURLOPT_NOBODY, 1);
curl_easy_setopt(curl, CURLOPT_FILETIME, 1L); curl_easy_setopt(curl, CURLOPT_FILETIME, 1L);
@ -271,12 +263,12 @@ void Link_set_file_stat(Link* this_link, CURL *curl)
this_link->content_length = cl; this_link->content_length = cl;
} }
} else { } else {
lprintf(debug, "Link_set_file_stat(): HTTP %ld", http_resp); lprintf(warning, "Link_set_file_stat(): HTTP %ld", http_resp);
if (HTTP_temp_failure(http_resp)) { if (HTTP_temp_failure(http_resp)) {
lprintf(debug, ", retrying later.\n"); lprintf(warning, ", retrying later.\n");
} else { } else {
this_link->type = LINK_INVALID; this_link->type = LINK_INVALID;
lprintf(debug, ".\n"); lprintf(warning, ".\n");
} }
} }
} }
@ -410,13 +402,13 @@ DataStruct Link_to_DataStruct(Link *head_link)
transfer_blocking(curl); transfer_blocking(curl);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_resp); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_resp);
if (HTTP_temp_failure(http_resp)) { if (HTTP_temp_failure(http_resp)) {
lprintf(debug, lprintf(warning,
"LinkTable_new(): URL: %s, HTTP %ld, retrying later.\n", "Link_to_DataStruct(): URL: %s, HTTP %ld, retrying later.\n"
url, http_resp); , url, http_resp);
sleep(CONFIG.http_wait_sec); sleep(CONFIG.http_wait_sec);
} else if (http_resp != HTTP_OK) { } else if (http_resp != HTTP_OK) {
lprintf(debug, lprintf(warning,
"LinkTable_new(): cannot retrieve URL: %s, HTTP %ld\n", "Link_to_DataStruct(): cannot retrieve URL: %s, HTTP %ld\n",
url, http_resp); url, http_resp);
buf.size = 0; buf.size = 0;
curl_easy_cleanup(curl); curl_easy_cleanup(curl);
@ -515,7 +507,7 @@ static void LinkTable_disk_delete(const char *dirn)
path = path_append(metadirn, "/.LinkTable"); path = path_append(metadirn, "/.LinkTable");
} }
if(unlink(path)) { if(unlink(path)) {
lprintf(debug, "LinkTable_disk_delete(): unlink(%s): %s\n", path, lprintf(error, "LinkTable_disk_delete(): unlink(%s): %s\n", path,
strerror(errno)); strerror(errno));
} }
FREE(path); FREE(path);
@ -535,7 +527,7 @@ int LinkTable_disk_save(LinkTable *linktbl, const char *dirn)
FREE(metadirn); FREE(metadirn);
if (!fp) { if (!fp) {
lprintf(debug, "LinkTable_disk_save(): fopen(%s): %s\n", path, lprintf(error, "LinkTable_disk_save(): fopen(%s): %s\n", path,
strerror(errno)); strerror(errno));
FREE(path); FREE(path);
return -1; return -1;
@ -554,12 +546,12 @@ int LinkTable_disk_save(LinkTable *linktbl, const char *dirn)
int res = 0; int res = 0;
if (ferror(fp)) { if (ferror(fp)) {
lprintf(debug, "LinkTable_disk_save(): encountered ferror!\n"); lprintf(error, "LinkTable_disk_save(): encountered ferror!\n");
res = -1; res = -1;
} }
if (fclose(fp)) { if (fclose(fp)) {
lprintf(debug, lprintf(error,
"LinkTable_disk_save(): cannot close the file pointer, %s\n", "LinkTable_disk_save(): cannot close the file pointer, %s\n",
strerror(errno)); strerror(errno));
res = -1; res = -1;
@ -598,21 +590,21 @@ LinkTable *LinkTable_disk_open(const char *dirn)
fread(&linktbl->links[i]->time, sizeof(long), 1, fp); fread(&linktbl->links[i]->time, sizeof(long), 1, fp);
if (feof(fp)) { if (feof(fp)) {
/* reached EOF */ /* reached EOF */
lprintf(debug, lprintf(error,
"LinkTable_disk_open(): reached EOF!\n"); "LinkTable_disk_open(): reached EOF!\n");
LinkTable_free(linktbl); LinkTable_free(linktbl);
LinkTable_disk_delete(dirn); LinkTable_disk_delete(dirn);
return NULL; return NULL;
} }
if (ferror(fp)) { if (ferror(fp)) {
lprintf(debug, "LinkTable_disk_open(): encountered ferror!\n"); lprintf(error, "LinkTable_disk_open(): encountered ferror!\n");
LinkTable_free(linktbl); LinkTable_free(linktbl);
LinkTable_disk_delete(dirn); LinkTable_disk_delete(dirn);
return NULL; return NULL;
} }
} }
if (fclose(fp)) { if (fclose(fp)) {
lprintf(debug, lprintf(error,
"LinkTable_disk_open(): cannot close the file pointer, %s\n", "LinkTable_disk_open(): cannot close the file pointer, %s\n",
strerror(errno)); strerror(errno));
} }
@ -711,8 +703,7 @@ Link *path_to_Link(const char *path)
PTHREAD_MUTEX_LOCK(&link_lock); PTHREAD_MUTEX_LOCK(&link_lock);
char *new_path = strndup(path, MAX_PATH_LEN); char *new_path = strndup(path, MAX_PATH_LEN);
if (!new_path) { if (!new_path) {
lprintf(debug, "path_to_Link(): cannot allocate memory\n"); lprintf(fatal, "path_to_Link(): cannot allocate memory\n");
exit_failure();
} }
Link *link = path_to_Link_recursive(new_path, ROOT_LINK_TBL); Link *link = path_to_Link_recursive(new_path, ROOT_LINK_TBL);
FREE(new_path); FREE(new_path);
@ -728,8 +719,7 @@ long path_download(const char *path, char *output_buf, size_t size,
off_t offset) off_t offset)
{ {
if (!path) { if (!path) {
lprintf(debug, "\npath_download(): NULL path supplied\n"); lprintf(fatal, "\npath_download(): NULL path supplied\n");
exit_failure();
} }
Link *link; Link *link;
link = path_to_Link(path); link = path_to_Link(path);
@ -761,8 +751,9 @@ long path_download(const char *path, char *output_buf, size_t size,
/* Check for range seek support */ /* Check for range seek support */
if (!CONFIG.no_range_check) { if (!CONFIG.no_range_check) {
if (!strcasestr((header.data), "Accept-Ranges: bytes")) { if (!strcasestr((header.data), "Accept-Ranges: bytes")) {
lprintf(fatal, "This web server does not support HTTP \ fprintf(stderr, "This web server does not support HTTP \
range requests\n"); range requests\n");
exit(EXIT_FAILURE);
} }
} }
@ -775,7 +766,7 @@ range requests\n");
(http_resp != HTTP_PARTIAL_CONTENT) || (http_resp != HTTP_PARTIAL_CONTENT) ||
(http_resp != HTTP_RANGE_NOT_SATISFIABLE) (http_resp != HTTP_RANGE_NOT_SATISFIABLE)
)) { )) {
lprintf(debug, "path_download(): Could not download %s, HTTP %ld\n", lprintf(warning, "path_download(): Could not download %s, HTTP %ld\n",
link->f_url, http_resp); link->f_url, http_resp);
return -ENOENT; return -ENOENT;
} }

View File

@ -106,11 +106,6 @@ extern int ROOT_LINK_OFFSET;
*/ */
LinkTable *LinkSystem_init(const char *raw_url); LinkTable *LinkSystem_init(const char *raw_url);
/**
* \brief Add a link to the curl multi bundle for querying stats
*/
void Link_req_file_stat(Link *this_link);
/** /**
* \brief Set the stats of a link, after curl multi handle finished querying * \brief Set the stats of a link, after curl multi handle finished querying
*/ */

View File

@ -84,7 +84,8 @@ activate Sonic mode.\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if(!LinkSystem_init(base_url)) { if(!LinkSystem_init(base_url)) {
lprintf(fatal, "Network initialisation failed.\n"); fprintf(stderr, "Network initialisation failed.\n");
exit(EXIT_FAILURE);
} }
} }

View File

@ -171,7 +171,6 @@ static void XMLCALL XML_parser_general(void *data, const char *elem,
for (int i = 0; attr[i]; i += 2) { for (int i = 0; attr[i]; i += 2) {
lprintf(error, "%s: %s\n", attr[i], attr[i+1]); lprintf(error, "%s: %s\n", attr[i], attr[i+1]);
} }
exit_failure();
} }
LinkTable *linktbl = (LinkTable *) data; LinkTable *linktbl = (LinkTable *) data;
@ -360,7 +359,6 @@ static void XMLCALL XML_parser_id3_root(void *data, const char *elem,
for (int i = 0; attr[i]; i += 2) { for (int i = 0; attr[i]; i += 2) {
lprintf(error, "%s: %s\n", attr[i], attr[i+1]); lprintf(error, "%s: %s\n", attr[i], attr[i+1]);
} }
exit_failure();
} }
LinkTable *root_linktbl = (LinkTable *) data; LinkTable *root_linktbl = (LinkTable *) data;