runc: avoid using unwrap() in util.rs
Introduce helper path_to_string() to avoid using unwrap(). Signed-off-by: Liu Jiang <gerry@linux.alibaba.com>
This commit is contained in:
parent
918693c248
commit
2cba19f230
|
|
@ -60,38 +60,38 @@ where
|
||||||
.to_path_buf())
|
.to_path_buf())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn path_to_string(path: impl AsRef<Path>) -> Result<String, Error> {
|
||||||
|
path.as_ref()
|
||||||
|
.to_str()
|
||||||
|
.map(|v| v.to_string())
|
||||||
|
.ok_or_else(|| {
|
||||||
|
let e = std::io::Error::new(
|
||||||
|
std::io::ErrorKind::Other,
|
||||||
|
format!("invalid UTF-8 string: {}", path.as_ref().to_string_lossy()),
|
||||||
|
);
|
||||||
|
Error::InvalidPath(e)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
pub fn abs_string<P>(path: P) -> Result<String, Error>
|
pub fn abs_string<P>(path: P) -> Result<String, Error>
|
||||||
where
|
where
|
||||||
P: AsRef<Path>,
|
P: AsRef<Path>,
|
||||||
{
|
{
|
||||||
Ok(abs_path_buf(path)?
|
path_to_string(abs_path_buf(path)?)
|
||||||
.to_string_lossy()
|
|
||||||
.parse::<String>()
|
|
||||||
.unwrap())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn temp_filename_in_runtime_dir() -> Result<String, Error> {
|
pub fn temp_filename_in_runtime_dir() -> Result<String, Error> {
|
||||||
env::var_os("XDG_RUNTIME_DIR")
|
match env::var_os("XDG_RUNTIME_DIR") {
|
||||||
.map(|runtime_dir| {
|
Some(runtime_dir) => {
|
||||||
format!(
|
let path = path_to_string(runtime_dir)?;
|
||||||
"{}/runc-process-{}",
|
Ok(format!("{}/runc-process-{}", path, Uuid::new_v4()))
|
||||||
runtime_dir.to_string_lossy().parse::<String>().unwrap(),
|
}
|
||||||
Uuid::new_v4(),
|
None => Err(Error::SpecFileNotFound),
|
||||||
)
|
}
|
||||||
})
|
|
||||||
.ok_or(Error::SpecFileNotFound)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn make_temp_file_in_runtime_dir() -> Result<(NamedTempFile, String), Error> {
|
pub fn make_temp_file_in_runtime_dir() -> Result<(NamedTempFile, String), Error> {
|
||||||
let file_name = env::var_os("XDG_RUNTIME_DIR")
|
let file_name = temp_filename_in_runtime_dir()?;
|
||||||
.map(|runtime_dir| {
|
|
||||||
format!(
|
|
||||||
"{}/runc-process-{}",
|
|
||||||
runtime_dir.to_string_lossy().parse::<String>().unwrap(),
|
|
||||||
Uuid::new_v4(),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.ok_or(Error::SpecFileNotFound)?;
|
|
||||||
let temp_file = Builder::new()
|
let temp_file = Builder::new()
|
||||||
.prefix(&file_name)
|
.prefix(&file_name)
|
||||||
.tempfile()
|
.tempfile()
|
||||||
|
|
@ -99,6 +99,11 @@ pub fn make_temp_file_in_runtime_dir() -> Result<(NamedTempFile, String), Error>
|
||||||
Ok((temp_file, file_name))
|
Ok((temp_file, file_name))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Resolve a binary path according to the `PATH` environment variable.
|
||||||
|
///
|
||||||
|
/// Note, the case that `path` is already an absolute path is implicitly handled by
|
||||||
|
/// `dir.join(path.as_ref())`. `Path::join(parent_path, path)` directly returns `path` when `path`
|
||||||
|
/// is an absolute path.
|
||||||
pub fn binary_path<P>(path: P) -> Option<PathBuf>
|
pub fn binary_path<P>(path: P) -> Option<PathBuf>
|
||||||
where
|
where
|
||||||
P: AsRef<Path>,
|
P: AsRef<Path>,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue