clean up lprintf statements - we have link.c and network.c left.

This commit is contained in:
Fufu Fang 2021-08-30 03:43:45 +01:00
parent 7813487c50
commit 2a4c61477a
No known key found for this signature in database
GPG Key ID: 0F6BB5EF6F8BB729
5 changed files with 59 additions and 72 deletions

View File

@ -719,8 +719,6 @@ Cache *Cache_open(const char *fn)
return NULL;
}
/*---------------- Cache_open() critical section -----------------*/
lprintf(cache_lock_debug, "Cache_open(): thread %x: locking cf_lock;\n",
pthread_self());
PTHREAD_MUTEX_LOCK(&cf_lock);
@ -733,11 +731,6 @@ Cache *Cache_open(const char *fn)
return link->cache_ptr;
}
lprintf(cache_lock_debug, "Cache_open(): thread %x: unlocking cf_lock;\n",
pthread_self());
PTHREAD_MUTEX_UNLOCK(&cf_lock);
/*----------------------------------------------------------------*/
/* Check if both metadata and data file exist */
if (CONFIG.mode == NORMAL) {
if (Cache_exist(fn)) {
@ -809,13 +802,15 @@ cf->content_length: %ld, Data_size(fn): %ld.\n", fn, cf->content_length,
/* Yup, we just created a circular loop. ;) */
cf->link->cache_ptr = cf;
lprintf(cache_lock_debug, "Cache_open(): thread %x: unlocking cf_lock;\n",
pthread_self());
PTHREAD_MUTEX_UNLOCK(&cf_lock);
return cf;
}
void Cache_close(Cache *cf)
{
/*--------------- Cache_close() critical section -----------------*/
lprintf(cache_lock_debug, "Cache_close(): thread %x: locking cf_lock;\n",
pthread_self());
PTHREAD_MUTEX_LOCK(&cf_lock);
@ -829,26 +824,24 @@ void Cache_close(Cache *cf)
return;
}
lprintf(cache_lock_debug, "Cache_close(): thread %x: unlocking cf_lock;\n",
pthread_self());
PTHREAD_MUTEX_UNLOCK(&cf_lock);
/*----------------------------------------------------------------*/
if (Meta_write(cf)) {
lprintf(debug, "Cache_close(): Meta_write() error.");
lprintf(error, "Cache_close(): Meta_write() error.");
}
if (fclose(cf->mfp)) {
lprintf(debug, "Cache_close(): cannot close metadata: %s.\n",
lprintf(error, "Cache_close(): cannot close metadata: %s.\n",
strerror(errno));
}
if (fclose(cf->dfp)) {
lprintf(debug, "Cache_close(): cannot close data file %s.\n",
lprintf(error, "Cache_close(): cannot close data file %s.\n",
strerror(errno));
}
lprintf(cache_lock_debug, "Cache_close(): thread %x: unlocking cf_lock;\n",
pthread_self());
PTHREAD_MUTEX_UNLOCK(&cf_lock);
return Cache_free(cf);
}
@ -884,19 +877,18 @@ static void Seg_set(Cache *cf, off_t offset, int i)
static void *Cache_bgdl(void *arg)
{
Cache *cf = (Cache *) arg;
#ifdef CACHE_LOCK_DEBUG
lprintf(debug, "Cache_bgdl(): thread %x: locking w_lock;\n",
lprintf(cache_lock_debug, "Cache_bgdl(): thread %x: locking w_lock;\n",
pthread_self());
#endif
PTHREAD_MUTEX_LOCK(&cf->w_lock);
uint8_t *recv_buf = CALLOC(cf->blksz, sizeof(uint8_t));
lprintf(debug, "Cache_bgdl(): thread %x: ", pthread_self());
lprintf(debug, "Cache_bgdl(): thread %x spawned.\n ", pthread_self());
long recv = path_download(cf->fs_path, (char *) recv_buf, cf->blksz,
cf->next_dl_offset);
if (recv < 0) {
lprintf(debug, "\nCache_bgdl(): received %lu bytes, \
which does't make sense\n", recv);
exit_failure();
lprintf(error, "Cache_bgdl(): thread %x received %ld bytes, \
which does't make sense\n", pthread_self(), recv);
}
if ( (recv == cf->blksz) ||
@ -905,20 +897,21 @@ which does't make sense\n", recv);
Data_write(cf, recv_buf, recv, cf->next_dl_offset);
Seg_set(cf, cf->next_dl_offset, 1);
} else {
lprintf(debug,
"Cache_bgdl(): received %ld, possible network error.\n", recv);
lprintf(error,
"Cache_bgdl(): received %ld rather than %ld, possible network \
error.\n", recv, cf->blksz);
}
FREE(recv_buf);
#ifdef CACHE_LOCK_DEBUG
lprintf(debug, "Cache_bgdl(): thread %x: unlocking bgt_lock;\n",
lprintf(cache_lock_debug, "Cache_bgdl(): thread %x: unlocking bgt_lock;\n",
pthread_self());
#endif
PTHREAD_MUTEX_UNLOCK(&cf->bgt_lock);
#ifdef CACHE_LOCK_DEBUG
lprintf(debug, "Cache_bgdl(): thread %x: unlocking w_lock;\n",
lprintf(cache_lock_debug, "Cache_bgdl(): thread %x: unlocking w_lock;\n",
pthread_self());
#endif
PTHREAD_MUTEX_UNLOCK(&cf->w_lock);
pthread_detach(pthread_self());
pthread_exit(NULL);
}
@ -937,20 +930,21 @@ long Cache_read(Cache *cf, char * const output_buf, const off_t len,
goto bgdl;
} else {
/* Wait for any other download thread to finish*/
#ifdef CACHE_LOCK_DEBUG
lprintf(debug, "Cache_read(): thread %ld: locking w_lock;\n",
lprintf(cache_lock_debug, "Cache_read(): thread %ld: locking w_lock;\n",
pthread_self());
#endif
PTHREAD_MUTEX_LOCK(&cf->w_lock);
if (Seg_exist(cf, dl_offset)) {
/* The segment now exists - it was downloaded by another
* download thread. Send it off and unlock the I/O */
send = Data_read(cf, (uint8_t *) output_buf, len, offset_start);
#ifdef CACHE_LOCK_DEBUG
lprintf(debug, "Cache_read(): thread %x: unlocking w_lock;\n",
lprintf(cache_lock_debug,
"Cache_read(): thread %x: unlocking w_lock;\n",
pthread_self());
#endif
PTHREAD_MUTEX_UNLOCK(&cf->w_lock);
goto bgdl;
}
}
@ -958,13 +952,12 @@ long Cache_read(Cache *cf, char * const output_buf, const off_t len,
/* ------------------------Download the segment -------------------------*/
uint8_t *recv_buf = CALLOC(cf->blksz, sizeof(uint8_t));
lprintf(debug, "Cache_read(): thread %x: ", pthread_self());
lprintf(debug, "Cache_read(): thread %x: spawned.\n ", pthread_self());
long recv = path_download(cf->fs_path, (char *) recv_buf, cf->blksz,
dl_offset);
if (recv < 0) {
lprintf(debug, "\nCache_read(): received %ld bytes, \
which does't make sense\n", recv);
exit_failure();
lprintf(error, "Cache_read(): thread %x received %ld bytes, \
which does't make sense\n", pthread_self(), recv);
}
/*
* check if we have received enough data, write it to the disk
@ -978,34 +971,32 @@ which does't make sense\n", recv);
Data_write(cf, recv_buf, recv, dl_offset);
Seg_set(cf, dl_offset, 1);
} else {
lprintf(debug,
"Cache_read(): received %ld, possible network error.\n", recv);
lprintf(error,
"Cache_read(): received %ld rather than %ld, possible network \
error.\n", recv, cf->blksz);
}
FREE(recv_buf);
send = Data_read(cf, (uint8_t *) output_buf, len, offset_start);
#ifdef CACHE_LOCK_DEBUG
lprintf(debug, "Cache_read(): thread %x: unlocking w_lock;\n",
lprintf(cache_lock_debug, "Cache_read(): thread %x: unlocking w_lock;\n",
pthread_self());
#endif
PTHREAD_MUTEX_UNLOCK(&cf->w_lock);
/* -----------Download the next segment in background -------------------*/
bgdl:
;
{}
off_t next_dl_offset = round_div(offset_start, cf->blksz) * cf->blksz;
if ( (next_dl_offset > dl_offset) &&
!Seg_exist(cf, next_dl_offset) &&
next_dl_offset < cf->content_length ){
/* Stop the spawning of multiple background pthreads */
if(!pthread_mutex_trylock(&cf->bgt_lock)) {
#ifdef CACHE_LOCK_DEBUG
lprintf(debug, "Cache_read(): thread %x: trylocked bgt_lock;\n",
lprintf(cache_lock_debug,
"Cache_read(): thread %x: trylocked bgt_lock;\n",
pthread_self());
#endif
cf->next_dl_offset = next_dl_offset;
if (pthread_create(&cf->bgt, NULL, Cache_bgdl, cf)) {
lprintf(debug,
lprintf(error,
"Cache_read(): Error creating background download thread\n"
);
}

View File

@ -761,9 +761,8 @@ 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(debug, "Error: This web server does not support HTTP \
lprintf(fatal, "This web server does not support HTTP \
range requests\n");
exit(EXIT_FAILURE);
}
}

View File

@ -43,7 +43,6 @@ void log_printf(LogType type, const char *file, const char *func, int line,
fprintf(out, "(%s:%s:%d): ", file, func, line);
print_actual_message:
/* A label can only be part of a statement, this is a statement. lol*/
{}
va_list args;
va_start(args, format);

View File

@ -23,7 +23,7 @@ int main(int argc, char **argv)
/* Automatically print help if not enough arguments are supplied */
if (argc < 2) {
print_help(argv[0], 0);
lprintf(debug, "For more information, run \"%s --help.\"\n", argv[0]);
fprintf(stderr, "For more information, run \"%s --help.\"\n", argv[0]);
exit(EXIT_FAILURE);
}
@ -71,21 +71,20 @@ int main(int argc, char **argv)
/* The second last remaining argument is the URL */
char *base_url = argv[argc-2];
if (strncmp(base_url, "http://", 7) && strncmp(base_url, "https://", 8)) {
lprintf(debug, "Error: Please supply a valid URL.\n");
fprintf(stderr, "Error: Please supply a valid URL.\n");
print_help(argv[0], 0);
exit(EXIT_FAILURE);
} else {
if (CONFIG.sonic_username && CONFIG.sonic_password) {
CONFIG.mode = SONIC;
} else if (CONFIG.sonic_username || CONFIG.sonic_password) {
lprintf(debug,
fprintf(stderr,
"Error: You have to supply both username and password to \
activate Sonic mode.\n");
exit(EXIT_FAILURE);
}
if(!LinkSystem_init(base_url)) {
lprintf(debug, "Error: Network initialisation failed.\n");
exit(EXIT_FAILURE);
lprintf(fatal, "Network initialisation failed.\n");
}
}
@ -262,12 +261,12 @@ parse_arg_list(int argc, char **argv, char ***fuse_argv, int *fuse_argc)
CONFIG.mode = SINGLE_FILE;
break;
default:
lprintf(debug, "see httpdirfs -h for usage\n");
fprintf(stderr, "see httpdirfs -h for usage\n");
return 1;
}
break;
default:
lprintf(debug, "see httpdirfs -h for usage\n");
fprintf(stderr, "see httpdirfs -h for usage\n");
return 1;
}
};

View File

@ -167,11 +167,11 @@ static void XMLCALL XML_parser_general(void *data, const char *elem,
{
/* Error checking */
if (!strcmp(elem, "error")) {
lprintf(debug, "XML_parser_general() error:\n");
lprintf(error, "XML_parser_general() error:\n");
for (int i = 0; attr[i]; i += 2) {
lprintf(debug, "%s: %s\n", attr[i], attr[i+1]);
lprintf(error, "%s: %s\n", attr[i], attr[i+1]);
}
exit(EXIT_FAILURE);
exit_failure();
}
LinkTable *linktbl = (LinkTable *) data;
@ -322,7 +322,7 @@ static LinkTable *sonic_url_to_LinkTable(const char *url,
XML_SetStartElementHandler(parser, handler);
if (XML_Parse(parser, xml.data, xml.size, 1) == XML_STATUS_ERROR) {
lprintf(debug,
lprintf(error,
"sonic_XML_to_LinkTable(): Parse error at line %lu: %s\n",
XML_GetCurrentLineNumber(parser),
XML_ErrorString(XML_GetErrorCode(parser)));
@ -356,11 +356,11 @@ static void XMLCALL XML_parser_id3_root(void *data, const char *elem,
const char **attr)
{
if (!strcmp(elem, "error")) {
lprintf(debug, "XML_parser_id3_root() error:\n");
lprintf(error, "XML_parser_id3_root():\n");
for (int i = 0; attr[i]; i += 2) {
lprintf(debug, "%s: %s\n", attr[i], attr[i+1]);
lprintf(error, "%s: %s\n", attr[i], attr[i+1]);
}
exit(EXIT_FAILURE);
exit_failure();
}
LinkTable *root_linktbl = (LinkTable *) data;
@ -454,8 +454,7 @@ LinkTable *sonic_LinkTable_new_id3(int depth, const char *id)
/*
* We shouldn't reach here.
*/
lprintf(debug, "sonic_LinkTable_new_id3(): case %d.\n", depth);
exit_failure();
lprintf(fatal, "sonic_LinkTable_new_id3(): case %d.\n", depth);
break;
}
return linktbl;