Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nesting files in subdir for getNewTempFile #30

Merged
merged 3 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions nimutils/hex.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];


Expand All @@ -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];
}
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
11 changes: 9 additions & 2 deletions nimutils/managedtmp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@ proc getNewTempDir*(tmpFilePrefix = defaultTmpPrefix,
result = createTempDir(tmpFilePrefix, tmpFileSuffix)
managedTmpDirs.add(result)

proc getNewTempFile*(prefix = defaultTmpPrefix, suffix = defaultTmpSuffix,
proc getNewTempFile*(prefix = defaultTmpPrefix,
suffix = defaultTmpSuffix,
autoClean = true): (FileStream, string) =
var (f, path) = createTempFile(prefix, suffix)
# 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
let dir = genTempPath(prefix, suffix)
createDir(dir)
var (f, path) = createTempFile(prefix, suffix, dir = dir)
if autoClean:
managedTmpFiles.add(path)

Expand Down
80 changes: 40 additions & 40 deletions nimutils/subproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ subproc_set_envp(subprocess_t *ctx, char *envp[])
if (ctx->run) {
return false;
}

ctx->envp = envp;

return true;
}

Expand All @@ -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) {
Expand All @@ -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 {
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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;

Expand All @@ -165,15 +165,15 @@ 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;
cbinfo->which = which;
cbinfo->cb = cb;

ctx->deferred_cbs = cbinfo;

return true;
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -264,18 +264,18 @@ setup_subscriptions(subprocess_t *ctx, bool pty)
"stdout", CAP_ALLOC);
}
}

stderr_dst = &ctx->capture_stdout;
}
else {
if (!pty && ctx->capture & SP_IO_STDERR) {
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);
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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.
Expand All @@ -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;
Expand All @@ -426,31 +426,31 @@ 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;
termcap.c_cc[VTIME] = 0;
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;
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
Expand All @@ -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 };
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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 };
Expand All @@ -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);
Expand Down
Loading