From df1e28548c1b1ce0816348a380b03bfd125b13eb Mon Sep 17 00:00:00 2001 From: Markus Sortland Dregi Date: Wed, 7 Jun 2017 15:07:33 +0200 Subject: [PATCH] Flush buffer before ecl_file_kw save --- lib/ecl/ecl_file_kw.c | 1 + lib/ecl/fortio.c | 11 +++++++++++ lib/ecl/tests/ecl_file.c | 22 ++++------------------ lib/include/ert/ecl/fortio.h | 1 + 4 files changed, 17 insertions(+), 18 deletions(-) diff --git a/lib/ecl/ecl_file_kw.c b/lib/ecl/ecl_file_kw.c index 4a137974c2..ab72cdbedb 100644 --- a/lib/ecl/ecl_file_kw.c +++ b/lib/ecl/ecl_file_kw.c @@ -318,6 +318,7 @@ void ecl_file_kw_inplace_fwrite( ecl_file_kw_type * file_kw , fortio_type * fort ecl_file_kw_assert_kw( file_kw ); fortio_fseek( fortio , file_kw->file_offset , SEEK_SET ); ecl_kw_fskip_header( fortio ); + fortio_fclean(fortio); ecl_kw_fwrite_data( file_kw->kw , fortio ); } diff --git a/lib/ecl/fortio.c b/lib/ecl/fortio.c index 5cc6f43860..6f9e9faf58 100644 --- a/lib/ecl/fortio.c +++ b/lib/ecl/fortio.c @@ -488,6 +488,17 @@ void fortio_data_fseek(fortio_type* fortio, offset_type data_offset, size_t data } } +int fortio_fclean(fortio_type * fortio) { + long current_pos = ftell(fortio->stream); + if(current_pos == -1) + return -1; + + int flush_status = fflush(fortio->stream); + if(flush_status != 0) + return flush_status; + + return fseek(fortio->stream, current_pos, SEEK_SET); +} bool fortio_complete_read(fortio_type *fortio , int record_size) { int trailer; diff --git a/lib/ecl/tests/ecl_file.c b/lib/ecl/tests/ecl_file.c index 296ad12c4f..2a8246e285 100644 --- a/lib/ecl/tests/ecl_file.c +++ b/lib/ecl/tests/ecl_file.c @@ -30,11 +30,10 @@ #include #include -void test_writable() { +void test_writable(size_t data_size) { test_work_area_type * work_area = test_work_area_alloc("ecl_file_writable"); const char * data_file_name = "test_file"; - size_t data_size = 10; ecl_kw_type * kw = ecl_kw_alloc("TEST_KW", data_size, ECL_INT); for(int i = 0; i < data_size; ++i) ecl_kw_iset_int(kw, i, ((i*37)+11)%data_size); @@ -43,7 +42,7 @@ void test_writable() { ecl_kw_fwrite(kw, fortio); fortio_fclose(fortio); - for(int i = 0; i < 2; ++i) { + for(int i = 0; i < 4; ++i) { ecl_file_type * ecl_file = ecl_file_open(data_file_name, ECL_FILE_WRITABLE); ecl_kw_type * loaded_kw = ecl_file_view_iget_kw( ecl_file_get_global_view(ecl_file), @@ -86,21 +85,8 @@ void test_truncated() { int main( int argc , char ** argv) { - /* - { - test_work_area_type * work_area = test_work_area_alloc("ecl_file"); - - test_work_area_copy_file( work_area , src_file ); - test_loadall(src_file , target_file ); - - test_close_stream1( src_file , target_file); - test_close_stream2( src_file , target_file); - test_writable( src_file ); - - test_work_area_free( work_area ); - } - */ - test_writable(); + test_writable(10); + test_writable(1337); test_truncated(); exit(0); } diff --git a/lib/include/ert/ecl/fortio.h b/lib/include/ert/ecl/fortio.h index 6781f25d06..177c518d08 100644 --- a/lib/include/ert/ecl/fortio.h +++ b/lib/include/ert/ecl/fortio.h @@ -73,6 +73,7 @@ typedef struct fortio_struct fortio_type; void fortio_data_fseek(fortio_type* fortio, offset_type data_offset, size_t data_element, const int element_size, const int element_count, const int block_size); int fortio_fileno( fortio_type * fortio ); bool fortio_ftruncate( fortio_type * fortio , offset_type size); + int fortio_fclean(fortio_type * fortio); bool fortio_fclose_stream( fortio_type * fortio ); bool fortio_fopen_stream( fortio_type * fortio );