From d0173067063ec9efbc08063c5e19fd654b87b0f1 Mon Sep 17 00:00:00 2001 From: Anthony Scemama Date: Wed, 13 Oct 2021 16:13:24 +0200 Subject: [PATCH] Better error handling with open in text back end --- src/templates_front/templator_front.org | 4 ++-- src/templates_text/templator_text.org | 27 ++++++++++++++++++------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/templates_front/templator_front.org b/src/templates_front/templator_front.org index dfe489a..106b3d9 100644 --- a/src/templates_front/templator_front.org +++ b/src/templates_front/templator_front.org @@ -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 diff --git a/src/templates_text/templator_text.org b/src/templates_text/templator_text.org index 4d6cb71..890c248 100644 --- a/src/templates_text/templator_text.org +++ b/src/templates_text/templator_text.org @@ -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;