Skip to content

Commit 6f7b45b

Browse files
Switch executable to .tar.gz format to fix Java support
1 parent d20b29a commit 6f7b45b

File tree

6 files changed

+138
-178
lines changed

6 files changed

+138
-178
lines changed

Cargo.lock

Lines changed: 43 additions & 80 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ nix = { version = "0.29.0", features = ["signal"] }
2323
regex = "1.10.5"
2424
serde = { version = "1.0.203", features = ["std", "derive"] }
2525
shell-words = "1.1.0"
26-
tempdir = "0.3.7"
26+
tempfile = "3.10.1"
2727

2828
tokio = { version = "1", features = ["macros"] }
2929

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,22 @@ double x; cin >> x; int y = x; for (int i = 0; i < y; i++) x += sqrt(x); cout <<
8080
```
8181

8282
Should benchmark this to determine how off the timings are / whether we can just add a multiplicative factor to it.
83+
84+
---
85+
86+
misc todos
87+
88+
```
89+
// timeout: warning: disabling core dumps failed: Operation not permitted
90+
// Command exited with non-zero status 137
91+
// UGH
92+
93+
// also, internal server error when output too large
94+
95+
// want smth like
96+
/*
97+
Line 15: Char 8: runtime error: signed integer overflow: 2147483647 + 2147483647 cannot be represented in type 'int' (solution.cpp)
98+
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior solution.cpp:15:8
99+
*/
100+
// which is what leetcode gives you
101+
```

src/compile.rs

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::{
22
fs::{self, File},
3-
io::Write,
4-
os::unix::process::ExitStatusExt,
3+
io::{Write},
4+
os::unix::{process::ExitStatusExt},
55
path::{Path, PathBuf},
66
process::{Command, ExitStatus},
77
};
@@ -11,7 +11,7 @@ use axum::Json;
1111
use base64::{prelude::BASE64_STANDARD, Engine};
1212
use regex::Regex;
1313
use serde::{Deserialize, Serialize};
14-
use tempdir::TempDir;
14+
use tempfile::tempdir;
1515

1616
use crate::{
1717
error::AppError,
@@ -90,8 +90,8 @@ fn precompile_headers(compile_request: &CompileRequest) -> Result<()> {
9090
}
9191

9292
pub fn compile(compile_request: CompileRequest) -> Result<CompileResponse> {
93-
let tmp_dir = TempDir::new("compile")?;
94-
let tmp_out_dir = TempDir::new("compile-out")?;
93+
let tmp_dir = tempdir()?;
94+
let tmp_out_dir = tempdir()?;
9595

9696
let program_filename: PathBuf = match compile_request.language {
9797
Language::Cpp => "program.cpp".into(),
@@ -157,39 +157,35 @@ pub fn compile(compile_request: CompileRequest) -> Result<CompileResponse> {
157157
},
158158
)?;
159159

160-
let executable = if ExitStatus::from_raw(compile_output.exit_code).success() {
161-
Some(match compile_request.language {
162-
Language::Cpp => Executable::Binary {
163-
value: BASE64_STANDARD.encode(fs::read(
164-
tmp_out_dir.path().join(program_filename.with_extension("")),
165-
)?),
166-
},
167-
Language::Java21 => Executable::JavaClass {
168-
class_name: program_filename
169-
.file_stem()
170-
.unwrap()
171-
.to_str()
172-
.unwrap()
173-
.to_owned(),
174-
value: BASE64_STANDARD.encode(fs::read(
175-
tmp_out_dir
176-
.path()
177-
.join(program_filename)
178-
.with_extension("class"),
179-
)?),
180-
},
181-
Language::Py12 => Executable::Script {
182-
language: Language::Py12,
183-
source_code: BASE64_STANDARD
184-
.encode(fs::read(tmp_out_dir.path().join(program_filename))?),
185-
},
186-
})
160+
let run_command = match compile_request.language {
161+
Language::Cpp => "./program".to_owned(),
162+
Language::Java21 => format!(
163+
"java {}",
164+
program_filename.file_stem().unwrap().to_str().unwrap()
165+
),
166+
Language::Py12 => "python3.12 program.py".to_owned(),
167+
};
168+
169+
let base64_files = if ExitStatus::from_raw(compile_output.exit_code).success() {
170+
if !Command::new("sh")
171+
.arg("-c")
172+
.arg("tar czf executable.tar.gz *")
173+
.current_dir(tmp_out_dir.path())
174+
.status()?
175+
.success()
176+
{
177+
return Err(anyhow!("Failed to tar executable file"));
178+
}
179+
Some(BASE64_STANDARD.encode(fs::read(tmp_out_dir.path().join("executable.tar.gz"))?))
187180
} else {
188-
Option::None
181+
None
189182
};
190183

191184
let response = CompileResponse {
192-
executable,
185+
executable: base64_files.map(|files| Executable {
186+
files,
187+
run_command,
188+
}),
193189
compile_output,
194190
};
195191

0 commit comments

Comments
 (0)