From e149c37f16b19236887afeadfbe774eabeb3510d Mon Sep 17 00:00:00 2001 From: syn Date: Thu, 16 Jan 2020 14:46:01 +0300 Subject: Support tmp dir to place binaries --- Cargo.lock | 1 + Cargo.toml | 1 + src/backends/mod.rs | 18 ++++++++++++++++++ src/main.rs | 2 ++ 4 files changed, 22 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 6888e33..3318e9d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -67,6 +67,7 @@ version = "0.1.0" dependencies = [ "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.102 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.103 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index e486b01..16893da 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,3 +14,4 @@ serde_derive = "1" log = "0.4" env_logger = "0.7.1" wait-timeout = "0.1.5" +lazy_static = "1.4.0" diff --git a/src/backends/mod.rs b/src/backends/mod.rs index f24a162..82ec6a7 100644 --- a/src/backends/mod.rs +++ b/src/backends/mod.rs @@ -1,4 +1,6 @@ use std::path::{ Path, PathBuf }; +use std::env::temp_dir; +use lazy_static::lazy_static; pub mod python; pub mod clang; @@ -6,6 +8,10 @@ pub mod clang; pub use python::PythonBackend; pub use clang::ClangBackend; +lazy_static! { + static ref EVR_TMP_DIR: PathBuf = temp_dir().join("evr-tmp"); +} + pub trait Backend { fn get_template(&self) -> Option<&str>; @@ -19,3 +25,15 @@ pub trait Backend { None } } + +fn mk_tmp_dir() -> std::io::Result<&'static std::path::PathBuf> { + if !EVR_TMP_DIR.exists() { + std::fs::create_dir(&*EVR_TMP_DIR)?; + } else { + if !EVR_TMP_DIR.is_dir() { + return Err(std::io::Error::new(std::io::ErrorKind::AlreadyExists, + "tmp dir already exists and not a directory")) + } + } + Ok(&*EVR_TMP_DIR) +} diff --git a/src/main.rs b/src/main.rs index 0e97682..589cae2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +extern crate lazy_static; + use clap::AppSettings; use structopt::StructOpt; use env_logger; -- cgit v1.2.1-18-gbd029