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);
DIR* dir;
dir = opendir(path);
if (!dir) {
lprintf(fatal,
"CacheSystem_init(): opendir(): %s\n", strerror(errno));
}
closedir(dir);
META_DIR = path_append(path, "meta/");
DATA_DIR = path_append(path, "data/");
/* Check if directories exist, if not, create them */
@ -270,12 +263,9 @@ static int Meta_write(Cache *cf)
/**
* \brief create a data file
* \details We use sparse creation here
* \return
* - 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.
* \return exit on failure
*/
static int Data_create(Cache *cf)
static void Data_create(Cache *cf)
{
int fd;
int mode;
@ -285,17 +275,14 @@ static int Data_create(Cache *cf)
fd = open(datafn, O_WRONLY | O_CREAT, mode);
FREE(datafn);
if (fd == -1) {
lprintf(error, "Data_create(): open(): %s\n", strerror(errno));
return -1;
lprintf(fatal, "Data_create(): open(): %s\n", strerror(errno));
}
if (ftruncate(fd, cf->content_length)) {
lprintf(warning, "Data_create(): ftruncate(): %s\n", strerror(errno));
}
if (close(fd)) {
lprintf(error, "Data_create(): close:(): %s\n", strerror(errno));
return -1;
lprintf(fatal, "Data_create(): close:(): %s\n", strerror(errno));
}
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);
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;
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(metadirn);
@ -458,25 +445,25 @@ static Cache *Cache_alloc()
Cache *cf = CALLOC(1, sizeof(Cache));
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)) {
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)) {
lprintf(error,
lprintf(fatal,
"Cache_alloc(): bgt_lock_attr initialisation failed!\n");
}
if (pthread_mutexattr_setpshared(&cf->bgt_lock_attr,
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)) {
lprintf(error, "Cache_alloc(): bgt_lock initialisation failed!\n");
lprintf(fatal, "Cache_alloc(): bgt_lock initialisation failed!\n");
}
return cf;
@ -488,19 +475,19 @@ static Cache *Cache_alloc()
static void Cache_free(Cache *cf)
{
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)) {
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)) {
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)) {
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) {
@ -630,23 +617,18 @@ static int Meta_open(Cache *cf)
/**
* \brief Create a metafile
* \return
* - 0 on success
* - -1 on failure, with appropriate errno set.
* \return exit on error
*/
static int Meta_create(Cache *cf)
static void Meta_create(Cache *cf)
{
char *metafn = path_append(META_DIR, cf->path);
cf->mfp = fopen(metafn, "w");
if (!cf->mfp) {
/* 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));
FREE(metafn);
return -1;
}
FREE(metafn);
return 0;
}
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->seg = CALLOC(cf->segbc, sizeof(Seg));
if (Meta_create(cf)) {
lprintf(error, "Cache_create(): cannot create metadata.\n");
}
Meta_create(cf);
if (fclose(cf->mfp)) {
lprintf(error,
@ -695,9 +675,7 @@ int Cache_create(const char *path)
strerror(errno));
}
if (Data_create(cf)) {
lprintf(error, "Cache_create(): Data_create() failed!\n");
}
Data_create(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) {
lprintf(fatal,
lprintf(error,
"link_system_init(): link_lock initialisation failed!\n");
exit_failure();
}
/* --------- Set the length of the root link ----------- */
@ -77,8 +76,7 @@ void LinkTable_add(LinkTable *linktbl, Link *link)
linktbl->num++;
linktbl->links = realloc(linktbl->links, linktbl->num * sizeof(Link *));
if (!linktbl->links) {
lprintf(debug, "LinkTable_add(): realloc failure!\n");
exit_failure();
lprintf(fatal, "LinkTable_add(): realloc() failure!\n");
}
linktbl->links[linktbl->num - 1] = link;
}
@ -186,7 +184,7 @@ static CURL *Link_to_curl(Link *link)
{
CURL *curl = curl_easy_init();
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 */
curl_easy_setopt(curl, CURLOPT_USERAGENT, CONFIG.user_agent);
@ -229,14 +227,8 @@ static CURL *Link_to_curl(Link *link)
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_easy_setopt(curl, CURLOPT_NOBODY, 1);
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;
}
} 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)) {
lprintf(debug, ", retrying later.\n");
lprintf(warning, ", retrying later.\n");
} else {
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);
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_resp);
if (HTTP_temp_failure(http_resp)) {
lprintf(debug,
"LinkTable_new(): URL: %s, HTTP %ld, retrying later.\n",
url, http_resp);
lprintf(warning,
"Link_to_DataStruct(): URL: %s, HTTP %ld, retrying later.\n"
, url, http_resp);
sleep(CONFIG.http_wait_sec);
} else if (http_resp != HTTP_OK) {
lprintf(debug,
"LinkTable_new(): cannot retrieve URL: %s, HTTP %ld\n",
lprintf(warning,
"Link_to_DataStruct(): cannot retrieve URL: %s, HTTP %ld\n",
url, http_resp);
buf.size = 0;
curl_easy_cleanup(curl);
@ -515,7 +507,7 @@ static void LinkTable_disk_delete(const char *dirn)
path = path_append(metadirn, "/.LinkTable");
}
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));
}
FREE(path);
@ -535,7 +527,7 @@ int LinkTable_disk_save(LinkTable *linktbl, const char *dirn)
FREE(metadirn);
if (!fp) {
lprintf(debug, "LinkTable_disk_save(): fopen(%s): %s\n", path,
lprintf(error, "LinkTable_disk_save(): fopen(%s): %s\n", path,
strerror(errno));
FREE(path);
return -1;
@ -554,12 +546,12 @@ int LinkTable_disk_save(LinkTable *linktbl, const char *dirn)
int res = 0;
if (ferror(fp)) {
lprintf(debug, "LinkTable_disk_save(): encountered ferror!\n");
lprintf(error, "LinkTable_disk_save(): encountered ferror!\n");
res = -1;
}
if (fclose(fp)) {
lprintf(debug,
lprintf(error,
"LinkTable_disk_save(): cannot close the file pointer, %s\n",
strerror(errno));
res = -1;
@ -598,21 +590,21 @@ LinkTable *LinkTable_disk_open(const char *dirn)
fread(&linktbl->links[i]->time, sizeof(long), 1, fp);
if (feof(fp)) {
/* reached EOF */
lprintf(debug,
lprintf(error,
"LinkTable_disk_open(): reached EOF!\n");
LinkTable_free(linktbl);
LinkTable_disk_delete(dirn);
return NULL;
}
if (ferror(fp)) {
lprintf(debug, "LinkTable_disk_open(): encountered ferror!\n");
lprintf(error, "LinkTable_disk_open(): encountered ferror!\n");
LinkTable_free(linktbl);
LinkTable_disk_delete(dirn);
return NULL;
}
}
if (fclose(fp)) {
lprintf(debug,
lprintf(error,
"LinkTable_disk_open(): cannot close the file pointer, %s\n",
strerror(errno));
}
@ -711,8 +703,7 @@ Link *path_to_Link(const char *path)
PTHREAD_MUTEX_LOCK(&link_lock);
char *new_path = strndup(path, MAX_PATH_LEN);
if (!new_path) {
lprintf(debug, "path_to_Link(): cannot allocate memory\n");
exit_failure();
lprintf(fatal, "path_to_Link(): cannot allocate memory\n");
}
Link *link = path_to_Link_recursive(new_path, ROOT_LINK_TBL);
FREE(new_path);
@ -728,8 +719,7 @@ long path_download(const char *path, char *output_buf, size_t size,
off_t offset)
{
if (!path) {
lprintf(debug, "\npath_download(): NULL path supplied\n");
exit_failure();
lprintf(fatal, "\npath_download(): NULL path supplied\n");
}
Link *link;
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 */
if (!CONFIG.no_range_check) {
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");
exit(EXIT_FAILURE);
}
}
@ -775,7 +766,7 @@ range requests\n");
(http_resp != HTTP_PARTIAL_CONTENT) ||
(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);
return -ENOENT;
}

View File

@ -106,11 +106,6 @@ extern int ROOT_LINK_OFFSET;
*/
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
*/

View File

@ -84,7 +84,8 @@ activate Sonic mode.\n");
exit(EXIT_FAILURE);
}
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) {
lprintf(error, "%s: %s\n", attr[i], attr[i+1]);
}
exit_failure();
}
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) {
lprintf(error, "%s: %s\n", attr[i], attr[i+1]);
}
exit_failure();
}
LinkTable *root_linktbl = (LinkTable *) data;