Skip to content

Commit 0ce1e6c

Browse files
committed
write: give good error message if try write to read-only file
1 parent da75396 commit 0ce1e6c

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

cmake/CheckHDF5.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ endif()
5858

5959
message(CHECK_FAIL "failed")
6060

61-
hdf5_run_err_diag(${_stderr})
61+
hdf5_run_err_diag("${_stderr}")
6262

6363
endfunction(check_hdf5_c)
6464

src/write.f90

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@
1212
h5tcopy_f, h5tclose_f, h5tset_size_f, &
1313
H5S_SCALAR_F, &
1414
H5D_COMPACT_F, &
15-
H5F_SCOPE_GLOBAL_F
15+
H5F_SCOPE_GLOBAL_F, &
16+
H5F_ACC_RDWR_F, H5F_ACC_TRUNC_F, H5F_ACC_EXCL_F
1617

1718

18-
use h5lt, only: h5ltpath_valid_f
19-
2019
implicit none
2120

2221
contains
@@ -60,6 +59,17 @@
6059
integer(HSIZE_T), dimension(:), allocatable :: ddims, maxdims
6160
character(:), allocatable :: emsg
6261

62+
if(.not. self%is_open()) error stop "ERROR:h5fortran:create: file is not open: " // self%filename
63+
64+
if(self%file_mode == -1) error stop "ERROR:h5fortran:create: file mode is not set, call h5f % open() first"
65+
66+
67+
if(all(self%file_mode /= [H5F_ACC_RDWR_F, H5F_ACC_TRUNC_F, H5F_ACC_EXCL_F])) then
68+
write(stderr, '(3a,i0,2a)') "ERROR:h5fortran:create(", dname, "): file mode is not writable: ", &
69+
self%file_mode, " in file: ", self%filename
70+
error stop
71+
endif
72+
6373

6474
call H5Tcopy_f(dtype, dtype_id, ier)
6575
call estop(ier, "create:H5Tcopy", self%filename, dname)
@@ -176,11 +186,23 @@
176186
endif
177187

178188
!> create dataset
179-
call h5dcreate_f(self%file_id, dname, type_id=dtype_id, space_id=filespace_id, dset_id=dset_id, hdferr=ier, dcpl_id=dcpl)
180-
call estop(ier, "create:H5Dcreate", self%filename, dname)
189+
!! https://portal.hdfgroup.org/documentation/hdf5/latest/_l_b_dset_create.html
190+
!! https://support.hdfgroup.org/documentation/hdf5/latest/group___f_h5_d.html#ga5422721017b75f11b6b018a09fa24797
191+
call H5Dcreate_f(self%file_id, dname, type_id=dtype_id, space_id=filespace_id, dset_id=dset_id, hdferr=ier, dcpl_id=dcpl)
192+
if(ier /= 0) then
193+
write(stderr,'(4a)') "ERROR:h5fortran:create: H5Dcreate(", dname, ") in file: ", self%filename
194+
write(stderr,'(a,i0,a,i0,a,i0,a,i0)') "H5Dcreate error code: ", ier, " dtype_id: ", dtype_id, " space_id: ", &
195+
filespace_id, " dset_id: ", dset_id
196+
if (dcpl /= H5P_DEFAULT_F) write(stderr,'(a,i0)') "H5Dcreate dcpl_id: ", dcpl
197+
if (dtype_id == H5T_NATIVE_CHARACTER) write(stderr,'(a,i0)') "H5Dcreate charlen: ", charlen
198+
199+
200+
error stop
201+
202+
endif
181203

182204
!> free resources
183-
call h5pclose_f(dcpl, ier)
205+
call H5Pclose_f(dcpl, ier)
184206
call estop(ier, "create:H5Pclose", self%filename, dname)
185207

186208
end procedure hdf_create

0 commit comments

Comments
 (0)