forked from cross-rs/cross
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support temporary data directories and files.
Create and robustly delete temporary data, which is stored in the user data directory under `${data_dir}/cross-rs/tmp`. Therefore, if program execution is ever halted, deleting the directory will automatically cleanup any remaining data. A termination handler cleans up the temporary data on exit by clearing a stack of temporary files/directories, and we have 2 resource handlers that cleanup the temporary data when the object is dropped. The new installer for the startup hooks is `install_exit_hooks`, which installs both the panic and termination handlers. To get the directory containing all temporary data, use `cross::temp::dir()`. An example of using temporary directories and files is: ```rust { // these are all safe due to single-threaded execution let tmpdir1 = unsafe { temp::TempFile::new() }?; let tmpdir2 = unsafe { temp::TempFile::new() }?; let tmpfile1 = unsafe { temp::TempFile::new() }?; let tmpfile2 = unsafe { temp::TempFile::new() }?; for entry in fs::read_dir(tmpdir1.path()) { ... } for entry in fs::read_dir(tmpdir2.path()) { ... } } // cleanup tmpfile2 -> tmpfile1 -> tmpdir2 -> tmpdir1 ``` Note that only these 2 wrappers are provided, since it guarantees the temporary file and directory stack stays ordered and resources are cleaned up as desired.
- Loading branch information
1 parent
3ded782
commit 12f51bb
Showing
10 changed files
with
340 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use std::fs; | ||
|
||
use super::images::RemoveImages; | ||
use clap::Args; | ||
|
||
#[derive(Args, Debug)] | ||
pub struct Clean { | ||
/// Provide verbose diagnostic output. | ||
#[clap(short, long)] | ||
pub verbose: bool, | ||
/// Force removal of images. | ||
#[clap(short, long)] | ||
pub force: bool, | ||
/// Remove local (development) images. | ||
#[clap(short, long)] | ||
pub local: bool, | ||
/// Remove images. Default is a dry run. | ||
#[clap(short, long)] | ||
pub execute: bool, | ||
/// Container engine (such as docker or podman). | ||
#[clap(long)] | ||
pub engine: Option<String>, | ||
} | ||
|
||
impl Clean { | ||
pub fn run(self, engine: cross::docker::Engine) -> cross::Result<()> { | ||
let tempdir = cross::temp::dir()?; | ||
match self.execute { | ||
true => { | ||
if tempdir.exists() { | ||
fs::remove_dir_all(tempdir)?; | ||
} | ||
} | ||
false => println!("fs::remove_dir_all({})", tempdir.display()), | ||
} | ||
|
||
let remove_images = RemoveImages { | ||
targets: vec![], | ||
verbose: self.verbose, | ||
force: self.force, | ||
local: self.local, | ||
execute: self.execute, | ||
engine: None, | ||
}; | ||
remove_images.run(engine)?; | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
mod clean; | ||
mod images; | ||
|
||
pub use self::clean::*; | ||
pub use self::images::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.