void enkf_main_select_fs( enkf_main_type * enkf_main , const char * case_path ) { if (enkf_main_case_is_current( enkf_main , case_path )) return; /* We have tried to select the currently selected case - just return. */ else { enkf_fs_type * new_fs = enkf_main_mount_alt_fs( enkf_main , case_path , true ); if (enkf_main->dbase == new_fs) util_abort("%s : return reference to current FS in situation where that should not happen.\n",__func__); if (new_fs != NULL) enkf_main_set_fs( enkf_main , new_fs , case_path); else { const char * ens_path = model_config_get_enspath( enkf_main->model_config ); util_exit("%s: select filesystem %s:%s failed \n",__func__ , ens_path , case_path ); } enkf_fs_decref( new_fs ); } }
enkf_fs_type * enkf_main_mount_alt_fs(const enkf_main_type * enkf_main , const char * case_path , bool create) { if (enkf_main_case_is_current( enkf_main , case_path )) { // Fast path - we just return a reference to the currently selected case; // with increased refcount. enkf_fs_incref( enkf_main->dbase ); return enkf_main->dbase; } else { // We have asked for an alterantive fs - must mount and possibly create that first. enkf_fs_type * new_fs = NULL; if (case_path != NULL) { char * new_mount_point = enkf_main_alloc_mount_point( enkf_main , case_path ); if (!enkf_fs_exists( new_mount_point )) { if (create) enkf_main_create_fs( enkf_main , case_path ); } new_fs = enkf_fs_mount( new_mount_point ); free( new_mount_point ); } return new_fs; } }
enkf_fs_type * enkf_main_mount_alt_fs(const enkf_main_type * enkf_main , const char * case_path , bool create) { if (enkf_main_case_is_current( enkf_main , case_path )) { // Fast path - we just return a reference to the currently selected case; // with increased refcount. enkf_fs_incref( enkf_main->dbase ); return enkf_main->dbase; } else { // We have asked for an alterantive fs - must mount and possibly create that first. enkf_fs_type * new_fs = NULL; if (case_path != NULL) { char * new_mount_point = enkf_main_alloc_mount_point( enkf_main , case_path ); if (!enkf_fs_exists( new_mount_point )) { if (create) enkf_main_create_fs( enkf_main , case_path ); } new_fs = enkf_fs_mount( new_mount_point ); if (new_fs) { const model_config_type * model_config = enkf_main_get_model_config( enkf_main ); const ecl_sum_type * refcase = model_config_get_refcase( model_config ); if (refcase) { time_map_type * time_map = enkf_fs_get_time_map( new_fs ); if (time_map_attach_refcase( time_map , refcase)) time_map_set_strict( time_map , false ); else ert_log_add_fmt_message(1 , stderr , "Warning mismatch between refcase:%s and existing case:%s" , ecl_sum_get_case( refcase ) , new_mount_point); } } free( new_mount_point ); } return new_fs; } }
int main(int argc, char ** argv) { const char * config_file = argv[1]; test_work_area_type * work_area = test_work_area_alloc( "enkf_main_fs" ); char * model_config; util_alloc_file_components( config_file , NULL , &model_config , NULL); test_work_area_copy_parent_content( work_area , config_file ); { const char * site_config = "/project/res/etc/ERT/site-config"; enkf_main_type * enkf_main = enkf_main_bootstrap( site_config , model_config , false , false ); enkf_main_select_fs( enkf_main , "enkf"); test_assert_true( enkf_main_case_is_current( enkf_main , "enkf")); test_assert_false( enkf_main_case_is_current( enkf_main , "default_fs")); test_assert_false( enkf_main_case_is_current( enkf_main , "does_not_exist")); test_assert_int_equal( 1 , enkf_fs_get_refcount( enkf_main_get_fs( enkf_main ))); { enkf_fs_type * fs_ref = enkf_main_get_fs_ref( enkf_main ); test_assert_int_equal( 2 , enkf_fs_get_refcount( enkf_main_get_fs( enkf_main ))); enkf_fs_decref( fs_ref ); test_assert_int_equal( 1 , enkf_fs_get_refcount( enkf_main_get_fs( enkf_main ))); } { state_map_type * map1 = enkf_fs_get_state_map( enkf_main_get_fs( enkf_main )); state_map_type * map2 = enkf_main_alloc_readonly_state_map(enkf_main , "enkf"); test_assert_true(state_map_equal( map1 , map2 )); state_map_free( map2 ); } { enkf_fs_type * fs1 = enkf_main_mount_alt_fs( enkf_main , "default" , false ); enkf_fs_type * fs2 = enkf_main_mount_alt_fs( enkf_main , "enkf" , false ); test_assert_int_equal( 2 , enkf_fs_get_refcount( enkf_main_get_fs( enkf_main ))); test_assert_int_equal( 2 , enkf_fs_get_refcount( fs2 )); test_assert_int_equal( 1 , enkf_fs_get_refcount( fs1 )); enkf_fs_decref( fs1 ); enkf_fs_decref( fs2 ); } { enkf_fs_type * enkf_fs = enkf_main_mount_alt_fs( enkf_main , "enkf" , false ); enkf_main_select_fs( enkf_main , "default"); test_assert_int_equal( 1 , enkf_fs_get_refcount( enkf_main_get_fs( enkf_main ))); enkf_fs_decref( enkf_fs ); } { enkf_fs_type * default_fs = enkf_main_mount_alt_fs( enkf_main , "default" , false ); test_assert_int_equal( 2 , enkf_fs_get_refcount( enkf_main_get_fs( enkf_main ))); enkf_main_select_fs( enkf_main , "default"); test_assert_int_equal( 2 , enkf_fs_get_refcount( enkf_main_get_fs( enkf_main ))); enkf_fs_decref( default_fs ); test_assert_int_equal( 1 , enkf_fs_get_refcount( enkf_main_get_fs( enkf_main ))); } /*****************************************************************/ { enkf_fs_type * fs = enkf_main_mount_alt_fs( enkf_main , "default" , false ); test_assert_int_equal( 2 , enkf_fs_get_refcount( enkf_main_get_fs( enkf_main ))); enkf_main_set_fs( enkf_main , fs , NULL ); enkf_fs_decref( fs ); test_assert_int_equal( 1 , enkf_fs_get_refcount( enkf_main_get_fs( enkf_main ))); } { enkf_fs_type * fs = enkf_main_mount_alt_fs( enkf_main , "enkf" , false ); enkf_fs_type * current = enkf_main_mount_alt_fs( enkf_main , "default" , false ); test_assert_int_equal( 2 , enkf_fs_get_refcount( current )); test_assert_int_equal( 1 , enkf_fs_get_refcount( fs)); enkf_main_set_fs( enkf_main , fs , NULL); test_assert_int_equal( 2 , enkf_fs_get_refcount( fs)); test_assert_int_equal( 1 , enkf_fs_get_refcount( current )); enkf_fs_decref( current ); enkf_fs_decref( fs); } test_assert_int_equal( 1 , enkf_fs_get_refcount( enkf_main_get_fs( enkf_main ))); enkf_main_free( enkf_main ); } test_work_area_free( work_area ); exit(0); }