Merge pull request #33 from lifupan/master

cgroup: fix the issue of remove cgroup
This commit is contained in:
Fupan Li
2021-01-25 17:39:38 +08:00
committed by GitHub

View File

@@ -330,7 +330,7 @@ where
return Ok(());
}
fs::remove_dir(self.get_path()).map_err(|e| Error::with_cause(ErrorKind::RemoveFailed, e))
remove_dir(self.get_path())
}
/// Attach a task to this controller.
@@ -379,6 +379,30 @@ where
}
}
// remove_dir aims to remove cgroup path. It does so recursively,
// by removing any subdirectories (sub-cgroups) first.
fn remove_dir(dir: &PathBuf) -> Result<()> {
// try the fast path first.
if fs::remove_dir(dir).is_ok() {
return Ok(());
}
if dir.exists() {
if dir.is_dir() {
for entry in fs::read_dir(dir).map_err(|e| Error::with_cause(ReadFailed, e))? {
let entry = entry.map_err(|e| Error::with_cause(ReadFailed, e))?;
let path = entry.path();
if path.is_dir() {
remove_dir(&path)?;
}
}
fs::remove_dir(dir).map_err(|e| Error::with_cause(RemoveFailed, e))?;
}
}
Ok(())
}
#[doc(hidden)]
pub trait ControllIdentifier {
fn controller_type() -> Controllers;