|
| 1 | +/* |
| 2 | +*+ |
| 3 | +* Name: |
| 4 | +* dat1CloseAllIds |
| 5 | +
|
| 6 | +* Purpose: |
| 7 | +* Close all HDF5 identifiers associated with a given file id. |
| 8 | +
|
| 9 | +* Language: |
| 10 | +* Starlink ANSI C |
| 11 | +
|
| 12 | +* Type of Module: |
| 13 | +* Library routine |
| 14 | +
|
| 15 | +* Invocation: |
| 16 | +* dat1CloseAllIds( hid_t file_id, int * status ); |
| 17 | +
|
| 18 | +* Arguments: |
| 19 | +* file_id = hid_t (Given) |
| 20 | +* The file id containing the objects t be closed. |
| 21 | +* status = int* (Given and Returned) |
| 22 | +* Pointer to global status. Attempts to run even if status is bad. |
| 23 | +
|
| 24 | +* Description: |
| 25 | +* Close all HDF5 object identifiers currently associated with a file, |
| 26 | +* either throught the supplied file id or some other file id. All |
| 27 | +* file ids associated with the file (inclusing the supplied file id) are |
| 28 | +* also closed. |
| 29 | +
|
| 30 | +* Authors: |
| 31 | +* DSB: David S Berry (EAO) |
| 32 | +* {enter_new_authors_here} |
| 33 | +
|
| 34 | +* Notes: |
| 35 | +* - This routine attempts to execute even if status is set on entry, |
| 36 | +* although no further error report will be made if it subsequently |
| 37 | +* fails under these circumstances. |
| 38 | +
|
| 39 | +* History: |
| 40 | +* 2020-10-13 (DSB): |
| 41 | +* Initial version |
| 42 | +* {enter_further_changes_here} |
| 43 | +
|
| 44 | +* Copyright: |
| 45 | +* Copyright (C) 2020 East Asian Observatory |
| 46 | +* All Rights Reserved. |
| 47 | +
|
| 48 | +* Licence: |
| 49 | +* This program is free software; you can redistribute it and/or |
| 50 | +* modify it under the terms of the GNU General Public License as |
| 51 | +* published by the Free Software Foundation; either version 3 of |
| 52 | +* the License, or (at your option) any later version. |
| 53 | +* |
| 54 | +* This program is distributed in the hope that it will be |
| 55 | +* useful, but WITHOUT ANY WARRANTY; without even the implied |
| 56 | +* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 57 | +* PURPOSE. See the GNU General Public License for more details. |
| 58 | +* |
| 59 | +* You should have received a copy of the GNU General Public License |
| 60 | +* along with this program; if not, write to the Free Software |
| 61 | +* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 62 | +* MA 02110-1301, USA. |
| 63 | +
|
| 64 | +* Bugs: |
| 65 | +* {note_any_bugs_here} |
| 66 | +*- |
| 67 | +*/ |
| 68 | + |
| 69 | + |
| 70 | +#include "hdf5.h" |
| 71 | +#include "sae_par.h" |
| 72 | +#include "ems.h" |
| 73 | +#include "hds1.h" |
| 74 | +#include "dat1.h" |
| 75 | +#include "dat_err.h" |
| 76 | +#include "hds.h" |
| 77 | + |
| 78 | +int dat1CloseAllIds( hid_t file_id, int * status ){ |
| 79 | + |
| 80 | +/* Local Variables: */ |
| 81 | + H5I_type_t objtype; |
| 82 | + HdsFile *hdsFile = NULL; |
| 83 | + herr_t herr; |
| 84 | + hid_t *objs; |
| 85 | + int howmany; |
| 86 | + int i; |
| 87 | + ssize_t cnt; |
| 88 | + |
| 89 | +/* Return if a null file_id is supplied, but do not check the inherited |
| 90 | + status. */ |
| 91 | + if( file_id <= 0 ) return *status; |
| 92 | + |
| 93 | +/* Begin an entirely new error context as we need to run this |
| 94 | + regardless of external errors */ |
| 95 | + emsBegin( status ); |
| 96 | + |
| 97 | +/* Get the number of active HDF5 identifiers of any type associated with |
| 98 | + the file. */ |
| 99 | + cnt = H5Fget_obj_count( file_id, H5F_OBJ_ALL ); |
| 100 | + |
| 101 | +/* There should always be at least one active identifier (the supplied |
| 102 | + file identifier). */ |
| 103 | + if( cnt == 0 ) { |
| 104 | + *status = DAT__FATAL; |
| 105 | + emsRepf( " ", "dat1CloseAllIds: No active HDF5 idenfifiers for supplied file.", |
| 106 | + status ); |
| 107 | + |
| 108 | +/* Alocate an array to hold the active identifiers then store the active |
| 109 | + identifiers in it. */ |
| 110 | + } else { |
| 111 | + objs = MEM_CALLOC( cnt, sizeof(hid_t) ); |
| 112 | + if( objs ) { |
| 113 | + howmany = H5Fget_obj_ids( file_id, H5F_OBJ_ALL, cnt, objs ); |
| 114 | + |
| 115 | +/* Loop round closing each one, except for the supplied file identifier. */ |
| 116 | + for( i = 0; i < howmany; i++ ) { |
| 117 | + if( objs[i] != file_id ){ |
| 118 | + objtype = H5Iget_type( objs[i] ); |
| 119 | + if ( objtype == H5I_FILE ) { |
| 120 | + herr = H5Fclose( objs[i] ); |
| 121 | + } else if ( objtype == H5I_GROUP || objtype == H5I_DATASET ) { |
| 122 | + herr = H5Oclose( objs[i] ); |
| 123 | + } else if ( objtype == H5I_DATASPACE ) { |
| 124 | + herr = H5Dclose( objs[i] ); |
| 125 | + } else if( *status == SAI__OK ){ |
| 126 | + herr = 0; |
| 127 | + *status = DAT__FATAL; |
| 128 | + emsRepf( " ", "dat1CloseAllIds: Cannot close HDF5 idenfifier - wrong type.", |
| 129 | + status ); |
| 130 | + } |
| 131 | + |
| 132 | + if( herr < 0 && *status == SAI__OK ){ |
| 133 | + *status = DAT__FATAL; |
| 134 | + dat1H5EtoEMS( status ); |
| 135 | + emsRepf( " ", "dat1CloseAllIds: Cannot close HDF5 idenfifier.", |
| 136 | + status ); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | +/* Free the memory allocated above. */ |
| 142 | + MEM_FREE( objs ); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | +/* Close the supplied file id. */ |
| 147 | + if( H5Fclose( file_id ) < 0 && *status == SAI__OK ){ |
| 148 | + *status = DAT__FATAL; |
| 149 | + dat1H5EtoEMS( status ); |
| 150 | + if( hdsFile && hdsFile->path ) { |
| 151 | + emsRepf( " ", "dat1CloseAllIds: Failed to close file '%s'.", |
| 152 | + status, hdsFile->path ); |
| 153 | + } else { |
| 154 | + emsRepf( " ", "dat1CloseAllIds: Failed to close file.", status ); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | +/* End the error context and return the final status */ |
| 159 | + emsEnd( status ); |
| 160 | + |
| 161 | + return *status; |
| 162 | +} |
| 163 | + |
0 commit comments