From 3fd3c5302831ec3825d02a78f2e3df18f9b7ca31 Mon Sep 17 00:00:00 2001 From: pavl-g Date: Sun, 12 Oct 2025 05:07:48 -0500 Subject: [PATCH 1/9] filesystem-mem-model: added separate op_processors for read/write operations --- .../util/filesystem/file_operations.h | 4 ++++ .../electronetsoft/util/filesystem/file_read.c | 13 ++++++------- .../electronetsoft/util/filesystem/file_write.c | 11 ++++++----- 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/electrostatic-sandbox-framework/electrostatic-core/src/include/electrostatic/electronetsoft/util/filesystem/file_operations.h b/electrostatic-sandbox-framework/electrostatic-core/src/include/electrostatic/electronetsoft/util/filesystem/file_operations.h index a3e5947..16013bf 100644 --- a/electrostatic-sandbox-framework/electrostatic-core/src/include/electrostatic/electronetsoft/util/filesystem/file_operations.h +++ b/electrostatic-sandbox-framework/electrostatic-core/src/include/electrostatic/electronetsoft/util/filesystem/file_operations.h @@ -30,6 +30,8 @@ extern "C" { #include struct read_op_processor { + void (*op_preprocessor)(file_mem *, void *); + void (*op_postprocessor)(file_mem *, void *); void (*on_bytes_processed)(file_mem *, ssize_t, void *); /* Executed on each successful read operation. */ void (*on_eof_reached)(file_mem *); /* Executed when EOF is reached. Could be used to chain calls to memory deallocators. */ void (*on_last_char_sampled)(file_mem *, void *caller); @@ -37,6 +39,8 @@ struct read_op_processor { }; struct write_op_processor { + void (*op_preprocessor)(file_mem *, void *); + void (*op_postprocessor)(file_mem *, void *); void (*on_bytes_processed)(file_mem *, ssize_t, void *); /* Executed on each successful read operation. */ void (*on_eob_reached)(file_mem *); void (*on_last_char_sampled)(file_mem *, void *caller); diff --git a/electrostatic-sandbox-framework/electrostatic-core/src/libs/electrostatic-primer/electronetsoft/util/filesystem/file_read.c b/electrostatic-sandbox-framework/electrostatic-core/src/libs/electrostatic-primer/electronetsoft/util/filesystem/file_read.c index 404f7dd..8acf08e 100644 --- a/electrostatic-sandbox-framework/electrostatic-core/src/libs/electrostatic-primer/electronetsoft/util/filesystem/file_read.c +++ b/electrostatic-sandbox-framework/electrostatic-core/src/libs/electrostatic-primer/electronetsoft/util/filesystem/file_read.c @@ -10,8 +10,8 @@ status_code read_into_mem(file_mem *mem, return EUNDEFINEDBUFFER; } - if (NULL != __processor && NULL != __processor->update_model_preprocessor) { - __processor->update_model_preprocessor(mem, &read_into_mem); + if (NULL != _processor && NULL != _processor->op_preprocessor) { + _processor->op_preprocessor(mem, &read_into_mem); } // pre-processing automata -- Calculating the file size, current position, @@ -23,11 +23,6 @@ status_code read_into_mem(file_mem *mem, } } - // postprocessing automata -- Invoke the update file postprocessor - if (NULL != __processor && NULL != __processor->update_model_postprocessor) { - __processor->update_model_postprocessor(mem, &read_into_mem); - } - if (NULL == mem->buffer) { return EUNDEFINEDBUFFER; } @@ -82,5 +77,9 @@ status_code read_into_mem(file_mem *mem, } } + if (NULL != _processor && NULL != _processor->op_postprocessor) { + _processor->op_postprocessor(mem, &read_into_mem); + } + return ASSERTION_FAILURE; } \ No newline at end of file diff --git a/electrostatic-sandbox-framework/electrostatic-core/src/libs/electrostatic-primer/electronetsoft/util/filesystem/file_write.c b/electrostatic-sandbox-framework/electrostatic-core/src/libs/electrostatic-primer/electronetsoft/util/filesystem/file_write.c index c4d5fc4..4fb96b4 100644 --- a/electrostatic-sandbox-framework/electrostatic-core/src/libs/electrostatic-primer/electronetsoft/util/filesystem/file_write.c +++ b/electrostatic-sandbox-framework/electrostatic-core/src/libs/electrostatic-primer/electronetsoft/util/filesystem/file_write.c @@ -10,8 +10,8 @@ status_code write_from_mem(file_mem *mem, return EUNDEFINEDBUFFER; } - if (NULL != __processor && NULL != __processor->update_model_preprocessor) { - __processor->update_model_preprocessor(mem, &write_from_mem); + if (NULL != _processor && NULL != _processor->op_preprocessor) { + _processor->op_preprocessor(mem, &write_from_mem); } if (mem->fd < 0 || !is_fexistential(mem->fd)) { @@ -73,9 +73,10 @@ status_code write_from_mem(file_mem *mem, return ___status; } - // postprocessing automata -- Invoke the update file postprocessor - if (NULL != __processor && NULL != __processor->update_model_postprocessor) { - __processor->update_model_postprocessor(mem, &write_from_mem); + // to avoid premature closure of files; use the postprocessor at the end of the + // write routine + if (NULL != _processor && NULL != _processor->op_postprocessor) { + _processor->op_postprocessor(mem, &write_from_mem); } return PASS; From 5dc6c069ce399e869b2009d29dd5c6a60200e21e Mon Sep 17 00:00:00 2001 From: pavl-g Date: Sun, 12 Oct 2025 05:08:42 -0500 Subject: [PATCH 2/9] file_update.c: corrected the dispatching algorithms --- .../electronetsoft/util/filesystem/file_update.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/electrostatic-sandbox-framework/electrostatic-core/src/libs/electrostatic-primer/electronetsoft/util/filesystem/file_update.c b/electrostatic-sandbox-framework/electrostatic-core/src/libs/electrostatic-primer/electronetsoft/util/filesystem/file_update.c index abe5c6e..2fd5176 100644 --- a/electrostatic-sandbox-framework/electrostatic-core/src/libs/electrostatic-primer/electronetsoft/util/filesystem/file_update.c +++ b/electrostatic-sandbox-framework/electrostatic-core/src/libs/electrostatic-primer/electronetsoft/util/filesystem/file_update.c @@ -9,6 +9,14 @@ status_code update_file_attrs(file_mem *mem, return EUNDEFINEDBUFFER; } + if (!mem->__auto_update_attrs) { + return PASS; + } + + if (NULL != _processor && NULL != _processor->update_model_preprocessor) { + _processor->update_model_preprocessor(mem, &read_into_mem); + } + if (mem->fd < 0 || !is_fexistential(mem->fd)) { return EUNDEFINEDBUFFER; } @@ -50,5 +58,10 @@ status_code update_file_attrs(file_mem *mem, } } + // postprocessing automata -- Invoke the update file postprocessor + if (NULL != _processor && NULL != _processor->update_model_postprocessor) { + _processor->update_model_postprocessor(mem, &read_into_mem); + } + return PASS; } \ No newline at end of file From 8138157fcfb3eae775d665606ba9a51c373332af Mon Sep 17 00:00:00 2001 From: pavl-g Date: Sun, 12 Oct 2025 11:49:17 -0500 Subject: [PATCH 3/9] file_operations: unified file processing algorithms --- .../util/filesystem/file_operations.h | 21 ++++++------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/electrostatic-sandbox-framework/electrostatic-core/src/include/electrostatic/electronetsoft/util/filesystem/file_operations.h b/electrostatic-sandbox-framework/electrostatic-core/src/include/electrostatic/electronetsoft/util/filesystem/file_operations.h index 16013bf..b41b55d 100644 --- a/electrostatic-sandbox-framework/electrostatic-core/src/include/electrostatic/electronetsoft/util/filesystem/file_operations.h +++ b/electrostatic-sandbox-framework/electrostatic-core/src/include/electrostatic/electronetsoft/util/filesystem/file_operations.h @@ -29,22 +29,13 @@ extern "C" { #include #include -struct read_op_processor { +struct op_processor { void (*op_preprocessor)(file_mem *, void *); void (*op_postprocessor)(file_mem *, void *); void (*on_bytes_processed)(file_mem *, ssize_t, void *); /* Executed on each successful read operation. */ - void (*on_eof_reached)(file_mem *); /* Executed when EOF is reached. Could be used to chain calls to memory deallocators. */ - void (*on_last_char_sampled)(file_mem *, void *caller); - void (*on_error_encountered)(file_mem *, int, void *caller); -}; - -struct write_op_processor { - void (*op_preprocessor)(file_mem *, void *); - void (*op_postprocessor)(file_mem *, void *); - void (*on_bytes_processed)(file_mem *, ssize_t, void *); /* Executed on each successful read operation. */ - void (*on_eob_reached)(file_mem *); - void (*on_last_char_sampled)(file_mem *, void *caller); - void (*on_error_encountered)(file_mem *, int, void *caller); + void (*on_last_byte_sampled)(file_mem *, void *); // uses count + void (*on_trailing_char_sampled)(file_mem *, void *); // uses predicate check + void (*on_error_encountered)(file_mem *, int, void *); }; struct update_op_processor { @@ -99,7 +90,7 @@ struct pipe_serializer { * @param _processor a pointer to the file read operations processor; that links the API to the user application. * @param __processor a pointer to the file attrs update processors. */ -status_code read_into_mem(file_mem *, read_op_processor *, update_op_processor *); +status_code read_into_mem(file_mem *, op_processor *, update_op_processor *); status_code update_file_attrs(file_mem *, update_op_processor *); @@ -111,7 +102,7 @@ status_code update_file_attrs(file_mem *, update_op_processor *); * @param mem a pointer to the file memory model struct. * @param processor a pointer to the file operations processor; that links the API to the user application. */ -status_code write_from_mem(file_mem *, write_op_processor *, update_op_processor *); +status_code write_from_mem(file_mem *, op_processor *, update_op_processor *); status_code init_serializer(pipe_serializer *, serializer_op_processor *, update_op_processor *); From 3a9fc019c1468f6272f981b4af45c68cad574d72 Mon Sep 17 00:00:00 2001 From: pavl-g Date: Sun, 12 Oct 2025 11:50:02 -0500 Subject: [PATCH 4/9] electronetsoft/types: unified file op processors types --- .../src/include/electrostatic/electronetsoft/util/types.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/electrostatic-sandbox-framework/electrostatic-core/src/include/electrostatic/electronetsoft/util/types.h b/electrostatic-sandbox-framework/electrostatic-core/src/include/electrostatic/electronetsoft/util/types.h index a962834..a02f2c0 100644 --- a/electrostatic-sandbox-framework/electrostatic-core/src/include/electrostatic/electronetsoft/util/types.h +++ b/electrostatic-sandbox-framework/electrostatic-core/src/include/electrostatic/electronetsoft/util/types.h @@ -81,8 +81,7 @@ typedef struct routine_data (routine_data); typedef struct dll_function_table (dll_function_table); typedef struct routine_callbacks (routine_callbacks); -typedef struct write_op_processor (write_op_processor); -typedef struct read_op_processor (read_op_processor); +typedef struct op_processor (op_processor); typedef struct update_op_processor (update_op_processor); typedef struct serializer_op_processor (serializer_op_processor); From dbf6ddc04c72314025b0e53383a313b2d9aa9341 Mon Sep 17 00:00:00 2001 From: pavl-g Date: Sun, 12 Oct 2025 11:51:02 -0500 Subject: [PATCH 5/9] electronetsoft/filesystem: applied API changes on the source code --- .../electronetsoft/util/filesystem/file_read.c | 14 +++++++------- .../electronetsoft/util/filesystem/file_write.c | 10 +++++----- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/electrostatic-sandbox-framework/electrostatic-core/src/libs/electrostatic-primer/electronetsoft/util/filesystem/file_read.c b/electrostatic-sandbox-framework/electrostatic-core/src/libs/electrostatic-primer/electronetsoft/util/filesystem/file_read.c index 8acf08e..adce94d 100644 --- a/electrostatic-sandbox-framework/electrostatic-core/src/libs/electrostatic-primer/electronetsoft/util/filesystem/file_read.c +++ b/electrostatic-sandbox-framework/electrostatic-core/src/libs/electrostatic-primer/electronetsoft/util/filesystem/file_read.c @@ -3,7 +3,7 @@ #include status_code read_into_mem(file_mem *mem, - read_op_processor *_processor, + op_processor *_processor, update_op_processor *__processor) { // pre-processing automata -- Input validation if (rvalue(mem) == NULL) { @@ -50,14 +50,14 @@ status_code read_into_mem(file_mem *mem, } else if (0 == read_bytes) { // EOF terminate! mem->buffer[total_bytes] = mem->trailing; /* add a null-terminating character */ - if (NULL != _processor && NULL != _processor->on_eof_reached) { - _processor->on_eof_reached(mem); + if (NULL != _processor && NULL != _processor->on_trailing_char_sampled) { + _processor->on_trailing_char_sampled(mem, &read_into_mem); } // post-processing sub-state (HACK!) if (mem->__continue_after_eof) { continue; } - return PASS; + break; } else if (read_bytes > 0) { // execute on_read if (NULL != _processor && NULL != _processor->on_bytes_processed) { @@ -68,8 +68,8 @@ status_code read_into_mem(file_mem *mem, // even though the EOF is not reached, yet. if ((mem->n_bytes - 1) == total_bytes) { mem->buffer[total_bytes] = mem->trailing; /* add a null-terminating character */ - if (NULL != _processor && NULL != _processor->on_last_char_sampled) { - _processor->on_last_char_sampled(mem, &read_into_mem); + if (NULL != _processor && NULL != _processor->on_last_byte_sampled) { + _processor->on_last_byte_sampled(mem, &read_into_mem); } } } else { @@ -81,5 +81,5 @@ status_code read_into_mem(file_mem *mem, _processor->op_postprocessor(mem, &read_into_mem); } - return ASSERTION_FAILURE; + return PASS; } \ No newline at end of file diff --git a/electrostatic-sandbox-framework/electrostatic-core/src/libs/electrostatic-primer/electronetsoft/util/filesystem/file_write.c b/electrostatic-sandbox-framework/electrostatic-core/src/libs/electrostatic-primer/electronetsoft/util/filesystem/file_write.c index 4fb96b4..584604b 100644 --- a/electrostatic-sandbox-framework/electrostatic-core/src/libs/electrostatic-primer/electronetsoft/util/filesystem/file_write.c +++ b/electrostatic-sandbox-framework/electrostatic-core/src/libs/electrostatic-primer/electronetsoft/util/filesystem/file_write.c @@ -3,7 +3,7 @@ #include status_code write_from_mem(file_mem *mem, - write_op_processor *_processor, + op_processor *_processor, update_op_processor *__processor) { // pre-processing automata -- Input validation if (rvalue(mem) == NULL) { @@ -32,8 +32,8 @@ status_code write_from_mem(file_mem *mem, ssize_t total_bytes = 0; while (1) { if (*(mem->buffer + total_bytes) == mem->trailing) { - if (NULL != _processor && NULL != _processor->on_eob_reached) { - _processor->on_eob_reached(mem); + if (NULL != _processor && NULL != _processor->on_trailing_char_sampled) { + _processor->on_trailing_char_sampled(mem, &write_from_mem); } break; } @@ -58,8 +58,8 @@ status_code write_from_mem(file_mem *mem, } else if (total_bytes == (mem->n_bytes - 1)) { // All bytes are written? -> terminate // Equivalent to EOF. - if (NULL != _processor && NULL != _processor->on_eob_reached) { - _processor->on_eob_reached(mem); + if (NULL != _processor && NULL != _processor->on_last_byte_sampled) { + _processor->on_last_byte_sampled(mem, &write_from_mem); } break; } else { From d8472eb5ca4ea049f8b80d7877f8e7040ebbed85 Mon Sep 17 00:00:00 2001 From: pavl-g Date: Sun, 12 Oct 2025 11:52:35 -0500 Subject: [PATCH 6/9] electrostatic-examples: applied API changes on the examples --- .../src/linux/hello_file_read.c | 18 +++++-- .../src/linux/hello_file_write.c | 24 ++++----- .../src/linux/hello_process_serialization.c | 50 +++++++++++-------- 3 files changed, 55 insertions(+), 37 deletions(-) diff --git a/electrostatic-sandbox-framework/electrostatic-examples/src/linux/hello_file_read.c b/electrostatic-sandbox-framework/electrostatic-examples/src/linux/hello_file_read.c index c6c5849..cb6f5fd 100644 --- a/electrostatic-sandbox-framework/electrostatic-examples/src/linux/hello_file_read.c +++ b/electrostatic-sandbox-framework/electrostatic-examples/src/linux/hello_file_read.c @@ -49,10 +49,12 @@ static inline void on_bytes_processed(file_mem *mem, if (caller != &read_into_mem) { return; } - fprintf(stdout, "Read bytes = %s\n", mem->buffer); } -static inline void on_eof_reached(file_mem *mem) { +static inline void on_eof_reached(file_mem *mem, void *caller) { + if (caller != &read_into_mem) { + return; + } // support for stdin if (mem->fd == STDIN_FILENO && mem->n_bytes < 20) { mem->n_bytes += mem->n_bytes; @@ -61,6 +63,13 @@ static inline void on_eof_reached(file_mem *mem) { } fprintf(stdout, "Read Bytes after EOF: %s\n", mem->buffer); fprintf(stdout, "EOF Reached!\n"); +} + +static inline void op_postprocessor(file_mem *mem, void *caller) { + if (&read_into_mem != caller) { + return; + } + // deallocates memory here! if (NULL != mem->buffer) { free(mem->buffer); @@ -87,9 +96,10 @@ int main() { .__continue_after_eof = 0 }; - read_op_processor _processor = { + op_processor _processor = { .on_bytes_processed = &on_bytes_processed, - .on_eof_reached = &on_eof_reached + .on_trailing_char_sampled = &on_eof_reached, + .op_postprocessor = &op_postprocessor }; update_op_processor __processor = { diff --git a/electrostatic-sandbox-framework/electrostatic-examples/src/linux/hello_file_write.c b/electrostatic-sandbox-framework/electrostatic-examples/src/linux/hello_file_write.c index 78954ba..3111c66 100644 --- a/electrostatic-sandbox-framework/electrostatic-examples/src/linux/hello_file_write.c +++ b/electrostatic-sandbox-framework/electrostatic-examples/src/linux/hello_file_write.c @@ -11,8 +11,7 @@ #include #include -static inline void update_model_preprocessor(file_mem *mem, - void *caller) { +static inline void op_preprocessor(file_mem *mem, void *caller) { if (caller != &write_from_mem) { return; } @@ -43,12 +42,14 @@ static inline void on_bytes_processed(file_mem *mem, ssize_t bytes, fprintf(stdout, "%s", mem->buffer); } -static inline void on_eob_reached(file_mem *mem) { +static inline void on_eob_reached(file_mem *mem, void *caller) { + if (caller != &write_from_mem) { + return; + } fprintf(stdout, "EOB Reached!\n"); } -static inline void update_model_postprocessor(file_mem *mem, - void *caller) { +static inline void op_postprocessor(file_mem *mem, void *caller) { if (caller != &write_from_mem) { return; } @@ -69,17 +70,14 @@ int main() { .__auto_update_attrs = 1, }; - write_op_processor _processor = { + op_processor _processor = { .on_bytes_processed = &on_bytes_processed, - .on_eob_reached = &on_eob_reached, - }; - - update_op_processor __processor = { - .update_model_preprocessor = &update_model_preprocessor, - .update_model_postprocessor = &update_model_postprocessor, + .on_trailing_char_sampled = &on_eob_reached, + .op_preprocessor = &op_preprocessor, + .op_postprocessor = &op_postprocessor }; - status_code __status = write_from_mem(&_file_mem, &_processor, &__processor); + status_code __status = write_from_mem(&_file_mem, &_processor, NULL); if (PASS != __status) { fprintf(stderr, "Error Encountered: %s\n", strerror(__status)); diff --git a/electrostatic-sandbox-framework/electrostatic-examples/src/linux/hello_process_serialization.c b/electrostatic-sandbox-framework/electrostatic-examples/src/linux/hello_process_serialization.c index fa938ce..f6f39bb 100644 --- a/electrostatic-sandbox-framework/electrostatic-examples/src/linux/hello_process_serialization.c +++ b/electrostatic-sandbox-framework/electrostatic-examples/src/linux/hello_process_serialization.c @@ -17,7 +17,17 @@ static inline void on_error_encountered(file_mem *mem, int err, void *caller) { fprintf(stderr, "%s\n", strerror(err)); } -static inline void on_eof_reached(file_mem *mem) { +static inline void op_postprocessor(file_mem *mem, void *caller) { + close(mem->fd); + + if (&read_into_mem != caller) { + return; + } + free(mem->buffer); + mem->buffer = NULL; +} + +static inline void on_eof_reached(file_mem *mem, void *caller) { // -------- Start the polling block -------- uint8_t __predicate = mem->bytes_processed == 0; if (__predicate) { @@ -27,19 +37,15 @@ static inline void on_eof_reached(file_mem *mem) { } // -------- End of the polling block -------- fprintf(stdout, "Read process = %s\n", mem->buffer); - free(mem->buffer); - mem->buffer = NULL; mem->__continue_after_eof = 0; - close(mem->fd); } -static inline void on_eob_reached(file_mem *mem) { +static inline void on_eob_reached(file_mem *mem, void *caller) { fprintf(stdout, "Write process = %s", mem->buffer); - close(mem->fd); } -static inline void on_last_char_sampled(file_mem *mem, void *caller) { +static inline void on_last_byte_sampled(file_mem *mem, void *caller) { mem->n_bytes += mem->n_bytes; mem->buffer = realloc(mem->buffer, sizeof (char) * (mem->n_bytes)); } @@ -49,19 +55,22 @@ static inline void on_bytes_processed(file_mem *mem, ssize_t bytes, } static inline void writing_process(pipe_serializer *serializer) { + fprintf(stdout, "%s\n", "Started the writing process..."); serializer->write_end.buffer = "Hello From the Electrostatic-Sandbox SDK Pipe Serializer...\n"; serializer->write_end.n_bytes = strlen(serializer->write_end.buffer) + 1; sleep(10); // simulate jobs by sleeping the process - write_from_mem(&serializer->write_end, &(write_op_processor ) { - .on_eob_reached = &on_eob_reached, - .on_bytes_processed = &on_bytes_processed + write_from_mem(&serializer->write_end, &(op_processor) { + .on_trailing_char_sampled = &on_eob_reached, + .on_bytes_processed = &on_bytes_processed, + .op_postprocessor = &op_postprocessor }, NULL); } static inline void reading_process(pipe_serializer *serializer) { + fprintf(stdout, "%s\n", "Started the reading process..."); serializer->read_end.n_bytes = 2; serializer->read_end.buffer = calloc(serializer->read_end.n_bytes, sizeof (char)); @@ -71,11 +80,12 @@ static inline void reading_process(pipe_serializer *serializer) { exit(errno); } - status_code __status = read_into_mem(&serializer->read_end, &(read_op_processor) { - .on_eof_reached = &on_eof_reached, - .on_last_char_sampled = &on_last_char_sampled, + status_code __status = read_into_mem(&serializer->read_end, &(op_processor) { + .on_trailing_char_sampled = &on_eof_reached, + .on_last_byte_sampled = &on_last_byte_sampled, .on_bytes_processed = &on_bytes_processed, - .on_error_encountered = &on_error_encountered + .on_error_encountered = &on_error_encountered, + .op_postprocessor = &op_postprocessor }, NULL); if (PASS != __status) { @@ -88,13 +98,12 @@ static inline void serializer_init_preprocessor(pipe_serializer *serializer) { // preprocessing check the file and remove it if it exists // for the test and the showcase + serializer->write_end.fd = open("/home/pavl-g/Desktop/test.txt", O_RDWR | O_CREAT); + serializer->read_end.fd = open("/home/pavl-g/Desktop/test.txt", O_RDONLY); } static inline void serializer_init_postprocessor(pipe_serializer *serializer) { // do the read/write testing here! - serializer->write_end.fd = open("/home/pavl-g/Desktop/test.txt", O_RDWR | O_CREAT); - serializer->read_end.fd = open("/home/pavl-g/Desktop/test.txt", O_RDONLY); - // start another process pid_t pid = fork(); if (pid > 0) { // parent process @@ -102,7 +111,7 @@ static inline void serializer_init_postprocessor(pipe_serializer *serializer) { } else if (pid == 0) { // subprocess reading_process(serializer); } else { - + exit(pid); } } @@ -120,7 +129,8 @@ int main() { .fd = -1, .trailing = '\0', .buffer = NULL, - .n_bytes = -1 + .n_bytes = -1, + .__auto_update_attrs = 0 } }; @@ -134,7 +144,7 @@ int main() { status_code __status_code = init_serializer(&serializer, &_processor, &__processor); if (PASS != __status_code) { - return __status_code; + fprintf(stderr, "%s\n", strerror(__status_code)); } return 0; From 7c36f67133c4a757d5420386826998977ec7cebe Mon Sep 17 00:00:00 2001 From: pavl-g Date: Tue, 14 Oct 2025 01:27:17 -0500 Subject: [PATCH 7/9] file_verify.h: introduced a system call to get path from fd --- .../util/filesystem/file_verify.h | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/electrostatic-sandbox-framework/electrostatic-core/src/include/electrostatic/electronetsoft/util/filesystem/file_verify.h b/electrostatic-sandbox-framework/electrostatic-core/src/include/electrostatic/electronetsoft/util/filesystem/file_verify.h index 52d49b3..877b504 100644 --- a/electrostatic-sandbox-framework/electrostatic-core/src/include/electrostatic/electronetsoft/util/filesystem/file_verify.h +++ b/electrostatic-sandbox-framework/electrostatic-core/src/include/electrostatic/electronetsoft/util/filesystem/file_verify.h @@ -10,6 +10,23 @@ extern "C" { #include #include #include +#include +#include +#include + +static inline int get_path_from_fd(int fd, char *buf, ssize_t len) { + if (fd < 0 || NULL == buf) { + return -1; + } + + pid_t pid = getpid(); + + char path[255]; + memset(path, '\0', sizeof (path)); + sprintf(path, "/proc/%d/fd/%d", pid, fd); + + return readlink(path, buf, len) > 0; +} static inline int is_existential(const char *file) { if (file == NULL) { @@ -19,7 +36,23 @@ static inline int is_existential(const char *file) { return stat(file, &statbuf) == 0; } +static inline int is_fd_existential(int fd) { + if (fd < 0) { + return -1; + } + char path[255]; + memset(path, '\0', sizeof (path)); + int __status = get_path_from_fd(fd, path, sizeof (path)); + if (!__status) { + return -1; + } + return is_existential(path); +} + static inline int is_fexistential(int fd) { + if (fd < 0) { + return -1; + } struct stat statbuf; return fstat(fd, &statbuf) == 0; } From 9853c127a715ad9e661d769b410d0c9c7be0ae97 Mon Sep 17 00:00:00 2001 From: pavl-g Date: Tue, 14 Oct 2025 01:29:11 -0500 Subject: [PATCH 8/9] electronetsoft/filesystem: added anti-failure patterns --- .../electronetsoft/util/filesystem/file_read.c | 7 +++++++ .../electronetsoft/util/filesystem/file_write.c | 7 +++++++ 2 files changed, 14 insertions(+) diff --git a/electrostatic-sandbox-framework/electrostatic-core/src/libs/electrostatic-primer/electronetsoft/util/filesystem/file_read.c b/electrostatic-sandbox-framework/electrostatic-core/src/libs/electrostatic-primer/electronetsoft/util/filesystem/file_read.c index adce94d..a660663 100644 --- a/electrostatic-sandbox-framework/electrostatic-core/src/libs/electrostatic-primer/electronetsoft/util/filesystem/file_read.c +++ b/electrostatic-sandbox-framework/electrostatic-core/src/libs/electrostatic-primer/electronetsoft/util/filesystem/file_read.c @@ -32,6 +32,13 @@ status_code read_into_mem(file_mem *mem, ssize_t read_bytes = 0; ssize_t total_bytes = 0; while (1) { + // sanity check the file + if (!is_fd_existential(mem->fd)) { + if (NULL != _processor && NULL != _processor->on_error_encountered) { + _processor->on_error_encountered(mem, errno, &read_into_mem); + } + return errno; + } read_bytes = read(mem->fd, (mem->buffer + total_bytes), /* Advance linearly over the File Mem model buffer * with the read bytes */ (mem->n_bytes - 1) - total_bytes); /* Retro-advance on the number of available bytes diff --git a/electrostatic-sandbox-framework/electrostatic-core/src/libs/electrostatic-primer/electronetsoft/util/filesystem/file_write.c b/electrostatic-sandbox-framework/electrostatic-core/src/libs/electrostatic-primer/electronetsoft/util/filesystem/file_write.c index 584604b..b6074d2 100644 --- a/electrostatic-sandbox-framework/electrostatic-core/src/libs/electrostatic-primer/electronetsoft/util/filesystem/file_write.c +++ b/electrostatic-sandbox-framework/electrostatic-core/src/libs/electrostatic-primer/electronetsoft/util/filesystem/file_write.c @@ -31,6 +31,13 @@ status_code write_from_mem(file_mem *mem, ssize_t written_bytes = 0; ssize_t total_bytes = 0; while (1) { + if (!is_fd_existential(mem->fd)) { + if (NULL != _processor && NULL != _processor->on_error_encountered) { + _processor->on_error_encountered(mem, errno, &read_into_mem); + } + return errno; + } + if (*(mem->buffer + total_bytes) == mem->trailing) { if (NULL != _processor && NULL != _processor->on_trailing_char_sampled) { _processor->on_trailing_char_sampled(mem, &write_from_mem); From 7d5c96c58c3d2730b6d3b9ddd1a821742918758b Mon Sep 17 00:00:00 2001 From: pavl-g Date: Tue, 14 Oct 2025 01:30:27 -0500 Subject: [PATCH 9/9] electrostatic-examples: added an example for getting a path from an opened file --- .../src/linux/hello_get_path_from_fd.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 electrostatic-sandbox-framework/electrostatic-examples/src/linux/hello_get_path_from_fd.c diff --git a/electrostatic-sandbox-framework/electrostatic-examples/src/linux/hello_get_path_from_fd.c b/electrostatic-sandbox-framework/electrostatic-examples/src/linux/hello_get_path_from_fd.c new file mode 100644 index 0000000..af5995c --- /dev/null +++ b/electrostatic-sandbox-framework/electrostatic-examples/src/linux/hello_get_path_from_fd.c @@ -0,0 +1,17 @@ +#include +#include +#include +#include +#include + +int main() { + char path[128]; + int __return = get_path_from_fd(STDOUT_FILENO, path, sizeof (path)); + if (!__return) { + fprintf(stderr, "%s\n", strerror(errno)); + exit(errno); + } + fprintf(stdout, "The path of fd=%d is %s\n", STDOUT_FILENO, path); + + return 0; +} \ No newline at end of file