Remove grib_file_close_force and add argument to grib_file_close (Preparation for GRIB-1010)

This commit is contained in:
Shahram Najm 2016-08-19 09:45:51 +01:00
parent 3ea30d6fd2
commit 267b6e9074
10 changed files with 24 additions and 38 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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);

View File

@ -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);

View File

@ -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;
}

View File

@ -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;i<set->size;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);

View File

@ -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);

View File

@ -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;
}

View File

@ -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 */

View File

@ -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");