Skip to content

Commit

Permalink
Code quality improvements
Browse files Browse the repository at this point in the history
- Improve error messages
- Enable -Wshadow
- Removed the extra 1 byte workaround (I am not sure why this
was needed in the first place.)
  • Loading branch information
fangfufu committed Nov 25, 2024
1 parent 614b9d8 commit 6cd006c
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ srcs = [

c_args = [
'-Wstrict-prototypes',
'-Wshadow',
'-pthread',
'-D_GNU_SOURCE',
'-DVERSION="' + meson.project_version() + '"'
Expand Down
8 changes: 5 additions & 3 deletions src/cache.c
Original file line number Diff line number Diff line change
Expand Up @@ -945,15 +945,16 @@ cf->content_length: %ld, Data_size(fn): %ld.\n", fn, cf->content_length,
void Cache_close(Cache *cf)
{
lprintf(cache_lock_debug,
"thread %x: locking cf_lock;\n", pthread_self());
"thread %x: locking cf_lock: %s\n", pthread_self(), cf->path);
PTHREAD_MUTEX_LOCK(&cf_lock);

cf->cache_opened--;

if (cf->cache_opened > 0) {

lprintf(cache_lock_debug,
"thread %x: unlocking cf_lock;\n", pthread_self());
"thread %x: unlocking cf_lock: %s, cache_opened: %d\n",
pthread_self(), cf->path, cf->cache_opened);
PTHREAD_MUTEX_UNLOCK(&cf_lock);
return;
}
Expand All @@ -973,7 +974,8 @@ void Cache_close(Cache *cf)
cf->link->cache_ptr = NULL;

lprintf(cache_lock_debug,
"thread %x: unlocking cf_lock;\n", pthread_self());
"thread %x: unlocking cf_lock, cache closed: %s\n", pthread_self(),
cf->path);
PTHREAD_MUTEX_UNLOCK(&cf_lock);
Cache_free(cf);
}
Expand Down
21 changes: 10 additions & 11 deletions src/link.c
Original file line number Diff line number Diff line change
Expand Up @@ -1077,31 +1077,30 @@ long Link_download(Link *link, char *output_buf, size_t req_size, off_t offset)
header.curr_size = 0;
header.data = NULL;

if (offset + req_size > link->content_length) {
size_t request_end = offset + req_size;
if (request_end > link->content_length) {
lprintf(info,
"requested size larger than remaining size, req_size: %lu, recv: %ld, content-length: %ld\n",
req_size, recv, link->content_length);
"requested size larger than remaining size, request_end: \
%lu, content-length: %ld\n", request_end, link->content_length);
req_size = link->content_length - offset;
}

CURL *curl = Link_download_curl_setup(link, req_size, offset, &header, &ts);

transfer_blocking(curl);

curl_off_t recv = Link_download_cleanup(curl, &header);
curl_off_t recv_sz = Link_download_cleanup(curl, &header);

/* The extra 1 byte is probably for '\0' */
if (recv - 1 == (long int) req_size) {
recv--;
} else {
if (recv_sz != (long int) req_size)
{
lprintf(error, "req_size != recv, req_size: %lu, recv: %ld\n",
req_size, recv);
req_size, recv_sz);
}

memmove(output_buf, ts.data, recv);
memmove(output_buf, ts.data, recv_sz);
FREE(ts.data);

return recv;
return recv_sz;
}

long path_download(const char *path, char *output_buf, size_t req_size,
Expand Down

0 comments on commit 6cd006c

Please sign in to comment.