Move MagiskInit::prepare_data to rust

This commit is contained in:
LoveSy 2025-01-30 20:29:50 +08:00 committed by John Wu
parent a85c4c6528
commit 449204e380
3 changed files with 32 additions and 12 deletions

View File

@ -56,6 +56,10 @@ pub mod ffi {
unsafe fn start_magisk_init(argv: *mut *mut c_char); unsafe fn start_magisk_init(argv: *mut *mut c_char);
} }
extern "Rust" {
fn prepare_data(self: &MagiskInit);
}
unsafe extern "C++" { unsafe extern "C++" {
include!("../base/include/base.hpp"); include!("../base/include/base.hpp");
@ -70,7 +74,6 @@ pub mod ffi {
unsafe fn setup_tmp(self: &MagiskInit, path: *const c_char); unsafe fn setup_tmp(self: &MagiskInit, path: *const c_char);
fn collect_devices(self: &MagiskInit); fn collect_devices(self: &MagiskInit);
fn mount_preinit_dir(self: &MagiskInit); fn mount_preinit_dir(self: &MagiskInit);
fn prepare_data(self: &MagiskInit);
unsafe fn find_block(self: &MagiskInit, partname: *const c_char) -> u64; unsafe fn find_block(self: &MagiskInit, partname: *const c_char) -> u64;
fn mount_system_root(self: &mut MagiskInit) -> bool; fn mount_system_root(self: &mut MagiskInit) -> bool;

View File

@ -221,16 +221,6 @@ void MagiskInit::exec_init() const noexcept {
exit(1); exit(1);
} }
void MagiskInit::prepare_data() const noexcept {
LOGD("Setup data tmp\n");
xmkdir("/data", 0755);
xmount("magisk", "/data", "tmpfs", 0, "mode=755");
cp_afc("/init", REDIR_PATH);
cp_afc("/.backup", "/data/.backup");
cp_afc("/overlay.d", "/data/overlay.d");
}
void MagiskInit::setup_tmp(const char *path) const noexcept { void MagiskInit::setup_tmp(const char *path) const noexcept {
LOGD("Setup Magisk tmp at %s\n", path); LOGD("Setup Magisk tmp at %s\n", path);
chdir("/data"); chdir("/data");

View File

@ -7,10 +7,11 @@ use std::{
use cxx::CxxString; use cxx::CxxString;
use crate::ffi::MagiskInit;
use base::{ use base::{
cstr, debug, cstr, debug,
libc::{chdir, chroot, mount, MS_MOVE}, libc::{chdir, chroot, mount, MS_MOVE},
parse_mount_info, raw_cstr, Directory, LibcReturn, LoggedResult, StringExt, Utf8CStr, parse_mount_info, raw_cstr, Directory, FsPath, LibcReturn, LoggedResult, StringExt, Utf8CStr,
}; };
pub fn switch_root(path: &Utf8CStr) { pub fn switch_root(path: &Utf8CStr) {
@ -68,3 +69,29 @@ pub fn is_device_mounted(dev: u64, target: Pin<&mut CxxString>) -> bool {
} }
false false
} }
impl MagiskInit {
pub(crate) fn prepare_data(&self) {
debug!("Setup data tmp");
fn inner() -> LoggedResult<()> {
FsPath::from(cstr!("/data")).mkdir(0o755)?;
unsafe {
mount(
raw_cstr!("magisk"),
raw_cstr!("/data"),
raw_cstr!("tmpfs"),
0,
raw_cstr!("mode=755").cast(),
)
}
.as_os_err()?;
FsPath::from(cstr!("/init")).copy_to(FsPath::from(cstr!("/data/magiskinit")))?;
FsPath::from(cstr!("/.backup")).copy_to(FsPath::from(cstr!("/data/.backup")))?;
FsPath::from(cstr!("/overlay.d")).copy_to(FsPath::from(cstr!("/data/overlay.d")))?;
Ok(())
}
inner().ok();
}
}