X-Git-Url: http://woldlab.caltech.edu/gitweb/?a=blobdiff_plain;f=razf.c;h=f67447c33bde6478787a5f95c9d60a14061bb6a3;hb=17d79b44a564aa4d5853bbc0b5f97ec2cefc927b;hp=a5e8f5161a8d0780a820556e0e6640fd9762bb12;hpb=d363084f0412f3bcdeb0304aeb0974c9a10c7649;p=samtools.git diff --git a/razf.c b/razf.c index a5e8f51..f67447c 100644 --- a/razf.c +++ b/razf.c @@ -38,6 +38,7 @@ #include #include "razf.h" + #if ZLIB_VERNUM < 0x1221 struct _gz_header_s { int text; @@ -78,10 +79,17 @@ static inline int is_big_endian(){ #ifndef _RZ_READONLY static void add_zindex(RAZF *rz, int64_t in, int64_t out){ + uint32_t *cores; + int64_t *bores; + if(rz->index->size == rz->index->cap){ rz->index->cap = rz->index->cap * 1.5 + 2; - rz->index->cell_offsets = realloc(rz->index->cell_offsets, sizeof(int) * rz->index->cap); - rz->index->bin_offsets = realloc(rz->index->bin_offsets, sizeof(int64_t) * (rz->index->cap/RZ_BIN_SIZE + 1)); + cores = realloc(rz->index->cell_offsets, sizeof(int) * rz->index->cap); + bores = realloc(rz->index->bin_offsets, sizeof(int64_t) * (rz->index->cap/RZ_BIN_SIZE + 1)); + if ((cores == NULL) || (bores == NULL)) { + fprintf(stderr, "[%s] failure to allocate space for new zindex.\n", __func__); + abort(); + } } if(rz->index->size % RZ_BIN_SIZE == 0) rz->index->bin_offsets[rz->index->size / RZ_BIN_SIZE] = out; rz->index->cell_offsets[rz->index->size] = out - rz->index->bin_offsets[rz->index->size / RZ_BIN_SIZE]; @@ -89,38 +97,95 @@ static void add_zindex(RAZF *rz, int64_t in, int64_t out){ } static void save_zindex(RAZF *rz, int fd){ + size_t count; int32_t i, v32; int is_be; is_be = is_big_endian(); - if(is_be) write(fd, &rz->index->size, sizeof(int)); + if(is_be) { + if (write(fd, &rz->index->size, sizeof(int)) < 0) { + fprintf(stderr, "[%s] failure to write zindex size.\n", __func__); + abort(); + } + } else { v32 = byte_swap_4((uint32_t)rz->index->size); - write(fd, &v32, sizeof(uint32_t)); + if (write(fd, &v32, sizeof(uint32_t)) < 0) { + fprintf(stderr, "[%s] failure to write zindex size.\n", __func__); + abort(); + } } v32 = rz->index->size / RZ_BIN_SIZE + 1; if(!is_be){ for(i=0;iindex->bin_offsets[i] = byte_swap_8((uint64_t)rz->index->bin_offsets[i]); for(i=0;iindex->size;i++) rz->index->cell_offsets[i] = byte_swap_4((uint32_t)rz->index->cell_offsets[i]); } - write(fd, rz->index->bin_offsets, sizeof(int64_t) * v32); - write(fd, rz->index->cell_offsets, sizeof(int32_t) * rz->index->size); + count = sizeof(int64_t) * v32; + if (write(fd, rz->index->bin_offsets, count) < 0) { + fprintf(stderr, "[%s] failure to write zindex bin_offsets.\n", __func__); + abort(); + } + + count = sizeof(int32_t) * rz->index->size; + if (write(fd, rz->index->cell_offsets, count) < 0) { + fprintf(stderr, "[%s] failure to write zindex cell_offsets.\n", __func__); + abort(); + } } #endif +#ifdef _USE_KNETFILE +static void load_zindex(RAZF *rz, knetFile *fp){ +#else static void load_zindex(RAZF *rz, int fd){ +#endif int32_t i, v32; int is_be; + size_t count; if(!rz->load_index) return; - if(rz->index == NULL) rz->index = malloc(sizeof(ZBlockIndex)); + if(rz->index == NULL) { + if ((rz->index = malloc(sizeof(ZBlockIndex))) == NULL) { + fprintf(stderr, "[%s] failure to allocate index.\n", __func__); + abort(); + } + } is_be = is_big_endian(); - read(fd, &rz->index->size, sizeof(int)); +#ifdef _USE_KNETFILE + if (knet_read(fp, &rz->index->size, sizeof(int)) < 0) { +#else + if (read(fd, &rz->index->size, sizeof(int)) < 0) { +#endif + fprintf(stderr, "[%s] failure to read zindex size.\n", __func__); + abort(); + } if(!is_be) rz->index->size = byte_swap_4((uint32_t)rz->index->size); rz->index->cap = rz->index->size; v32 = rz->index->size / RZ_BIN_SIZE + 1; - rz->index->bin_offsets = malloc(sizeof(int64_t) * v32); - read(fd, rz->index->bin_offsets, sizeof(int64_t) * v32); - rz->index->cell_offsets = malloc(sizeof(int) * rz->index->size); - read(fd, rz->index->cell_offsets, sizeof(int) * rz->index->size); + count = sizeof(int64_t) * v32; + if ((rz->index->bin_offsets = malloc(count)) == NULL) { + fprintf(stderr, "[%s] failure to allocate bin_offsets array.\n", __func__); + abort(); + } +#ifdef _USE_KNETFILE + if (knet_read(fp, rz->index->bin_offsets, count) < 0) { +#else + if (read(fd, rz->index->bin_offsets, count) < 0) { +#endif + fprintf(stderr, "[%s] failure to read bin_offsets.\n", __func__); + abort(); + } + count = sizeof(int) * rz->index->size; + if ((rz->index->cell_offsets = malloc(count)) == NULL) { + fprintf(stderr, "[%s] failure to allocate cell_offsets array.\n", __func__); + abort(); + } +#ifdef _USE_KNETFILE + if (knet_read(fp, rz->index->cell_offsets, count) < count) { +#else + if (read(fd, rz->index->cell_offsets, count) < count) { +#endif + fprintf(stderr, "[%s] failure to read cell_offsets.\n", __func__); + abort(); + } if(!is_be){ for(i=0;iindex->bin_offsets[i] = byte_swap_8((uint64_t)rz->index->bin_offsets[i]); for(i=0;iindex->size;i++) rz->index->cell_offsets[i] = byte_swap_4((uint32_t)rz->index->cell_offsets[i]); @@ -139,21 +204,68 @@ static RAZF* razf_open_w(int fd){ #ifdef _WIN32 setmode(fd, O_BINARY); #endif - rz = calloc(1, sizeof(RAZF)); + if ((rz = calloc(1, sizeof(RAZF))) == NULL) { + fprintf(stderr, "[%s] failure to allocate RAZF structure.\n", __func__); + return NULL; + } rz->mode = 'w'; +#ifdef _USE_KNETFILE + rz->x.fpw = fd; +#else rz->filedes = fd; - rz->stream = calloc(sizeof(z_stream), 1); - rz->inbuf = malloc(RZ_BUFFER_SIZE); - rz->outbuf = malloc(RZ_BUFFER_SIZE); - rz->index = calloc(sizeof(ZBlockIndex), 1); +#endif + if ((rz->stream = calloc(sizeof(z_stream), 1)) == NULL) { + fprintf(stderr, "[%s] failure to allocate stream buffer.\n", __func__); + free(rz); + return NULL; + } + if ((rz->inbuf = malloc(RZ_BUFFER_SIZE)) == NULL) { + fprintf(stderr, "[%s] failure to allocate input buffer.\n", __func__); + free(rz->stream); + free(rz); + return NULL; + } + if ((rz->outbuf = malloc(RZ_BUFFER_SIZE)) == NULL) { + fprintf(stderr, "[%s] failure to allocate output buffer.\n", __func__); + free(rz->stream); + free(rz->inbuf); + free(rz); + return NULL; + } + if ((rz->index = calloc(sizeof(ZBlockIndex), 1)) == NULL) { + fprintf(stderr, "[%s] failure to allocate index.\n", __func__); + free(rz->stream); + free(rz->inbuf); + free(rz->outbuf); + free(rz); + return NULL; + } + deflateInit2(rz->stream, RZ_COMPRESS_LEVEL, Z_DEFLATED, WINDOW_BITS + 16, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY); rz->stream->avail_out = RZ_BUFFER_SIZE; rz->stream->next_out = rz->outbuf; - rz->header = calloc(sizeof(gz_header), 1); + if ((rz->header = calloc(sizeof(gz_header), 1)) == NULL) { + fprintf(stderr, "[%s] failure to allocate header buffer.\n", __func__); + free(rz->stream); + free(rz->inbuf); + free(rz->outbuf); + free(rz->index); + free(rz); + return NULL; + } rz->header->os = 0x03; //Unix rz->header->text = 0; rz->header->time = 0; - rz->header->extra = malloc(7); + if ((rz->header->extra = malloc(7)) == NULL) { + fprintf(stderr, "[%s] failure to allocate header buffer.\n", __func__); + free(rz->stream); + free(rz->inbuf); + free(rz->outbuf); + free(rz->index); + free(rz->header); + free(rz); + return NULL; + } strncpy((char*)rz->header->extra, "RAZF", 4); rz->header->extra[4] = 1; // obsolete field // block size = RZ_BLOCK_SIZE, Big-Endian @@ -169,6 +281,7 @@ static RAZF* razf_open_w(int fd){ static void _razf_write(RAZF* rz, const void *data, int size){ int tout; + size_t count; rz->stream->avail_in = size; rz->stream->next_in = (void*)data; while(1){ @@ -176,7 +289,15 @@ static void _razf_write(RAZF* rz, const void *data, int size){ deflate(rz->stream, Z_NO_FLUSH); rz->out += tout - rz->stream->avail_out; if(rz->stream->avail_out) break; - write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out); + count = RZ_BUFFER_SIZE - rz->stream->avail_out; +#ifdef _USE_KNETFILE + if (write(rz->x.fpw, rz->outbuf, count) < 0) { +#else + if (write(rz->filedes, rz->outbuf, count) < 0) { +#endif + fprintf(stderr, "[%s] failed to write output buffer.\n", __func__); + abort(); + } rz->stream->avail_out = RZ_BUFFER_SIZE; rz->stream->next_out = rz->outbuf; if(rz->stream->avail_in == 0) break; @@ -186,13 +307,22 @@ static void _razf_write(RAZF* rz, const void *data, int size){ } static void razf_flush(RAZF *rz){ + size_t count; uint32_t tout; if(rz->buf_len){ _razf_write(rz, rz->inbuf, rz->buf_len); rz->buf_off = rz->buf_len = 0; } if(rz->stream->avail_out){ - write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out); + count = RZ_BUFFER_SIZE - rz->stream->avail_out; +#ifdef _USE_KNETFILE + if (write(rz->x.fpw, rz->outbuf, count) < 0) { +#else + if (write(rz->filedes, rz->outbuf, count) < 0) { +#endif + fprintf(stderr, "[%s] failed to flush output buffer.\n", __func__); + abort(); + } rz->stream->avail_out = RZ_BUFFER_SIZE; rz->stream->next_out = rz->outbuf; } @@ -201,7 +331,15 @@ static void razf_flush(RAZF *rz){ deflate(rz->stream, Z_FULL_FLUSH); rz->out += tout - rz->stream->avail_out; if(rz->stream->avail_out == 0){ - write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out); + count = RZ_BUFFER_SIZE - rz->stream->avail_out; +#ifdef _USE_KNETFILE + if (write(rz->x.fpw, rz->outbuf, count) < 0) { +#else + if (write(rz->filedes, rz->outbuf, count) < 0) { +#endif + fprintf(stderr, "[%s] failed to flush output buffer.\n", __func__); + abort(); + } rz->stream->avail_out = RZ_BUFFER_SIZE; rz->stream->next_out = rz->outbuf; } else break; @@ -211,6 +349,7 @@ static void razf_flush(RAZF *rz){ } static void razf_end_flush(RAZF *rz){ + size_t count; uint32_t tout; if(rz->buf_len){ _razf_write(rz, rz->inbuf, rz->buf_len); @@ -221,7 +360,15 @@ static void razf_end_flush(RAZF *rz){ deflate(rz->stream, Z_FINISH); rz->out += tout - rz->stream->avail_out; if(rz->stream->avail_out < RZ_BUFFER_SIZE){ - write(rz->filedes, rz->outbuf, RZ_BUFFER_SIZE - rz->stream->avail_out); + count = RZ_BUFFER_SIZE - rz->stream->avail_out; +#ifdef _USE_KNETFILE + if (write(rz->x.fpw, rz->outbuf, count) < 0) { +#else + if (write(rz->filedes, rz->outbuf, count) < 0) { +#endif + fprintf(stderr, "[%s] failed to flush output buffer.\n", __func__); + abort(); + } rz->stream->avail_out = RZ_BUFFER_SIZE; rz->stream->next_out = rz->outbuf; } else break; @@ -308,23 +455,53 @@ static int _read_gz_header(unsigned char *data, int size, int *extra_off, int *e return n; } +#ifdef _USE_KNETFILE +static RAZF* razf_open_r(knetFile *fp, int _load_index){ +#else static RAZF* razf_open_r(int fd, int _load_index){ +#endif RAZF *rz; int ext_off, ext_len; int n, is_be, ret; int64_t end; unsigned char c[] = "RAZF"; + if ((rz = calloc(1, sizeof(RAZF))) == NULL) { + fprintf(stderr, "[%s] failure to allocate RAZF structure.\n", __func__); + return NULL; + } + rz->mode = 'r'; +#ifdef _USE_KNETFILE + rz->x.fpr = fp; +#else #ifdef _WIN32 setmode(fd, O_BINARY); #endif - rz = calloc(1, sizeof(RAZF)); - rz->mode = 'r'; rz->filedes = fd; - rz->stream = calloc(sizeof(z_stream), 1); - rz->inbuf = malloc(RZ_BUFFER_SIZE); - rz->outbuf = malloc(RZ_BUFFER_SIZE); +#endif + if ((rz->stream = calloc(sizeof(z_stream), 1)) == NULL) { + fprintf(stderr, "[%s] failure to allocate z_stream.\n", __func__); + free(rz); + return NULL; + } + if ((rz->inbuf = malloc(RZ_BUFFER_SIZE)) == NULL) { + fprintf(stderr, "[%s] failure to allocate input buffer.\n", __func__); + free(rz->stream); + free(rz); + return NULL; + } + if ((rz->outbuf = malloc(RZ_BUFFER_SIZE)) == NULL) { + fprintf(stderr, "[%s] failure to allocate output buffer.\n", __func__); + free(rz->inbuf); + free(rz->stream); + free(rz); + return NULL; + } rz->end = rz->src_end = 0x7FFFFFFFFFFFFFFFLL; +#ifdef _USE_KNETFILE + n = knet_read(rz->x.fpr, rz->inbuf, RZ_BUFFER_SIZE); +#else n = read(rz->filedes, rz->inbuf, RZ_BUFFER_SIZE); +#endif ret = _read_gz_header(rz->inbuf, n, &ext_off, &ext_len); if(ret == 0){ PLAIN_FILE: @@ -355,7 +532,11 @@ static RAZF* razf_open_r(int fd, int _load_index){ } rz->load_index = _load_index; rz->file_type = FILE_TYPE_RZ; +#ifdef _USE_KNETFILE + if(knet_seek(fp, -16, SEEK_END) == -1){ +#else if(lseek(fd, -16, SEEK_END) == -1){ +#endif UNSEEKABLE: rz->seekable = 0; rz->index = NULL; @@ -363,10 +544,19 @@ static RAZF* razf_open_r(int fd, int _load_index){ } else { is_be = is_big_endian(); rz->seekable = 1; +#ifdef _USE_KNETFILE + knet_read(fp, &end, sizeof(int64_t)); +#else read(fd, &end, sizeof(int64_t)); +#endif if(!is_be) rz->src_end = (int64_t)byte_swap_8((uint64_t)end); else rz->src_end = end; + +#ifdef _USE_KNETFILE + knet_read(fp, &end, sizeof(int64_t)); +#else read(fd, &end, sizeof(int64_t)); +#endif if(!is_be) rz->end = (int64_t)byte_swap_8((uint64_t)end); else rz->end = end; if(n > rz->end){ @@ -374,19 +564,47 @@ static RAZF* razf_open_r(int fd, int _load_index){ n = rz->end; } if(rz->end > rz->src_end){ +#ifdef _USE_KNETFILE + knet_seek(fp, rz->in, SEEK_SET); +#else lseek(fd, rz->in, SEEK_SET); +#endif goto UNSEEKABLE; } +#ifdef _USE_KNETFILE + knet_seek(fp, rz->end, SEEK_SET); + if(knet_tell(fp) != rz->end){ + knet_seek(fp, rz->in, SEEK_SET); +#else if(lseek(fd, rz->end, SEEK_SET) != rz->end){ lseek(fd, rz->in, SEEK_SET); +#endif goto UNSEEKABLE; } +#ifdef _USE_KNETFILE + load_zindex(rz, fp); + knet_seek(fp, n, SEEK_SET); +#else load_zindex(rz, fd); lseek(fd, n, SEEK_SET); +#endif } return rz; } +#ifdef _USE_KNETFILE +RAZF* razf_dopen(int fd, const char *mode){ + if (strstr(mode, "r")) fprintf(stderr,"[razf_dopen] implement me\n"); + else if(strstr(mode, "w")) return razf_open_w(fd); + return NULL; +} + +RAZF* razf_dopen2(int fd, const char *mode) +{ + fprintf(stderr,"[razf_dopen2] implement me\n"); + return NULL; +} +#else RAZF* razf_dopen(int fd, const char *mode){ if(strstr(mode, "r")) return razf_open_r(fd, 1); else if(strstr(mode, "w")) return razf_open_w(fd); @@ -399,23 +617,34 @@ RAZF* razf_dopen2(int fd, const char *mode) else if(strstr(mode, "w")) return razf_open_w(fd); else return NULL; } +#endif static inline RAZF* _razf_open(const char *filename, const char *mode, int _load_index){ int fd; RAZF *rz; if(strstr(mode, "r")){ +#ifdef _USE_KNETFILE + knetFile *fd = knet_open(filename, "r"); + if (fd == 0) { + fprintf(stderr, "[_razf_open] fail to open %s\n", filename); + return NULL; + } +#else #ifdef _WIN32 fd = open(filename, O_RDONLY | O_BINARY); #else fd = open(filename, O_RDONLY); #endif +#endif + if(fd < 0) return NULL; rz = razf_open_r(fd, _load_index); } else if(strstr(mode, "w")){ #ifdef _WIN32 - fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644); + fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666); #else - fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644); + fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666); #endif + if(fd < 0) return NULL; rz = razf_open_w(fd); } else return NULL; return rz; @@ -435,9 +664,17 @@ int razf_get_data_size(RAZF *rz, int64_t *u_size, int64_t *c_size){ switch(rz->file_type){ case FILE_TYPE_PLAIN: if(rz->end == 0x7fffffffffffffffLL){ +#ifdef _USE_KNETFILE + if(knet_seek(rz->x.fpr, 0, SEEK_CUR) == -1) return 0; + n = knet_tell(rz->x.fpr); + knet_seek(rz->x.fpr, 0, SEEK_END); + rz->end = knet_tell(rz->x.fpr); + knet_seek(rz->x.fpr, n, SEEK_SET); +#else if((n = lseek(rz->filedes, 0, SEEK_CUR)) == -1) return 0; rz->end = lseek(rz->filedes, 0, SEEK_END); lseek(rz->filedes, n, SEEK_SET); +#endif } *u_size = *c_size = rz->end; return 1; @@ -457,7 +694,11 @@ static int _razf_read(RAZF* rz, void *data, int size){ int ret, tin; if(rz->z_eof || rz->z_err) return 0; if (rz->file_type == FILE_TYPE_PLAIN) { +#ifdef _USE_KNETFILE + ret = knet_read(rz->x.fpr, data, size); +#else ret = read(rz->filedes, data, size); +#endif if (ret == 0) rz->z_eof = 1; return ret; } @@ -467,9 +708,17 @@ static int _razf_read(RAZF* rz, void *data, int size){ if(rz->stream->avail_in == 0){ if(rz->in >= rz->end){ rz->z_eof = 1; break; } if(rz->end - rz->in < RZ_BUFFER_SIZE){ +#ifdef _USE_KNETFILE + rz->stream->avail_in = knet_read(rz->x.fpr, rz->inbuf, rz->end -rz->in); +#else rz->stream->avail_in = read(rz->filedes, rz->inbuf, rz->end -rz->in); +#endif } else { +#ifdef _USE_KNETFILE + rz->stream->avail_in = knet_read(rz->x.fpr, rz->inbuf, RZ_BUFFER_SIZE); +#else rz->stream->avail_in = read(rz->filedes, rz->inbuf, RZ_BUFFER_SIZE); +#endif } if(rz->stream->avail_in == 0){ rz->z_eof = 1; @@ -481,7 +730,7 @@ static int _razf_read(RAZF* rz, void *data, int size){ ret = inflate(rz->stream, Z_BLOCK); rz->in += tin - rz->stream->avail_in; if(ret == Z_NEED_DICT || ret == Z_MEM_ERROR || ret == Z_DATA_ERROR){ - fprintf(stderr, "[_razf_read] inflate error: %d (at %s:%d)\n", ret, __FILE__, __LINE__); + fprintf(stderr, "[_razf_read] inflate error: %d %s (at %s:%d)\n", ret, rz->stream->msg ? rz->stream->msg : "", __FILE__, __LINE__); rz->z_err = 1; break; } @@ -566,14 +815,18 @@ int razf_skip(RAZF* rz, int size){ } if(rz->buf_flush) continue; rz->buf_len = _razf_read(rz, rz->outbuf, RZ_BUFFER_SIZE); - if(rz->z_eof) break; + if(rz->z_eof || rz->z_err) break; } rz->out += ori_size - size; return ori_size - size; } static void _razf_reset_read(RAZF *rz, int64_t in, int64_t out){ +#ifdef _USE_KNETFILE + knet_seek(rz->x.fpr, in, SEEK_SET); +#else lseek(rz->filedes, in, SEEK_SET); +#endif rz->in = in; rz->out = out; rz->block_pos = in; @@ -592,7 +845,12 @@ int64_t razf_jump(RAZF *rz, int64_t block_start, int block_offset){ if(rz->file_type == FILE_TYPE_PLAIN){ rz->buf_off = rz->buf_len = 0; pos = block_start + block_offset; +#ifdef _USE_KNETFILE + knet_seek(rz->x.fpr, pos, SEEK_SET); + pos = knet_tell(rz->x.fpr); +#else pos = lseek(rz->filedes, pos, SEEK_SET); +#endif rz->out = rz->in = pos; return pos; } @@ -614,7 +872,12 @@ int64_t razf_seek(RAZF* rz, int64_t pos, int where){ if (where == SEEK_CUR) pos += rz->out; else if (where == SEEK_END) pos += rz->src_end; if(rz->file_type == FILE_TYPE_PLAIN){ +#ifdef _USE_KNETFILE + knet_seek(rz->x.fpr, pos, SEEK_SET); + seek_pos = knet_tell(rz->x.fpr); +#else seek_pos = lseek(rz->filedes, pos, SEEK_SET); +#endif rz->buf_off = rz->buf_len = 0; rz->out = rz->in = seek_pos; return seek_pos; @@ -663,16 +926,53 @@ void razf_close(RAZF *rz){ #ifndef _RZ_READONLY razf_end_flush(rz); deflateEnd(rz->stream); +#ifdef _USE_KNETFILE + save_zindex(rz, rz->x.fpw); + if(is_big_endian()){ + if (write(rz->x.fpw, &rz->in, sizeof(int64_t)) < 0) { + fprintf(stderr, "[%s] failed to write rz.in.\n", __func__); + abort(); + } + if (write(rz->x.fpw, &rz->out, sizeof(int64_t)) < 0) { + fprintf(stderr, "[%s] failed to write rz.out.\n", __func__); + abort(); + } + } else { + uint64_t v64 = byte_swap_8((uint64_t)rz->in); + if (write(rz->x.fpw, &v64, sizeof(int64_t)) < 0) { + fprintf(stderr, "[%s] failed to write rz.in.\n", __func__); + abort(); + } + v64 = byte_swap_8((uint64_t)rz->out); + if (write(rz->x.fpw, &v64, sizeof(int64_t)) < 0) { + fprintf(stderr, "[%s] failed to write rz.out.\n", __func__); + abort(); + } + } +#else save_zindex(rz, rz->filedes); if(is_big_endian()){ - write(rz->filedes, &rz->in, sizeof(int64_t)); - write(rz->filedes, &rz->out, sizeof(int64_t)); + if (write(rz->filedes, &rz->in, sizeof(int64_t)) < 0) { + fprintf(stderr, "[%s] failed to write rz.in.\n", __func__); + abort(); + } + if (write(rz->filedes, &rz->out, sizeof(int64_t)) < 0) { + fprintf(stderr, "[%s] failed to write rz.out.\n", __func__); + abort(); + } } else { uint64_t v64 = byte_swap_8((uint64_t)rz->in); - write(rz->filedes, &v64, sizeof(int64_t)); + if (write(rz->filedes, &v64, sizeof(int64_t)) < 0) { + fprintf(stderr, "[%s] failed to write rz.in.\n", __func__); + abort(); + } v64 = byte_swap_8((uint64_t)rz->out); - write(rz->filedes, &v64, sizeof(int64_t)); + if (write(rz->filedes, &v64, sizeof(int64_t)) < 0) { + fprintf(stderr, "[%s] failed to write rz.out.\n", __func__); + abort(); + } } +#endif #endif } else if(rz->mode == 'r'){ if(rz->stream) inflateEnd(rz->stream); @@ -691,7 +991,14 @@ void razf_close(RAZF *rz){ free(rz->index); } free(rz->stream); +#ifdef _USE_KNETFILE + if (rz->mode == 'r') + knet_close(rz->x.fpr); + if (rz->mode == 'w') + close(rz->x.fpw); +#else close(rz->filedes); +#endif free(rz); }