Skip to content

Commit

Permalink
Merge pull request #4 from dpryan79/fix_Remote_Access
Browse files Browse the repository at this point in the history
Fix remote access, which doesn't always work since urlFetchdata was a…
  • Loading branch information
dpryan79 committed Jan 5, 2016
2 parents eada708 + 99e7f5f commit df73e52
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion bigWig.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
25 changes: 18 additions & 7 deletions io.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
Expand All @@ -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, (remaining<URL->bufSize)?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);
Expand Down

0 comments on commit df73e52

Please sign in to comment.