mirror of
https://github.com/fangfufu/httpdirfs.git
synced 2024-09-27 20:51:55 +02:00
updated logging facility
This commit is contained in:
parent
f37cdefa47
commit
14c4b3b486
192
src/cache.c
192
src/cache.c
@ -41,7 +41,7 @@ static pthread_mutex_t cf_lock;
|
|||||||
static char *DATA_DIR;
|
static char *DATA_DIR;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Calculate cache system directory
|
* \brief Calculate cache system directory path
|
||||||
*/
|
*/
|
||||||
static char *CacheSystem_calc_dir(const char *url)
|
static char *CacheSystem_calc_dir(const char *url)
|
||||||
{
|
{
|
||||||
@ -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)
|
if (mkdir(xdg_cache_home, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
|
||||||
&& (errno != EEXIST)) {
|
&& (errno != EEXIST)) {
|
||||||
lprintf(ldebug, "CacheSystem_calc_dir(): mkdir(): %s\n",
|
lprintf(debug, "CacheSystem_calc_dir(): mkdir(): %s\n",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
}
|
}
|
||||||
char *cache_dir_root = path_append(xdg_cache_home, "/httpdirfs/");
|
char *cache_dir_root = path_append(xdg_cache_home, "/httpdirfs/");
|
||||||
if (mkdir(cache_dir_root, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
|
if (mkdir(cache_dir_root, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
|
||||||
&& (errno != EEXIST)) {
|
&& (errno != EEXIST)) {
|
||||||
lprintf(ldebug, "CacheSystem_calc_dir(): mkdir(): %s\n",
|
lprintf(debug, "CacheSystem_calc_dir(): mkdir(): %s\n",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,15 +72,15 @@ static char *CacheSystem_calc_dir(const char *url)
|
|||||||
# For information about cache directory tags, see:\n\
|
# For information about cache directory tags, see:\n\
|
||||||
# http://www.brynosaurus.com/cachedir/\n");
|
# http://www.brynosaurus.com/cachedir/\n");
|
||||||
} else {
|
} else {
|
||||||
lprintf(ldebug, "CacheSystem_calc_dir(): fopen(%s): %s", fn,
|
lprintf(debug, "CacheSystem_calc_dir(): fopen(%s): %s", fn,
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
}
|
}
|
||||||
if (ferror(fp)) {
|
if (ferror(fp)) {
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"CacheSystem_calc_dir(): fwrite(): encountered error!\n");
|
"CacheSystem_calc_dir(): fwrite(): encountered error!\n");
|
||||||
}
|
}
|
||||||
if (fclose(fp)) {
|
if (fclose(fp)) {
|
||||||
lprintf(ldebug, "CacheSystem_calc_dir(): fclose(%s): %s\n", fn,
|
lprintf(debug, "CacheSystem_calc_dir(): fclose(%s): %s\n", fn,
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
}
|
}
|
||||||
CURL* c = curl_easy_init();
|
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);
|
char *full_path = path_append(cache_dir_root, escaped_url);
|
||||||
if (mkdir(full_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
|
if (mkdir(full_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
|
||||||
&& (errno != EEXIST)) {
|
&& (errno != EEXIST)) {
|
||||||
lprintf(ldebug, "CacheSystem_calc_dir(): mkdir(): %s\n",
|
lprintf(debug, "CacheSystem_calc_dir(): mkdir(): %s\n",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
}
|
}
|
||||||
free(fn);
|
free(fn);
|
||||||
@ -101,7 +101,7 @@ static char *CacheSystem_calc_dir(const char *url)
|
|||||||
void CacheSystem_init(const char *path, int url_supplied)
|
void CacheSystem_init(const char *path, int url_supplied)
|
||||||
{
|
{
|
||||||
if (pthread_mutex_init(&cf_lock, NULL) != 0) {
|
if (pthread_mutex_init(&cf_lock, NULL) != 0) {
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"CacheSystem_init(): cf_lock initialisation failed!\n");
|
"CacheSystem_init(): cf_lock initialisation failed!\n");
|
||||||
exit_failure();
|
exit_failure();
|
||||||
}
|
}
|
||||||
@ -110,12 +110,12 @@ void CacheSystem_init(const char *path, int url_supplied)
|
|||||||
path = CacheSystem_calc_dir(path);
|
path = CacheSystem_calc_dir(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
lprintf(ldebug, "CacheSystem_init(): directory: %s\n", path);
|
lprintf(debug, "CacheSystem_init(): directory: %s\n", path);
|
||||||
DIR* dir;
|
DIR* dir;
|
||||||
|
|
||||||
dir = opendir(path);
|
dir = opendir(path);
|
||||||
if (!dir) {
|
if (!dir) {
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"CacheSystem_init(): opendir(): %s\n", strerror(errno));
|
"CacheSystem_init(): opendir(): %s\n", strerror(errno));
|
||||||
exit_failure();
|
exit_failure();
|
||||||
}
|
}
|
||||||
@ -125,14 +125,14 @@ void CacheSystem_init(const char *path, int url_supplied)
|
|||||||
/* Check if directories exist, if not, create them */
|
/* Check if directories exist, if not, create them */
|
||||||
if (mkdir(META_DIR, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
|
if (mkdir(META_DIR, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
|
||||||
&& (errno != EEXIST)) {
|
&& (errno != EEXIST)) {
|
||||||
lprintf(ldebug, "CacheSystem_init(): mkdir(): %s\n",
|
lprintf(debug, "CacheSystem_init(): mkdir(): %s\n",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
exit_failure();
|
exit_failure();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mkdir(DATA_DIR, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
|
if (mkdir(DATA_DIR, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
|
||||||
&& (errno != EEXIST)) {
|
&& (errno != EEXIST)) {
|
||||||
lprintf(ldebug, "CacheSystem_init(): mkdir(): %s\n",
|
lprintf(debug, "CacheSystem_init(): mkdir(): %s\n",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
exit_failure();
|
exit_failure();
|
||||||
}
|
}
|
||||||
@ -143,7 +143,7 @@ void CacheSystem_init(const char *path, int url_supplied)
|
|||||||
sonic_path = path_append(META_DIR, "rest/");
|
sonic_path = path_append(META_DIR, "rest/");
|
||||||
if (mkdir(sonic_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
|
if (mkdir(sonic_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
|
||||||
&& (errno != EEXIST)) {
|
&& (errno != EEXIST)) {
|
||||||
lprintf(ldebug, "CacheSystem_init(): mkdir(): %s\n",
|
lprintf(debug, "CacheSystem_init(): mkdir(): %s\n",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
exit_failure();
|
exit_failure();
|
||||||
}
|
}
|
||||||
@ -153,7 +153,7 @@ void CacheSystem_init(const char *path, int url_supplied)
|
|||||||
sonic_path = path_append(DATA_DIR, "rest/");
|
sonic_path = path_append(DATA_DIR, "rest/");
|
||||||
if (mkdir(sonic_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
|
if (mkdir(sonic_path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)
|
||||||
&& (errno != EEXIST)) {
|
&& (errno != EEXIST)) {
|
||||||
lprintf(ldebug, "CacheSystem_init(): mkdir(): %s\n",
|
lprintf(debug, "CacheSystem_init(): mkdir(): %s\n",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
exit_failure();
|
exit_failure();
|
||||||
}
|
}
|
||||||
@ -179,7 +179,7 @@ static int Meta_read(Cache *cf)
|
|||||||
|
|
||||||
if (!fp) {
|
if (!fp) {
|
||||||
/* The metadata file does not exist */
|
/* The metadata file does not exist */
|
||||||
lprintf(ldebug, "Meta_read(): fopen(): %s\n", strerror(errno));
|
lprintf(debug, "Meta_read(): fopen(): %s\n", strerror(errno));
|
||||||
return EFREAD;
|
return EFREAD;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -190,25 +190,25 @@ static int Meta_read(Cache *cf)
|
|||||||
|
|
||||||
/* Error checking for fread */
|
/* Error checking for fread */
|
||||||
if (ferror(fp)) {
|
if (ferror(fp)) {
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"Meta_read(): error reading core metadata!\n");
|
"Meta_read(): error reading core metadata!\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* These things really should not be zero!!! */
|
/* These things really should not be zero!!! */
|
||||||
if (!cf->content_length || !cf->blksz || !cf->segbc) {
|
if (!cf->content_length || !cf->blksz || !cf->segbc) {
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"Meta_read(): corrupt metadata: %s, content_length: %ld, \
|
"Meta_read(): corrupt metadata: %s, content_length: %ld, \
|
||||||
blksz: %d, segbc: %ld\n", cf->path, cf->content_length, cf->blksz, cf->segbc);
|
blksz: %d, segbc: %ld\n", cf->path, cf->content_length, cf->blksz, cf->segbc);
|
||||||
return EZERO;
|
return EZERO;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cf->blksz != CONFIG.data_blksz) {
|
if (cf->blksz != CONFIG.data_blksz) {
|
||||||
lprintf(ldebug, "Meta_read(): Warning: cf->blksz != CONFIG.data_blksz\n");
|
lprintf(debug, "Meta_read(): Warning: cf->blksz != CONFIG.data_blksz\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Allocate some memory for the segment */
|
/* Allocate some memory for the segment */
|
||||||
if (cf->segbc > CONFIG.max_segbc) {
|
if (cf->segbc > CONFIG.max_segbc) {
|
||||||
lprintf(ldebug, "Meta_read(): Error: segbc: %ld\n", cf->segbc);
|
lprintf(debug, "Meta_read(): Error: segbc: %ld\n", cf->segbc);
|
||||||
return EMEM;
|
return EMEM;
|
||||||
}
|
}
|
||||||
cf->seg = CALLOC(cf->segbc, sizeof(Seg));
|
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 */
|
/* We shouldn't have gone past the end of the file */
|
||||||
if (feof(fp)) {
|
if (feof(fp)) {
|
||||||
/* reached EOF */
|
/* reached EOF */
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"Meta_read(): attempted to read past the end of the file!\n");
|
"Meta_read(): attempted to read past the end of the file!\n");
|
||||||
return EINCONSIST;
|
return EINCONSIST;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Error checking for fread */
|
/* Error checking for fread */
|
||||||
if (ferror(fp)) {
|
if (ferror(fp)) {
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"Meta_read(): error reading bitmap!\n");
|
"Meta_read(): error reading bitmap!\n");
|
||||||
return EFREAD;
|
return EFREAD;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Check for inconsistent metadata file */
|
/* Check for inconsistent metadata file */
|
||||||
if (nmemb != cf-> segbc) {
|
if (nmemb != cf-> segbc) {
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"Meta_read(): corrupted metadata!\n");
|
"Meta_read(): corrupted metadata!\n");
|
||||||
return EINCONSIST;
|
return EINCONSIST;
|
||||||
}
|
}
|
||||||
@ -254,13 +254,13 @@ static int Meta_write(Cache *cf)
|
|||||||
|
|
||||||
if (!fp) {
|
if (!fp) {
|
||||||
/* Cannot create the metadata file */
|
/* Cannot create the metadata file */
|
||||||
lprintf(ldebug, "Meta_write(): fopen(): %s\n", strerror(errno));
|
lprintf(debug, "Meta_write(): fopen(): %s\n", strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* These things really should not be zero!!! */
|
/* These things really should not be zero!!! */
|
||||||
if (!cf->content_length || !cf->blksz || !cf->segbc) {
|
if (!cf->content_length || !cf->blksz || !cf->segbc) {
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"Meta_write(): Warning: content_length: %ld, blksz: %d, segbc: \
|
"Meta_write(): Warning: content_length: %ld, blksz: %d, segbc: \
|
||||||
%ld\n", cf->content_length, cf->blksz, cf->segbc);
|
%ld\n", cf->content_length, cf->blksz, cf->segbc);
|
||||||
}
|
}
|
||||||
@ -275,7 +275,7 @@ static int Meta_write(Cache *cf)
|
|||||||
|
|
||||||
/* Error checking for fwrite */
|
/* Error checking for fwrite */
|
||||||
if (ferror(fp)) {
|
if (ferror(fp)) {
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"Meta_write(): fwrite(): encountered error!\n");
|
"Meta_write(): fwrite(): encountered error!\n");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -301,14 +301,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(ldebug, "Data_create(): open(): %s\n", strerror(errno));
|
lprintf(debug, "Data_create(): open(): %s\n", strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
if (ftruncate(fd, cf->content_length)) {
|
if (ftruncate(fd, cf->content_length)) {
|
||||||
lprintf(ldebug, "Data_create(): ftruncate(): %s\n", strerror(errno));
|
lprintf(debug, "Data_create(): ftruncate(): %s\n", strerror(errno));
|
||||||
}
|
}
|
||||||
if (close(fd)) {
|
if (close(fd)) {
|
||||||
lprintf(ldebug, "Data_create(): close:(): %s\n", strerror(errno));
|
lprintf(debug, "Data_create(): close:(): %s\n", strerror(errno));
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -325,7 +325,7 @@ static long Data_size(const char *fn)
|
|||||||
if (!s) {
|
if (!s) {
|
||||||
return st.st_size;
|
return st.st_size;
|
||||||
}
|
}
|
||||||
lprintf(ldebug, "Data_size(): stat(): %s\n", strerror(errno));
|
lprintf(debug, "Data_size(): stat(): %s\n", strerror(errno));
|
||||||
return -1;
|
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)
|
static long Data_read(Cache *cf, uint8_t *buf, off_t len, off_t offset)
|
||||||
{
|
{
|
||||||
if (len == 0) {
|
if (len == 0) {
|
||||||
lprintf(ldebug, "Data_read(): requested to read 0 byte!\n");
|
lprintf(debug, "Data_read(): requested to read 0 byte!\n");
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef CACHE_LOCK_DEBUG
|
#ifdef CACHE_LOCK_DEBUG
|
||||||
lprintf(ldebug, "Data_read(): thread %lu: locking seek_lock;\n",
|
lprintf(debug, "Data_read(): thread %lu: locking seek_lock;\n",
|
||||||
pthread_self());
|
pthread_self());
|
||||||
#endif
|
#endif
|
||||||
PTHREAD_MUTEX_LOCK(&cf->seek_lock);
|
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)) {
|
if (fseeko(cf->dfp, offset, SEEK_SET)) {
|
||||||
/* fseeko failed */
|
/* fseeko failed */
|
||||||
lprintf(ldebug, "Data_read(): fseeko(): %s\n", strerror(errno));
|
lprintf(debug, "Data_read(): fseeko(): %s\n", strerror(errno));
|
||||||
byte_read = -EIO;
|
byte_read = -EIO;
|
||||||
goto end;
|
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);
|
byte_read = fread(buf, sizeof(uint8_t), len, cf->dfp);
|
||||||
if (byte_read != len) {
|
if (byte_read != len) {
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"Data_read(): fread(): requested %ld, returned %ld!\n",
|
"Data_read(): fread(): requested %ld, returned %ld!\n",
|
||||||
len, byte_read);
|
len, byte_read);
|
||||||
if (feof(cf->dfp)) {
|
if (feof(cf->dfp)) {
|
||||||
/* reached EOF */
|
/* reached EOF */
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"Data_read(): fread(): reached the end of the file!\n");
|
"Data_read(): fread(): reached the end of the file!\n");
|
||||||
}
|
}
|
||||||
if (ferror(cf->dfp)) {
|
if (ferror(cf->dfp)) {
|
||||||
/* filesystem error */
|
/* filesystem error */
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"Data_read(): fread(): encountered error!\n");
|
"Data_read(): fread(): encountered error!\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
end:
|
end:
|
||||||
#ifdef CACHE_LOCK_DEBUG
|
#ifdef CACHE_LOCK_DEBUG
|
||||||
lprintf(ldebug, "Data_read(): thread %lu: unlocking seek_lock;\n",
|
lprintf(debug, "Data_read(): thread %lu: unlocking seek_lock;\n",
|
||||||
pthread_self());
|
pthread_self());
|
||||||
#endif
|
#endif
|
||||||
PTHREAD_MUTEX_UNLOCK(&cf->seek_lock);
|
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)
|
off_t offset)
|
||||||
{
|
{
|
||||||
if (len == 0) {
|
if (len == 0) {
|
||||||
lprintf(ldebug, "Data_write(): requested to write 0 byte!\n");
|
lprintf(debug, "Data_write(): requested to write 0 byte!\n");
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef CACHE_LOCK_DEBUG
|
#ifdef CACHE_LOCK_DEBUG
|
||||||
lprintf(ldebug, "Data_write(): thread %lu: locking seek_lock;\n",
|
lprintf(debug, "Data_write(): thread %lu: locking seek_lock;\n",
|
||||||
pthread_self());
|
pthread_self());
|
||||||
#endif
|
#endif
|
||||||
PTHREAD_MUTEX_LOCK(&cf->seek_lock);
|
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)) {
|
if (fseeko(cf->dfp, offset, SEEK_SET)) {
|
||||||
/* fseeko failed */
|
/* fseeko failed */
|
||||||
lprintf(ldebug, "Data_write(): fseeko(): %s\n", strerror(errno));
|
lprintf(debug, "Data_write(): fseeko(): %s\n", strerror(errno));
|
||||||
byte_written = -EIO;
|
byte_written = -EIO;
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
byte_written = fwrite(buf, sizeof(uint8_t), len, cf->dfp);
|
byte_written = fwrite(buf, sizeof(uint8_t), len, cf->dfp);
|
||||||
if (byte_written != len) {
|
if (byte_written != len) {
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"Data_write(): fwrite(): requested %ld, returned %ld!\n",
|
"Data_write(): fwrite(): requested %ld, returned %ld!\n",
|
||||||
len, byte_written);
|
len, byte_written);
|
||||||
exit_failure();
|
exit_failure();
|
||||||
if (ferror(cf->dfp)) {
|
if (ferror(cf->dfp)) {
|
||||||
/* filesystem error */
|
/* filesystem error */
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"Data_write(): fwrite(): encountered error!\n");
|
"Data_write(): fwrite(): encountered error!\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
end:
|
end:
|
||||||
#ifdef CACHE_LOCK_DEBUG
|
#ifdef CACHE_LOCK_DEBUG
|
||||||
lprintf(ldebug, "Data_write(): thread %lu: unlocking seek_lock;\n",
|
lprintf(debug, "Data_write(): thread %lu: unlocking seek_lock;\n",
|
||||||
pthread_self());
|
pthread_self());
|
||||||
#endif
|
#endif
|
||||||
PTHREAD_MUTEX_UNLOCK(&cf->seek_lock);
|
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);
|
i = -mkdir(metadirn, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
|
||||||
if (i && (errno != EEXIST)) {
|
if (i && (errno != EEXIST)) {
|
||||||
lprintf(ldebug, "CacheDir_create(): mkdir(): %s\n", strerror(errno));
|
lprintf(debug, "CacheDir_create(): mkdir(): %s\n", strerror(errno));
|
||||||
}
|
}
|
||||||
|
|
||||||
i |= -mkdir(datadirn, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) << 1;
|
i |= -mkdir(datadirn, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) << 1;
|
||||||
if (i && (errno != EEXIST)) {
|
if (i && (errno != EEXIST)) {
|
||||||
lprintf(ldebug, "CacheDir_create(): mkdir(): %s\n", strerror(errno));
|
lprintf(debug, "CacheDir_create(): mkdir(): %s\n", strerror(errno));
|
||||||
}
|
}
|
||||||
free(datadirn);
|
free(datadirn);
|
||||||
free(metadirn);
|
free(metadirn);
|
||||||
@ -479,25 +479,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(ldebug, "Cache_alloc(): seek_lock initialisation failed!\n");
|
lprintf(debug, "Cache_alloc(): seek_lock initialisation failed!\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pthread_mutex_init(&cf->w_lock, NULL)) {
|
if (pthread_mutex_init(&cf->w_lock, NULL)) {
|
||||||
lprintf(ldebug, "Cache_alloc(): w_lock initialisation failed!\n");
|
lprintf(debug, "Cache_alloc(): w_lock initialisation failed!\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pthread_mutexattr_init(&cf->bgt_lock_attr)) {
|
if (pthread_mutexattr_init(&cf->bgt_lock_attr)) {
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"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(ldebug, "Cache_alloc(): could not set bgt_lock_attr!\n");
|
lprintf(debug, "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(ldebug, "Cache_alloc(): bgt_lock initialisation failed!\n");
|
lprintf(debug, "Cache_alloc(): bgt_lock initialisation failed!\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
return cf;
|
return cf;
|
||||||
@ -509,19 +509,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(ldebug, "Cache_free(): could not destroy seek_lock!\n");
|
lprintf(debug, "Cache_free(): could not destroy seek_lock!\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pthread_mutex_destroy(&cf->w_lock)) {
|
if (pthread_mutex_destroy(&cf->w_lock)) {
|
||||||
lprintf(ldebug, "Cache_free(): could not destroy w_lock!\n");
|
lprintf(debug, "Cache_free(): could not destroy w_lock!\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pthread_mutex_destroy(&cf->bgt_lock)) {
|
if (pthread_mutex_destroy(&cf->bgt_lock)) {
|
||||||
lprintf(ldebug, "Cache_free(): could not destroy bgt_lock!\n");
|
lprintf(debug, "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(ldebug, "Cache_alloc(): could not destroy bgt_lock_attr!\n");
|
lprintf(debug, "Cache_alloc(): could not destroy bgt_lock_attr!\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cf->path) {
|
if (cf->path) {
|
||||||
@ -559,13 +559,13 @@ static int Cache_exist(const char *fn)
|
|||||||
if (no_meta ^ no_data) {
|
if (no_meta ^ no_data) {
|
||||||
if (no_meta) {
|
if (no_meta) {
|
||||||
if(unlink(datafn)) {
|
if(unlink(datafn)) {
|
||||||
lprintf(ldebug, "Cache_exist(): unlink(): %s\n",
|
lprintf(debug, "Cache_exist(): unlink(): %s\n",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (no_data) {
|
if (no_data) {
|
||||||
if(unlink(metafn)) {
|
if(unlink(metafn)) {
|
||||||
lprintf(ldebug, "Cache_exist(): unlink(): %s\n",
|
lprintf(debug, "Cache_exist(): unlink(): %s\n",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -591,14 +591,14 @@ void Cache_delete(const char *fn)
|
|||||||
char *datafn = path_append(DATA_DIR, fn);
|
char *datafn = path_append(DATA_DIR, fn);
|
||||||
if (!access(metafn, F_OK)) {
|
if (!access(metafn, F_OK)) {
|
||||||
if(unlink(metafn)) {
|
if(unlink(metafn)) {
|
||||||
lprintf(ldebug, "Cache_delete(): unlink(): %s\n",
|
lprintf(debug, "Cache_delete(): unlink(): %s\n",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!access(datafn, F_OK)) {
|
if (!access(datafn, F_OK)) {
|
||||||
if(unlink(datafn)) {
|
if(unlink(datafn)) {
|
||||||
lprintf(ldebug, "Cache_delete(): unlink(): %s\n",
|
lprintf(debug, "Cache_delete(): unlink(): %s\n",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -619,7 +619,7 @@ static int Data_open(Cache *cf)
|
|||||||
free(datafn);
|
free(datafn);
|
||||||
if (!cf->dfp) {
|
if (!cf->dfp) {
|
||||||
/* Failed to open the data file */
|
/* Failed to open the data file */
|
||||||
lprintf(ldebug, "Data_open(): fopen(%s): %s\n", datafn,
|
lprintf(debug, "Data_open(): fopen(%s): %s\n", datafn,
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -638,7 +638,7 @@ static int Meta_open(Cache *cf)
|
|||||||
cf->mfp = fopen(metafn, "r+");
|
cf->mfp = fopen(metafn, "r+");
|
||||||
if (!cf->mfp) {
|
if (!cf->mfp) {
|
||||||
/* Failed to open the data file */
|
/* Failed to open the data file */
|
||||||
lprintf(ldebug, "Meta_open(): fopen(%s): %s\n", metafn,
|
lprintf(debug, "Meta_open(): fopen(%s): %s\n", metafn,
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
free(metafn);
|
free(metafn);
|
||||||
return -1;
|
return -1;
|
||||||
@ -659,7 +659,7 @@ static int Meta_create(Cache *cf)
|
|||||||
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(ldebug, "Meta_create(): fopen(%s): %s\n", metafn,
|
lprintf(debug, "Meta_create(): fopen(%s): %s\n", metafn,
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
free(metafn);
|
free(metafn);
|
||||||
return -1;
|
return -1;
|
||||||
@ -679,7 +679,7 @@ int Cache_create(const char *path)
|
|||||||
} else if (CONFIG.mode == SONIC) {
|
} else if (CONFIG.mode == SONIC) {
|
||||||
fn = this_link->sonic_id;
|
fn = this_link->sonic_id;
|
||||||
}
|
}
|
||||||
lprintf(ldebug, "Cache_create(): Creating cache files for %s.\n", fn);
|
lprintf(debug, "Cache_create(): Creating cache files for %s.\n", fn);
|
||||||
|
|
||||||
Cache *cf = Cache_alloc();
|
Cache *cf = Cache_alloc();
|
||||||
cf->path = strndup(fn, MAX_PATH_LEN);
|
cf->path = strndup(fn, MAX_PATH_LEN);
|
||||||
@ -690,33 +690,33 @@ int Cache_create(const char *path)
|
|||||||
cf->seg = CALLOC(cf->segbc, sizeof(Seg));
|
cf->seg = CALLOC(cf->segbc, sizeof(Seg));
|
||||||
|
|
||||||
if (Meta_create(cf)) {
|
if (Meta_create(cf)) {
|
||||||
lprintf(ldebug, "Cache_create(): cannot create metadata.\n");
|
lprintf(debug, "Cache_create(): cannot create metadata.\n");
|
||||||
exit_failure();
|
exit_failure();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fclose(cf->mfp)) {
|
if (fclose(cf->mfp)) {
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"Cache_create(): cannot close metadata after creation: %s.\n",
|
"Cache_create(): cannot close metadata after creation: %s.\n",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Meta_open(cf)) {
|
if (Meta_open(cf)) {
|
||||||
Cache_free(cf);
|
Cache_free(cf);
|
||||||
lprintf(ldebug, "Cache_create(): cannot open metadata file, %s.\n", fn);
|
lprintf(debug, "Cache_create(): cannot open metadata file, %s.\n", fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Meta_write(cf)) {
|
if (Meta_write(cf)) {
|
||||||
lprintf(ldebug, "Cache_create(): Meta_write() failed!\n");
|
lprintf(debug, "Cache_create(): Meta_write() failed!\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fclose(cf->mfp)) {
|
if (fclose(cf->mfp)) {
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"Cache_create(): cannot close metadata after write, %s.\n",
|
"Cache_create(): cannot close metadata after write, %s.\n",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Data_create(cf)) {
|
if (Data_create(cf)) {
|
||||||
lprintf(ldebug, "Cache_create(): Data_create() failed!\n");
|
lprintf(debug, "Cache_create(): Data_create() failed!\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
Cache_free(cf);
|
Cache_free(cf);
|
||||||
@ -742,7 +742,7 @@ Cache *Cache_open(const char *fn)
|
|||||||
/*---------------- Cache_open() critical section -----------------*/
|
/*---------------- Cache_open() critical section -----------------*/
|
||||||
|
|
||||||
#ifdef CACHE_LOCK_DEBUG
|
#ifdef CACHE_LOCK_DEBUG
|
||||||
lprintf(ldebug, "Cache_open(): thread %lu: locking cf_lock;\n",
|
lprintf(debug, "Cache_open(): thread %lu: locking cf_lock;\n",
|
||||||
pthread_self());
|
pthread_self());
|
||||||
#endif
|
#endif
|
||||||
PTHREAD_MUTEX_LOCK(&cf_lock);
|
PTHREAD_MUTEX_LOCK(&cf_lock);
|
||||||
@ -750,7 +750,7 @@ Cache *Cache_open(const char *fn)
|
|||||||
if (link->cache_opened) {
|
if (link->cache_opened) {
|
||||||
link->cache_opened++;
|
link->cache_opened++;
|
||||||
#ifdef CACHE_LOCK_DEBUG
|
#ifdef CACHE_LOCK_DEBUG
|
||||||
lprintf(ldebug, "Cache_open(): thread %lu: unlocking cf_lock;\n",
|
lprintf(debug, "Cache_open(): thread %lu: unlocking cf_lock;\n",
|
||||||
pthread_self());
|
pthread_self());
|
||||||
#endif
|
#endif
|
||||||
PTHREAD_MUTEX_UNLOCK(&cf_lock);
|
PTHREAD_MUTEX_UNLOCK(&cf_lock);
|
||||||
@ -758,7 +758,7 @@ Cache *Cache_open(const char *fn)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef CACHE_LOCK_DEBUG
|
#ifdef CACHE_LOCK_DEBUG
|
||||||
lprintf(ldebug, "Cache_open(): thread %lu: unlocking cf_lock;\n",
|
lprintf(debug, "Cache_open(): thread %lu: unlocking cf_lock;\n",
|
||||||
pthread_self());
|
pthread_self());
|
||||||
#endif
|
#endif
|
||||||
PTHREAD_MUTEX_UNLOCK(&cf_lock);
|
PTHREAD_MUTEX_UNLOCK(&cf_lock);
|
||||||
@ -794,7 +794,7 @@ Cache *Cache_open(const char *fn)
|
|||||||
|
|
||||||
if (Meta_open(cf)) {
|
if (Meta_open(cf)) {
|
||||||
Cache_free(cf);
|
Cache_free(cf);
|
||||||
lprintf(ldebug, "Cache_open(): cannot open metadata file %s.\n", fn);
|
lprintf(debug, "Cache_open(): cannot open metadata file %s.\n", fn);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -805,7 +805,7 @@ Cache *Cache_open(const char *fn)
|
|||||||
*/
|
*/
|
||||||
if ((rtn == EINCONSIST) || (rtn == EMEM)) {
|
if ((rtn == EINCONSIST) || (rtn == EMEM)) {
|
||||||
Cache_free(cf);
|
Cache_free(cf);
|
||||||
lprintf(ldebug, "Cache_open(): metadata error: %s, %d.\n", fn, rtn);
|
lprintf(debug, "Cache_open(): metadata error: %s, %d.\n", fn, rtn);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -815,7 +815,7 @@ Cache *Cache_open(const char *fn)
|
|||||||
* allocation policy.
|
* allocation policy.
|
||||||
*/
|
*/
|
||||||
if (cf->content_length > Data_size(fn)) {
|
if (cf->content_length > Data_size(fn)) {
|
||||||
lprintf(ldebug, "Cache_open(): metadata inconsistency %s, \
|
lprintf(debug, "Cache_open(): metadata inconsistency %s, \
|
||||||
cf->content_length: %ld, Data_size(fn): %ld.\n", fn, cf->content_length,
|
cf->content_length: %ld, Data_size(fn): %ld.\n", fn, cf->content_length,
|
||||||
Data_size(fn));
|
Data_size(fn));
|
||||||
Cache_free(cf);
|
Cache_free(cf);
|
||||||
@ -824,14 +824,14 @@ cf->content_length: %ld, Data_size(fn): %ld.\n", fn, cf->content_length,
|
|||||||
|
|
||||||
/* Check if the cache files are not outdated */
|
/* Check if the cache files are not outdated */
|
||||||
if (cf->time != cf->link->time) {
|
if (cf->time != cf->link->time) {
|
||||||
lprintf(ldebug, "Cache_open(): outdated cache file: %s.\n", fn);
|
lprintf(debug, "Cache_open(): outdated cache file: %s.\n", fn);
|
||||||
Cache_free(cf);
|
Cache_free(cf);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Data_open(cf)) {
|
if (Data_open(cf)) {
|
||||||
Cache_free(cf);
|
Cache_free(cf);
|
||||||
lprintf(ldebug, "Cache_open(): cannot open data file %s.\n", fn);
|
lprintf(debug, "Cache_open(): cannot open data file %s.\n", fn);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -847,7 +847,7 @@ void Cache_close(Cache *cf)
|
|||||||
/*--------------- Cache_close() critical section -----------------*/
|
/*--------------- Cache_close() critical section -----------------*/
|
||||||
|
|
||||||
#ifdef CACHE_LOCK_DEBUG
|
#ifdef CACHE_LOCK_DEBUG
|
||||||
lprintf(ldebug, "Cache_close(): thread %lu: locking cf_lock;\n",
|
lprintf(debug, "Cache_close(): thread %lu: locking cf_lock;\n",
|
||||||
pthread_self());
|
pthread_self());
|
||||||
#endif
|
#endif
|
||||||
PTHREAD_MUTEX_LOCK(&cf_lock);
|
PTHREAD_MUTEX_LOCK(&cf_lock);
|
||||||
@ -856,7 +856,7 @@ void Cache_close(Cache *cf)
|
|||||||
|
|
||||||
if (cf->link->cache_opened > 0) {
|
if (cf->link->cache_opened > 0) {
|
||||||
#ifdef CACHE_LOCK_DEBUG
|
#ifdef CACHE_LOCK_DEBUG
|
||||||
lprintf(ldebug, "Cache_close(): thread %lu: unlocking cf_lock;\n",
|
lprintf(debug, "Cache_close(): thread %lu: unlocking cf_lock;\n",
|
||||||
pthread_self());
|
pthread_self());
|
||||||
#endif
|
#endif
|
||||||
PTHREAD_MUTEX_UNLOCK(&cf_lock);
|
PTHREAD_MUTEX_UNLOCK(&cf_lock);
|
||||||
@ -864,7 +864,7 @@ void Cache_close(Cache *cf)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef CACHE_LOCK_DEBUG
|
#ifdef CACHE_LOCK_DEBUG
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"Cache_close(): thread %lu: locking and unlocking bgt_lock;\n",
|
"Cache_close(): thread %lu: locking and unlocking bgt_lock;\n",
|
||||||
pthread_self());
|
pthread_self());
|
||||||
#endif
|
#endif
|
||||||
@ -873,7 +873,7 @@ void Cache_close(Cache *cf)
|
|||||||
|
|
||||||
|
|
||||||
#ifdef CACHE_LOCK_DEBUG
|
#ifdef CACHE_LOCK_DEBUG
|
||||||
lprintf(ldebug, "Cache_close(): thread %lu: unlocking cf_lock;\n",
|
lprintf(debug, "Cache_close(): thread %lu: unlocking cf_lock;\n",
|
||||||
pthread_self());
|
pthread_self());
|
||||||
#endif
|
#endif
|
||||||
PTHREAD_MUTEX_UNLOCK(&cf_lock);
|
PTHREAD_MUTEX_UNLOCK(&cf_lock);
|
||||||
@ -881,16 +881,16 @@ void Cache_close(Cache *cf)
|
|||||||
/*----------------------------------------------------------------*/
|
/*----------------------------------------------------------------*/
|
||||||
|
|
||||||
if (Meta_write(cf)) {
|
if (Meta_write(cf)) {
|
||||||
lprintf(ldebug, "Cache_close(): Meta_write() error.");
|
lprintf(debug, "Cache_close(): Meta_write() error.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fclose(cf->mfp)) {
|
if (fclose(cf->mfp)) {
|
||||||
lprintf(ldebug, "Cache_close(): cannot close metadata: %s.\n",
|
lprintf(debug, "Cache_close(): cannot close metadata: %s.\n",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fclose(cf->dfp)) {
|
if (fclose(cf->dfp)) {
|
||||||
lprintf(ldebug, "Cache_close(): cannot close data file %s.\n",
|
lprintf(debug, "Cache_close(): cannot close data file %s.\n",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -930,16 +930,16 @@ static void *Cache_bgdl(void *arg)
|
|||||||
{
|
{
|
||||||
Cache *cf = (Cache *) arg;
|
Cache *cf = (Cache *) arg;
|
||||||
#ifdef CACHE_LOCK_DEBUG
|
#ifdef CACHE_LOCK_DEBUG
|
||||||
lprintf(ldebug, "Cache_bgdl(): thread %lu: locking w_lock;\n",
|
lprintf(debug, "Cache_bgdl(): thread %lu: locking w_lock;\n",
|
||||||
pthread_self());
|
pthread_self());
|
||||||
#endif
|
#endif
|
||||||
PTHREAD_MUTEX_LOCK(&cf->w_lock);
|
PTHREAD_MUTEX_LOCK(&cf->w_lock);
|
||||||
uint8_t *recv_buf = CALLOC(cf->blksz, sizeof(uint8_t));
|
uint8_t *recv_buf = CALLOC(cf->blksz, sizeof(uint8_t));
|
||||||
lprintf(ldebug, "Cache_bgdl(): thread %lu: ", pthread_self());
|
lprintf(debug, "Cache_bgdl(): thread %lu: ", pthread_self());
|
||||||
long recv = path_download(cf->fs_path, (char *) recv_buf, cf->blksz,
|
long recv = path_download(cf->fs_path, (char *) recv_buf, cf->blksz,
|
||||||
cf->next_dl_offset);
|
cf->next_dl_offset);
|
||||||
if (recv < 0) {
|
if (recv < 0) {
|
||||||
lprintf(ldebug, "\nCache_bgdl(): received %lu bytes, \
|
lprintf(debug, "\nCache_bgdl(): received %lu bytes, \
|
||||||
which does't make sense\n", recv);
|
which does't make sense\n", recv);
|
||||||
exit_failure();
|
exit_failure();
|
||||||
}
|
}
|
||||||
@ -950,17 +950,17 @@ which does't make sense\n", recv);
|
|||||||
Data_write(cf, recv_buf, recv, cf->next_dl_offset);
|
Data_write(cf, recv_buf, recv, cf->next_dl_offset);
|
||||||
Seg_set(cf, cf->next_dl_offset, 1);
|
Seg_set(cf, cf->next_dl_offset, 1);
|
||||||
} else {
|
} else {
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"Cache_bgdl(): received %ld, possible network error.\n", recv);
|
"Cache_bgdl(): received %ld, possible network error.\n", recv);
|
||||||
}
|
}
|
||||||
free(recv_buf);
|
free(recv_buf);
|
||||||
#ifdef CACHE_LOCK_DEBUG
|
#ifdef CACHE_LOCK_DEBUG
|
||||||
lprintf(ldebug, "Cache_bgdl(): thread %lu: unlocking bgt_lock;\n",
|
lprintf(debug, "Cache_bgdl(): thread %lu: unlocking bgt_lock;\n",
|
||||||
pthread_self());
|
pthread_self());
|
||||||
#endif
|
#endif
|
||||||
PTHREAD_MUTEX_UNLOCK(&cf->bgt_lock);
|
PTHREAD_MUTEX_UNLOCK(&cf->bgt_lock);
|
||||||
#ifdef CACHE_LOCK_DEBUG
|
#ifdef CACHE_LOCK_DEBUG
|
||||||
lprintf(ldebug, "Cache_bgdl(): thread %lu: unlocking w_lock;\n",
|
lprintf(debug, "Cache_bgdl(): thread %lu: unlocking w_lock;\n",
|
||||||
pthread_self());
|
pthread_self());
|
||||||
#endif
|
#endif
|
||||||
PTHREAD_MUTEX_UNLOCK(&cf->w_lock);
|
PTHREAD_MUTEX_UNLOCK(&cf->w_lock);
|
||||||
@ -983,7 +983,7 @@ long Cache_read(Cache *cf, char * const output_buf, const off_t len,
|
|||||||
} else {
|
} else {
|
||||||
/* Wait for any other download thread to finish*/
|
/* Wait for any other download thread to finish*/
|
||||||
#ifdef CACHE_LOCK_DEBUG
|
#ifdef CACHE_LOCK_DEBUG
|
||||||
lprintf(ldebug, "Cache_read(): thread %ld: locking w_lock;\n",
|
lprintf(debug, "Cache_read(): thread %ld: locking w_lock;\n",
|
||||||
pthread_self());
|
pthread_self());
|
||||||
#endif
|
#endif
|
||||||
PTHREAD_MUTEX_LOCK(&cf->w_lock);
|
PTHREAD_MUTEX_LOCK(&cf->w_lock);
|
||||||
@ -992,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 */
|
* download thread. Send it off and unlock the I/O */
|
||||||
send = Data_read(cf, (uint8_t *) output_buf, len, offset_start);
|
send = Data_read(cf, (uint8_t *) output_buf, len, offset_start);
|
||||||
#ifdef CACHE_LOCK_DEBUG
|
#ifdef CACHE_LOCK_DEBUG
|
||||||
lprintf(ldebug, "Cache_read(): thread %lu: unlocking w_lock;\n",
|
lprintf(debug, "Cache_read(): thread %lu: unlocking w_lock;\n",
|
||||||
pthread_self());
|
pthread_self());
|
||||||
#endif
|
#endif
|
||||||
PTHREAD_MUTEX_UNLOCK(&cf->w_lock);
|
PTHREAD_MUTEX_UNLOCK(&cf->w_lock);
|
||||||
@ -1003,11 +1003,11 @@ long Cache_read(Cache *cf, char * const output_buf, const off_t len,
|
|||||||
/* ------------------------Download the segment -------------------------*/
|
/* ------------------------Download the segment -------------------------*/
|
||||||
|
|
||||||
uint8_t *recv_buf = CALLOC(cf->blksz, sizeof(uint8_t));
|
uint8_t *recv_buf = CALLOC(cf->blksz, sizeof(uint8_t));
|
||||||
lprintf(ldebug, "Cache_read(): thread %lu: ", pthread_self());
|
lprintf(debug, "Cache_read(): thread %lu: ", pthread_self());
|
||||||
long recv = path_download(cf->fs_path, (char *) recv_buf, cf->blksz,
|
long recv = path_download(cf->fs_path, (char *) recv_buf, cf->blksz,
|
||||||
dl_offset);
|
dl_offset);
|
||||||
if (recv < 0) {
|
if (recv < 0) {
|
||||||
lprintf(ldebug, "\nCache_read(): received %ld bytes, \
|
lprintf(debug, "\nCache_read(): received %ld bytes, \
|
||||||
which does't make sense\n", recv);
|
which does't make sense\n", recv);
|
||||||
exit_failure();
|
exit_failure();
|
||||||
}
|
}
|
||||||
@ -1023,14 +1023,14 @@ which does't make sense\n", recv);
|
|||||||
Data_write(cf, recv_buf, recv, dl_offset);
|
Data_write(cf, recv_buf, recv, dl_offset);
|
||||||
Seg_set(cf, dl_offset, 1);
|
Seg_set(cf, dl_offset, 1);
|
||||||
} else {
|
} else {
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"Cache_read(): received %ld, possible network error.\n", recv);
|
"Cache_read(): received %ld, possible network error.\n", recv);
|
||||||
}
|
}
|
||||||
free(recv_buf);
|
free(recv_buf);
|
||||||
send = Data_read(cf, (uint8_t *) output_buf, len, offset_start);
|
send = Data_read(cf, (uint8_t *) output_buf, len, offset_start);
|
||||||
|
|
||||||
#ifdef CACHE_LOCK_DEBUG
|
#ifdef CACHE_LOCK_DEBUG
|
||||||
lprintf(ldebug, "Cache_read(): thread %lu: unlocking w_lock;\n",
|
lprintf(debug, "Cache_read(): thread %lu: unlocking w_lock;\n",
|
||||||
pthread_self());
|
pthread_self());
|
||||||
#endif
|
#endif
|
||||||
PTHREAD_MUTEX_UNLOCK(&cf->w_lock);
|
PTHREAD_MUTEX_UNLOCK(&cf->w_lock);
|
||||||
@ -1045,12 +1045,12 @@ which does't make sense\n", recv);
|
|||||||
/* Stop the spawning of multiple background pthreads */
|
/* Stop the spawning of multiple background pthreads */
|
||||||
if(!pthread_mutex_trylock(&cf->bgt_lock)) {
|
if(!pthread_mutex_trylock(&cf->bgt_lock)) {
|
||||||
#ifdef CACHE_LOCK_DEBUG
|
#ifdef CACHE_LOCK_DEBUG
|
||||||
lprintf(ldebug, "Cache_read(): thread %lu: trylocked bgt_lock;\n",
|
lprintf(debug, "Cache_read(): thread %lu: trylocked bgt_lock;\n",
|
||||||
pthread_self());
|
pthread_self());
|
||||||
#endif
|
#endif
|
||||||
cf->next_dl_offset = next_dl_offset;
|
cf->next_dl_offset = next_dl_offset;
|
||||||
if (pthread_create(&cf->bgt, NULL, Cache_bgdl, cf)) {
|
if (pthread_create(&cf->bgt, NULL, Cache_bgdl, cf)) {
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"Cache_read(): Error creating background download thread\n"
|
"Cache_read(): Error creating background download thread\n"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
10
src/config.c
10
src/config.c
@ -28,8 +28,10 @@ ConfigStruct CONFIG;
|
|||||||
*/
|
*/
|
||||||
void Config_init(void)
|
void Config_init(void)
|
||||||
{
|
{
|
||||||
/** \brief Operation Mode */
|
|
||||||
CONFIG.mode = NORMAL;
|
CONFIG.mode = NORMAL;
|
||||||
|
|
||||||
|
CONFIG.log_level = log_level_init();
|
||||||
|
|
||||||
/*---------------- Network related --------------*/
|
/*---------------- Network related --------------*/
|
||||||
CONFIG.http_username = NULL;
|
CONFIG.http_username = NULL;
|
||||||
|
|
||||||
@ -68,10 +70,4 @@ void Config_init(void)
|
|||||||
CONFIG.sonic_id3 = 0;
|
CONFIG.sonic_id3 = 0;
|
||||||
|
|
||||||
CONFIG.sonic_insecure = 0;
|
CONFIG.sonic_insecure = 0;
|
||||||
|
|
||||||
/*--------------- Log related -------------*/
|
|
||||||
CONFIG.log_level = log_level_init();
|
|
||||||
|
|
||||||
CONFIG.log_verbosity = log_verbosity_init();
|
|
||||||
|
|
||||||
}
|
}
|
13
src/config.h
13
src/config.h
@ -40,6 +40,8 @@ typedef enum {
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
/** \brief Operation Mode */
|
/** \brief Operation Mode */
|
||||||
OperationMode mode;
|
OperationMode mode;
|
||||||
|
/** \brief Current log level */
|
||||||
|
int log_level;
|
||||||
/*---------------- Network related --------------*/
|
/*---------------- Network related --------------*/
|
||||||
/** \brief HTTP username */
|
/** \brief HTTP username */
|
||||||
char *http_username;
|
char *http_username;
|
||||||
@ -79,17 +81,6 @@ typedef struct {
|
|||||||
int sonic_id3;
|
int sonic_id3;
|
||||||
/** \brief Whether we use the legacy sonic authentication mode */
|
/** \brief Whether we use the legacy sonic authentication mode */
|
||||||
int sonic_insecure;
|
int sonic_insecure;
|
||||||
/*--------------- Log related -------------*/
|
|
||||||
/** \brief Current log level */
|
|
||||||
int log_level;
|
|
||||||
/**
|
|
||||||
* \brief Whether we print the filename and line number in log
|
|
||||||
* \details
|
|
||||||
* - 0 : No filename and line nubmer
|
|
||||||
* - 1 : Filename only
|
|
||||||
* - 2 : Filename and line number
|
|
||||||
*/
|
|
||||||
int log_verbosity;
|
|
||||||
} ConfigStruct;
|
} ConfigStruct;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
68
src/link.c
68
src/link.c
@ -39,7 +39,7 @@ LinkTable *LinkSystem_init(const char *raw_url)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (pthread_mutex_init(&link_lock, NULL) != 0) {
|
if (pthread_mutex_init(&link_lock, NULL) != 0) {
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"link_system_init(): link_lock initialisation failed!\n");
|
"link_system_init(): link_lock initialisation failed!\n");
|
||||||
exit_failure();
|
exit_failure();
|
||||||
}
|
}
|
||||||
@ -77,7 +77,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(ldebug, "LinkTable_add(): realloc failure!\n");
|
lprintf(debug, "LinkTable_add(): realloc failure!\n");
|
||||||
exit_failure();
|
exit_failure();
|
||||||
}
|
}
|
||||||
linktbl->links[linktbl->num - 1] = link;
|
linktbl->links[linktbl->num - 1] = link;
|
||||||
@ -186,7 +186,7 @@ static CURL *Link_to_curl(Link *link)
|
|||||||
{
|
{
|
||||||
CURL *curl = curl_easy_init();
|
CURL *curl = curl_easy_init();
|
||||||
if (!curl) {
|
if (!curl) {
|
||||||
lprintf(ldebug, "Link_to_curl(): curl_easy_init() failed!\n");
|
lprintf(debug, "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);
|
||||||
@ -232,7 +232,7 @@ static CURL *Link_to_curl(Link *link)
|
|||||||
void Link_req_file_stat(Link *this_link)
|
void Link_req_file_stat(Link *this_link)
|
||||||
{
|
{
|
||||||
if (this_link->type != LINK_UNINITIALISED_FILE) {
|
if (this_link->type != LINK_UNINITIALISED_FILE) {
|
||||||
lprintf(ldebug, "Link_req_file_stat(), invalid request, LinkType: %c",
|
lprintf(debug, "Link_req_file_stat(), invalid request, LinkType: %c",
|
||||||
this_link->type);
|
this_link->type);
|
||||||
exit_failure();
|
exit_failure();
|
||||||
}
|
}
|
||||||
@ -271,12 +271,12 @@ void Link_set_file_stat(Link* this_link, CURL *curl)
|
|||||||
this_link->content_length = cl;
|
this_link->content_length = cl;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
lprintf(ldebug, "Link_set_file_stat(): HTTP %ld", http_resp);
|
lprintf(debug, "Link_set_file_stat(): HTTP %ld", http_resp);
|
||||||
if (HTTP_temp_failure(http_resp)) {
|
if (HTTP_temp_failure(http_resp)) {
|
||||||
lprintf(ldebug, ", retrying later.\n");
|
lprintf(debug, ", retrying later.\n");
|
||||||
} else {
|
} else {
|
||||||
this_link->type = LINK_INVALID;
|
this_link->type = LINK_INVALID;
|
||||||
lprintf(ldebug, ".\n");
|
lprintf(debug, ".\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -290,7 +290,7 @@ static void LinkTable_uninitialised_fill(LinkTable *linktbl)
|
|||||||
{
|
{
|
||||||
int u;
|
int u;
|
||||||
char s[STATUS_LEN];
|
char s[STATUS_LEN];
|
||||||
lprintf(ldebug, "LinkTable_uninitialised_fill(): ... ");
|
lprintf(debug, "LinkTable_uninitialised_fill(): ... ");
|
||||||
do {
|
do {
|
||||||
u = 0;
|
u = 0;
|
||||||
for (int i = 0; i < linktbl->num; i++) {
|
for (int i = 0; i < linktbl->num; i++) {
|
||||||
@ -350,7 +350,7 @@ static void LinkTable_invalid_reset(LinkTable *linktbl)
|
|||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lprintf(ldebug, "LinkTable_invalid_reset(): %d invalid links\n", j);
|
lprintf(debug, "LinkTable_invalid_reset(): %d invalid links\n", j);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LinkTable_free(LinkTable *linktbl)
|
void LinkTable_free(LinkTable *linktbl)
|
||||||
@ -365,13 +365,13 @@ void LinkTable_free(LinkTable *linktbl)
|
|||||||
void LinkTable_print(LinkTable *linktbl)
|
void LinkTable_print(LinkTable *linktbl)
|
||||||
{
|
{
|
||||||
int j = 0;
|
int j = 0;
|
||||||
lprintf(ldebug, "--------------------------------------------\n");
|
lprintf(debug, "--------------------------------------------\n");
|
||||||
lprintf(ldebug, " LinkTable %p for %s\n", linktbl,
|
lprintf(debug, " LinkTable %p for %s\n", linktbl,
|
||||||
linktbl->links[0]->f_url);
|
linktbl->links[0]->f_url);
|
||||||
lprintf(ldebug, "--------------------------------------------\n");
|
lprintf(debug, "--------------------------------------------\n");
|
||||||
for (int i = 0; i < linktbl->num; i++) {
|
for (int i = 0; i < linktbl->num; i++) {
|
||||||
Link *this_link = linktbl->links[i];
|
Link *this_link = linktbl->links[i];
|
||||||
lprintf(ldebug, "%d %c %lu %s %s\n",
|
lprintf(debug, "%d %c %lu %s %s\n",
|
||||||
i,
|
i,
|
||||||
this_link->type,
|
this_link->type,
|
||||||
this_link->content_length,
|
this_link->content_length,
|
||||||
@ -384,9 +384,9 @@ void LinkTable_print(LinkTable *linktbl)
|
|||||||
j++;
|
j++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lprintf(ldebug, "--------------------------------------------\n");
|
lprintf(debug, "--------------------------------------------\n");
|
||||||
lprintf(ldebug, "LinkTable_print(): Invalid link count: %d\n", j);
|
lprintf(debug, "LinkTable_print(): Invalid link count: %d\n", j);
|
||||||
lprintf(ldebug, "--------------------------------------------\n");
|
lprintf(debug, "--------------------------------------------\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
DataStruct Link_to_DataStruct(Link *head_link)
|
DataStruct Link_to_DataStruct(Link *head_link)
|
||||||
@ -406,12 +406,12 @@ 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(ldebug,
|
lprintf(debug,
|
||||||
"LinkTable_new(): URL: %s, HTTP %ld, retrying later.\n",
|
"LinkTable_new(): 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(ldebug,
|
lprintf(debug,
|
||||||
"LinkTable_new(): cannot retrieve URL: %s, HTTP %ld\n",
|
"LinkTable_new(): cannot retrieve URL: %s, HTTP %ld\n",
|
||||||
url, http_resp);
|
url, http_resp);
|
||||||
buf.size = 0;
|
buf.size = 0;
|
||||||
@ -440,7 +440,7 @@ LinkTable *LinkTable_alloc(const char *url)
|
|||||||
LinkTable *LinkTable_new(const char *url)
|
LinkTable *LinkTable_new(const char *url)
|
||||||
{
|
{
|
||||||
#ifdef LINK_LOCK_DEBUG
|
#ifdef LINK_LOCK_DEBUG
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"LinkTable_new(): thread %lu: locking link_lock;\n",
|
"LinkTable_new(): thread %lu: locking link_lock;\n",
|
||||||
pthread_self());
|
pthread_self());
|
||||||
#endif
|
#endif
|
||||||
@ -471,7 +471,7 @@ LinkTable *LinkTable_new(const char *url)
|
|||||||
disk_linktbl = LinkTable_disk_open(unescaped_path);
|
disk_linktbl = LinkTable_disk_open(unescaped_path);
|
||||||
if (disk_linktbl) {
|
if (disk_linktbl) {
|
||||||
/* Check if we need to update the link table */
|
/* Check if we need to update the link table */
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"LinkTable_new(): disk_linktbl->num: %d, linktbl->num: %d\n",
|
"LinkTable_new(): disk_linktbl->num: %d, linktbl->num: %d\n",
|
||||||
disk_linktbl->num, linktbl->num);
|
disk_linktbl->num, linktbl->num);
|
||||||
if (disk_linktbl->num == linktbl->num) {
|
if (disk_linktbl->num == linktbl->num) {
|
||||||
@ -505,7 +505,7 @@ LinkTable *LinkTable_new(const char *url)
|
|||||||
|
|
||||||
LinkTable_print(linktbl);
|
LinkTable_print(linktbl);
|
||||||
#ifdef LINK_LOCK_DEBUG
|
#ifdef LINK_LOCK_DEBUG
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"LinkTable_new(): thread %lu: unlocking link_lock;\n",
|
"LinkTable_new(): thread %lu: unlocking link_lock;\n",
|
||||||
pthread_self());
|
pthread_self());
|
||||||
#endif
|
#endif
|
||||||
@ -523,7 +523,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(ldebug, "LinkTable_disk_delete(): unlink(%s): %s\n", path,
|
lprintf(debug, "LinkTable_disk_delete(): unlink(%s): %s\n", path,
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
}
|
}
|
||||||
free(path);
|
free(path);
|
||||||
@ -543,7 +543,7 @@ int LinkTable_disk_save(LinkTable *linktbl, const char *dirn)
|
|||||||
free(metadirn);
|
free(metadirn);
|
||||||
|
|
||||||
if (!fp) {
|
if (!fp) {
|
||||||
lprintf(ldebug, "LinkTable_disk_save(): fopen(%s): %s\n", path,
|
lprintf(debug, "LinkTable_disk_save(): fopen(%s): %s\n", path,
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
free(path);
|
free(path);
|
||||||
return -1;
|
return -1;
|
||||||
@ -562,12 +562,12 @@ int LinkTable_disk_save(LinkTable *linktbl, const char *dirn)
|
|||||||
int res = 0;
|
int res = 0;
|
||||||
|
|
||||||
if (ferror(fp)) {
|
if (ferror(fp)) {
|
||||||
lprintf(ldebug, "LinkTable_disk_save(): encountered ferror!\n");
|
lprintf(debug, "LinkTable_disk_save(): encountered ferror!\n");
|
||||||
res = -1;
|
res = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fclose(fp)) {
|
if (fclose(fp)) {
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"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;
|
||||||
@ -606,21 +606,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(ldebug,
|
lprintf(debug,
|
||||||
"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(ldebug, "LinkTable_disk_open(): encountered ferror!\n");
|
lprintf(debug, "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(ldebug,
|
lprintf(debug,
|
||||||
"LinkTable_disk_open(): cannot close the file pointer, %s\n",
|
"LinkTable_disk_open(): cannot close the file pointer, %s\n",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
}
|
}
|
||||||
@ -714,7 +714,7 @@ Link *path_to_Link(const char *path)
|
|||||||
{
|
{
|
||||||
char *new_path = strndup(path, MAX_PATH_LEN);
|
char *new_path = strndup(path, MAX_PATH_LEN);
|
||||||
if (!new_path) {
|
if (!new_path) {
|
||||||
lprintf(ldebug, "path_to_Link(): cannot allocate memory\n");
|
lprintf(debug, "path_to_Link(): cannot allocate memory\n");
|
||||||
exit_failure();
|
exit_failure();
|
||||||
}
|
}
|
||||||
Link *link = path_to_Link_recursive(new_path, ROOT_LINK_TBL);
|
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)
|
off_t offset)
|
||||||
{
|
{
|
||||||
if (!path) {
|
if (!path) {
|
||||||
lprintf(ldebug, "\npath_download(): NULL path supplied\n");
|
lprintf(debug, "\npath_download(): NULL path supplied\n");
|
||||||
exit_failure();
|
exit_failure();
|
||||||
}
|
}
|
||||||
Link *link;
|
Link *link;
|
||||||
@ -739,7 +739,7 @@ long path_download(const char *path, char *output_buf, size_t size,
|
|||||||
size_t end = start + size;
|
size_t end = start + size;
|
||||||
char range_str[64];
|
char range_str[64];
|
||||||
snprintf(range_str, sizeof(range_str), "%lu-%lu", start, end);
|
snprintf(range_str, sizeof(range_str), "%lu-%lu", start, end);
|
||||||
lprintf(ldebug, "path_download(%s, %s);\n", path, range_str);
|
lprintf(debug, "path_download(%s, %s);\n", path, range_str);
|
||||||
|
|
||||||
DataStruct buf;
|
DataStruct buf;
|
||||||
buf.size = 0;
|
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);
|
curl_easy_setopt(curl, CURLOPT_RANGE, range_str);
|
||||||
|
|
||||||
#ifdef LINK_LOCK_DEBUG
|
#ifdef LINK_LOCK_DEBUG
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"path_download(): thread %lu: locking and unlocking link_lock;\n",
|
"path_download(): thread %lu: locking and unlocking link_lock;\n",
|
||||||
pthread_self());
|
pthread_self());
|
||||||
#endif
|
#endif
|
||||||
@ -768,7 +768,7 @@ 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(ldebug, "Error: This web server does not support HTTP \
|
lprintf(debug, "Error: This web server does not support HTTP \
|
||||||
range requests\n");
|
range requests\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@ -783,7 +783,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(ldebug, "path_download(): Could not download %s, HTTP %ld\n",
|
lprintf(debug, "path_download(): Could not download %s, HTTP %ld\n",
|
||||||
link->f_url, http_resp);
|
link->f_url, http_resp);
|
||||||
return -ENOENT;
|
return -ENOENT;
|
||||||
}
|
}
|
||||||
|
46
src/log.c
46
src/log.c
@ -15,46 +15,40 @@ int log_level_init()
|
|||||||
return DEFAULT_LOG_LEVEL;
|
return DEFAULT_LOG_LEVEL;
|
||||||
}
|
}
|
||||||
|
|
||||||
int log_verbosity_init()
|
void log_printf(LogType type, const char *file, int line, const char *format, ...)
|
||||||
{
|
|
||||||
char *env = getenv("HTTPDIRFS_LOG_VERBOSITY");
|
|
||||||
if (env) {
|
|
||||||
return atoi(env);
|
|
||||||
}
|
|
||||||
return DEFAULT_LOG_VERBOSITY;
|
|
||||||
}
|
|
||||||
|
|
||||||
void log_printf(int type, const char *file, int line, const char *format, ...)
|
|
||||||
{
|
{
|
||||||
|
FILE *out = stderr;
|
||||||
if (type & CONFIG.log_level) {
|
if (type & CONFIG.log_level) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case linfo:
|
case fatal:
|
||||||
|
fprintf(out, "Fatal: ");
|
||||||
|
break;
|
||||||
|
case error:
|
||||||
|
fprintf(out, "Error: ");
|
||||||
|
break;
|
||||||
|
case warning:
|
||||||
|
fprintf(out, "Warning: ");
|
||||||
|
break;
|
||||||
|
case info:
|
||||||
|
out = stdout;
|
||||||
goto print_actual_message;
|
goto print_actual_message;
|
||||||
break;
|
break;
|
||||||
case lerror:
|
case debug:
|
||||||
fprintf(stderr, "Error: ");
|
fprintf(out, "Debug: ");
|
||||||
break;
|
|
||||||
case ldebug:
|
|
||||||
fprintf(stderr, "Debug: ");
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
fprintf(stderr, "Unknown (%x):", type);
|
fprintf(out, "Unknown (%x):", type);
|
||||||
break;
|
|
||||||
}
|
|
||||||
switch (CONFIG.log_verbosity) {
|
|
||||||
case LOG_SHOW_FILENAME:
|
|
||||||
fprintf(stderr, "(%s): ", file);
|
|
||||||
break;
|
|
||||||
case LOG_SHOW_FILENAME_LINE_NUM:
|
|
||||||
fprintf(stderr, "(%s:%d): ", file, line);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fprintf(out, "(%s:%d): ", file, line);
|
||||||
|
|
||||||
print_actual_message:
|
print_actual_message:
|
||||||
/* A label can only be part of a statement, this is a statement. lol*/
|
/* A label can only be part of a statement, this is a statement. lol*/
|
||||||
{}
|
{}
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, format);
|
va_start(args, format);
|
||||||
vfprintf(stderr, format, args);
|
vfprintf(out, format, args);
|
||||||
va_end(args);
|
va_end(args);
|
||||||
}
|
}
|
||||||
}
|
}
|
43
src/log.h
43
src/log.h
@ -1,55 +1,32 @@
|
|||||||
#ifndef LOG_H
|
#ifndef LOG_H
|
||||||
#define LOG_H
|
#define LOG_H
|
||||||
/**
|
|
||||||
* \brief Log type: Informational
|
|
||||||
*/
|
|
||||||
#define linfo 1 << 0
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Log type: Error
|
* \brief Log types
|
||||||
*/
|
*/
|
||||||
#define lerror 1 << 1
|
typedef enum {
|
||||||
|
fatal = 1 << 0,
|
||||||
/**
|
error = 1 << 1,
|
||||||
* \brief Log type: Debug
|
warning = 1 << 2,
|
||||||
*/
|
info = 1 << 3,
|
||||||
#define ldebug 1 << 2
|
debug = 1 << 4,
|
||||||
|
} LogType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief The default log level
|
* \brief The default log level
|
||||||
*/
|
*/
|
||||||
#define DEFAULT_LOG_LEVEL ldebug
|
#define DEFAULT_LOG_LEVEL fatal | error | warning | info | debug
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Display filename in log
|
|
||||||
*/
|
|
||||||
#define LOG_SHOW_FILENAME 1
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Display line number in log
|
|
||||||
*/
|
|
||||||
#define LOG_SHOW_FILENAME_LINE_NUM 2
|
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief The default log verbosity
|
|
||||||
*/
|
|
||||||
#define DEFAULT_LOG_VERBOSITY LOG_SHOW_FILENAME_LINE_NUM
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Get the log level from the environment.
|
* \brief Get the log level from the environment.
|
||||||
*/
|
*/
|
||||||
int log_level_init();
|
int log_level_init();
|
||||||
|
|
||||||
/**
|
|
||||||
* \brief Get the log verbosity from the environment
|
|
||||||
*/
|
|
||||||
int log_verbosity_init();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Log printf
|
* \brief Log printf
|
||||||
* \details This is for printing nice log messages
|
* \details This is for printing nice log messages
|
||||||
*/
|
*/
|
||||||
void log_printf(int type, const char *file, int line, const char *format, ...);
|
void log_printf(LogType type, const char *file, int line, const char *format, ...);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \brief Log type printf
|
* \brief Log type printf
|
||||||
|
12
src/main.c
12
src/main.c
@ -23,7 +23,7 @@ int main(int argc, char **argv)
|
|||||||
/* Automatically print help if not enough arguments are supplied */
|
/* Automatically print help if not enough arguments are supplied */
|
||||||
if (argc < 2) {
|
if (argc < 2) {
|
||||||
print_help(argv[0], 0);
|
print_help(argv[0], 0);
|
||||||
lprintf(ldebug, "For more information, run \"%s --help.\"\n", argv[0]);
|
lprintf(debug, "For more information, run \"%s --help.\"\n", argv[0]);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,20 +71,20 @@ int main(int argc, char **argv)
|
|||||||
/* The second last remaining argument is the URL */
|
/* The second last remaining argument is the URL */
|
||||||
char *base_url = argv[argc-2];
|
char *base_url = argv[argc-2];
|
||||||
if (strncmp(base_url, "http://", 7) && strncmp(base_url, "https://", 8)) {
|
if (strncmp(base_url, "http://", 7) && strncmp(base_url, "https://", 8)) {
|
||||||
lprintf(ldebug, "Error: Please supply a valid URL.\n");
|
lprintf(debug, "Error: Please supply a valid URL.\n");
|
||||||
print_help(argv[0], 0);
|
print_help(argv[0], 0);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
} else {
|
} else {
|
||||||
if (CONFIG.sonic_username && CONFIG.sonic_password) {
|
if (CONFIG.sonic_username && CONFIG.sonic_password) {
|
||||||
CONFIG.mode = SONIC;
|
CONFIG.mode = SONIC;
|
||||||
} else if (CONFIG.sonic_username || CONFIG.sonic_password) {
|
} else if (CONFIG.sonic_username || CONFIG.sonic_password) {
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"Error: You have to supply both username and password to \
|
"Error: You have to supply both username and password to \
|
||||||
activate Sonic mode.\n");
|
activate Sonic mode.\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
if(!LinkSystem_init(base_url)) {
|
if(!LinkSystem_init(base_url)) {
|
||||||
lprintf(ldebug, "Error: Network initialisation failed.\n");
|
lprintf(debug, "Error: Network initialisation failed.\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -262,12 +262,12 @@ parse_arg_list(int argc, char **argv, char ***fuse_argv, int *fuse_argc)
|
|||||||
CONFIG.mode = SINGLE_FILE;
|
CONFIG.mode = SINGLE_FILE;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
lprintf(ldebug, "see httpdirfs -h for usage\n");
|
lprintf(debug, "see httpdirfs -h for usage\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
lprintf(ldebug, "see httpdirfs -h for usage\n");
|
lprintf(debug, "see httpdirfs -h for usage\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -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);
|
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_resp);
|
||||||
if (HTTP_temp_failure(http_resp)) {
|
if (HTTP_temp_failure(http_resp)) {
|
||||||
if (!slept) {
|
if (!slept) {
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"curl_process_msgs(): HTTP %ld, sleeping for %d sec\n",
|
"curl_process_msgs(): HTTP %ld, sleeping for %d sec\n",
|
||||||
http_resp, CONFIG.http_wait_sec);
|
http_resp, CONFIG.http_wait_sec);
|
||||||
sleep(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);
|
Link_set_file_stat(transfer->link, curl);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
lprintf(ldebug, "curl_process_msgs(): %d - %s <%s>\n",
|
lprintf(debug, "curl_process_msgs(): %d - %s <%s>\n",
|
||||||
curl_msg->data.result,
|
curl_msg->data.result,
|
||||||
curl_easy_strerror(curl_msg->data.result),
|
curl_easy_strerror(curl_msg->data.result),
|
||||||
url);
|
url);
|
||||||
@ -147,7 +147,7 @@ static void curl_process_msgs(CURLMsg *curl_msg, int n_running_curl,
|
|||||||
free(transfer);
|
free(transfer);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
lprintf(ldebug, "curl_process_msgs(): curl_msg->msg: %d\n",
|
lprintf(debug, "curl_process_msgs(): curl_msg->msg: %d\n",
|
||||||
curl_msg->msg);
|
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)
|
int curl_multi_perform_once(void)
|
||||||
{
|
{
|
||||||
#ifdef NETWORK_LOCK_DEBUG
|
#ifdef NETWORK_LOCK_DEBUG
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"curl_multi_perform_once(): thread %lu: locking transfer_lock;\n",
|
"curl_multi_perform_once(): thread %lu: locking transfer_lock;\n",
|
||||||
pthread_self());
|
pthread_self());
|
||||||
#endif
|
#endif
|
||||||
@ -168,7 +168,7 @@ int curl_multi_perform_once(void)
|
|||||||
int n_running_curl;
|
int n_running_curl;
|
||||||
CURLMcode mc = curl_multi_perform(curl_multi, &n_running_curl);
|
CURLMcode mc = curl_multi_perform(curl_multi, &n_running_curl);
|
||||||
if(mc > 0) {
|
if(mc > 0) {
|
||||||
lprintf(ldebug, "curl_multi_perform(): %s\n", curl_multi_strerror(mc));
|
lprintf(debug, "curl_multi_perform(): %s\n", curl_multi_strerror(mc));
|
||||||
}
|
}
|
||||||
|
|
||||||
fd_set fdread;
|
fd_set fdread;
|
||||||
@ -202,14 +202,14 @@ int curl_multi_perform_once(void)
|
|||||||
mc = curl_multi_fdset(curl_multi, &fdread, &fdwrite, &fdexcep, &maxfd);
|
mc = curl_multi_fdset(curl_multi, &fdread, &fdwrite, &fdexcep, &maxfd);
|
||||||
|
|
||||||
if (mc > 0) {
|
if (mc > 0) {
|
||||||
lprintf(ldebug, "curl_multi_fdset(): %s.\n", curl_multi_strerror(mc));
|
lprintf(debug, "curl_multi_fdset(): %s.\n", curl_multi_strerror(mc));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (maxfd == -1) {
|
if (maxfd == -1) {
|
||||||
usleep(100*1000);
|
usleep(100*1000);
|
||||||
} else {
|
} else {
|
||||||
if (select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout) < 0) {
|
if (select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout) < 0) {
|
||||||
lprintf(ldebug, "curl_multi_perform_once(): select(): %s.\n",
|
lprintf(debug, "curl_multi_perform_once(): select(): %s.\n",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -221,7 +221,7 @@ int curl_multi_perform_once(void)
|
|||||||
curl_process_msgs(curl_msg, n_running_curl, n_mesgs);
|
curl_process_msgs(curl_msg, n_running_curl, n_mesgs);
|
||||||
}
|
}
|
||||||
#ifdef NETWORK_LOCK_DEBUG
|
#ifdef NETWORK_LOCK_DEBUG
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"curl_multi_perform_once(): thread %lu: unlocking transfer_lock;\n",
|
"curl_multi_perform_once(): thread %lu: unlocking transfer_lock;\n",
|
||||||
pthread_self());
|
pthread_self());
|
||||||
#endif
|
#endif
|
||||||
@ -233,14 +233,14 @@ void NetworkSystem_init(void)
|
|||||||
{
|
{
|
||||||
/* ------- Global related ----------*/
|
/* ------- Global related ----------*/
|
||||||
if (curl_global_init(CURL_GLOBAL_ALL)) {
|
if (curl_global_init(CURL_GLOBAL_ALL)) {
|
||||||
lprintf(ldebug, "network_init(): curl_global_init() failed!\n");
|
lprintf(debug, "network_init(): curl_global_init() failed!\n");
|
||||||
exit_failure();
|
exit_failure();
|
||||||
}
|
}
|
||||||
|
|
||||||
/* -------- Share related ----------*/
|
/* -------- Share related ----------*/
|
||||||
CURL_SHARE = curl_share_init();
|
CURL_SHARE = curl_share_init();
|
||||||
if (!(CURL_SHARE)) {
|
if (!(CURL_SHARE)) {
|
||||||
lprintf(ldebug, "network_init(): curl_share_init() failed!\n");
|
lprintf(debug, "network_init(): curl_share_init() failed!\n");
|
||||||
exit_failure();
|
exit_failure();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -249,7 +249,7 @@ void NetworkSystem_init(void)
|
|||||||
curl_share_setopt(CURL_SHARE, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
|
curl_share_setopt(CURL_SHARE, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
|
||||||
|
|
||||||
if (pthread_mutex_init(&curl_lock, NULL) != 0) {
|
if (pthread_mutex_init(&curl_lock, NULL) != 0) {
|
||||||
lprintf(ldebug, "network_init(): curl_lock initialisation failed!\n");
|
lprintf(debug, "network_init(): curl_lock initialisation failed!\n");
|
||||||
exit_failure();
|
exit_failure();
|
||||||
}
|
}
|
||||||
curl_share_setopt(CURL_SHARE, CURLSHOPT_LOCKFUNC, curl_callback_lock);
|
curl_share_setopt(CURL_SHARE, CURLSHOPT_LOCKFUNC, curl_callback_lock);
|
||||||
@ -258,7 +258,7 @@ void NetworkSystem_init(void)
|
|||||||
/* ------------- Multi related -----------*/
|
/* ------------- Multi related -----------*/
|
||||||
curl_multi = curl_multi_init();
|
curl_multi = curl_multi_init();
|
||||||
if (!curl_multi) {
|
if (!curl_multi) {
|
||||||
lprintf(ldebug, "network_init(): curl_multi_init() failed!\n");
|
lprintf(debug, "network_init(): curl_multi_init() failed!\n");
|
||||||
exit_failure();
|
exit_failure();
|
||||||
}
|
}
|
||||||
curl_multi_setopt(curl_multi, CURLMOPT_MAX_TOTAL_CONNECTIONS,
|
curl_multi_setopt(curl_multi, CURLMOPT_MAX_TOTAL_CONNECTIONS,
|
||||||
@ -268,7 +268,7 @@ void NetworkSystem_init(void)
|
|||||||
|
|
||||||
/* ------------ Initialise locks ---------*/
|
/* ------------ Initialise locks ---------*/
|
||||||
if (pthread_mutex_init(&transfer_lock, NULL)) {
|
if (pthread_mutex_init(&transfer_lock, NULL)) {
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"network_init(): transfer_lock initialisation failed!\n");
|
"network_init(): transfer_lock initialisation failed!\n");
|
||||||
exit_failure();
|
exit_failure();
|
||||||
}
|
}
|
||||||
@ -291,21 +291,21 @@ void transfer_blocking(CURL *curl)
|
|||||||
transfer.transferring = 1;
|
transfer.transferring = 1;
|
||||||
curl_easy_setopt(curl, CURLOPT_PRIVATE, &transfer);
|
curl_easy_setopt(curl, CURLOPT_PRIVATE, &transfer);
|
||||||
#ifdef NETWORK_LOCK_DEBUG
|
#ifdef NETWORK_LOCK_DEBUG
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"transfer_blocking(): thread %lu: locking transfer_lock;\n",
|
"transfer_blocking(): thread %lu: locking transfer_lock;\n",
|
||||||
pthread_self());
|
pthread_self());
|
||||||
#endif
|
#endif
|
||||||
PTHREAD_MUTEX_LOCK(&transfer_lock);
|
PTHREAD_MUTEX_LOCK(&transfer_lock);
|
||||||
CURLMcode res = curl_multi_add_handle(curl_multi, curl);
|
CURLMcode res = curl_multi_add_handle(curl_multi, curl);
|
||||||
#ifdef NETWORK_LOCK_DEBUG
|
#ifdef NETWORK_LOCK_DEBUG
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"transfer_blocking(): thread %lu: unlocking transfer_lock;\n",
|
"transfer_blocking(): thread %lu: unlocking transfer_lock;\n",
|
||||||
pthread_self());
|
pthread_self());
|
||||||
#endif
|
#endif
|
||||||
PTHREAD_MUTEX_UNLOCK(&transfer_lock);
|
PTHREAD_MUTEX_UNLOCK(&transfer_lock);
|
||||||
|
|
||||||
if(res > 0) {
|
if(res > 0) {
|
||||||
lprintf(ldebug, "transfer_blocking(): %d, %s\n",
|
lprintf(debug, "transfer_blocking(): %d, %s\n",
|
||||||
res, curl_multi_strerror(res));
|
res, curl_multi_strerror(res));
|
||||||
exit_failure();
|
exit_failure();
|
||||||
}
|
}
|
||||||
@ -318,21 +318,21 @@ void transfer_blocking(CURL *curl)
|
|||||||
void transfer_nonblocking(CURL *curl)
|
void transfer_nonblocking(CURL *curl)
|
||||||
{
|
{
|
||||||
#ifdef NETWORK_LOCK_DEBUG
|
#ifdef NETWORK_LOCK_DEBUG
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"transfer_nonblocking(): thread %lu: locking transfer_lock;\n",
|
"transfer_nonblocking(): thread %lu: locking transfer_lock;\n",
|
||||||
pthread_self());
|
pthread_self());
|
||||||
#endif
|
#endif
|
||||||
PTHREAD_MUTEX_LOCK(&transfer_lock);
|
PTHREAD_MUTEX_LOCK(&transfer_lock);
|
||||||
CURLMcode res = curl_multi_add_handle(curl_multi, curl);
|
CURLMcode res = curl_multi_add_handle(curl_multi, curl);
|
||||||
#ifdef NETWORK_LOCK_DEBUG
|
#ifdef NETWORK_LOCK_DEBUG
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"transfer_nonblocking(): thread %lu: unlocking transfer_lock;\n",
|
"transfer_nonblocking(): thread %lu: unlocking transfer_lock;\n",
|
||||||
pthread_self());
|
pthread_self());
|
||||||
#endif
|
#endif
|
||||||
PTHREAD_MUTEX_UNLOCK(&transfer_lock);
|
PTHREAD_MUTEX_UNLOCK(&transfer_lock);
|
||||||
|
|
||||||
if(res > 0) {
|
if(res > 0) {
|
||||||
lprintf(ldebug, "transfer_nonblocking(): %s\n",
|
lprintf(debug, "transfer_nonblocking(): %s\n",
|
||||||
curl_multi_strerror(res));
|
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);
|
mem->data = realloc(mem->data, mem->size + realsize + 1);
|
||||||
if(!mem->data) {
|
if(!mem->data) {
|
||||||
/* out of memory! */
|
/* out of memory! */
|
||||||
lprintf(ldebug, "write_memory_callback(): realloc failure!\n");
|
lprintf(debug, "write_memory_callback(): realloc failure!\n");
|
||||||
exit_failure();
|
exit_failure();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
12
src/sonic.c
12
src/sonic.c
@ -167,9 +167,9 @@ static void XMLCALL XML_parser_general(void *data, const char *elem,
|
|||||||
{
|
{
|
||||||
/* Error checking */
|
/* Error checking */
|
||||||
if (!strcmp(elem, "error")) {
|
if (!strcmp(elem, "error")) {
|
||||||
lprintf(ldebug, "XML_parser_general() error:\n");
|
lprintf(debug, "XML_parser_general() error:\n");
|
||||||
for (int i = 0; attr[i]; i += 2) {
|
for (int i = 0; attr[i]; i += 2) {
|
||||||
lprintf(ldebug, "%s: %s\n", attr[i], attr[i+1]);
|
lprintf(debug, "%s: %s\n", attr[i], attr[i+1]);
|
||||||
}
|
}
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@ -322,7 +322,7 @@ static LinkTable *sonic_url_to_LinkTable(const char *url,
|
|||||||
XML_SetStartElementHandler(parser, handler);
|
XML_SetStartElementHandler(parser, handler);
|
||||||
|
|
||||||
if (XML_Parse(parser, xml.data, xml.size, 1) == XML_STATUS_ERROR) {
|
if (XML_Parse(parser, xml.data, xml.size, 1) == XML_STATUS_ERROR) {
|
||||||
lprintf(ldebug,
|
lprintf(debug,
|
||||||
"sonic_XML_to_LinkTable(): Parse error at line %lu: %s\n",
|
"sonic_XML_to_LinkTable(): Parse error at line %lu: %s\n",
|
||||||
XML_GetCurrentLineNumber(parser),
|
XML_GetCurrentLineNumber(parser),
|
||||||
XML_ErrorString(XML_GetErrorCode(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)
|
const char **attr)
|
||||||
{
|
{
|
||||||
if (!strcmp(elem, "error")) {
|
if (!strcmp(elem, "error")) {
|
||||||
lprintf(ldebug, "XML_parser_id3_root() error:\n");
|
lprintf(debug, "XML_parser_id3_root() error:\n");
|
||||||
for (int i = 0; attr[i]; i += 2) {
|
for (int i = 0; attr[i]; i += 2) {
|
||||||
lprintf(ldebug, "%s: %s\n", attr[i], attr[i+1]);
|
lprintf(debug, "%s: %s\n", attr[i], attr[i+1]);
|
||||||
}
|
}
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@ -454,7 +454,7 @@ LinkTable *sonic_LinkTable_new_id3(int depth, const char *id)
|
|||||||
/*
|
/*
|
||||||
* We shouldn't reach here.
|
* We shouldn't reach here.
|
||||||
*/
|
*/
|
||||||
lprintf(ldebug, "sonic_LinkTable_new_id3(): case %d.\n", depth);
|
lprintf(debug, "sonic_LinkTable_new_id3(): case %d.\n", depth);
|
||||||
exit_failure();
|
exit_failure();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
10
src/util.c
10
src/util.c
@ -56,7 +56,7 @@ void PTHREAD_MUTEX_UNLOCK(pthread_mutex_t *x)
|
|||||||
int i;
|
int i;
|
||||||
i = pthread_mutex_unlock(x);
|
i = pthread_mutex_unlock(x);
|
||||||
if (i) {
|
if (i) {
|
||||||
lprintf(ldebug, "thread %lu: pthread_mutex_unlock() failed, %d, %s\n",
|
lprintf(debug, "thread %lu: pthread_mutex_unlock() failed, %d, %s\n",
|
||||||
pthread_self(), i, strerror(i));
|
pthread_self(), i, strerror(i));
|
||||||
exit_failure();
|
exit_failure();
|
||||||
}
|
}
|
||||||
@ -67,7 +67,7 @@ void PTHREAD_MUTEX_LOCK(pthread_mutex_t *x)
|
|||||||
int i;
|
int i;
|
||||||
i = pthread_mutex_lock(x);
|
i = pthread_mutex_lock(x);
|
||||||
if (i) {
|
if (i) {
|
||||||
lprintf(ldebug, "thread %lu: pthread_mutex_lock() failed, %d, %s\n",
|
lprintf(debug, "thread %lu: pthread_mutex_lock() failed, %d, %s\n",
|
||||||
pthread_self(), i, strerror(i));
|
pthread_self(), i, strerror(i));
|
||||||
exit_failure();
|
exit_failure();
|
||||||
}
|
}
|
||||||
@ -79,8 +79,8 @@ void exit_failure(void)
|
|||||||
void *buffer[BT_BUF_SIZE];
|
void *buffer[BT_BUF_SIZE];
|
||||||
|
|
||||||
nptrs = backtrace(buffer, BT_BUF_SIZE);
|
nptrs = backtrace(buffer, BT_BUF_SIZE);
|
||||||
lprintf(ldebug, "\nOops! HTTPDirFS crashed! :(\n");
|
lprintf(debug, "\nOops! HTTPDirFS crashed! :(\n");
|
||||||
lprintf(ldebug, "backtrace() returned the following %d addresses:\n",
|
lprintf(debug, "backtrace() returned the following %d addresses:\n",
|
||||||
nptrs);
|
nptrs);
|
||||||
backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO);
|
backtrace_symbols_fd(buffer, nptrs, STDERR_FILENO);
|
||||||
|
|
||||||
@ -132,7 +132,7 @@ void *CALLOC(size_t nmemb, size_t size)
|
|||||||
{
|
{
|
||||||
void *ptr = calloc(nmemb, size);
|
void *ptr = calloc(nmemb, size);
|
||||||
if (!ptr) {
|
if (!ptr) {
|
||||||
lprintf(ldebug, "calloc() failed, %s!\n", strerror(errno));
|
lprintf(debug, "calloc() failed, %s!\n", strerror(errno));
|
||||||
exit_failure();
|
exit_failure();
|
||||||
}
|
}
|
||||||
return ptr;
|
return ptr;
|
||||||
|
Loading…
Reference in New Issue
Block a user