fixed an issue caused by a Mac/Darwin bug

On Mac/Darwin, it is not possible to read >2GB data with one fread().
This commit is contained in:
Heng Li 2013-04-09 15:17:04 -04:00
parent d7ca0885eb
commit d64eaa851d
2 changed files with 15 additions and 3 deletions

16
bwt.c
View File

@ -372,6 +372,18 @@ void bwt_dump_sa(const char *fn, const bwt_t *bwt)
fclose(fp);
}
static bwtint_t fread_fix(FILE *fp, bwtint_t size, void *a)
{ // Mac/Darwin has a bug when reading data longer than 2GB. This function fixes this issue by reading data in small chunks
const int bufsize = 0x1000000; // 16M block
bwtint_t offset = 0;
while (size) {
int x = bufsize < size? bufsize : size;
if ((x = fread(a + offset, 1, x, fp)) == 0) break;
size -= x; offset += x;
}
return offset;
}
void bwt_restore_sa(const char *fn, bwt_t *bwt)
{
char skipped[256];
@ -390,7 +402,7 @@ void bwt_restore_sa(const char *fn, bwt_t *bwt)
bwt->sa = (bwtint_t*)calloc(bwt->n_sa, sizeof(bwtint_t));
bwt->sa[0] = -1;
fread(bwt->sa + 1, sizeof(bwtint_t), bwt->n_sa - 1, fp);
fread_fix(fp, sizeof(bwtint_t) * (bwt->n_sa - 1), bwt->sa + 1);
fclose(fp);
}
@ -407,7 +419,7 @@ bwt_t *bwt_restore_bwt(const char *fn)
fseek(fp, 0, SEEK_SET);
fread(&bwt->primary, sizeof(bwtint_t), 1, fp);
fread(bwt->L2+1, sizeof(bwtint_t), 4, fp);
fread(bwt->bwt, 4, bwt->bwt_size, fp);
fread_fix(fp, bwt->bwt_size<<2, bwt->bwt);
bwt->seq_len = bwt->L2[4];
fclose(fp);
bwt_gen_cnt_table(bwt);

2
main.c
View File

@ -3,7 +3,7 @@
#include "utils.h"
#ifndef PACKAGE_VERSION
#define PACKAGE_VERSION "0.7.3-r371-beta"
#define PACKAGE_VERSION "0.7.3-r372-beta"
#endif
int bwa_fa2pac(int argc, char *argv[]);