From 88e596b6d14f0e4ce35cf6e98e71327103e58a6f Mon Sep 17 00:00:00 2001 From: Miroslav Shubernetskiy Date: Thu, 2 Nov 2023 21:53:07 -0400 Subject: [PATCH 1/3] adding nestInDir option to getNewTempFile --- nimutils/managedtmp.nim | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/nimutils/managedtmp.nim b/nimutils/managedtmp.nim index 324b5a3..b5c9bb6 100644 --- a/nimutils/managedtmp.nim +++ b/nimutils/managedtmp.nim @@ -18,9 +18,19 @@ proc getNewTempDir*(tmpFilePrefix = defaultTmpPrefix, result = createTempDir(tmpFilePrefix, tmpFileSuffix) managedTmpDirs.add(result) -proc getNewTempFile*(prefix = defaultTmpPrefix, suffix = defaultTmpSuffix, - autoClean = true): (FileStream, string) = - var (f, path) = createTempFile(prefix, suffix) +proc getNewTempFile*(prefix = defaultTmpPrefix, + suffix = defaultTmpSuffix, + autoClean = true, + nestInDir: bool = false): (FileStream, string) = + var dir = "" + # in some cases such as docker, due to snap permissions + # it does not have access directly to files created in /tmp + # but it can access those files if they are nested in another + # nested dir + if nestInDir: + dir = genTempPath(prefix, suffix) + createDir(dir) + var (f, path) = createTempFile(prefix, suffix, dir = dir) if autoClean: managedTmpFiles.add(path) From 8cc4cb15c2648507797693b8a7b5aca8825b8b6c Mon Sep 17 00:00:00 2001 From: Miroslav Shubernetskiy Date: Fri, 3 Nov 2023 09:10:34 -0400 Subject: [PATCH 2/3] fixing linting violations --- nimutils/hex.c | 8 +-- nimutils/subproc.c | 80 +++++++++++++------------- nimutils/switchboard.c | 126 ++++++++++++++++++++--------------------- nimutils/switchboard.h | 19 +++---- nimutils/test.c | 2 +- 5 files changed, 117 insertions(+), 118 deletions(-) diff --git a/nimutils/hex.c b/nimutils/hex.c index cc8eca5..1f6b743 100644 --- a/nimutils/hex.c +++ b/nimutils/hex.c @@ -38,7 +38,7 @@ add_offset(char **optr, uint64_t start_offset, uint64_t offset_len, uint8_t chr; char *p = *optr; uint64_t value = start_offset + (uint64_t)(line * cpl); - uint64_t ix = offset_len; + uint64_t ix = offset_len; char buf[offset_len]; @@ -51,7 +51,7 @@ add_offset(char **optr, uint64_t start_offset, uint64_t offset_len, value = value >> 4; buf[--ix] = hex_map[chr]; } - + for (ix = 0; ix < offset_len; ix++) { *p++ = buf[ix]; } @@ -72,7 +72,7 @@ add_offset(char **optr, uint64_t start_offset, uint64_t offset_len, char * chexl(void *ptr, unsigned int len, unsigned int start_offset, unsigned int width, char *prefix) { - + struct winsize ws; uint64_t offset_len = calculate_size_prefix(len, start_offset); uint64_t chars_per_line; @@ -257,7 +257,7 @@ chex(void *ptr, unsigned int len, unsigned int start_offset, char buf[1024] = {0, }; sprintf(buf, "Dump of %d bytes at: %p\n", len, ptr); - + return chexl(ptr, len, start_offset, width, buf); } diff --git a/nimutils/subproc.c b/nimutils/subproc.c index e3454bb..7ec9686 100644 --- a/nimutils/subproc.c +++ b/nimutils/subproc.c @@ -57,9 +57,9 @@ subproc_set_envp(subprocess_t *ctx, char *envp[]) if (ctx->run) { return false; } - + ctx->envp = envp; - + return true; } @@ -74,7 +74,7 @@ bool subproc_pass_to_stdin(subprocess_t *ctx, char *str, size_t len, bool close_fd) { if (ctx->str_waiting || ctx->sb.done) { - return false; + return false; } if (ctx->run && close_fd) { @@ -83,7 +83,7 @@ subproc_pass_to_stdin(subprocess_t *ctx, char *str, size_t len, bool close_fd) sb_init_party_input_buf(&ctx->sb, &ctx->str_stdin, str, len, false, close_fd); - + if (ctx->run) { return sb_route(&ctx->sb, &ctx->str_stdin, &ctx->subproc_stdin); } else { @@ -92,7 +92,7 @@ subproc_pass_to_stdin(subprocess_t *ctx, char *str, size_t len, bool close_fd) if (close_fd) { ctx->pty_stdin_pipe = true; } - + return true; } } @@ -151,7 +151,7 @@ subproc_set_capture(subprocess_t *ctx, unsigned char which, bool combine) if (ctx->run || which > SP_IO_ALL) { return false; } - + ctx->capture = which; ctx->pt_all_to_stdout = combine; @@ -165,7 +165,7 @@ subproc_set_io_callback(subprocess_t *ctx, unsigned char which, if (ctx->run || which > SP_IO_ALL) { return false; } - + deferred_cb_t *cbinfo = (deferred_cb_t *)malloc(sizeof(deferred_cb_t)); cbinfo->next = ctx->deferred_cbs; @@ -173,7 +173,7 @@ subproc_set_io_callback(subprocess_t *ctx, unsigned char which, cbinfo->cb = cb; ctx->deferred_cbs = cbinfo; - + return true; } @@ -228,7 +228,7 @@ setup_subscriptions(subprocess_t *ctx, bool pty) if (ctx->pt_all_to_stdout) { stderr_dst = &ctx->parent_stdout; } - + if (ctx->passthrough) { if (ctx->passthrough & SP_IO_STDIN) { if (pty) { @@ -255,7 +255,7 @@ setup_subscriptions(subprocess_t *ctx, bool pty) sb_init_party_output_buf(&ctx->sb, &ctx->capture_stdout, "stdout", CAP_ALLOC); } - + if (ctx->combine_captures) { if (!(ctx->capture & SP_IO_STDOUT) && ctx->capture & SP_IO_STDERR) { @@ -264,7 +264,7 @@ setup_subscriptions(subprocess_t *ctx, bool pty) "stdout", CAP_ALLOC); } } - + stderr_dst = &ctx->capture_stdout; } else { @@ -272,10 +272,10 @@ setup_subscriptions(subprocess_t *ctx, bool pty) sb_init_party_output_buf(&ctx->sb, &ctx->capture_stderr, "stderr", CAP_ALLOC); } - + stderr_dst = &ctx->capture_stderr; } - + if (ctx->capture & SP_IO_STDIN) { sb_route(&ctx->sb, &ctx->parent_stdin, &ctx->capture_stdin); } @@ -286,12 +286,12 @@ setup_subscriptions(subprocess_t *ctx, bool pty) sb_route(&ctx->sb, &ctx->subproc_stderr, stderr_dst); } } - + if (ctx->str_waiting) { sb_route(&ctx->sb, &ctx->str_stdin, &ctx->subproc_stdin); ctx->str_waiting = false; } - + // Make sure calls to the API know we've started! ctx->run = true; } @@ -369,10 +369,10 @@ subproc_spawn_fork(subprocess_t *ctx) sb_monitor_pid(&ctx->sb, pid, &ctx->subproc_stdin, &ctx->subproc_stdout, &ctx->subproc_stderr, true); subproc_install_callbacks(ctx); - setup_subscriptions(ctx, false); + setup_subscriptions(ctx, false); } else { close(stdin_pipe[1]); - close(stdout_pipe[0]); + close(stdout_pipe[0]); close(stderr_pipe[0]); dup2(stdin_pipe[0], 0); dup2(stdout_pipe[1], 1); @@ -397,7 +397,7 @@ subproc_spawn_forkpty(subprocess_t *ctx) if (ctx->pty_stdin_pipe) { pipe(stdin_pipe); } - + // We're going to use a pipe for stderr to get a separate // stream. The tty FD will be stdin and stdout for the child // process. @@ -408,8 +408,8 @@ subproc_spawn_forkpty(subprocess_t *ctx) // Note that this means the child process will see isatty() return // true for stdin and stdout, but not stderr. setvbuf(stdout, NULL, _IONBF, (size_t) 0); - setvbuf(stdin, NULL, _IONBF, (size_t) 0); - + setvbuf(stdin, NULL, _IONBF, (size_t) 0); + if(!isatty(0)) { term_ptr = NULL; win_ptr = NULL; @@ -426,17 +426,17 @@ subproc_spawn_forkpty(subprocess_t *ctx) sb_init_party_fd(&ctx->sb, &ctx->subproc_stdin, stdin_pipe[1], O_WRONLY, false, false); } - + ctx->pty_fd = pty_fd; - + sb_init_party_fd(&ctx->sb, &ctx->subproc_stdout, pty_fd, O_RDWR, true, true); - + sb_monitor_pid(&ctx->sb, pid, &ctx->subproc_stdout, - &ctx->subproc_stdout, NULL, true); + &ctx->subproc_stdout, NULL, true); subproc_install_callbacks(ctx); setup_subscriptions(ctx, true); - + tcgetattr(0, &ctx->saved_termcap); termcap.c_lflag &= ~(ECHO|ICANON); termcap.c_cc[VMIN] = 0; @@ -444,13 +444,13 @@ subproc_spawn_forkpty(subprocess_t *ctx) tcsetattr(0, TCSANOW, term_ptr); int flags = fcntl(pty_fd, F_GETFL, 0) | O_NONBLOCK; fcntl(pty_fd, F_SETFL, flags); - + } else { if (ctx->pty_stdin_pipe) { close(stdin_pipe[1]); dup2(stdin_pipe[0], 0); } - + termcap.c_lflag &= ~(ICANON | ISIG | IEXTEN); termcap.c_oflag &= ~OPOST; termcap.c_cc[VMIN] = 0; @@ -476,7 +476,7 @@ termcap_set_typical_parent() { struct termios cap; tcgetattr(0, &cap); - + cap.c_lflag &= ~(ECHO|ICANON); cap.c_cc[VMIN] = 0; cap.c_cc[VTIME] = 0; @@ -520,7 +520,7 @@ subproc_poll(subprocess_t *ctx) void subproc_prepare_results(subprocess_t *ctx) { - sb_prepare_results(&ctx->sb); + sb_prepare_results(&ctx->sb); // Post-run cleanup. if (ctx->use_pty) { @@ -622,7 +622,7 @@ subproc_get_exit(subprocess_t *ctx, bool wait_for_exit) return -1; } - process_status_check(subproc, wait_for_exit); + process_status_check(subproc, wait_for_exit); return subproc->exit_status; } @@ -634,7 +634,7 @@ subproc_get_errno(subprocess_t *ctx, bool wait_for_exit) if (!subproc) { return -1; } - + process_status_check(subproc, wait_for_exit); return subproc->found_errno; } @@ -648,7 +648,7 @@ subproc_get_signal(subprocess_t *ctx, bool wait_for_exit) return -1; } - process_status_check(subproc, wait_for_exit); + process_status_check(subproc, wait_for_exit); return subproc->term_signal; } @@ -693,7 +693,7 @@ test1() { subproc_set_io_callback(&ctx, SP_IO_STDOUT, capture_tty_data); result = subproc_run(&ctx); - + while(result) { if (result->tag) { print_hex(result->contents, result->content_len, result->tag); @@ -710,8 +710,8 @@ test1() { int test2() { char *cmd = "/bin/cat"; - char *args[] = { "/bin/cat", "-", 0 }; - + char *args[] = { "/bin/cat", "-", 0 }; + subprocess_t ctx; sb_result_t *result; struct timeval timeout = {.tv_sec = 0, .tv_usec = 1000 }; @@ -724,7 +724,7 @@ test2() { subproc_set_io_callback(&ctx, SP_IO_STDOUT, capture_tty_data); result = subproc_run(&ctx); - + while(result) { if (result->tag) { print_hex(result->contents, result->content_len, result->tag); @@ -754,7 +754,7 @@ test3() { subproc_set_io_callback(&ctx, SP_IO_STDOUT, capture_tty_data); result = subproc_run(&ctx); - + while(result) { if (result->tag) { print_hex(result->contents, result->content_len, result->tag); @@ -771,8 +771,8 @@ test3() { int test4() { char *cmd = "/bin/cat"; - char *args[] = { "/bin/cat", "-", 0 }; - + char *args[] = { "/bin/cat", "-", 0 }; + subprocess_t ctx; sb_result_t *result; struct timeval timeout = {.tv_sec = 0, .tv_usec = 1000 }; @@ -785,7 +785,7 @@ test4() { subproc_set_io_callback(&ctx, SP_IO_STDOUT, capture_tty_data); result = subproc_run(&ctx); - + while(result) { if (result->tag) { print_hex(result->contents, result->content_len, result->tag); diff --git a/nimutils/switchboard.c b/nimutils/switchboard.c index ed5f4c1..a6ce7c8 100644 --- a/nimutils/switchboard.c +++ b/nimutils/switchboard.c @@ -86,7 +86,7 @@ party_fd(party_t *party) return party->info.fdinfo.fd; } else { - return party->info.listenerinfo.fd; + return party->info.listenerinfo.fd; } } @@ -178,12 +178,12 @@ sb_init_party_listener(switchboard_t *ctx, party_t *party, int sockfd, ctx->parties_for_reading = party; party->can_read_from_it = true; party->can_write_to_it = false; - + listener_party_t *lobj = get_listener_obj(party); lobj->fd = sockfd; lobj->accept_cb = (accept_cb_decl)callback; lobj->saved_flags = fcntl(sockfd, F_GETFL, 0); - + int flags = lobj->saved_flags | O_NONBLOCK; fcntl(sockfd, F_SETFL, flags); @@ -202,7 +202,7 @@ sb_new_party_listener(switchboard_t *ctx, int sockfd, accept_cb_t callback, party_t *result = (party_t *)calloc(sizeof(party_t), 1); sb_init_party_listener(ctx, result, sockfd, callback, stop_when_closed, close_on_destroy); - + return result; } @@ -224,7 +224,7 @@ sb_init_party_fd(switchboard_t *ctx, party_t *party, int fd, int perms, bool stop_when_closed, bool close_on_destroy) { memset(party, 0, sizeof(party_t)); - + party->party_type = PT_FD; party->close_on_destroy = close_on_destroy; party->can_read_from_it = false; @@ -240,10 +240,10 @@ sb_init_party_fd(switchboard_t *ctx, party_t *party, int fd, int perms, party->open_for_read = true; party->can_read_from_it = true; register_read_fd(ctx, party); - - } + + } if (perms != O_RDONLY) { - party->open_for_write = true; + party->open_for_write = true; party->can_write_to_it = true; register_writer_fd(ctx, party); } @@ -274,11 +274,11 @@ sb_init_party_input_buf(switchboard_t *ctx, party_t *party, char *input, size_t len, bool free, bool close_fd_when_done) { party->open_for_read = true; - party->open_for_write = false; + party->open_for_write = false; party->can_read_from_it = true; party->can_write_to_it = false; party->party_type = PT_STRING; - + str_src_party_t *sobj = get_sstr_obj(party); sobj->strbuf = input; sobj->len = len; @@ -325,7 +325,7 @@ sb_init_party_output_buf(switchboard_t *ctx, party_t *party, char *tag, party->can_read_from_it = false; party->can_write_to_it = true; - party->open_for_read = false; + party->open_for_read = false; party->open_for_write = true; party->party_type = PT_STRING; @@ -361,8 +361,8 @@ sb_new_party_output_buf(switchboard_t *ctx, char *tag, size_t buflen) void sb_init_party_callback(switchboard_t *ctx, party_t *party, switchboard_cb_t cb) { - party->open_for_read = false; - party->open_for_write = true; + party->open_for_read = false; + party->open_for_write = true; party->can_read_from_it = false; party->can_write_to_it = true; party->party_type = PT_CALLBACK; @@ -385,7 +385,7 @@ sb_monitor_pid(switchboard_t *ctx, pid_t pid, party_t *stdin_fd_party, monitor->pid = pid; monitor->stdin_fd_party = stdin_fd_party; monitor->stdout_fd_party = stdout_fd_party; - monitor->stderr_fd_party = stderr_fd_party; + monitor->stderr_fd_party = stderr_fd_party; monitor->next = ctx->pid_watch_list; monitor->shutdown_when_closed = shutdown; ctx->pid_watch_list = monitor; @@ -503,15 +503,15 @@ publish(switchboard_t *ctx, char *buf, ssize_t len, party_t *party) } else { msg->len = 0; } - + msg->next = NULL; - + if (receiver->first_msg == NULL) { receiver->first_msg = msg; } else { receiver->last_msg->next = msg; } - + receiver->last_msg = msg; #ifdef SB_DEBUG @@ -545,20 +545,20 @@ sb_route(switchboard_t *ctx, party_t *read_from, party_t *write_to) if (read_from == NULL || write_to == NULL) { return false; } - + if (!read_from->open_for_read || !write_to->open_for_write) { return false; } - + if (read_from->party_type & (PT_LISTENER | PT_CALLBACK)) { return false; } - + if (write_to->party_type & PT_LISTENER) { return false; } - + if (read_from->party_type == PT_STRING) { if (write_to->party_type != PT_FD) { return false; @@ -568,7 +568,7 @@ sb_route(switchboard_t *ctx, party_t *read_from, party_t *write_to) char *p = s->strbuf; char *end = p + remaining; int total = 0; - + while (p < (end - SB_MSG_LEN)) { publish(ctx, p, SB_MSG_LEN, write_to); p += SB_MSG_LEN; @@ -582,7 +582,7 @@ sb_route(switchboard_t *ctx, party_t *read_from, party_t *write_to) if (s->close_fd_when_done) { publish(ctx, NULL, 0, write_to); } - + return true; } else { @@ -618,7 +618,7 @@ sb_route(switchboard_t *ctx, party_t *read_from, party_t *write_to) r_fd_obj->subscribers = subscription; } - + return true; } @@ -648,16 +648,16 @@ sb_init(switchboard_t *ctx, size_t heap_size) * We also track to make sure that *some* party is open for either * reading that has a valid subscriber, OR at least one party open for * writing that has enqueued messages. - * + * * If neither of these conditions are met, we shut down. * down. */ -static inline void +static inline void set_fdinfo(switchboard_t *ctx) { party_t *cur; bool open = false; // If this stays false, we give up. - + FD_ZERO(&ctx->readset); FD_ZERO(&ctx->writeset); @@ -666,7 +666,7 @@ set_fdinfo(switchboard_t *ctx) while (cur != NULL) { fd_party_t *r_fd_obj = get_fd_obj(cur); subscription_t *subscribers = r_fd_obj->subscribers; - + if (cur->open_for_read) { while (subscribers != NULL) { if (cur->party_type == PT_FD) { @@ -676,7 +676,7 @@ set_fdinfo(switchboard_t *ctx) FD_SET(party_fd(cur), &ctx->readset); open = true; break; - + } } else { open = true; @@ -767,7 +767,7 @@ add_data_to_string_out(str_dst_party_t *party, char *buf, ssize_t len) { printf("tag = %s, buf = %s, len = %d\n", party->tag, buf, len); print_hex(buf, len, ">> add_data_to_string_out: "); #endif - + if (party->ix + len >= party->len) { int newlen = party->len + len + party->step; int rem = newlen % party->step; @@ -781,7 +781,7 @@ add_data_to_string_out(str_dst_party_t *party, char *buf, ssize_t len) { #endif return; } - + party->len = newlen; memset(party->strbuf + party->ix, 0, newlen - party->ix); } @@ -822,7 +822,7 @@ handle_one_read(switchboard_t *ctx, party_t *party) { char buf[SB_MSG_LEN + 1] = {0, }; ssize_t read_result = read_one(party_fd(party), buf, SB_MSG_LEN); - + if (read_result <= 0) { party->found_errno = errno; party->open_for_read = false; @@ -838,10 +838,10 @@ handle_one_read(switchboard_t *ctx, party_t *party) printf(">>One read from fd %d", party_fd(party)); print_hex(buf, read_result, ": "); #endif - + fd_party_t *obj = get_fd_obj(party); subscription_t *sublist = obj->subscribers; - + while (sublist != NULL) { party_t *sub = sublist->subscriber; switch(sub->party_type) { @@ -889,7 +889,7 @@ handle_one_accept(switchboard_t *ctx, party_t *party) break; } party->found_errno = errno; - party->open_for_read = false; + party->open_for_read = false; break; } } @@ -913,14 +913,14 @@ handle_one_write(switchboard_t *ctx, party_t *party) if (!msg) { return; } - + if (msg && (msg->next == NULL || msg == fdobj->last_msg)) { fdobj->first_msg = NULL; fdobj->last_msg = NULL; } else { fdobj->first_msg = msg->next; } - + if (!msg->len) { /* Real messages should always have lengths. We get passed a * zero-length message only if a string was fed in for input, @@ -944,7 +944,7 @@ handle_one_write(switchboard_t *ctx, party_t *party) printf("Writing from queue to fd %d", party_fd(party)); print_hex(msg->data, msg->len, ": "); #endif - + if (!write_data(party_fd(party), msg->data, msg->len)) { party->found_errno = errno; @@ -952,7 +952,7 @@ handle_one_write(switchboard_t *ctx, party_t *party) if (!party->open_for_read) { close(party_fd(party)); } - + if (party->stop_on_close) { ctx->done = true; } @@ -965,7 +965,7 @@ handle_one_write(switchboard_t *ctx, party_t *party) while (to_free) { sb_msg_t *next = to_free->next; - + free_msg_slot(ctx, to_free); to_free = next; } @@ -986,7 +986,7 @@ handle_ready_reads(switchboard_t *ctx) while(reader != NULL) { if (reader_ready(ctx, reader)) { FD_CLR(party_fd(reader), &ctx->readset); - + if (reader->party_type == PT_FD) { handle_one_read(ctx, reader); } else { @@ -1006,7 +1006,7 @@ handle_ready_writes(switchboard_t *ctx) while (writer != NULL) { if (writer_ready(ctx, writer)) { FD_CLR(party_fd(writer), &ctx->readset); - + handle_one_write(ctx, writer); } writer = writer->next_writer; @@ -1021,12 +1021,12 @@ subproc_mark_closed(monitor_t *proc, bool error) if (error) { proc->found_errno = errno; - } + } // We can mark the write side of this as closed right away. But we // can't do the same w/ the read side; we need to drain them // first. - + if (proc->stdin_fd_party) { proc->stdin_fd_party->open_for_write = false; } @@ -1053,22 +1053,22 @@ sb_default_check_exit_conditions(switchboard_t *ctx) if (subproc->stdout_fd_party && subproc->stdout_fd_party->open_for_read) { return false; } else { - #if defined(SB_DEBUG) || defined(SB_TEST) + #if defined(SB_DEBUG) || defined(SB_TEST) printf("Subproc stdout is closed for business.\n"); #endif } if (subproc->stderr_fd_party && subproc->stderr_fd_party->open_for_read) { - #if defined(SB_DEBUG) || defined(SB_TEST) + #if defined(SB_DEBUG) || defined(SB_TEST) printf("Read side of stderr is still open.\n"); #endif return false; } - + party_t *writers = ctx->parties_for_writing; while (writers) { - + if (writers->party_type == PT_FD) { fd_party_t *fd_writer = get_fd_obj(writers); @@ -1076,12 +1076,12 @@ sb_default_check_exit_conditions(switchboard_t *ctx) printf("checking for pending writes to fd %d\n", fd_writer->fd); #endif if (writers->open_for_write && fd_writer->first_msg) { - #if defined(SB_DEBUG) || defined(SB_TEST) + #if defined(SB_DEBUG) || defined(SB_TEST) printf("Don't exit yet.\n"); #endif return false; } - #if defined(SB_DEBUG) || defined(SB_TEST) + #if defined(SB_DEBUG) || defined(SB_TEST) if (writers->open_for_write) { printf("No queue.\n"); } @@ -1108,7 +1108,7 @@ process_status_check(monitor_t *subproc, bool wait_on_exit) } else { flag = WNOHANG; } - + if (subproc->closed) { return; } @@ -1127,7 +1127,7 @@ process_status_check(monitor_t *subproc, bool wait_on_exit) default: subproc->closed = true; subproc->exit_status = WEXITSTATUS(stat_info); - + if (WIFSIGNALED(stat_info)) { subproc->term_signal = WTERMSIG(stat_info); } @@ -1163,7 +1163,7 @@ handle_loop_end(switchboard_t *ctx) if (!ctx->progress_callback) { ctx->progress_callback = sb_default_check_exit_conditions; } - + if (!ctx->progress_on_timeout_only) { if ((*ctx->progress_callback)(ctx)) { ctx->done = true; @@ -1187,7 +1187,7 @@ is_registered_writer(switchboard_t *ctx, party_t *target) } cur = cur->next_writer; } - + return false; } @@ -1217,7 +1217,7 @@ sb_destroy(switchboard_t *ctx, bool free_parties) party_t *cur, *next; cur = ctx->parties_for_reading; - + while (cur) { if (cur->close_on_destroy) { if (cur->party_type & (PT_FD | PT_LISTENER) ) { @@ -1227,7 +1227,7 @@ sb_destroy(switchboard_t *ctx, bool free_parties) fd_party_t *fdobj = get_fd_obj(cur); subscription_t *sub = fdobj->subscribers; - + while (sub->subscriber) { subscription_t *next_sub = sub->next; free(sub); @@ -1236,13 +1236,13 @@ sb_destroy(switchboard_t *ctx, bool free_parties) if (cur->party_type == PT_STRING) { str_src_party_t *sstr = get_sstr_obj(cur); - + if (sstr->strbuf != NULL) { free(sstr->strbuf); } } next = cur->next_reader; - + if (free_parties) { if (!cur->can_write_to_it || !is_registered_writer(ctx, cur)) { free(cur); @@ -1295,17 +1295,17 @@ sb_prepare_results(switchboard_t *ctx) ctx->result.num_captures = capcount; ctx->result.captures = calloc(sizeof(capture_result_t), capcount+1); - - party = ctx->party_loners; - - while (party) { + + party = ctx->party_loners; + + while (party) { if (party->party_type == PT_STRING && party->can_write_to_it) { capture_result_t *r = ctx->result.captures + ix; strobj = get_dstr_obj(party); r->tag = strobj->tag; r->len = strobj->ix; - + if (strobj->ix) { char *s = (char *)calloc(strobj->len, 1); memcpy(s, strobj->strbuf, strobj->ix); diff --git a/nimutils/switchboard.h b/nimutils/switchboard.h index 6aa45d1..8aa281d 100644 --- a/nimutils/switchboard.h +++ b/nimutils/switchboard.h @@ -16,7 +16,7 @@ #include #include -#define DEFAULT_HEAP_SIZE (256) +#define DEFAULT_HEAP_SIZE (256) #define SB_ALLOC_LEN (PIPE_BUF + sizeof(struct sb_msg_t)) #define SB_MSG_LEN PIPE_BUF @@ -134,7 +134,7 @@ typedef struct { * The union for the five party types above. */ typedef union { - str_src_party_t rstrinfo; // Strings used as an input source only + str_src_party_t rstrinfo; // Strings used as an input source only str_dst_party_t wstrinfo; // Strings used as an output sink only fd_party_t fdinfo; // Can be source, sink or both. listener_party_t listenerinfo; // We only read from it to kick off accept cb @@ -147,10 +147,10 @@ typedef union { * ran accross. This is only used for PT_FD and PT_LISTENER. * - `open` tracks whether we should deal with this party at all anymore; * it can mean the fd is closed, or that nothing is routed to it anymore. - * - `can_read_from_it` and `can_write_to_it` indicates whether a party is + * - `can_read_from_it` and `can_write_to_it` indicates whether a party is * a source (the former) or a sink (the later). Can be both, too. * - `close_on_destroy` indicates that we should call close() on any file - * descriptors when tearing down the switchboard. + * descriptors when tearing down the switchboard. * When this is set, we do not report errors in close(), and we * assume the same fd won't have been reused if it was otherwise * closed during the switchboard operation. @@ -186,9 +186,9 @@ typedef struct party_t { bool close_on_destroy; bool stop_on_close; struct party_t *next_reader; - struct party_t *next_writer; + struct party_t *next_writer; struct party_t *next_loner; - void *extra; + void *extra; } party_t; /* @@ -201,14 +201,14 @@ typedef struct monitor_t { struct monitor_t *next; int exit_status; pid_t pid; - party_t *stdin_fd_party; + party_t *stdin_fd_party; party_t *stdout_fd_party; party_t *stderr_fd_party; bool shutdown_when_closed; bool closed; int found_errno; int term_signal; -} monitor_t; +} monitor_t; typedef struct { char *tag; @@ -353,7 +353,7 @@ extern int subproc_get_errno(subprocess_t *, bool); extern int subproc_get_signal(subprocess_t *, bool); extern void subproc_set_extra(subprocess_t *, void *); extern void *subproc_get_extra(subprocess_t *); -extern int subproc_get_pty_fd(subprocess_t *); +extern int subproc_get_pty_fd(subprocess_t *); extern void termcap_get(struct termios *); extern void termcap_set(struct termios *); extern void termcap_set_typical_parent(); @@ -361,4 +361,3 @@ extern void process_status_check(monitor_t *, bool); // pty params. // ASCII Cinema. #endif - diff --git a/nimutils/test.c b/nimutils/test.c index 9fb3170..a17e1a0 100644 --- a/nimutils/test.c +++ b/nimutils/test.c @@ -1,4 +1,4 @@ -char *test_txt = +char *test_txt = " 9: 9 -# -& -= A\n" "12: 12 -# -& -= a\n" " 2: 2 -# -& -= A.\n" From 633ae4fd867c897171dce78069fa9004513b26ea Mon Sep 17 00:00:00 2001 From: Miroslav Shubernetskiy Date: Fri, 3 Nov 2023 10:28:51 -0400 Subject: [PATCH 3/3] defaulting to nesting tmp files in dir for getNewTempFile --- nimutils/managedtmp.nim | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/nimutils/managedtmp.nim b/nimutils/managedtmp.nim index b5c9bb6..1c551bb 100644 --- a/nimutils/managedtmp.nim +++ b/nimutils/managedtmp.nim @@ -20,16 +20,13 @@ proc getNewTempDir*(tmpFilePrefix = defaultTmpPrefix, proc getNewTempFile*(prefix = defaultTmpPrefix, suffix = defaultTmpSuffix, - autoClean = true, - nestInDir: bool = false): (FileStream, string) = - var dir = "" + autoClean = true): (FileStream, string) = # in some cases such as docker, due to snap permissions # it does not have access directly to files created in /tmp # but it can access those files if they are nested in another # nested dir - if nestInDir: - dir = genTempPath(prefix, suffix) - createDir(dir) + let dir = genTempPath(prefix, suffix) + createDir(dir) var (f, path) = createTempFile(prefix, suffix, dir = dir) if autoClean: managedTmpFiles.add(path)