1
0
mirror of https://github.com/TREX-CoE/trexio.git synced 2024-12-23 04:43:57 +01:00

Better error handling with open in text back end

This commit is contained in:
Anthony Scemama 2021-10-13 16:13:24 +02:00
parent 51e64094e6
commit d017306706
2 changed files with 22 additions and 9 deletions

View File

@ -217,7 +217,7 @@ return '\n'.join(result)
#+RESULTS:
:RESULTS:
:results:
#+begin_src c :tangle prefix_front.h :exports none
#define TREXIO_FAILURE ((trexio_exit_code) -1)
#define TREXIO_SUCCESS ((trexio_exit_code) 0)
@ -311,7 +311,7 @@ return '\n'.join(result)
TREXIO_DSET_MISSING = 25
TREXIO_INVALID_STR_LEN = 30
#+end_src
:END:
:end:
*** Decoding errors

View File

@ -134,13 +134,26 @@ trexio_text_init (trexio_t* const file)
/* If directory doesn't exist, create it in write mode */
struct stat st;
if (stat(file->file_name, &st) == 0 && S_ISDIR(st.st_mode)) {
/* Do nothing */
} else {
if (file->mode == 'r') return TREXIO_READONLY;
int rc = stat(file->file_name, &st);
if (mkdir(file->file_name, 0777) != 0) {
return TREXIO_FAILURE;
bool file_exists = rc == 0;
if (file_exists) {
bool is_a_directory = st.st_mode & S_IFDIR;
if (!is_a_directory) {
return TREXIO_FILE_ERROR;
}
} else {
if (file->mode == 'r') {
return TREXIO_READONLY;
}
rc = mkdir(file->file_name, 0777);
if (rc != 0) {
return TREXIO_ERRNO;
}
}
@ -159,7 +172,7 @@ trexio_text_init (trexio_t* const file)
f->lock_file = open(file_name,O_WRONLY|O_CREAT|O_TRUNC, 0644);
if (f->lock_file <= 0) {
return TREXIO_FAILURE;
return TREXIO_ERRNO;
}
return TREXIO_SUCCESS;