diff --git a/bwRead.c b/bwRead.c index 28e6431..2a1b6f3 100644 --- a/bwRead.c +++ b/bwRead.c @@ -157,6 +157,9 @@ static void bwHdrRead(bigWigFile_t *bw) { if(bwRead((void*) &(bw->hdr->sumSquared), sizeof(uint64_t), 1, bw) != 1) goto error; } + //In case of uncompressed remote files, let the IO functions know to request larger chunks + bw->URL->isCompressed = (bw->hdr->bufSize > 0)?1:0; + return; error: diff --git a/bwStats.c b/bwStats.c index d0bc100..cfca02e 100644 --- a/bwStats.c +++ b/bwStats.c @@ -92,6 +92,7 @@ static struct vals_t *getVals(bigWigFile_t *fp, bwOverlapBlock_t *o, int i, uint if(rv != Z_OK) goto error; } else { buf = compBuf; + sz = o->size[i]; } p = buf; @@ -123,12 +124,12 @@ static struct vals_t *getVals(bigWigFile_t *fp, bwOverlapBlock_t *o, int i, uint free(v); free(buf); - free(compBuf); + if(compressed) free(compBuf); return vals; error: if(buf) free(buf); - if(compBuf) free(compBuf); + if(compBuf && compressed) free(compBuf); if(v) free(v); destroyVals_t(vals); return NULL; diff --git a/io.c b/io.c index e6d9c66..5d59eff 100644 --- a/io.c +++ b/io.c @@ -7,6 +7,7 @@ #include #include "io.h" #include +#include size_t GLOBAL_DEFAULTBUFFERSIZE; @@ -37,15 +38,17 @@ CURLcode urlFetchData(URL_t *URL, unsigned long bufSize) { } rv = curl_easy_perform(URL->x.curl); + errno = 0; //Sometimes curl_easy_perform leaves a random errno remnant return rv; } //Read data into a buffer, ideally from a buffer already in memory //The loop is likely no longer needed. size_t url_fread(void *obuf, size_t obufSize, URL_t *URL) { - size_t remaining = obufSize; + size_t remaining = obufSize, fetchSize; void *p = obuf; CURLcode rv; + while(remaining) { if(!URL->bufLen) { rv = urlFetchData(URL, URL->bufSize); @@ -59,7 +62,12 @@ size_t url_fread(void *obuf, size_t obufSize, URL_t *URL) { p += URL->bufLen - URL->bufPos; remaining -= URL->bufLen - URL->bufPos; if(remaining) { - rv = urlFetchData(URL, (remainingbufSize)?remaining:URL->bufSize); + if(!URL->isCompressed) { + fetchSize = URL->bufSize; + } else { + fetchSize = (remainingbufSize)?remaining:URL->bufSize; + } + rv = urlFetchData(URL, fetchSize); if(rv != CURLE_OK) { fprintf(stderr, "[url_fread] urlFetchData (B) returned %s\n", curl_easy_strerror(rv)); return 0; @@ -227,6 +235,7 @@ URL_t *urlOpen(char *fname, CURLcode (*callBack)(CURL*), const char *mode) { } } code = curl_easy_perform(URL->x.curl); + errno = 0; //Sometimes curl_easy_perform leaves a random errno remnant if(code != CURLE_OK) { fprintf(stderr, "[urlOpen] curl_easy_perform received an error: %s\n", curl_easy_strerror(code)); goto error; diff --git a/io.h b/io.h index 53d0476..ed415b8 100644 --- a/io.h +++ b/io.h @@ -42,6 +42,7 @@ typedef struct { size_t bufSize; /**