@@ -37,6 +37,7 @@ DEALINGS IN THE SOFTWARE.
3737
3838#include "../htslib/bgzf.h"
3939#include "../htslib/hfile.h"
40+ #include "../htslib/hts_log.h"
4041#include "../hfile_internal.h"
4142
4243const char * bgzf_suffix = ".gz" ;
@@ -159,13 +160,19 @@ static BGZF * try_bgzf_hopen(const char *name, const char *mode,
159160 return bgz ;
160161}
161162
162- static int try_bgzf_close (BGZF * * bgz , const char * name , const char * func ) {
163+ static int try_bgzf_close (BGZF * * bgz , const char * name , const char * func , int expected_fail ) {
163164 BGZF * to_close = * bgz ;
164165 * bgz = NULL ;
165166 if (bgzf_close (to_close ) != 0 ) {
166- fprintf (stderr , "%s : bgzf_close failed on %s : %s\n" ,
167- func , name , strerror (errno ));
167+ if (!expected_fail )
168+ fprintf (stderr , "%s : bgzf_close failed on %s%s%s\n" ,
169+ func , name ,
170+ errno ? " : " : "" ,
171+ errno ? strerror (errno ) : "" );
168172 return -1 ;
173+ } else if (expected_fail ) {
174+ fprintf (stderr , "%s : bgzf_close worked on %s, but expected failure\n" ,
175+ func , name );
169176 }
170177 return 0 ;
171178}
@@ -398,6 +405,7 @@ static int test_read(Files *f) {
398405 ssize_t bg_got , f_got ;
399406 unsigned char bg_buf [BUFSZ ], f_buf [BUFSZ ];
400407
408+ errno = 0 ;
401409 bgz = try_bgzf_open (f -> src_bgzf , "r" , __func__ );
402410 if (!bgz ) return -1 ;
403411
@@ -414,7 +422,7 @@ static int test_read(Files *f) {
414422 }
415423 } while (bg_got > 0 && f_got > 0 );
416424
417- if (try_bgzf_close (& bgz , f -> src_bgzf , __func__ ) != 0 ) return -1 ;
425+ if (try_bgzf_close (& bgz , f -> src_bgzf , __func__ , 0 ) != 0 ) return -1 ;
418426 if (try_fseek_start (f -> f_plain , f -> src_plain , __func__ ) != 0 ) return -1 ;
419427
420428 return 0 ;
@@ -449,7 +457,7 @@ static int test_write_read(Files *f, const char *mode, Open_method method,
449457 bg_put = try_bgzf_write (bgz , f -> text , f -> ltext , f -> tmp_bgzf , __func__ );
450458 if (bg_put < 0 ) goto fail ;
451459
452- if (try_bgzf_close (& bgz , f -> tmp_bgzf , __func__ ) != 0 ) goto fail ;
460+ if (try_bgzf_close (& bgz , f -> tmp_bgzf , __func__ , 0 ) != 0 ) goto fail ;
453461
454462 switch (method ) {
455463 case USE_BGZF_DOPEN :
@@ -491,7 +499,7 @@ static int test_write_read(Files *f, const char *mode, Open_method method,
491499 goto fail ;
492500 }
493501
494- if (try_bgzf_close (& bgz , f -> tmp_bgzf , __func__ ) != 0 ) goto fail ;
502+ if (try_bgzf_close (& bgz , f -> tmp_bgzf , __func__ , 0 ) != 0 ) goto fail ;
495503
496504 return 0 ;
497505
@@ -521,7 +529,7 @@ static int test_embed_eof(Files *f, const char *mode, int nthreads) {
521529 bg_put = try_bgzf_write (bgz , f -> text , half , f -> tmp_bgzf , __func__ );
522530 if (bg_put < 0 ) goto fail ;
523531
524- if (try_bgzf_close (& bgz , f -> tmp_bgzf , __func__ ) != 0 ) goto fail ;
532+ if (try_bgzf_close (& bgz , f -> tmp_bgzf , __func__ , 0 ) != 0 ) goto fail ;
525533
526534
527535 // Write second half. Append mode, so an EOF block should be in the
@@ -535,7 +543,7 @@ static int test_embed_eof(Files *f, const char *mode, int nthreads) {
535543 __func__ );
536544 if (bg_put < 0 ) goto fail ;
537545
538- if (try_bgzf_close (& bgz , f -> tmp_bgzf , __func__ ) != 0 ) goto fail ;
546+ if (try_bgzf_close (& bgz , f -> tmp_bgzf , __func__ , 0 ) != 0 ) goto fail ;
539547
540548 // Try reading
541549 pos = 0 ;
@@ -564,7 +572,7 @@ static int test_embed_eof(Files *f, const char *mode, int nthreads) {
564572 goto fail ;
565573 }
566574
567- if (try_bgzf_close (& bgz , f -> tmp_bgzf , __func__ ) != 0 ) goto fail ;
575+ if (try_bgzf_close (& bgz , f -> tmp_bgzf , __func__ , 0 ) != 0 ) goto fail ;
568576
569577 return 0 ;
570578
@@ -601,7 +609,7 @@ static int test_index_load_dump(Files *f) {
601609 } while (got_src > 0 && got_dest > 0 );
602610 if (try_fclose (& fdest , f -> tmp_idx , __func__ ) != 0 ) goto fail ;
603611
604- if (try_bgzf_close (& bgz , f -> src_bgzf , __func__ ) != 0 ) goto fail ;
612+ if (try_bgzf_close (& bgz , f -> src_bgzf , __func__ , 0 ) != 0 ) goto fail ;
605613
606614 return 0 ;
607615
@@ -624,7 +632,7 @@ static int test_check_EOF(char *name, int expected) {
624632 return -1 ;
625633 }
626634
627- return try_bgzf_close (& bgz , name , __func__ );
635+ return try_bgzf_close (& bgz , name , __func__ , 0 );
628636}
629637
630638static int test_index_useek_getc (Files * f , const char * mode ,
@@ -651,7 +659,7 @@ static int test_index_useek_getc(Files *f, const char *mode,
651659 }
652660 }
653661
654- if (try_bgzf_close (& bgz , f -> tmp_bgzf , __func__ ) != 0 ) goto fail ;
662+ if (try_bgzf_close (& bgz , f -> tmp_bgzf , __func__ , 0 ) != 0 ) goto fail ;
655663
656664 bgz = try_bgzf_open (f -> tmp_bgzf , "r" , __func__ );
657665 if (!bgz ) goto fail ;
@@ -710,7 +718,7 @@ static int test_index_useek_getc(Files *f, const char *mode,
710718 }
711719 }
712720
713- if (try_bgzf_close (& bgz , f -> tmp_bgzf , __func__ ) != 0 ) goto fail ;
721+ if (try_bgzf_close (& bgz , f -> tmp_bgzf , __func__ , 0 ) != 0 ) goto fail ;
714722
715723 return 0 ;
716724
@@ -741,7 +749,7 @@ static int test_tell_seek_getc(Files *f, const char *mode,
741749 if (bg_put < 0 ) goto fail ;
742750 }
743751
744- if (try_bgzf_close (& bgz , f -> tmp_bgzf , __func__ ) != 0 ) goto fail ;
752+ if (try_bgzf_close (& bgz , f -> tmp_bgzf , __func__ , 0 ) != 0 ) goto fail ;
745753
746754 bgz = try_bgzf_open (f -> tmp_bgzf , "r" , __func__ );
747755 if (!bgz ) goto fail ;
@@ -811,7 +819,7 @@ static int test_tell_seek_getc(Files *f, const char *mode,
811819 }
812820 }
813821
814- if (try_bgzf_close (& bgz , f -> tmp_bgzf , __func__ ) != 0 ) goto fail ;
822+ if (try_bgzf_close (& bgz , f -> tmp_bgzf , __func__ , 0 ) != 0 ) goto fail ;
815823
816824 return 0 ;
817825
@@ -841,7 +849,7 @@ static int test_tell_read(Files *f, const char *mode) {
841849 if (bg_put < 0 ) goto fail ;
842850 }
843851
844- if (try_bgzf_close (& bgz , f -> tmp_bgzf , __func__ ) != 0 ) goto fail ;
852+ if (try_bgzf_close (& bgz , f -> tmp_bgzf , __func__ , 0 ) != 0 ) goto fail ;
845853
846854 bgz = try_bgzf_open (f -> tmp_bgzf , "r" , __func__ );
847855 if (!bgz ) goto fail ;
@@ -859,7 +867,7 @@ static int test_tell_read(Files *f, const char *mode) {
859867 }
860868 }
861869
862- if (try_bgzf_close (& bgz , f -> tmp_bgzf , __func__ ) != 0 ) goto fail ;
870+ if (try_bgzf_close (& bgz , f -> tmp_bgzf , __func__ , 0 ) != 0 ) goto fail ;
863871 free (bg_buf );
864872 return 0 ;
865873
@@ -885,7 +893,7 @@ static int test_bgzf_getline(Files *f, const char *mode, int nthreads) {
885893 bg_put = try_bgzf_write (bgz , f -> text , f -> ltext , f -> tmp_bgzf , __func__ );
886894 if (bg_put < 0 ) goto fail ;
887895
888- if (try_bgzf_close (& bgz , f -> tmp_bgzf , __func__ ) != 0 ) goto fail ;
896+ if (try_bgzf_close (& bgz , f -> tmp_bgzf , __func__ , 0 ) != 0 ) goto fail ;
889897
890898 bgz = try_bgzf_open (f -> tmp_bgzf , "r" , __func__ );
891899 if (!bgz ) goto fail ;
@@ -917,7 +925,7 @@ static int test_bgzf_getline(Files *f, const char *mode, int nthreads) {
917925 pos += l + 1 ;
918926 }
919927
920- if (try_bgzf_close (& bgz , f -> tmp_bgzf , __func__ ) != 0 ) goto fail ;
928+ if (try_bgzf_close (& bgz , f -> tmp_bgzf , __func__ , 0 ) != 0 ) goto fail ;
921929 free (ks_release (& str ));
922930 return 0 ;
923931
@@ -934,6 +942,10 @@ static int test_bgzf_getline_on_truncated_file(Files *f, const char *mode, int n
934942 kstring_t str = { 0 , 0 , NULL };
935943 const char * text = (const char * ) f -> text ;
936944
945+ // Turn off bgzf errors as they're expected.
946+ enum htsLogLevel lvl = hts_get_log_level ();
947+ hts_set_log_level (HTS_LOG_OFF );
948+
937949 bgz = try_bgzf_open (f -> tmp_bgzf , mode , __func__ );
938950 if (!bgz ) goto fail ;
939951
@@ -951,7 +963,7 @@ static int test_bgzf_getline_on_truncated_file(Files *f, const char *mode, int n
951963 if (bgzf_flush (bgz ) < 0 ) goto fail ;
952964 int64_t block3_start = bgz -> block_address ;
953965
954- if (try_bgzf_close (& bgz , f -> tmp_bgzf , __func__ ) != 0 ) goto fail ;
966+ if (try_bgzf_close (& bgz , f -> tmp_bgzf , __func__ , 0 ) != 0 ) goto fail ;
955967
956968 int64_t newsize ;
957969 for (newsize = block3_start - 1 ; newsize > block2_start ; newsize -- ) {
@@ -1002,12 +1014,14 @@ static int test_bgzf_getline_on_truncated_file(Files *f, const char *mode, int n
10021014 }
10031015 }
10041016 // closing a stream with error returns error
1005- if (try_bgzf_close (& bgz , f -> tmp_bgzf , __func__ ) == 0 ) goto fail ;
1017+ if (try_bgzf_close (& bgz , f -> tmp_bgzf , __func__ , 1 ) == 0 ) goto fail ;
10061018 }
10071019 free (ks_release (& str ));
1020+ hts_set_log_level (lvl );
10081021 return 0 ;
10091022
10101023 fail :
1024+ hts_set_log_level (lvl );
10111025 if (bgz ) bgzf_close (bgz );
10121026 free (ks_release (& str ));
10131027 return -1 ;
0 commit comments