Skip to content

Commit 37f9593

Browse files
committed
Add option to provide script temporary directory path instead of system temporary
1 parent cde96d6 commit 37f9593

4 files changed

Lines changed: 64 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
## CHANGELOG
22

3-
### v0.1.12
3+
### v0.1.12 (2023-08-14)
44

5+
* Add option to provide script temporary directory path instead of system temporary
56
* Updated header include guard macro name
67

78
### v0.1.11 (2022-05-9)

include/scriptexec.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ struct ScriptExecOptions
99
char *working_directory;
1010
bool exit_on_error;
1111
bool print_commands;
12+
// The directory used to write the temporary script in
13+
// If NULL, it will use the system temporary directory as parent
14+
char *temporary_directory;
1215
};
1316

1417
struct ScriptExecResult

src/scriptexec.c

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ struct ScriptExecOptions scriptexec_create_options(void)
1515
{
1616
struct ScriptExecOptions options =
1717
{
18-
.runner = NULL, .working_directory = NULL, .exit_on_error = true, .print_commands = false
18+
.runner = NULL,
19+
.working_directory = NULL,
20+
.exit_on_error = true,
21+
.print_commands = false,
22+
.temporary_directory = NULL
1923
};
2024

2125
return(options);
@@ -70,8 +74,18 @@ struct ScriptExecResult scriptexec_run_with_options(const char *script, struct S
7074
stringbuffer_append_string(buffer, (char *)script);
7175
char *updated_script = stringbuffer_to_string(buffer);
7276

73-
char template[] = "/tmp/scriptexec_XXXXXX";
74-
char *dir_name = mkdtemp(template);
77+
bool delete_temporary_directory = false;
78+
char *dir_name = NULL;
79+
if (options.temporary_directory == NULL)
80+
{
81+
char template[] = "/tmp/scriptexec_XXXXXX";
82+
dir_name = mkdtemp(template);
83+
delete_temporary_directory = true;
84+
}
85+
else
86+
{
87+
dir_name = options.temporary_directory;
88+
}
7589

7690
if (dir_name == NULL)
7791
{
@@ -83,16 +97,18 @@ struct ScriptExecResult scriptexec_run_with_options(const char *script, struct S
8397
}
8498

8599
stringbuffer_clear(buffer);
86-
stringbuffer_append_string(buffer, dir_name);
87-
stringbuffer_append_string(buffer, "/script");
88-
char *script_file = stringbuffer_to_string(buffer);
100+
char *script_file = fsio_join_paths(dir_name, "script");
101+
stringbuffer_append_string(buffer, script_file);
89102

90103
// write script file
91104
bool written = fsio_write_text_file(script_file, updated_script);
92105
free(updated_script);
93106
if (!written)
94107
{
95-
rmdir(dir_name);
108+
if (delete_temporary_directory)
109+
{
110+
rmdir(dir_name);
111+
}
96112

97113
stringbuffer_release(buffer);
98114
free(script_file);
@@ -140,7 +156,10 @@ struct ScriptExecResult scriptexec_run_with_options(const char *script, struct S
140156
free(err_file);
141157

142158
// delete temp directory
143-
rmdir(dir_name);
159+
if (delete_temporary_directory)
160+
{
161+
rmdir(dir_name);
162+
}
144163

145164
return(result);
146165
} /* scriptexec_run_with_options */
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "fsio.h"
2+
#include "scriptexec.h"
3+
#include "test.h"
4+
5+
#define TEMPORARY_DIRECTORY "./tempscripts/"
6+
7+
8+
void test_impl()
9+
{
10+
struct ScriptExecOptions options = scriptexec_create_options();
11+
12+
fsio_remove(TEMPORARY_DIRECTORY);
13+
assert_true(!fsio_dir_exists(TEMPORARY_DIRECTORY));
14+
options.temporary_directory = TEMPORARY_DIRECTORY;
15+
16+
struct ScriptExecResult result = scriptexec_run_with_options("echo 1", options);
17+
18+
assert_num_equal(result.code, 0);
19+
20+
free(result.out);
21+
free(result.err);
22+
23+
assert_true(fsio_dir_exists(TEMPORARY_DIRECTORY));
24+
fsio_remove(TEMPORARY_DIRECTORY);
25+
}
26+
27+
28+
int main()
29+
{
30+
test_run(test_impl);
31+
}
32+

0 commit comments

Comments
 (0)