diff --git a/src/cache.c b/src/cache.c index 6646a27..bbe15f9 100644 --- a/src/cache.c +++ b/src/cache.c @@ -53,13 +53,13 @@ static char *CacheSystem_calc_dir(const char *url) } if (mkdir(xdg_cache_home, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) && (errno != EEXIST)) { - lprintf(debug, "CacheSystem_calc_dir(): mkdir(): %s\n", + lprintf(ldebug, "CacheSystem_calc_dir(): mkdir(): %s\n", strerror(errno)); } char *cache_dir_root = path_append(xdg_cache_home, "/httpdirfs/"); if (mkdir(cache_dir_root, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) && (errno != EEXIST)) { - lprintf(debug, "CacheSystem_calc_dir(): mkdir(): %s\n", + lprintf(ldebug, "CacheSystem_calc_dir(): mkdir(): %s\n", strerror(errno)); } @@ -72,15 +72,15 @@ static char *CacheSystem_calc_dir(const char *url) # For information about cache directory tags, see:\n\ # http://www.brynosaurus.com/cachedir/\n"); } else { - lprintf(debug, "CacheSystem_calc_dir(): fopen(%s): %s", fn, + lprintf(ldebug, "CacheSystem_calc_dir(): fopen(%s): %s", fn, strerror(errno)); } if (ferror(fp)) { - lprintf(debug, + lprintf(ldebug, "CacheSystem_calc_dir(): fwrite(): encountered error!\n"); } if (fclose(fp)) { - lprintf(debug, "CacheSystem_calc_dir(): fclose(%s): %s\n", fn, + lprintf(ldebug, "CacheSystem_calc_dir(): fclose(%s): %s\n", fn, strerror(errno)); } CURL* c = curl_easy_init(); @@ -88,7 +88,7 @@ static char *CacheSystem_calc_dir(const char *url) char *full_path = path_append(cache_dir_root, escaped_url); if (mkdir(full_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) && (errno != EEXIST)) { - lprintf(debug, "CacheSystem_calc_dir(): mkdir(): %s\n", + lprintf(ldebug, "CacheSystem_calc_dir(): mkdir(): %s\n", strerror(errno)); } free(fn); @@ -101,7 +101,7 @@ static char *CacheSystem_calc_dir(const char *url) void CacheSystem_init(const char *path, int url_supplied) { if (pthread_mutex_init(&cf_lock, NULL) != 0) { - lprintf(debug, + lprintf(ldebug, "CacheSystem_init(): cf_lock initialisation failed!\n"); exit_failure(); } @@ -110,12 +110,12 @@ void CacheSystem_init(const char *path, int url_supplied) path = CacheSystem_calc_dir(path); } - lprintf(debug, "CacheSystem_init(): directory: %s\n", path); + lprintf(ldebug, "CacheSystem_init(): directory: %s\n", path); DIR* dir; dir = opendir(path); if (!dir) { - lprintf(debug, + lprintf(ldebug, "CacheSystem_init(): opendir(): %s\n", strerror(errno)); exit_failure(); } @@ -125,25 +125,25 @@ void CacheSystem_init(const char *path, int url_supplied) /* Check if directories exist, if not, create them */ if (mkdir(META_DIR, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) && (errno != EEXIST)) { - lprintf(debug, "CacheSystem_init(): mkdir(): %s\n", + lprintf(ldebug, "CacheSystem_init(): mkdir(): %s\n", strerror(errno)); exit_failure(); } if (mkdir(DATA_DIR, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) && (errno != EEXIST)) { - lprintf(debug, "CacheSystem_init(): mkdir(): %s\n", + lprintf(ldebug, "CacheSystem_init(): mkdir(): %s\n", strerror(errno)); exit_failure(); } - if (CONFIG.sonic_mode) { + if (CONFIG.mode == SONIC) { char *sonic_path; /* Create "rest" sub-directory for META_DIR */ sonic_path = path_append(META_DIR, "rest/"); if (mkdir(sonic_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) && (errno != EEXIST)) { - lprintf(debug, "CacheSystem_init(): mkdir(): %s\n", + lprintf(ldebug, "CacheSystem_init(): mkdir(): %s\n", strerror(errno)); exit_failure(); } @@ -153,7 +153,7 @@ void CacheSystem_init(const char *path, int url_supplied) sonic_path = path_append(DATA_DIR, "rest/"); if (mkdir(sonic_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) && (errno != EEXIST)) { - lprintf(debug, "CacheSystem_init(): mkdir(): %s\n", + lprintf(ldebug, "CacheSystem_init(): mkdir(): %s\n", strerror(errno)); exit_failure(); } @@ -179,7 +179,7 @@ static int Meta_read(Cache *cf) if (!fp) { /* The metadata file does not exist */ - lprintf(debug, "Meta_read(): fopen(): %s\n", strerror(errno)); + lprintf(ldebug, "Meta_read(): fopen(): %s\n", strerror(errno)); return EFREAD; } @@ -190,25 +190,25 @@ static int Meta_read(Cache *cf) /* Error checking for fread */ if (ferror(fp)) { - lprintf(debug, + lprintf(ldebug, "Meta_read(): error reading core metadata!\n"); } /* These things really should not be zero!!! */ if (!cf->content_length || !cf->blksz || !cf->segbc) { - lprintf(debug, + lprintf(ldebug, "Meta_read(): corrupt metadata: %s, content_length: %ld, \ blksz: %d, segbc: %ld\n", cf->path, cf->content_length, cf->blksz, cf->segbc); return EZERO; } if (cf->blksz != CONFIG.data_blksz) { - lprintf(debug, "Meta_read(): Warning: cf->blksz != CONFIG.data_blksz\n"); + lprintf(ldebug, "Meta_read(): Warning: cf->blksz != CONFIG.data_blksz\n"); } /* Allocate some memory for the segment */ if (cf->segbc > CONFIG.max_segbc) { - lprintf(debug, "Meta_read(): Error: segbc: %ld\n", cf->segbc); + lprintf(ldebug, "Meta_read(): Error: segbc: %ld\n", cf->segbc); return EMEM; } cf->seg = CALLOC(cf->segbc, sizeof(Seg)); @@ -219,21 +219,21 @@ blksz: %d, segbc: %ld\n", cf->path, cf->content_length, cf->blksz, cf->segbc); /* We shouldn't have gone past the end of the file */ if (feof(fp)) { /* reached EOF */ - lprintf(debug, + lprintf(ldebug, "Meta_read(): attempted to read past the end of the file!\n"); return EINCONSIST; } /* Error checking for fread */ if (ferror(fp)) { - lprintf(debug, + lprintf(ldebug, "Meta_read(): error reading bitmap!\n"); return EFREAD; } /* Check for inconsistent metadata file */ if (nmemb != cf-> segbc) { - lprintf(debug, + lprintf(ldebug, "Meta_read(): corrupted metadata!\n"); return EINCONSIST; } @@ -254,13 +254,13 @@ static int Meta_write(Cache *cf) if (!fp) { /* Cannot create the metadata file */ - lprintf(debug, "Meta_write(): fopen(): %s\n", strerror(errno)); + lprintf(ldebug, "Meta_write(): fopen(): %s\n", strerror(errno)); return -1; } /* These things really should not be zero!!! */ if (!cf->content_length || !cf->blksz || !cf->segbc) { - lprintf(debug, + lprintf(ldebug, "Meta_write(): Warning: content_length: %ld, blksz: %d, segbc: \ %ld\n", cf->content_length, cf->blksz, cf->segbc); } @@ -275,7 +275,7 @@ static int Meta_write(Cache *cf) /* Error checking for fwrite */ if (ferror(fp)) { - lprintf(debug, + lprintf(ldebug, "Meta_write(): fwrite(): encountered error!\n"); return -1; } @@ -301,14 +301,14 @@ static int Data_create(Cache *cf) fd = open(datafn, O_WRONLY | O_CREAT, mode); free(datafn); if (fd == -1) { - lprintf(debug, "Data_create(): open(): %s\n", strerror(errno)); + lprintf(ldebug, "Data_create(): open(): %s\n", strerror(errno)); return -1; } if (ftruncate(fd, cf->content_length)) { - lprintf(debug, "Data_create(): ftruncate(): %s\n", strerror(errno)); + lprintf(ldebug, "Data_create(): ftruncate(): %s\n", strerror(errno)); } if (close(fd)) { - lprintf(debug, "Data_create(): close:(): %s\n", strerror(errno)); + lprintf(ldebug, "Data_create(): close:(): %s\n", strerror(errno)); } return 0; } @@ -325,7 +325,7 @@ static long Data_size(const char *fn) if (!s) { return st.st_size; } - lprintf(debug, "Data_size(): stat(): %s\n", strerror(errno)); + lprintf(ldebug, "Data_size(): stat(): %s\n", strerror(errno)); return -1; } @@ -342,12 +342,12 @@ static long Data_size(const char *fn) static long Data_read(Cache *cf, uint8_t *buf, off_t len, off_t offset) { if (len == 0) { - lprintf(debug, "Data_read(): requested to read 0 byte!\n"); + lprintf(ldebug, "Data_read(): requested to read 0 byte!\n"); return -EINVAL; } #ifdef CACHE_LOCK_DEBUG - lprintf(debug, "Data_read(): thread %lu: locking seek_lock;\n", + lprintf(ldebug, "Data_read(): thread %lu: locking seek_lock;\n", pthread_self()); #endif PTHREAD_MUTEX_LOCK(&cf->seek_lock); @@ -356,7 +356,7 @@ static long Data_read(Cache *cf, uint8_t *buf, off_t len, off_t offset) if (fseeko(cf->dfp, offset, SEEK_SET)) { /* fseeko failed */ - lprintf(debug, "Data_read(): fseeko(): %s\n", strerror(errno)); + lprintf(ldebug, "Data_read(): fseeko(): %s\n", strerror(errno)); byte_read = -EIO; goto end; } @@ -370,24 +370,24 @@ static long Data_read(Cache *cf, uint8_t *buf, off_t len, off_t offset) byte_read = fread(buf, sizeof(uint8_t), len, cf->dfp); if (byte_read != len) { - lprintf(debug, + lprintf(ldebug, "Data_read(): fread(): requested %ld, returned %ld!\n", len, byte_read); if (feof(cf->dfp)) { /* reached EOF */ - lprintf(debug, + lprintf(ldebug, "Data_read(): fread(): reached the end of the file!\n"); } if (ferror(cf->dfp)) { /* filesystem error */ - lprintf(debug, + lprintf(ldebug, "Data_read(): fread(): encountered error!\n"); } } end: #ifdef CACHE_LOCK_DEBUG - lprintf(debug, "Data_read(): thread %lu: unlocking seek_lock;\n", + lprintf(ldebug, "Data_read(): thread %lu: unlocking seek_lock;\n", pthread_self()); #endif PTHREAD_MUTEX_UNLOCK(&cf->seek_lock); @@ -410,12 +410,12 @@ static long Data_write(Cache *cf, const uint8_t *buf, off_t len, off_t offset) { if (len == 0) { - lprintf(debug, "Data_write(): requested to write 0 byte!\n"); + lprintf(ldebug, "Data_write(): requested to write 0 byte!\n"); return -EINVAL; } #ifdef CACHE_LOCK_DEBUG - lprintf(debug, "Data_write(): thread %lu: locking seek_lock;\n", + lprintf(ldebug, "Data_write(): thread %lu: locking seek_lock;\n", pthread_self()); #endif PTHREAD_MUTEX_LOCK(&cf->seek_lock); @@ -424,27 +424,27 @@ static long Data_write(Cache *cf, const uint8_t *buf, off_t len, if (fseeko(cf->dfp, offset, SEEK_SET)) { /* fseeko failed */ - lprintf(debug, "Data_write(): fseeko(): %s\n", strerror(errno)); + lprintf(ldebug, "Data_write(): fseeko(): %s\n", strerror(errno)); byte_written = -EIO; goto end; } byte_written = fwrite(buf, sizeof(uint8_t), len, cf->dfp); if (byte_written != len) { - lprintf(debug, + lprintf(ldebug, "Data_write(): fwrite(): requested %ld, returned %ld!\n", len, byte_written); exit_failure(); if (ferror(cf->dfp)) { /* filesystem error */ - lprintf(debug, + lprintf(ldebug, "Data_write(): fwrite(): encountered error!\n"); } } end: #ifdef CACHE_LOCK_DEBUG - lprintf(debug, "Data_write(): thread %lu: unlocking seek_lock;\n", + lprintf(ldebug, "Data_write(): thread %lu: unlocking seek_lock;\n", pthread_self()); #endif PTHREAD_MUTEX_UNLOCK(&cf->seek_lock); @@ -459,12 +459,12 @@ int CacheDir_create(const char *dirn) i = -mkdir(metadirn, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); if (i && (errno != EEXIST)) { - lprintf(debug, "CacheDir_create(): mkdir(): %s\n", strerror(errno)); + lprintf(ldebug, "CacheDir_create(): mkdir(): %s\n", strerror(errno)); } i |= -mkdir(datadirn, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) << 1; if (i && (errno != EEXIST)) { - lprintf(debug, "CacheDir_create(): mkdir(): %s\n", strerror(errno)); + lprintf(ldebug, "CacheDir_create(): mkdir(): %s\n", strerror(errno)); } free(datadirn); free(metadirn); @@ -479,25 +479,25 @@ static Cache *Cache_alloc() Cache *cf = CALLOC(1, sizeof(Cache)); if (pthread_mutex_init(&cf->seek_lock, NULL)) { - lprintf(debug, "Cache_alloc(): seek_lock initialisation failed!\n"); + lprintf(ldebug, "Cache_alloc(): seek_lock initialisation failed!\n"); } if (pthread_mutex_init(&cf->w_lock, NULL)) { - lprintf(debug, "Cache_alloc(): w_lock initialisation failed!\n"); + lprintf(ldebug, "Cache_alloc(): w_lock initialisation failed!\n"); } if (pthread_mutexattr_init(&cf->bgt_lock_attr)) { - lprintf(debug, + lprintf(ldebug, "Cache_alloc(): bgt_lock_attr initialisation failed!\n"); } if (pthread_mutexattr_setpshared(&cf->bgt_lock_attr, PTHREAD_PROCESS_SHARED)) { - lprintf(debug, "Cache_alloc(): could not set bgt_lock_attr!\n"); + lprintf(ldebug, "Cache_alloc(): could not set bgt_lock_attr!\n"); } if (pthread_mutex_init(&cf->bgt_lock, &cf->bgt_lock_attr)) { - lprintf(debug, "Cache_alloc(): bgt_lock initialisation failed!\n"); + lprintf(ldebug, "Cache_alloc(): bgt_lock initialisation failed!\n"); } return cf; @@ -509,19 +509,19 @@ static Cache *Cache_alloc() static void Cache_free(Cache *cf) { if (pthread_mutex_destroy(&cf->seek_lock)) { - lprintf(debug, "Cache_free(): could not destroy seek_lock!\n"); + lprintf(ldebug, "Cache_free(): could not destroy seek_lock!\n"); } if (pthread_mutex_destroy(&cf->w_lock)) { - lprintf(debug, "Cache_free(): could not destroy w_lock!\n"); + lprintf(ldebug, "Cache_free(): could not destroy w_lock!\n"); } if (pthread_mutex_destroy(&cf->bgt_lock)) { - lprintf(debug, "Cache_free(): could not destroy bgt_lock!\n"); + lprintf(ldebug, "Cache_free(): could not destroy bgt_lock!\n"); } if (pthread_mutexattr_destroy(&cf->bgt_lock_attr)) { - lprintf(debug, "Cache_alloc(): could not destroy bgt_lock_attr!\n"); + lprintf(ldebug, "Cache_alloc(): could not destroy bgt_lock_attr!\n"); } if (cf->path) { @@ -551,29 +551,21 @@ static void Cache_free(Cache *cf) */ static int Cache_exist(const char *fn) { - int meta_exists = 1; - int data_exists = 1; char *metafn = path_append(META_DIR, fn); char *datafn = path_append(DATA_DIR, fn); + int no_meta = access(metafn, F_OK); + int no_data = access(datafn, F_OK); - if (access(metafn, F_OK)) { - meta_exists = 0; - } - - if (access(datafn, F_OK)) { - data_exists = 0; - } - - if (meta_exists ^ data_exists) { - if (meta_exists) { - if(unlink(metafn)) { - lprintf(debug, "Cache_exist(): unlink(): %s\n", + if (no_meta ^ no_data) { + if (no_meta) { + if(unlink(datafn)) { + lprintf(ldebug, "Cache_exist(): unlink(): %s\n", strerror(errno)); } } - if (data_exists) { - if(unlink(datafn)) { - lprintf(debug, "Cache_exist(): unlink(): %s\n", + if (no_data) { + if(unlink(metafn)) { + lprintf(ldebug, "Cache_exist(): unlink(): %s\n", strerror(errno)); } } @@ -582,7 +574,7 @@ static int Cache_exist(const char *fn) free(metafn); free(datafn); - return meta_exists & data_exists; + return no_meta | no_data; } /** @@ -590,7 +582,7 @@ static int Cache_exist(const char *fn) */ void Cache_delete(const char *fn) { - if (CONFIG.sonic_mode) { + if (CONFIG.mode == SONIC) { Link *link = path_to_Link(fn); fn = link->sonic_id; } @@ -599,14 +591,14 @@ void Cache_delete(const char *fn) char *datafn = path_append(DATA_DIR, fn); if (!access(metafn, F_OK)) { if(unlink(metafn)) { - lprintf(debug, "Cache_delete(): unlink(): %s\n", + lprintf(ldebug, "Cache_delete(): unlink(): %s\n", strerror(errno)); } } if (!access(datafn, F_OK)) { if(unlink(datafn)) { - lprintf(debug, "Cache_delete(): unlink(): %s\n", + lprintf(ldebug, "Cache_delete(): unlink(): %s\n", strerror(errno)); } } @@ -627,7 +619,7 @@ static int Data_open(Cache *cf) free(datafn); if (!cf->dfp) { /* Failed to open the data file */ - lprintf(debug, "Data_open(): fopen(%s): %s\n", datafn, + lprintf(ldebug, "Data_open(): fopen(%s): %s\n", datafn, strerror(errno)); return -1; } @@ -646,7 +638,7 @@ static int Meta_open(Cache *cf) cf->mfp = fopen(metafn, "r+"); if (!cf->mfp) { /* Failed to open the data file */ - lprintf(debug, "Meta_open(): fopen(%s): %s\n", metafn, + lprintf(ldebug, "Meta_open(): fopen(%s): %s\n", metafn, strerror(errno)); free(metafn); return -1; @@ -667,7 +659,7 @@ static int Meta_create(Cache *cf) cf->mfp = fopen(metafn, "w"); if (!cf->mfp) { /* Failed to open the data file */ - lprintf(debug, "Meta_create(): fopen(%s): %s\n", metafn, + lprintf(ldebug, "Meta_create(): fopen(%s): %s\n", metafn, strerror(errno)); free(metafn); return -1; @@ -680,14 +672,14 @@ int Cache_create(const char *path) { Link *this_link = path_to_Link(path); - char *fn; - if (!CONFIG.sonic_mode) { + char *fn = ""; + if (CONFIG.mode == NORMAL) { fn = curl_easy_unescape(NULL, this_link->f_url + ROOT_LINK_OFFSET, 0, NULL); - } else { + } else if (CONFIG.mode == SONIC) { fn = this_link->sonic_id; } - lprintf(debug, "Cache_create(): Creating cache files for %s.\n", fn); + lprintf(ldebug, "Cache_create(): Creating cache files for %s.\n", fn); Cache *cf = Cache_alloc(); cf->path = strndup(fn, MAX_PATH_LEN); @@ -698,43 +690,40 @@ int Cache_create(const char *path) cf->seg = CALLOC(cf->segbc, sizeof(Seg)); if (Meta_create(cf)) { - lprintf(debug, "Cache_create(): cannot create metadata.\n"); + lprintf(ldebug, "Cache_create(): cannot create metadata.\n"); exit_failure(); } if (fclose(cf->mfp)) { - lprintf(debug, + lprintf(ldebug, "Cache_create(): cannot close metadata after creation: %s.\n", strerror(errno)); } if (Meta_open(cf)) { Cache_free(cf); - lprintf(debug, "Cache_create(): cannot open metadata file, %s.\n", fn); + lprintf(ldebug, "Cache_create(): cannot open metadata file, %s.\n", fn); } if (Meta_write(cf)) { - lprintf(debug, "Cache_create(): Meta_write() failed!\n"); + lprintf(ldebug, "Cache_create(): Meta_write() failed!\n"); } if (fclose(cf->mfp)) { - lprintf(debug, + lprintf(ldebug, "Cache_create(): cannot close metadata after write, %s.\n", strerror(errno)); } if (Data_create(cf)) { - lprintf(debug, "Cache_create(): Data_create() failed!\n"); + lprintf(ldebug, "Cache_create(): Data_create() failed!\n"); } Cache_free(cf); - /* - * Cache_exist() returns 1, if cache files exist and valid. Whereas this - * function returns 0 on success. - */ - int res = -(!Cache_exist(fn)); - if (!CONFIG.sonic_mode) { + int res = Cache_exist(fn); + + if (CONFIG.mode == NORMAL) { curl_free(fn); } @@ -753,7 +742,7 @@ Cache *Cache_open(const char *fn) /*---------------- Cache_open() critical section -----------------*/ #ifdef CACHE_LOCK_DEBUG - lprintf(debug, "Cache_open(): thread %lu: locking cf_lock;\n", + lprintf(ldebug, "Cache_open(): thread %lu: locking cf_lock;\n", pthread_self()); #endif PTHREAD_MUTEX_LOCK(&cf_lock); @@ -761,7 +750,7 @@ Cache *Cache_open(const char *fn) if (link->cache_opened) { link->cache_opened++; #ifdef CACHE_LOCK_DEBUG - lprintf(debug, "Cache_open(): thread %lu: unlocking cf_lock;\n", + lprintf(ldebug, "Cache_open(): thread %lu: unlocking cf_lock;\n", pthread_self()); #endif PTHREAD_MUTEX_UNLOCK(&cf_lock); @@ -769,19 +758,19 @@ Cache *Cache_open(const char *fn) } #ifdef CACHE_LOCK_DEBUG - lprintf(debug, "Cache_open(): thread %lu: unlocking cf_lock;\n", + lprintf(ldebug, "Cache_open(): thread %lu: unlocking cf_lock;\n", pthread_self()); #endif PTHREAD_MUTEX_UNLOCK(&cf_lock); /*----------------------------------------------------------------*/ /* Check if both metadata and data file exist */ - if (!CONFIG.sonic_mode) { - if (!Cache_exist(fn)) { + if (CONFIG.mode == NORMAL) { + if (Cache_exist(fn)) { return NULL; } - } else { - if (!Cache_exist(link->sonic_id)) { + } else if (CONFIG.mode == SONIC) { + if (Cache_exist(link->sonic_id)) { return NULL; } } @@ -794,7 +783,7 @@ Cache *Cache_open(const char *fn) strncpy(cf->fs_path, fn, MAX_PATH_LEN); /* Set the path for the local cache file, if we are in sonic mode */ - if (CONFIG.sonic_mode) { + if (CONFIG.mode == SONIC) { fn = link->sonic_id; } @@ -805,7 +794,7 @@ Cache *Cache_open(const char *fn) if (Meta_open(cf)) { Cache_free(cf); - lprintf(debug, "Cache_open(): cannot open metadata file %s.\n", fn); + lprintf(ldebug, "Cache_open(): cannot open metadata file %s.\n", fn); return NULL; } @@ -816,7 +805,7 @@ Cache *Cache_open(const char *fn) */ if ((rtn == EINCONSIST) || (rtn == EMEM)) { Cache_free(cf); - lprintf(debug, "Cache_open(): metadata error: %s, %d.\n", fn, rtn); + lprintf(ldebug, "Cache_open(): metadata error: %s, %d.\n", fn, rtn); return NULL; } @@ -826,7 +815,7 @@ Cache *Cache_open(const char *fn) * allocation policy. */ if (cf->content_length > Data_size(fn)) { - lprintf(debug, "Cache_open(): metadata inconsistency %s, \ + lprintf(ldebug, "Cache_open(): metadata inconsistency %s, \ cf->content_length: %ld, Data_size(fn): %ld.\n", fn, cf->content_length, Data_size(fn)); Cache_free(cf); @@ -835,14 +824,14 @@ cf->content_length: %ld, Data_size(fn): %ld.\n", fn, cf->content_length, /* Check if the cache files are not outdated */ if (cf->time != cf->link->time) { - lprintf(debug, "Cache_open(): outdated cache file: %s.\n", fn); + lprintf(ldebug, "Cache_open(): outdated cache file: %s.\n", fn); Cache_free(cf); return NULL; } if (Data_open(cf)) { Cache_free(cf); - lprintf(debug, "Cache_open(): cannot open data file %s.\n", fn); + lprintf(ldebug, "Cache_open(): cannot open data file %s.\n", fn); return NULL; } @@ -858,7 +847,7 @@ void Cache_close(Cache *cf) /*--------------- Cache_close() critical section -----------------*/ #ifdef CACHE_LOCK_DEBUG - lprintf(debug, "Cache_close(): thread %lu: locking cf_lock;\n", + lprintf(ldebug, "Cache_close(): thread %lu: locking cf_lock;\n", pthread_self()); #endif PTHREAD_MUTEX_LOCK(&cf_lock); @@ -867,7 +856,7 @@ void Cache_close(Cache *cf) if (cf->link->cache_opened > 0) { #ifdef CACHE_LOCK_DEBUG - lprintf(debug, "Cache_close(): thread %lu: unlocking cf_lock;\n", + lprintf(ldebug, "Cache_close(): thread %lu: unlocking cf_lock;\n", pthread_self()); #endif PTHREAD_MUTEX_UNLOCK(&cf_lock); @@ -875,7 +864,7 @@ void Cache_close(Cache *cf) } #ifdef CACHE_LOCK_DEBUG - lprintf(debug, + lprintf(ldebug, "Cache_close(): thread %lu: locking and unlocking bgt_lock;\n", pthread_self()); #endif @@ -884,7 +873,7 @@ void Cache_close(Cache *cf) #ifdef CACHE_LOCK_DEBUG - lprintf(debug, "Cache_close(): thread %lu: unlocking cf_lock;\n", + lprintf(ldebug, "Cache_close(): thread %lu: unlocking cf_lock;\n", pthread_self()); #endif PTHREAD_MUTEX_UNLOCK(&cf_lock); @@ -892,16 +881,16 @@ void Cache_close(Cache *cf) /*----------------------------------------------------------------*/ if (Meta_write(cf)) { - lprintf(debug, "Cache_close(): Meta_write() error."); + lprintf(ldebug, "Cache_close(): Meta_write() error."); } if (fclose(cf->mfp)) { - lprintf(debug, "Cache_close(): cannot close metadata: %s.\n", + lprintf(ldebug, "Cache_close(): cannot close metadata: %s.\n", strerror(errno)); } if (fclose(cf->dfp)) { - lprintf(debug, "Cache_close(): cannot close data file %s.\n", + lprintf(ldebug, "Cache_close(): cannot close data file %s.\n", strerror(errno)); } @@ -941,16 +930,16 @@ static void *Cache_bgdl(void *arg) { Cache *cf = (Cache *) arg; #ifdef CACHE_LOCK_DEBUG - lprintf(debug, "Cache_bgdl(): thread %lu: locking w_lock;\n", + lprintf(ldebug, "Cache_bgdl(): thread %lu: 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 %lu: ", pthread_self()); + lprintf(ldebug, "Cache_bgdl(): thread %lu: ", 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, \ + lprintf(ldebug, "\nCache_bgdl(): received %lu bytes, \ which does't make sense\n", recv); exit_failure(); } @@ -961,17 +950,17 @@ 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, + lprintf(ldebug, "Cache_bgdl(): received %ld, possible network error.\n", recv); } free(recv_buf); #ifdef CACHE_LOCK_DEBUG - lprintf(debug, "Cache_bgdl(): thread %lu: unlocking bgt_lock;\n", + lprintf(ldebug, "Cache_bgdl(): thread %lu: unlocking bgt_lock;\n", pthread_self()); #endif PTHREAD_MUTEX_UNLOCK(&cf->bgt_lock); #ifdef CACHE_LOCK_DEBUG - lprintf(debug, "Cache_bgdl(): thread %lu: unlocking w_lock;\n", + lprintf(ldebug, "Cache_bgdl(): thread %lu: unlocking w_lock;\n", pthread_self()); #endif PTHREAD_MUTEX_UNLOCK(&cf->w_lock); @@ -994,7 +983,7 @@ long Cache_read(Cache *cf, char * const output_buf, const off_t len, } else { /* Wait for any other download thread to finish*/ #ifdef CACHE_LOCK_DEBUG - lprintf(debug, "Cache_read(): thread %ld: locking w_lock;\n", + lprintf(ldebug, "Cache_read(): thread %ld: locking w_lock;\n", pthread_self()); #endif PTHREAD_MUTEX_LOCK(&cf->w_lock); @@ -1003,7 +992,7 @@ long Cache_read(Cache *cf, char * const output_buf, const off_t len, * 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 %lu: unlocking w_lock;\n", + lprintf(ldebug, "Cache_read(): thread %lu: unlocking w_lock;\n", pthread_self()); #endif PTHREAD_MUTEX_UNLOCK(&cf->w_lock); @@ -1014,11 +1003,11 @@ 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 %lu: ", pthread_self()); + lprintf(ldebug, "Cache_read(): thread %lu: ", 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, \ + lprintf(ldebug, "\nCache_read(): received %ld bytes, \ which does't make sense\n", recv); exit_failure(); } @@ -1034,14 +1023,14 @@ which does't make sense\n", recv); Data_write(cf, recv_buf, recv, dl_offset); Seg_set(cf, dl_offset, 1); } else { - lprintf(debug, + lprintf(ldebug, "Cache_read(): received %ld, possible network error.\n", recv); } free(recv_buf); send = Data_read(cf, (uint8_t *) output_buf, len, offset_start); #ifdef CACHE_LOCK_DEBUG - lprintf(debug, "Cache_read(): thread %lu: unlocking w_lock;\n", + lprintf(ldebug, "Cache_read(): thread %lu: unlocking w_lock;\n", pthread_self()); #endif PTHREAD_MUTEX_UNLOCK(&cf->w_lock); @@ -1056,12 +1045,12 @@ which does't make sense\n", recv); /* Stop the spawning of multiple background pthreads */ if(!pthread_mutex_trylock(&cf->bgt_lock)) { #ifdef CACHE_LOCK_DEBUG - lprintf(debug, "Cache_read(): thread %lu: trylocked bgt_lock;\n", + lprintf(ldebug, "Cache_read(): thread %lu: 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(ldebug, "Cache_read(): Error creating background download thread\n" ); } diff --git a/src/config.c b/src/config.c index a39fba5..045b60f 100644 --- a/src/config.c +++ b/src/config.c @@ -28,6 +28,8 @@ ConfigStruct CONFIG; */ void Config_init(void) { + /** \brief Operation Mode */ + CONFIG.mode = NORMAL; /*---------------- Network related --------------*/ CONFIG.http_username = NULL; @@ -72,6 +74,4 @@ void Config_init(void) CONFIG.log_verbosity = log_verbosity_init(); - /*-----------Single file mode related ----------*/ - CONFIG.single_file_mode = 0; } \ No newline at end of file diff --git a/src/config.h b/src/config.h index 3d8bf4c..bfee9d3 100644 --- a/src/config.h +++ b/src/config.h @@ -23,12 +23,23 @@ */ #define DEFAULT_NETWORK_MAX_CONNS 10 +/** + * \brief Operation modes + */ +typedef enum { + NORMAL = 1, + SONIC = 2, + SINGLE_FILE = 3, +} OperationMode; + /** * \brief configuration data structure * \note The opening curly bracket should be at line 39, so the code belong * lines up with the initialisation code in util.c */ typedef struct { + /** \brief Operation Mode */ + OperationMode mode; /*---------------- Network related --------------*/ /** \brief HTTP username */ char *http_username; @@ -60,8 +71,6 @@ typedef struct { /** \brief The maximum segment count for a single cache file */ int max_segbc; /*-------------- Sonic related -------------*/ - /** \brief Whether we are using the Sonic mode */ - int sonic_mode; /** \brief The Sonic server username */ char *sonic_username; /** \brief The Sonic server password */ @@ -81,9 +90,6 @@ typedef struct { * - 2 : Filename and line number */ int log_verbosity; - /*-----------Single file mode related ----------*/ - /** \brief Single file mode */ - int single_file_mode; } ConfigStruct; /** diff --git a/src/link.c b/src/link.c index 0f825cb..bb02fdb 100644 --- a/src/link.c +++ b/src/link.c @@ -39,7 +39,7 @@ LinkTable *LinkSystem_init(const char *raw_url) } if (pthread_mutex_init(&link_lock, NULL) != 0) { - lprintf(debug, + lprintf(ldebug, "link_system_init(): link_lock initialisation failed!\n"); exit_failure(); } @@ -58,9 +58,9 @@ LinkTable *LinkSystem_init(const char *raw_url) } /* ----------- Create the root link table --------------*/ - if (!CONFIG.sonic_mode) { + if (CONFIG.mode == NORMAL) { ROOT_LINK_TBL = LinkTable_new(url); - } else { + } else if (CONFIG.mode == SONIC) { sonic_config_init(url, CONFIG.sonic_username, CONFIG.sonic_password); if (!CONFIG.sonic_id3) { ROOT_LINK_TBL = sonic_LinkTable_new_index("0"); @@ -77,7 +77,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"); + lprintf(ldebug, "LinkTable_add(): realloc failure!\n"); exit_failure(); } linktbl->links[linktbl->num - 1] = link; @@ -186,7 +186,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(ldebug, "Link_to_curl(): curl_easy_init() failed!\n"); } /* set up some basic curl stuff */ curl_easy_setopt(curl, CURLOPT_USERAGENT, CONFIG.user_agent); @@ -232,7 +232,7 @@ static CURL *Link_to_curl(Link *link) 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", + lprintf(ldebug, "Link_req_file_stat(), invalid request, LinkType: %c", this_link->type); exit_failure(); } @@ -271,12 +271,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(ldebug, "Link_set_file_stat(): HTTP %ld", http_resp); if (HTTP_temp_failure(http_resp)) { - lprintf(debug, ", retrying later.\n"); + lprintf(ldebug, ", retrying later.\n"); } else { this_link->type = LINK_INVALID; - lprintf(debug, ".\n"); + lprintf(ldebug, ".\n"); } } } @@ -290,7 +290,7 @@ static void LinkTable_uninitialised_fill(LinkTable *linktbl) { int u; char s[STATUS_LEN]; - lprintf(debug, "LinkTable_uninitialised_fill(): ... "); + lprintf(ldebug, "LinkTable_uninitialised_fill(): ... "); do { u = 0; for (int i = 0; i < linktbl->num; i++) { @@ -350,7 +350,7 @@ static void LinkTable_invalid_reset(LinkTable *linktbl) j++; } } - lprintf(debug, "LinkTable_invalid_reset(): %d invalid links\n", j); + lprintf(ldebug, "LinkTable_invalid_reset(): %d invalid links\n", j); } void LinkTable_free(LinkTable *linktbl) @@ -365,13 +365,13 @@ void LinkTable_free(LinkTable *linktbl) void LinkTable_print(LinkTable *linktbl) { int j = 0; - lprintf(debug, "--------------------------------------------\n"); - lprintf(debug, " LinkTable %p for %s\n", linktbl, + lprintf(ldebug, "--------------------------------------------\n"); + lprintf(ldebug, " LinkTable %p for %s\n", linktbl, linktbl->links[0]->f_url); - lprintf(debug, "--------------------------------------------\n"); + lprintf(ldebug, "--------------------------------------------\n"); for (int i = 0; i < linktbl->num; i++) { Link *this_link = linktbl->links[i]; - lprintf(debug, "%d %c %lu %s %s\n", + lprintf(ldebug, "%d %c %lu %s %s\n", i, this_link->type, this_link->content_length, @@ -384,9 +384,9 @@ void LinkTable_print(LinkTable *linktbl) j++; } } - lprintf(debug, "--------------------------------------------\n"); - lprintf(debug, "LinkTable_print(): Invalid link count: %d\n", j); - lprintf(debug, "--------------------------------------------\n"); + lprintf(ldebug, "--------------------------------------------\n"); + lprintf(ldebug, "LinkTable_print(): Invalid link count: %d\n", j); + lprintf(ldebug, "--------------------------------------------\n"); } DataStruct Link_to_DataStruct(Link *head_link) @@ -406,12 +406,12 @@ 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, + lprintf(ldebug, "LinkTable_new(): URL: %s, HTTP %ld, retrying later.\n", url, http_resp); sleep(CONFIG.http_wait_sec); } else if (http_resp != HTTP_OK) { - lprintf(debug, + lprintf(ldebug, "LinkTable_new(): cannot retrieve URL: %s, HTTP %ld\n", url, http_resp); buf.size = 0; @@ -440,7 +440,7 @@ LinkTable *LinkTable_alloc(const char *url) LinkTable *LinkTable_new(const char *url) { #ifdef LINK_LOCK_DEBUG - lprintf(debug, + lprintf(ldebug, "LinkTable_new(): thread %lu: locking link_lock;\n", pthread_self()); #endif @@ -471,7 +471,7 @@ LinkTable *LinkTable_new(const char *url) disk_linktbl = LinkTable_disk_open(unescaped_path); if (disk_linktbl) { /* Check if we need to update the link table */ - lprintf(debug, + lprintf(ldebug, "LinkTable_new(): disk_linktbl->num: %d, linktbl->num: %d\n", disk_linktbl->num, linktbl->num); if (disk_linktbl->num == linktbl->num) { @@ -505,7 +505,7 @@ LinkTable *LinkTable_new(const char *url) LinkTable_print(linktbl); #ifdef LINK_LOCK_DEBUG - lprintf(debug, + lprintf(ldebug, "LinkTable_new(): thread %lu: unlocking link_lock;\n", pthread_self()); #endif @@ -523,7 +523,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(ldebug, "LinkTable_disk_delete(): unlink(%s): %s\n", path, strerror(errno)); } free(path); @@ -543,7 +543,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(ldebug, "LinkTable_disk_save(): fopen(%s): %s\n", path, strerror(errno)); free(path); return -1; @@ -562,12 +562,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(ldebug, "LinkTable_disk_save(): encountered ferror!\n"); res = -1; } if (fclose(fp)) { - lprintf(debug, + lprintf(ldebug, "LinkTable_disk_save(): cannot close the file pointer, %s\n", strerror(errno)); res = -1; @@ -606,21 +606,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(ldebug, "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(ldebug, "LinkTable_disk_open(): encountered ferror!\n"); LinkTable_free(linktbl); LinkTable_disk_delete(dirn); return NULL; } } if (fclose(fp)) { - lprintf(debug, + lprintf(ldebug, "LinkTable_disk_open(): cannot close the file pointer, %s\n", strerror(errno)); } @@ -633,9 +633,9 @@ LinkTable *path_to_Link_LinkTable_new(const char *path) Link *link = path_to_Link(path); LinkTable *next_table = link->next_table; if (!next_table) { - if (!CONFIG.sonic_mode) { + if (CONFIG.mode == NORMAL) { next_table = LinkTable_new(link->f_url); - } else { + } else if (CONFIG.mode == SONIC) { if (!CONFIG.sonic_id3) { next_table = sonic_LinkTable_new_index(link->sonic_id); } else { @@ -688,10 +688,10 @@ static Link *path_to_Link_recursive(char *path, LinkTable *linktbl) /* The next sub-directory exists */ LinkTable *next_table = linktbl->links[i]->next_table; if (!next_table) { - if (!CONFIG.sonic_mode) { + if (CONFIG.mode == NORMAL) { next_table = LinkTable_new( linktbl->links[i]->f_url); - } else { + } else if (CONFIG.mode == SONIC) { if (!CONFIG.sonic_id3) { next_table = sonic_LinkTable_new_index( linktbl->links[i]->sonic_id); @@ -714,7 +714,7 @@ Link *path_to_Link(const char *path) { char *new_path = strndup(path, MAX_PATH_LEN); if (!new_path) { - lprintf(debug, "path_to_Link(): cannot allocate memory\n"); + lprintf(ldebug, "path_to_Link(): cannot allocate memory\n"); exit_failure(); } Link *link = path_to_Link_recursive(new_path, ROOT_LINK_TBL); @@ -726,7 +726,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"); + lprintf(ldebug, "\npath_download(): NULL path supplied\n"); exit_failure(); } Link *link; @@ -739,7 +739,7 @@ long path_download(const char *path, char *output_buf, size_t size, size_t end = start + size; char range_str[64]; snprintf(range_str, sizeof(range_str), "%lu-%lu", start, end); - lprintf(debug, "path_download(%s, %s);\n", path, range_str); + lprintf(ldebug, "path_download(%s, %s);\n", path, range_str); DataStruct buf; buf.size = 0; @@ -750,7 +750,7 @@ long path_download(const char *path, char *output_buf, size_t size, curl_easy_setopt(curl, CURLOPT_RANGE, range_str); #ifdef LINK_LOCK_DEBUG - lprintf(debug, + lprintf(ldebug, "path_download(): thread %lu: locking and unlocking link_lock;\n", pthread_self()); #endif @@ -768,7 +768,7 @@ 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(ldebug, "Error: This web server does not support HTTP \ range requests\n"); exit(EXIT_FAILURE); } @@ -783,7 +783,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(ldebug, "path_download(): Could not download %s, HTTP %ld\n", link->f_url, http_resp); return -ENOENT; } diff --git a/src/log.c b/src/log.c index e20a7c5..03035f2 100644 --- a/src/log.c +++ b/src/log.c @@ -28,13 +28,13 @@ void log_printf(int type, const char *file, int line, const char *format, ...) { if (type & CONFIG.log_level) { switch (type) { - case notice: + case linfo: goto print_actual_message; break; - case error: + case lerror: fprintf(stderr, "Error: "); break; - case debug: + case ldebug: fprintf(stderr, "Debug: "); break; default: diff --git a/src/log.h b/src/log.h index 7ae0e42..20bd7ec 100644 --- a/src/log.h +++ b/src/log.h @@ -1,24 +1,24 @@ #ifndef LOG_H #define LOG_H /** - * \brief Log type: Notice + * \brief Log type: Informational */ -#define notice 1 << 0 +#define linfo 1 << 0 /** * \brief Log type: Error */ -#define error 1 << 1 +#define lerror 1 << 1 /** * \brief Log type: Debug */ -#define debug 1 << 2 +#define ldebug 1 << 2 /** * \brief The default log level */ -#define DEFAULT_LOG_LEVEL debug +#define DEFAULT_LOG_LEVEL ldebug /** * \brief Display filename in log @@ -46,13 +46,13 @@ int log_level_init(); int log_verbosity_init(); /** - * \brief log printf + * \brief Log printf * \details This is for printing nice log messages */ void log_printf(int type, const char *file, int line, const char *format, ...); /** - * \brief log printf + * \brief Log type printf * \details This macro automatically prints out the filename and line number */ #define lprintf(type, ...) \ diff --git a/src/main.c b/src/main.c index 776c3a7..5855778 100644 --- a/src/main.c +++ b/src/main.c @@ -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]); + lprintf(ldebug, "For more information, run \"%s --help.\"\n", argv[0]); exit(EXIT_FAILURE); } @@ -71,20 +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"); + lprintf(ldebug, "Error: Please supply a valid URL.\n"); print_help(argv[0], 0); exit(EXIT_FAILURE); } else { if (CONFIG.sonic_username && CONFIG.sonic_password) { - CONFIG.sonic_mode = 1; + CONFIG.mode = SONIC; } else if (CONFIG.sonic_username || CONFIG.sonic_password) { - lprintf(debug, + lprintf(ldebug, "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"); + lprintf(ldebug, "Error: Network initialisation failed.\n"); exit(EXIT_FAILURE); } } @@ -259,15 +259,15 @@ parse_arg_list(int argc, char **argv, char ***fuse_argv, int *fuse_argc) /* This is for --config, we don't need to do anything */ break; case 22: - CONFIG.single_file_mode = 1; + CONFIG.mode = SINGLE_FILE; break; default: - lprintf(debug, "see httpdirfs -h for usage\n"); + lprintf(ldebug, "see httpdirfs -h for usage\n"); return 1; } break; default: - lprintf(debug, "see httpdirfs -h for usage\n"); + lprintf(ldebug, "see httpdirfs -h for usage\n"); return 1; } }; diff --git a/src/network.c b/src/network.c index 03bdee8..7d4dbb8 100644 --- a/src/network.c +++ b/src/network.c @@ -119,7 +119,7 @@ static void curl_process_msgs(CURLMsg *curl_msg, int n_running_curl, curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_resp); if (HTTP_temp_failure(http_resp)) { if (!slept) { - lprintf(debug, + lprintf(ldebug, "curl_process_msgs(): HTTP %ld, sleeping for %d sec\n", http_resp, CONFIG.http_wait_sec); sleep(CONFIG.http_wait_sec); @@ -135,7 +135,7 @@ static void curl_process_msgs(CURLMsg *curl_msg, int n_running_curl, Link_set_file_stat(transfer->link, curl); } } else { - lprintf(debug, "curl_process_msgs(): %d - %s <%s>\n", + lprintf(ldebug, "curl_process_msgs(): %d - %s <%s>\n", curl_msg->data.result, curl_easy_strerror(curl_msg->data.result), url); @@ -147,7 +147,7 @@ static void curl_process_msgs(CURLMsg *curl_msg, int n_running_curl, free(transfer); } } else { - lprintf(debug, "curl_process_msgs(): curl_msg->msg: %d\n", + lprintf(ldebug, "curl_process_msgs(): curl_msg->msg: %d\n", curl_msg->msg); } } @@ -159,7 +159,7 @@ static void curl_process_msgs(CURLMsg *curl_msg, int n_running_curl, int curl_multi_perform_once(void) { #ifdef NETWORK_LOCK_DEBUG - lprintf(debug, + lprintf(ldebug, "curl_multi_perform_once(): thread %lu: locking transfer_lock;\n", pthread_self()); #endif @@ -168,7 +168,7 @@ int curl_multi_perform_once(void) int n_running_curl; CURLMcode mc = curl_multi_perform(curl_multi, &n_running_curl); if(mc > 0) { - lprintf(debug, "curl_multi_perform(): %s\n", curl_multi_strerror(mc)); + lprintf(ldebug, "curl_multi_perform(): %s\n", curl_multi_strerror(mc)); } fd_set fdread; @@ -202,14 +202,14 @@ int curl_multi_perform_once(void) mc = curl_multi_fdset(curl_multi, &fdread, &fdwrite, &fdexcep, &maxfd); if (mc > 0) { - lprintf(debug, "curl_multi_fdset(): %s.\n", curl_multi_strerror(mc)); + lprintf(ldebug, "curl_multi_fdset(): %s.\n", curl_multi_strerror(mc)); } if (maxfd == -1) { usleep(100*1000); } else { if (select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout) < 0) { - lprintf(debug, "curl_multi_perform_once(): select(): %s.\n", + lprintf(ldebug, "curl_multi_perform_once(): select(): %s.\n", strerror(errno)); } } @@ -221,7 +221,7 @@ int curl_multi_perform_once(void) curl_process_msgs(curl_msg, n_running_curl, n_mesgs); } #ifdef NETWORK_LOCK_DEBUG - lprintf(debug, + lprintf(ldebug, "curl_multi_perform_once(): thread %lu: unlocking transfer_lock;\n", pthread_self()); #endif @@ -233,14 +233,14 @@ void NetworkSystem_init(void) { /* ------- Global related ----------*/ if (curl_global_init(CURL_GLOBAL_ALL)) { - lprintf(debug, "network_init(): curl_global_init() failed!\n"); + lprintf(ldebug, "network_init(): curl_global_init() failed!\n"); exit_failure(); } /* -------- Share related ----------*/ CURL_SHARE = curl_share_init(); if (!(CURL_SHARE)) { - lprintf(debug, "network_init(): curl_share_init() failed!\n"); + lprintf(ldebug, "network_init(): curl_share_init() failed!\n"); exit_failure(); } @@ -249,7 +249,7 @@ void NetworkSystem_init(void) curl_share_setopt(CURL_SHARE, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION); if (pthread_mutex_init(&curl_lock, NULL) != 0) { - lprintf(debug, "network_init(): curl_lock initialisation failed!\n"); + lprintf(ldebug, "network_init(): curl_lock initialisation failed!\n"); exit_failure(); } curl_share_setopt(CURL_SHARE, CURLSHOPT_LOCKFUNC, curl_callback_lock); @@ -258,7 +258,7 @@ void NetworkSystem_init(void) /* ------------- Multi related -----------*/ curl_multi = curl_multi_init(); if (!curl_multi) { - lprintf(debug, "network_init(): curl_multi_init() failed!\n"); + lprintf(ldebug, "network_init(): curl_multi_init() failed!\n"); exit_failure(); } curl_multi_setopt(curl_multi, CURLMOPT_MAX_TOTAL_CONNECTIONS, @@ -268,7 +268,7 @@ void NetworkSystem_init(void) /* ------------ Initialise locks ---------*/ if (pthread_mutex_init(&transfer_lock, NULL)) { - lprintf(debug, + lprintf(ldebug, "network_init(): transfer_lock initialisation failed!\n"); exit_failure(); } @@ -291,21 +291,21 @@ void transfer_blocking(CURL *curl) transfer.transferring = 1; curl_easy_setopt(curl, CURLOPT_PRIVATE, &transfer); #ifdef NETWORK_LOCK_DEBUG - lprintf(debug, + lprintf(ldebug, "transfer_blocking(): thread %lu: locking transfer_lock;\n", pthread_self()); #endif PTHREAD_MUTEX_LOCK(&transfer_lock); CURLMcode res = curl_multi_add_handle(curl_multi, curl); #ifdef NETWORK_LOCK_DEBUG - lprintf(debug, + lprintf(ldebug, "transfer_blocking(): thread %lu: unlocking transfer_lock;\n", pthread_self()); #endif PTHREAD_MUTEX_UNLOCK(&transfer_lock); if(res > 0) { - lprintf(debug, "transfer_blocking(): %d, %s\n", + lprintf(ldebug, "transfer_blocking(): %d, %s\n", res, curl_multi_strerror(res)); exit_failure(); } @@ -318,21 +318,21 @@ void transfer_blocking(CURL *curl) void transfer_nonblocking(CURL *curl) { #ifdef NETWORK_LOCK_DEBUG - lprintf(debug, + lprintf(ldebug, "transfer_nonblocking(): thread %lu: locking transfer_lock;\n", pthread_self()); #endif PTHREAD_MUTEX_LOCK(&transfer_lock); CURLMcode res = curl_multi_add_handle(curl_multi, curl); #ifdef NETWORK_LOCK_DEBUG - lprintf(debug, + lprintf(ldebug, "transfer_nonblocking(): thread %lu: unlocking transfer_lock;\n", pthread_self()); #endif PTHREAD_MUTEX_UNLOCK(&transfer_lock); if(res > 0) { - lprintf(debug, "transfer_nonblocking(): %s\n", + lprintf(ldebug, "transfer_nonblocking(): %s\n", curl_multi_strerror(res)); } } @@ -346,7 +346,7 @@ size_t write_memory_callback(void *contents, size_t size, size_t nmemb, mem->data = realloc(mem->data, mem->size + realsize + 1); if(!mem->data) { /* out of memory! */ - lprintf(debug, "write_memory_callback(): realloc failure!\n"); + lprintf(ldebug, "write_memory_callback(): realloc failure!\n"); exit_failure(); return 0; } diff --git a/src/sonic.c b/src/sonic.c index fafb849..a26e469 100644 --- a/src/sonic.c +++ b/src/sonic.c @@ -167,9 +167,9 @@ 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(ldebug, "XML_parser_general() error:\n"); for (int i = 0; attr[i]; i += 2) { - lprintf(debug, "%s: %s\n", attr[i], attr[i+1]); + lprintf(ldebug, "%s: %s\n", attr[i], attr[i+1]); } exit(EXIT_FAILURE); } @@ -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(ldebug, "sonic_XML_to_LinkTable(): Parse error at line %lu: %s\n", XML_GetCurrentLineNumber(parser), XML_ErrorString(XML_GetErrorCode(parser))); @@ -356,9 +356,9 @@ 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(ldebug, "XML_parser_id3_root() error:\n"); for (int i = 0; attr[i]; i += 2) { - lprintf(debug, "%s: %s\n", attr[i], attr[i+1]); + lprintf(ldebug, "%s: %s\n", attr[i], attr[i+1]); } exit(EXIT_FAILURE); } @@ -454,7 +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); + lprintf(ldebug, "sonic_LinkTable_new_id3(): case %d.\n", depth); exit_failure(); break; } diff --git a/src/util.c b/src/util.c index 75fc087..da0edfc 100644 --- a/src/util.c +++ b/src/util.c @@ -56,7 +56,7 @@ void PTHREAD_MUTEX_UNLOCK(pthread_mutex_t *x) int i; i = pthread_mutex_unlock(x); if (i) { - lprintf(debug, "thread %lu: pthread_mutex_unlock() failed, %d, %s\n", + lprintf(ldebug, "thread %lu: pthread_mutex_unlock() failed, %d, %s\n", pthread_self(), i, strerror(i)); exit_failure(); } @@ -67,7 +67,7 @@ void PTHREAD_MUTEX_LOCK(pthread_mutex_t *x) int i; i = pthread_mutex_lock(x); if (i) { - lprintf(debug, "thread %lu: pthread_mutex_lock() failed, %d, %s\n", + lprintf(ldebug, "thread %lu: pthread_mutex_lock() failed, %d, %s\n", pthread_self(), i, strerror(i)); exit_failure(); } @@ -79,8 +79,8 @@ void exit_failure(void) void *buffer[BT_BUF_SIZE]; nptrs = backtrace(buffer, BT_BUF_SIZE); - lprintf(debug, "\nOops! HTTPDirFS crashed! :(\n"); - lprintf(debug, "backtrace() returned the following %d addresses:\n", + lprintf(ldebug, "\nOops! HTTPDirFS crashed! :(\n"); + lprintf(ldebug, "backtrace() returned the following %d addresses:\n", nptrs); backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO); @@ -132,7 +132,7 @@ void *CALLOC(size_t nmemb, size_t size) { void *ptr = calloc(nmemb, size); if (!ptr) { - lprintf(debug, "calloc() failed, %s!\n", strerror(errno)); + lprintf(ldebug, "calloc() failed, %s!\n", strerror(errno)); exit_failure(); } return ptr;