From 267b6e90743441531dd7fd05765f46aaa0476980 Mon Sep 17 00:00:00 2001 From: Shahram Najm Date: Fri, 19 Aug 2016 09:45:51 +0100 Subject: [PATCH] Remove grib_file_close_force and add argument to grib_file_close (Preparation for GRIB-1010) --- src/action_class_close.c | 2 +- src/action_class_write.c | 2 +- src/grib_api.h | 3 --- src/grib_api_prototypes.h | 3 +-- src/grib_db.c | 4 ++-- src/grib_fieldset.c | 9 +++++++-- src/grib_filepool.c | 25 +++++-------------------- src/grib_index.c | 8 ++++---- tools/grib_to_netcdf.c | 4 ++-- tools/grib_tools.c | 2 +- 10 files changed, 24 insertions(+), 38 deletions(-) diff --git a/src/action_class_close.c b/src/action_class_close.c index b06f8bebc..cde82fe7c 100644 --- a/src/action_class_close.c +++ b/src/action_class_close.c @@ -104,7 +104,7 @@ static int execute(grib_action* act, grib_handle *h) err=grib_get_string(h,self->filename,filename,&len); /* fprintf(stderr,"++++ name %s\n",filename); */ if (err) return err; - /* grib_file_close_force(filename,&err); */ + /* grib_file_close(filename,1,&err); */ file=grib_get_file(filename,&err); if (err) return err; if (file) grib_file_pool_delete_file(file); diff --git a/src/action_class_write.c b/src/action_class_write.c index 608708eae..f5fdfdafa 100644 --- a/src/action_class_write.c +++ b/src/action_class_write.c @@ -173,7 +173,7 @@ static int execute(grib_action* act, grib_handle *h) } } - grib_file_close(filename, &err); + grib_file_close(filename, 0, &err); if (err != GRIB_SUCCESS) { grib_context_log(act->context, GRIB_LOG_ERROR,"unable to write message\n"); return err; diff --git a/src/grib_api.h b/src/grib_api.h index 520a384d1..9f272e96f 100644 --- a/src/grib_api.h +++ b/src/grib_api.h @@ -221,9 +221,6 @@ typedef struct bufr_descriptor bufr_descriptor; typedef struct bufr_descriptors_array bufr_descriptors_array; grib_fieldset *grib_fieldset_new_from_files(grib_context *c, char *filenames[], int nfiles, char **keys, int nkeys, char *where_string, char *order_by_string, int *err); - - - void grib_fieldset_delete(grib_fieldset* set); void grib_fieldset_rewind(grib_fieldset* set); int grib_fieldset_apply_order_by(grib_fieldset* set,const char* order_by_string); diff --git a/src/grib_api_prototypes.h b/src/grib_api_prototypes.h index 8587db82f..d7c68201d 100644 --- a/src/grib_api_prototypes.h +++ b/src/grib_api_prototypes.h @@ -987,8 +987,7 @@ int grib_file_pool_read(grib_context *c, FILE *fh); int grib_file_pool_write(FILE *fh); grib_file *grib_file_open(const char *filename, const char *mode, int *err); void grib_file_pool_delete_file(grib_file *file); -void grib_file_close_force(const char *filename, int *err); -void grib_file_close(const char *filename, int *err); +void grib_file_close(const char *filename, int force, int *err); void grib_file_close_all(int *err); grib_file *grib_get_file(const char *filename, int *err); grib_file *grib_find_file(short id); diff --git a/src/grib_db.c b/src/grib_db.c index fd898730e..d55128de8 100644 --- a/src/grib_db.c +++ b/src/grib_db.c @@ -643,7 +643,7 @@ int grib_db_load(grib_db* db,char* filename) { } if (h) grib_handle_delete(h); - grib_file_close(file->name,&err); + grib_file_close(file->name, 0, &err); return ret; } @@ -695,7 +695,7 @@ grib_handle* grib_db_retrieve(grib_fieldset* set,int i,int* err) { h=grib_handle_new_from_file(set->context,field->file->handle,err); if (*err!=GRIB_SUCCESS) return NULL; - grib_file_close(field->file->name,err); + grib_file_close(field->file->name, 0, err); return h; } diff --git a/src/grib_fieldset.c b/src/grib_fieldset.c index 782384184..61d5e1955 100644 --- a/src/grib_fieldset.c +++ b/src/grib_fieldset.c @@ -634,7 +634,7 @@ int grib_fieldset_add(grib_fieldset* set,char* filename) } if (h) grib_handle_delete(h); - grib_file_close(file->name,&err); + grib_file_close(file->name, 0, &err); grib_fieldset_rewind(set); @@ -697,7 +697,7 @@ grib_handle* grib_fieldset_retrieve(grib_fieldset* set,int i,int* err) h=grib_handle_new_from_file(set->context,field->file->handle,err); if (*err!=GRIB_SUCCESS) return NULL; - grib_file_close(field->file->name,err); + grib_file_close(field->file->name, 0, err); return h; } @@ -800,6 +800,11 @@ static void grib_fieldset_delete_fields(grib_fieldset* set) for (i=0;isize;i++) { if (!set->fields[i]) continue; set->fields[i]->file->refcount--; + /* See GRIB-1010: force file close */ + { + /* int err = 0; */ + /* grib_file_close(set->fields[i]->file->name, 1, &err); */ + } grib_context_free(set->context,set->fields[i]); } grib_context_free(set->context,set->fields); diff --git a/src/grib_filepool.c b/src/grib_filepool.c index 7c5af315d..b20a19a91 100644 --- a/src/grib_filepool.c +++ b/src/grib_filepool.c @@ -285,31 +285,16 @@ void grib_file_pool_delete_file(grib_file* file) { } -void grib_file_close_force(const char* filename,int* err) -{ - grib_file* file=NULL; - - /* fprintf(stderr,"++++ closing file %s\n",filename); */ - GRIB_MUTEX_INIT_ONCE(&once,&init); - GRIB_MUTEX_LOCK(&mutex1); - file=grib_get_file(filename,err); - fclose(file->handle); - if (file->buffer) { - free(file->buffer); - file->buffer=0; - } - file->handle=NULL; - file_pool.number_of_opened_files--; - GRIB_MUTEX_UNLOCK(&mutex1); -} - -void grib_file_close(const char* filename,int* err) +void grib_file_close(const char* filename, int force, int* err) { grib_file* file=NULL; /* Performance: keep the files open to avoid opening and closing files when writing the output. */ /* So only call fclose() when too many files are open */ - if ( file_pool.number_of_opened_files > GRIB_MAX_OPENED_FILES ) { + int do_close = (file_pool.number_of_opened_files > GRIB_MAX_OPENED_FILES); + if (force == 1) do_close=1; /* Can be overridden with the force argument */ + + if ( do_close ) { /*printf("++ closing file %s\n",filename);*/ GRIB_MUTEX_INIT_ONCE(&once,&init); GRIB_MUTEX_LOCK(&mutex1); diff --git a/src/grib_index.c b/src/grib_index.c index 0b4b457f5..64b2c26cb 100644 --- a/src/grib_index.c +++ b/src/grib_index.c @@ -641,7 +641,7 @@ static void grib_field_delete(grib_context* c,grib_field* field) grib_field_delete(c,field->next); if (field->file) { - grib_file_close(field->file->name,&err); + grib_file_close(field->file->name, 0, &err); field->file=NULL; } @@ -1083,7 +1083,7 @@ int _codes_index_add_file(grib_index* index,const char* filename,int message_typ } - grib_file_close(file->name,&err); + grib_file_close(file->name, 0, &err); if (err) return err; index->rewind=1; @@ -1247,7 +1247,7 @@ int grib_index_add_file(grib_index* index, const char* filename) } - grib_file_close(file->name,&err); + grib_file_close(file->name, 0, &err); if (err) return err; index->rewind=1; @@ -1481,7 +1481,7 @@ grib_handle* codes_index_get_handle(grib_field* field,int message_type,int *err) h=message_new(0,field->file->handle,0,err); if (*err!=GRIB_SUCCESS) return NULL; - grib_file_close(field->file->name,err); + grib_file_close(field->file->name, 0, err); return h; } diff --git a/tools/grib_to_netcdf.c b/tools/grib_to_netcdf.c index 0cb5efb28..cffb9ce2b 100644 --- a/tools/grib_to_netcdf.c +++ b/tools/grib_to_netcdf.c @@ -1058,7 +1058,7 @@ static err to_expand_mem(field *g) if(g->handle) grib_get_message(g->handle, &dummy, &g->length); - grib_file_close(file->name, &e); + grib_file_close(file->name, 0, &e); if(!g->handle) return -1; @@ -4094,7 +4094,7 @@ int grib_tool_new_filename_action(grib_runtime_options* options, const char* fil grib_handle_delete(h); } - grib_file_close(file->name, &e); + grib_file_close(file->name, 0, &e); { /* Now do some checks */ diff --git a/tools/grib_tools.c b/tools/grib_tools.c index 8e7b4c277..4f2fba439 100644 --- a/tools/grib_tools.c +++ b/tools/grib_tools.c @@ -996,7 +996,7 @@ void grib_tools_write_message(grib_runtime_options* options, grib_handle* h) } } - grib_file_close(filename, &err); + grib_file_close(filename, 0, &err); if (err != GRIB_SUCCESS) { grib_context_log(h->context, GRIB_LOG_ERROR,"unable to write message\n");