Skip to content

Commit

Permalink
rust: Close all stdio when executing external commands
Browse files Browse the repository at this point in the history
`exec.Command(...).Run()` closes all stdio by default.
  • Loading branch information
eagletmt committed Sep 14, 2021
1 parent 2c28f6a commit 679e929
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions webapp/rust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ async fn initialize(pool: web::Data<sqlx::MySqlPool>) -> actix_web::Result<HttpR
}

if !tokio::process::Command::new("rm")
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.arg("-rf")
.arg(ASSIGNMENTS_DIRECTORY)
.status()
Expand All @@ -194,6 +197,9 @@ async fn initialize(pool: web::Data<sqlx::MySqlPool>) -> actix_web::Result<HttpR
return Err(actix_web::error::ErrorInternalServerError(""));
}
if !tokio::process::Command::new("cp")
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.arg("-r")
.arg(INIT_DATA_DIRECTORY)
.arg(ASSIGNMENTS_DIRECTORY)
Expand Down Expand Up @@ -1539,18 +1545,27 @@ async fn create_submissions_zip(
) -> std::io::Result<()> {
let tmp_dir = format!("{}{}/", ASSIGNMENTS_DIRECTORY, class_id);
tokio::process::Command::new("rm")
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.arg("-rf")
.arg(&tmp_dir)
.status()
.await?;
tokio::process::Command::new("mkdir")
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.arg(&tmp_dir)
.status()
.await?;

// ファイル名を指定の形式に変更
for submission in submissions {
tokio::process::Command::new("cp")
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.arg(&format!(
"{}{}-{}.pdf",
ASSIGNMENTS_DIRECTORY, class_id, submission.user_id
Expand All @@ -1565,6 +1580,9 @@ async fn create_submissions_zip(

// -i 'tmp_dir/*': 空zipを許す
tokio::process::Command::new("zip")
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.arg("-j")
.arg("-r")
.arg(zip_file_path)
Expand Down

0 comments on commit 679e929

Please sign in to comment.