magic_mount: refine

This commit is contained in:
5ec1cff
2024-11-22 21:46:44 +08:00
parent 43563b654a
commit 44f13e4a1f

View File

@@ -1,4 +1,6 @@
use crate::defs::{KSU_MOUNT_SOURCE, MODULE_DIR, SKIP_MOUNT_FILE_NAME, TEMP_DIR};
use crate::defs::{
DISABLE_FILE_NAME, KSU_MOUNT_SOURCE, MODULE_DIR, SKIP_MOUNT_FILE_NAME, TEMP_DIR,
};
use crate::magic_mount::NodeFileType::{Directory, RegularFile, Symlink, Whiteout};
use crate::restorecon::{lgetfilecon, lsetfilecon};
use anyhow::{bail, Context, Result};
@@ -49,6 +51,7 @@ struct Node {
// the module that owned this node
module_path: Option<PathBuf>,
replace: bool,
skip: bool,
}
impl Node {
@@ -82,6 +85,7 @@ impl Node {
children: Default::default(),
module_path: None,
replace: false,
skip: false,
}
}
@@ -108,6 +112,7 @@ impl Node {
children: Default::default(),
module_path: Some(path),
replace,
skip: false,
});
}
}
@@ -126,7 +131,8 @@ fn collect_module_files() -> Result<Option<Node>> {
continue;
}
if entry.path().join("disable").exists() || entry.path().join(SKIP_MOUNT_FILE_NAME).exists()
if entry.path().join(DISABLE_FILE_NAME).exists()
|| entry.path().join(SKIP_MOUNT_FILE_NAME).exists()
{
continue;
}
@@ -255,9 +261,10 @@ fn do_magic_mount<P: AsRef<Path>, WP: AsRef<Path>>(
}
}
Directory => {
let mut create_tmpfs = current.replace;
let mut create_tmpfs = !has_tmpfs && current.replace && current.module_path.is_some();
if !has_tmpfs && !create_tmpfs {
for (name, node) in &current.children {
for it in &mut current.children {
let (name, node) = it;
let real_path = path.join(name);
let need = match node.file_type {
Symlink => true,
@@ -274,6 +281,14 @@ fn do_magic_mount<P: AsRef<Path>, WP: AsRef<Path>>(
}
};
if need {
if current.module_path.is_none() {
log::error!(
"cannot create tmpfs on {}, ignore: {name}",
path.display()
);
node.skip = true;
continue;
}
create_tmpfs = true;
break;
}
@@ -320,6 +335,9 @@ fn do_magic_mount<P: AsRef<Path>, WP: AsRef<Path>>(
for entry in path.read_dir()?.flatten() {
let name = entry.file_name().to_string_lossy().to_string();
let result = if let Some(node) = current.children.remove(&name) {
if node.skip {
continue;
}
do_magic_mount(&path, &work_dir_path, node, has_tmpfs)
.with_context(|| format!("magic mount {}/{name}", path.display()))
} else if has_tmpfs {
@@ -351,6 +369,9 @@ fn do_magic_mount<P: AsRef<Path>, WP: AsRef<Path>>(
}
for (name, node) in current.children.into_iter() {
if node.skip {
continue;
}
if let Err(e) = do_magic_mount(&path, &work_dir_path, node, has_tmpfs)
.with_context(|| format!("magic mount {}/{name}", path.display()))
{