Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Make BasePath::new_temp_dir return the same path for the program li…
Browse files Browse the repository at this point in the history
…fetime (#12246)

* Make `BasePath::new_temp_dir` return the same path for the program lifetime

Instead of returning always a different path, this now returns the same path for the entire lifetime
of the program. We still ensure that the path is cleared at the end of the program.

* Update client/service/src/config.rs

Co-authored-by: Koute <koute@users.noreply.github.com>

* Update client/service/src/config.rs

Co-authored-by: Nitwit <47109040+nitwit69@users.noreply.github.com>

* FMT

Co-authored-by: Koute <koute@users.noreply.github.com>
Co-authored-by: Nitwit <47109040+nitwit69@users.noreply.github.com>
  • Loading branch information
3 people authored and altonen committed Sep 14, 2022
1 parent e731ad0 commit 426c686
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 16 deletions.
37 changes: 36 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions client/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ async-trait = "0.1.57"
tokio = { version = "1.17.0", features = ["time", "rt-multi-thread", "parking_lot"] }
tempfile = "3.1.0"
directories = "4.0.1"
static_init = "1.0.3"

[dev-dependencies]
substrate-test-runtime-client = { version = "2.0.0", path = "../../test-utils/runtime/client" }
Expand Down
39 changes: 24 additions & 15 deletions client/service/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,31 +260,43 @@ impl Default for RpcMethods {
}
}

/// The base path that is used for everything that needs to be write on disk to run a node.
#[static_init::dynamic(drop, lazy)]
static mut BASE_PATH_TEMP: Option<TempDir> = None;

/// The base path that is used for everything that needs to be written on disk to run a node.
#[derive(Debug)]
pub enum BasePath {
/// A temporary directory is used as base path and will be deleted when dropped.
Temporary(TempDir),
/// A path on the disk.
Permanenent(PathBuf),
pub struct BasePath {
path: PathBuf,
}

impl BasePath {
/// Create a `BasePath` instance using a temporary directory prefixed with "substrate" and use
/// it as base path.
///
/// Note: the temporary directory will be created automatically and deleted when the `BasePath`
/// instance is dropped.
/// Note: The temporary directory will be created automatically and deleted when the program
/// exits. Every call to this function will return the same path for the lifetime of the
/// program.
pub fn new_temp_dir() -> io::Result<BasePath> {
Ok(BasePath::Temporary(tempfile::Builder::new().prefix("substrate").tempdir()?))
let mut temp = BASE_PATH_TEMP.write();

match &*temp {
Some(p) => Ok(Self::new(p.path())),
None => {
let temp_dir = tempfile::Builder::new().prefix("substrate").tempdir()?;
let path = PathBuf::from(temp_dir.path());

*temp = Some(temp_dir);
Ok(Self::new(path))
},
}
}

/// Create a `BasePath` instance based on an existing path on disk.
///
/// Note: this function will not ensure that the directory exist nor create the directory. It
/// will also not delete the directory when the instance is dropped.
pub fn new<P: AsRef<Path>>(path: P) -> BasePath {
BasePath::Permanenent(path.as_ref().to_path_buf())
pub fn new<P: Into<PathBuf>>(path: P) -> BasePath {
Self { path: path.into() }
}

/// Create a base path from values describing the project.
Expand All @@ -298,10 +310,7 @@ impl BasePath {

/// Retrieve the base path.
pub fn path(&self) -> &Path {
match self {
BasePath::Temporary(temp_dir) => temp_dir.path(),
BasePath::Permanenent(path) => path.as_path(),
}
&self.path
}

/// Returns the configuration directory inside this base path.
Expand Down

0 comments on commit 426c686

Please sign in to comment.