Skip to content

Commit

Permalink
Merge pull request #23 from dpryan79/fix20
Browse files Browse the repository at this point in the history
Fix #20 and #22
  • Loading branch information
dpryan79 authored Mar 17, 2017
2 parents 4bd59fb + 33d436b commit 266865e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
3 changes: 3 additions & 0 deletions bwRead.c
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions bwStats.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
13 changes: 11 additions & 2 deletions io.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <unistd.h>
#include "io.h"
#include <inttypes.h>
#include <errno.h>

size_t GLOBAL_DEFAULTBUFFERSIZE;

Expand Down Expand Up @@ -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);
Expand All @@ -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, (remaining<URL->bufSize)?remaining:URL->bufSize);
if(!URL->isCompressed) {
fetchSize = URL->bufSize;
} else {
fetchSize = (remaining<URL->bufSize)?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;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions io.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ typedef struct {
size_t bufSize; /**<The size of the buffer.*/
size_t bufLen; /**<The actual size of the buffer used.*/
enum bigWigFile_type_enum type; /**<The connection type*/
int isCompressed; /**<1 if the file is compressed, otherwise 0*/
char *fname; /**<Only needed for remote connections. The original URL/filename requested, since we need to make multiple connections.*/
} URL_t;

Expand Down

0 comments on commit 266865e

Please sign in to comment.