1717#include < unistd.h>
1818#include < fcntl.h>
1919
20+ constexpr long DIR_NAME_LEN = 12 ;
21+
2022constexpr long BUF_LEN = 1024 ;
2123constexpr long MAX_RESPONSE_SIZE = 10 * 1024 ;
2224
@@ -44,9 +46,10 @@ bool read_to_buffer(std::array<uint8_t, BUFFER_SIZE>& buffer, long& read_size, i
4446 return read_size > 0 ;
4547}
4648
47- cppevent::awaitable_task<void > upload (std::string_view dir, std::string_view name,
48- cppevent::output& o_stdout) {
49- int fd = executor::open_file (dir, name, O_RDONLY);
49+ cppevent::awaitable_task<void > executor::exec_endpoint::upload (std::string_view dir,
50+ std::string_view name,
51+ cppevent::output& o_stdout) {
52+ int fd = open_file (dir, name, O_RDONLY);
5053 std::array<uint8_t , BUF_LEN> buffer;
5154 long response_size = 0 ;
5255 long read_size;
@@ -55,6 +58,7 @@ cppevent::awaitable_task<void> upload(std::string_view dir, std::string_view nam
5558 response_size += read_size;
5659 }
5760 close (fd);
61+ m_unused_dirs.push (std::string { dir });
5862}
5963
6064const std::unordered_map<std::string_view, std::string_view> source_file_names = {
@@ -74,20 +78,24 @@ cppevent::awaitable_task<void> executor::exec_endpoint::process(const cppevent::
7478 co_await o_stdout.write (" status: 400\n content-length: 16\n\n unknown language" );
7579 co_return ;
7680 }
77- char dir_name[12 ];
78- strcpy (dir_name, " code_XXXXXX" );
79- if (mkdtemp (dir_name) == NULL ) {
81+
82+ char dir_name[DIR_NAME_LEN];
83+ strncpy (dir_name, " code_XXXXXX" , DIR_NAME_LEN);
84+ if (!m_unused_dirs.empty ()) {
85+ strncpy (dir_name, m_unused_dirs.front ().c_str (), DIR_NAME_LEN);
86+ m_unused_dirs.pop ();
87+ } else if (mkdtemp (dir_name) == NULL ) {
8088 co_await o_stdout.write (" status: 500\n content-type: text/plain\n\n " );
8189 co_await o_stdout.write (strerror (errno));
8290 co_return ;
8391 }
8492
8593 const std::string_view& source_file_name = it->second ;
86- const int source_fd = open_file (dir_name, source_file_name, O_WRONLY | O_CREAT);
94+ const int source_fd = open_file (dir_name, source_file_name, O_WRONLY | O_CREAT | O_TRUNC );
8795 co_await download (content_len, s_stdin, source_fd);
8896 close (source_fd);
8997
90- const int compile_err_fd = open_file (dir_name, COMPILE_ERR, O_WRONLY | O_CREAT);
98+ const int compile_err_fd = open_file (dir_name, COMPILE_ERR, O_WRONLY | O_CREAT | O_TRUNC );
9199 bool compiled_success = co_await await_compile (compile_err_fd, m_loop, dir_name, lang);
92100 close (compile_err_fd);
93101
@@ -97,7 +105,7 @@ cppevent::awaitable_task<void> executor::exec_endpoint::process(const cppevent::
97105 co_return ;
98106 }
99107
100- const int run_out_fd = open_file (dir_name, RUN_STDOUT, O_WRONLY | O_CREAT);
108+ const int run_out_fd = open_file (dir_name, RUN_STDOUT, O_WRONLY | O_CREAT | O_TRUNC );
101109 CODE_EXEC_STATUS run_status = co_await await_run (run_out_fd, m_loop, dir_name, lang);
102110 close (run_out_fd);
103111
@@ -112,6 +120,5 @@ cppevent::awaitable_task<void> executor::exec_endpoint::process(const cppevent::
112120 co_await o_stdout.write (" status: 200\n x-exec-status: success\n content-type: text/plain\n\n " );
113121 break ;
114122 }
115-
116123 co_await upload (dir_name, RUN_STDOUT, o_stdout);
117124}
0 commit comments