diff --git a/src/trexio.c b/src/trexio.c index 627f8e6..2d940d4 100644 --- a/src/trexio.c +++ b/src/trexio.c @@ -31,6 +31,7 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b trexio_t* result = NULL; + /* Allocate data structures */ switch (back_end) { case TREXIO_TEXT: @@ -47,8 +48,10 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b */ } - /* TODO: Error handling */ - assert (result != NULL); + assert (result != NULL); /* TODO: Error handling */ + + + /* Data for the parent type */ result->file_name = (char*) calloc(strlen(file_name)+1,sizeof(char)); strcpy(result->file_name, file_name); @@ -57,7 +60,11 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b int irc = pthread_mutex_init ( &(result->thread_lock), NULL); assert (irc == 0); - trexio_exit_code rc = TREXIO_FAILURE; + trexio_exit_code rc; + + /* Back end initialization */ + + rc = TREXIO_FAILURE; switch (back_end) { @@ -76,6 +83,31 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b } if (rc != TREXIO_SUCCESS) return NULL; + + /* File locking */ + + rc = TREXIO_FAILURE; + + switch (back_end) { + + case TREXIO_TEXT: + rc = trexio_text_lock(result); + break; + + case TREXIO_HDF5: + rc = TREXIO_SUCCESS; +/* + rc = trexio_hdf5_lock(result); + break; + + case TREXIO_JSON: + rc = trexio_json_lock(result); + break; +*/ + } + if (rc != TREXIO_SUCCESS) return NULL; + + return result; } @@ -85,6 +117,7 @@ trexio_exit_code trexio_close(trexio_t* file) { trexio_exit_code rc; + /* Terminate the back end */ switch (file->back_end) { case TREXIO_TEXT: @@ -106,14 +139,42 @@ trexio_exit_code trexio_close(trexio_t* file) { if (rc != TREXIO_SUCCESS) { return TREXIO_FAILURE; } + + + /* File unlocking */ + + rc = TREXIO_FAILURE; + + switch (file->back_end) { + + case TREXIO_TEXT: + rc = trexio_text_unlock(file); + break; + + case TREXIO_HDF5: + rc = TREXIO_SUCCESS; +/* + rc = trexio_hdf5_unlock(file); + break; + + case TREXIO_JSON: + rc = trexio_json_unlock(file); + break; +*/ + } + if (rc != TREXIO_SUCCESS) return TREXIO_FAILURE; + + + /* Terminate front end */ free(file->file_name); file->file_name = NULL; int irc = pthread_mutex_destroy( &(file->thread_lock) ); - assert (irc == 0); - free(file); + + if (irc != 0) return TREXIO_ERRNO; + return TREXIO_SUCCESS; } diff --git a/src/trexio.h b/src/trexio.h index d00c1ff..26aad39 100644 --- a/src/trexio.h +++ b/src/trexio.h @@ -21,6 +21,7 @@ typedef int32_t trexio_exit_code; #define TREXIO_INVALID_ARG_5 ( (trexio_exit_code) 5 ) #define TREXIO_END ( (trexio_exit_code) 10 ) #define TREXIO_READONLY ( (trexio_exit_code) 11 ) +#define TREXIO_ERRNO ( (trexio_exit_code) 12 ) typedef int32_t back_end_t; diff --git a/src/trexio.org b/src/trexio.org index 050df1c..0bc5bac 100644 --- a/src/trexio.org +++ b/src/trexio.org @@ -78,6 +78,7 @@ typedef int32_t trexio_exit_code; #define TREXIO_INVALID_ARG_5 ( (trexio_exit_code) 5 ) #define TREXIO_END ( (trexio_exit_code) 10 ) #define TREXIO_READONLY ( (trexio_exit_code) 11 ) +#define TREXIO_ERRNO ( (trexio_exit_code) 12 ) #+end_src ** Back ends @@ -168,6 +169,7 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b trexio_t* result = NULL; + /* Allocate data structures */ switch (back_end) { case TREXIO_TEXT: @@ -184,8 +186,10 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b ,*/ } - /* TODO: Error handling */ - assert (result != NULL); + assert (result != NULL); /* TODO: Error handling */ + + + /* Data for the parent type */ result->file_name = (char*) calloc(strlen(file_name)+1,sizeof(char)); strcpy(result->file_name, file_name); @@ -194,7 +198,11 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b int irc = pthread_mutex_init ( &(result->thread_lock), NULL); assert (irc == 0); - trexio_exit_code rc = TREXIO_FAILURE; + trexio_exit_code rc; + + /* Back end initialization */ + + rc = TREXIO_FAILURE; switch (back_end) { @@ -213,6 +221,31 @@ trexio_t* trexio_open(const char* file_name, const char mode, const back_end_t b } if (rc != TREXIO_SUCCESS) return NULL; + + /* File locking */ + + rc = TREXIO_FAILURE; + + switch (back_end) { + + case TREXIO_TEXT: + rc = trexio_text_lock(result); + break; + + case TREXIO_HDF5: + rc = TREXIO_SUCCESS; +/* + rc = trexio_hdf5_lock(result); + break; + + case TREXIO_JSON: + rc = trexio_json_lock(result); + break; +,*/ + } + if (rc != TREXIO_SUCCESS) return NULL; + + return result; } #+end_src @@ -230,6 +263,7 @@ trexio_exit_code trexio_close(trexio_t* file) { trexio_exit_code rc; + /* Terminate the back end */ switch (file->back_end) { case TREXIO_TEXT: @@ -251,14 +285,42 @@ trexio_exit_code trexio_close(trexio_t* file) { if (rc != TREXIO_SUCCESS) { return TREXIO_FAILURE; } + + + /* File unlocking */ + + rc = TREXIO_FAILURE; + + switch (file->back_end) { + + case TREXIO_TEXT: + rc = trexio_text_unlock(file); + break; + + case TREXIO_HDF5: + rc = TREXIO_SUCCESS; +/* + rc = trexio_hdf5_unlock(file); + break; + + case TREXIO_JSON: + rc = trexio_json_unlock(file); + break; +,*/ + } + if (rc != TREXIO_SUCCESS) return TREXIO_FAILURE; + + + /* Terminate front end */ free(file->file_name); file->file_name = NULL; int irc = pthread_mutex_destroy( &(file->thread_lock) ); - assert (irc == 0); - free(file); + + if (irc != 0) return TREXIO_ERRNO; + return TREXIO_SUCCESS; } @@ -618,5 +680,4 @@ trexio_exit_code trexio_buffered_write_rdm_two_e(trexio_t* file, const int64_t o - [ ] Error handling with errno - [ ] HDF5 back-end - [ ] JSON back-end - - [ ] File locking with flock - [ ] Caching of the struct saving last modification date in structs diff --git a/src/trexio_hdf5.org b/src/trexio_hdf5.org index 97d2b91..c37925f 100644 --- a/src/trexio_hdf5.org +++ b/src/trexio_hdf5.org @@ -526,7 +526,7 @@ trexio_exit_code trexio_hdf5_write_nucleus_num(const trexio_t* file, const uint6 if (nucleus->num != 0) { printf("%ld -> %ld %s \n", num, nucleus->num, - "This variable alreasy exists. Overwriting it is not supported"); + "This variable already exists. Overwriting it is not supported"); trexio_hdf5_free_nucleus(nucleus); return TREXIO_FAILURE; } diff --git a/src/trexio_text.c b/src/trexio_text.c index 5b795f0..d18dc3b 100644 --- a/src/trexio_text.c +++ b/src/trexio_text.c @@ -38,7 +38,18 @@ trexio_exit_code trexio_text_init(trexio_t* file) { assert (f->lock_file > 0); free(file_name); - /* Lock the directory for the current process */ + f->nucleus = NULL; + f->electron= NULL; + f->rdm = NULL; + + return TREXIO_SUCCESS; +} + +trexio_exit_code trexio_text_lock(trexio_t* file) { + if (file == NULL) return TREXIO_INVALID_ARG_1; + + trexio_text_t* f = (trexio_text_t*) file; + struct flock fl; fl.l_type = F_WRLCK; @@ -50,20 +61,12 @@ trexio_exit_code trexio_text_init(trexio_t* file) { int rc = fcntl(f->lock_file, F_SETLKW, &fl); if (rc == -1) return TREXIO_FAILURE; - trexio_text_t* f_child = (trexio_text_t*) file; - - f_child->nucleus = NULL; - f_child->electron= NULL; - f_child->rdm = NULL; - return TREXIO_SUCCESS; } trexio_exit_code trexio_text_finalize(trexio_t* file) { if (file == NULL) return TREXIO_INVALID_ARG_1; - trexio_text_t* f = (trexio_text_t*) file; - trexio_exit_code rc; rc = trexio_text_free_nucleus( (trexio_text_t*) file); assert (rc == TREXIO_SUCCESS); @@ -71,7 +74,14 @@ trexio_exit_code trexio_text_finalize(trexio_t* file) { rc = trexio_text_free_rdm( (trexio_text_t*) file); assert (rc == TREXIO_SUCCESS); - /* Release lock */ + return TREXIO_SUCCESS; +} + +trexio_exit_code trexio_text_unlock(trexio_t* file) { + if (file == NULL) return TREXIO_INVALID_ARG_1; + + trexio_text_t* f = (trexio_text_t*) file; + struct flock fl; fl.l_type = F_WRLCK; diff --git a/src/trexio_text.h b/src/trexio_text.h index 593ea1a..ea8c1c4 100644 --- a/src/trexio_text.h +++ b/src/trexio_text.h @@ -56,8 +56,12 @@ typedef struct trexio_text_s { trexio_exit_code trexio_text_init(trexio_t* file); +trexio_exit_code trexio_text_lock(trexio_t* file); + trexio_exit_code trexio_text_finalize(trexio_t* file); +trexio_exit_code trexio_text_unlock(trexio_t* file); + nucleus_t* trexio_text_read_nucleus(trexio_text_t* file); trexio_exit_code trexio_text_flush_nucleus(const trexio_text_t* file); diff --git a/src/trexio_text.org b/src/trexio_text.org index 208cd5e..649309d 100644 --- a/src/trexio_text.org +++ b/src/trexio_text.org @@ -136,7 +136,25 @@ trexio_exit_code trexio_text_init(trexio_t* file) { assert (f->lock_file > 0); free(file_name); - /* Lock the directory for the current process */ + f->nucleus = NULL; + f->electron= NULL; + f->rdm = NULL; + + return TREXIO_SUCCESS; +} + + #+end_src + + #+begin_src c :tangle trexio_text.h +trexio_exit_code trexio_text_lock(trexio_t* file); + #+end_src + + #+begin_src c :tangle trexio_text.c +trexio_exit_code trexio_text_lock(trexio_t* file) { + if (file == NULL) return TREXIO_INVALID_ARG_1; + + trexio_text_t* f = (trexio_text_t*) file; + struct flock fl; fl.l_type = F_WRLCK; @@ -148,12 +166,6 @@ trexio_exit_code trexio_text_init(trexio_t* file) { int rc = fcntl(f->lock_file, F_SETLKW, &fl); if (rc == -1) return TREXIO_FAILURE; - trexio_text_t* f_child = (trexio_text_t*) file; - - f_child->nucleus = NULL; - f_child->electron= NULL; - f_child->rdm = NULL; - return TREXIO_SUCCESS; } @@ -168,8 +180,6 @@ trexio_exit_code trexio_text_finalize(trexio_t* file); trexio_exit_code trexio_text_finalize(trexio_t* file) { if (file == NULL) return TREXIO_INVALID_ARG_1; - trexio_text_t* f = (trexio_text_t*) file; - trexio_exit_code rc; rc = trexio_text_free_nucleus( (trexio_text_t*) file); assert (rc == TREXIO_SUCCESS); @@ -177,7 +187,20 @@ trexio_exit_code trexio_text_finalize(trexio_t* file) { rc = trexio_text_free_rdm( (trexio_text_t*) file); assert (rc == TREXIO_SUCCESS); - /* Release lock */ + return TREXIO_SUCCESS; +} + #+end_src + + #+begin_src c :tangle trexio_text.h +trexio_exit_code trexio_text_unlock(trexio_t* file); + #+end_src + + #+begin_src c :tangle trexio_text.c +trexio_exit_code trexio_text_unlock(trexio_t* file) { + if (file == NULL) return TREXIO_INVALID_ARG_1; + + trexio_text_t* f = (trexio_text_t*) file; + struct flock fl; fl.l_type = F_WRLCK;