From 99e7f5f925d37aa8d615295a668ff10a09b39fed Mon Sep 17 00:00:00 2001 From: Devon Ryan Date: Wed, 6 Jan 2016 00:35:42 +0100 Subject: [PATCH] Fix remote access, which doesn't always work since urlFetchdata was apparently broken --- bigWig.h | 2 +- io.c | 25 ++++++++++++++++++------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/bigWig.h b/bigWig.h index 7193364..1e4e433 100644 --- a/bigWig.h +++ b/bigWig.h @@ -41,7 +41,7 @@ /*! * The library version number */ -#define LIBBIGWIG_VERSION 0.1.0 +#define LIBBIGWIG_VERSION 0.1.1 /*! * The magic number of a bigWig file. diff --git a/io.c b/io.c index 7acf162..4985779 100644 --- a/io.c +++ b/io.c @@ -18,14 +18,23 @@ uint64_t getContentLength(URL_t *URL) { } //Fill the buffer, note that URL may be left in an unusable state on error! -CURLcode urlFetchData(URL_t *URL) { +CURLcode urlFetchData(URL_t *URL, unsigned long bufSize) { CURLcode rv; + char range[1024]; size_t len; if(URL->filePos != -1) URL->filePos += URL->bufLen; else URL->filePos = 0; - rv = curl_easy_recv(URL->x.curl, URL->memBuf+URL->bufLen, URL->bufSize, &len); + URL->bufPos = URL->bufLen = 0; //Otherwise, we can't copy anything into the buffer! + sprintf(range,"%lu-%lu", URL->filePos, URL->filePos+bufSize-1); + rv = curl_easy_setopt(URL->x.curl, CURLOPT_RANGE, range); + if(rv != CURLE_OK) { + fprintf(stderr, "[urlFetchData] Couldn't set the range (%s)\n", range); + return rv; + } + + rv = curl_easy_perform(URL->x.curl); return rv; } @@ -37,7 +46,7 @@ size_t url_fread(void *obuf, size_t obufSize, URL_t *URL) { CURLcode rv; while(remaining) { if(!URL->bufLen) { - rv = urlFetchData(URL); + rv = urlFetchData(URL, URL->bufSize); if(rv != CURLE_OK) { fprintf(stderr, "[url_fread] urlFetchData (A) returned %s\n", curl_easy_strerror(rv)); return 0; @@ -47,10 +56,12 @@ size_t url_fread(void *obuf, size_t obufSize, URL_t *URL) { if(!p) return 0; p += URL->bufLen - URL->bufPos; remaining -= URL->bufLen - URL->bufPos; - rv = urlFetchData(URL); - if(rv != CURLE_OK) { - fprintf(stderr, "[url_fread] urlFetchData (B) returned %s\n", curl_easy_strerror(rv)); - return 0; + if(remaining) { + rv = urlFetchData(URL, (remainingbufSize)?remaining:URL->bufSize); + if(rv != CURLE_OK) { + fprintf(stderr, "[url_fread] urlFetchData (B) returned %s\n", curl_easy_strerror(rv)); + return 0; + } } } else { p = memcpy(p, URL->memBuf+URL->bufPos, remaining);