1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-11-03 20:54:07 +01:00

add metadata_unsafe attribute to trex.json; write it upon first unsafe trexio_open

This commit is contained in:
q-posev 2022-01-25 11:13:32 +01:00
parent 8947f6caa7
commit dcdac7dbf9
2 changed files with 73 additions and 1 deletions

View File

@ -903,6 +903,7 @@ trexio_open(const char* file_name, const char mode,
break;
#else
if (rc_open != NULL) *rc_open = TREXIO_BACK_END_MISSING;
free(result);
return NULL;
#endif
/*
@ -935,6 +936,7 @@ trexio_open(const char* file_name, const char mode,
break;
#else
if (rc_open != NULL) *rc_open = TREXIO_BACK_END_MISSING;
free(result);
return NULL;
#endif
/*
@ -971,6 +973,7 @@ trexio_open(const char* file_name, const char mode,
break;
#else
if (rc_open != NULL) *rc_open = TREXIO_BACK_END_MISSING;
free(result);
return NULL;
#endif
@ -978,11 +981,51 @@ trexio_open(const char* file_name, const char mode,
}
if (rc != TREXIO_SUCCESS) {
if (rc_open != NULL) *rc_open = TREXIO_OPEN_ERROR;
if (rc_open != NULL) *rc_open = rc;
free(result);
return NULL;
}
/* Mark the file as unsafe upon opening in UNSAFE 'u' mode */
if (mode == 'u') {
rc = trexio_has_metadata_unsafe(result);
if (rc == TREXIO_FAILURE) {
if (rc_open != NULL) *rc_open = TREXIO_OPEN_ERROR;
free(result);
return NULL;
}
if (rc == TREXIO_HAS_NOT) {
int64_t unsafe_val = 1;
switch (back_end) {
case TREXIO_TEXT:
rc = trexio_text_write_metadata_unsafe(result, unsafe_val);
break;
case TREXIO_HDF5:
#ifdef HAVE_HDF5
rc = trexio_hdf5_write_metadata_unsafe(result, unsafe_val);
break;
#else
if (rc_open != NULL) *rc_open = TREXIO_BACK_END_MISSING;
free(result);
return NULL;
#endif
}
}
if (rc != TREXIO_SUCCESS) {
if (rc_open != NULL) *rc_open = rc;
free(result);
return NULL;
}
}
/* Exit upon success */
if (rc_open != NULL) *rc_open = TREXIO_SUCCESS;
@ -4013,10 +4056,17 @@ def delete_$group$(trexio_file) -> None:
3) ~HDF5_VERSION~ [string] (optional, only if ~HAVE_HDF5~ is ~true~)
4) ~TREXIO_GIT_HASH~ [string]
~trexio_mark_safety~ checks if the file has been open in UNSAFE mode.
If it was, the ~metadata_unsafe~ attribute can be overwritten with the value provided in a second argument of the function.
Since ~metadata_unsafe~ is set to ~1~ (~true~) upon the first opening of the file in UNSAFE mode, this value is immutable.
However, if the user validated that the file is correct (e.g. using ~trexio-tools~),
then value of the ~metadata_unsafe~ attribute can be changed using the aforementioned function.
** C
#+begin_src c :tangle prefix_front.h :exports none
trexio_exit_code trexio_info(void);
trexio_exit_code trexio_mark_safety(trexio_t* const file, const int32_t safety_flag);
#+end_src
#+begin_src c :tangle prefix_front.c
@ -4043,6 +4093,21 @@ def delete_$group$(trexio_file) -> None:
}
#+end_src
#+begin_src c :tangle prefix_front.c
trexio_exit_code
trexio_mark_safety (trexio_t* const file, const int32_t safety_flag)
{
if (file == NULL) return TREXIO_INVALID_ARG_1;
/* 1 for true ; 0 for false */
if (safety_flag != 0 && safety_flag != 1) return TREXIO_INVALID_ARG_2;
/* Cannot mark the file in safe mode */
if (file->mode != 'u') return TREXIO_FAILURE;
return trexio_write_metadata_unsafe(file, safety_flag);
}
#+end_src
** Fortran
#+begin_src f90 :tangle prefix_fortran.f90

View File

@ -57,6 +57,12 @@ fetched using multiple function calls to perform I/O on buffers.
| ~author~ | ~str~ | ~(metadata.author_num)~ | Names of the authors of the file |
| ~package_version~ | ~str~ | | TREXIO version used to produce the file |
| ~description~ | ~str~ | | Text describing the content of file |
| ~unsafe~ | ~int~ | | ~1~: true, ~0~: false |
**Note:** ~unsafe~ attribute of the ~metadata~ group indicates whether the file has been previously opened with ~'u'~ mode.
It is automatically written in the file upon the first unsafe opening.
If the user has checked that the TREXIO file is valid (e.g. using ~trexio-tools~) after unsafe operations,
then the ~unsafe~ attribute value can be manually overwritten (in unsafe mode) from ~1~ to ~0~.
#+CALL: json(data=metadata, title="metadata")
#+RESULTS:
@ -69,6 +75,7 @@ fetched using multiple function calls to perform I/O on buffers.
, "author" : [ "str", [ "metadata.author_num" ] ]
, "package_version" : [ "str", [] ]
, "description" : [ "str", [] ]
, "unsafe" : [ "int", [] ]
} ,
#+end_src
:END: