Files
Magisk/native/src/base/cxx_extern.rs

190 lines
5.4 KiB
Rust
Raw Normal View History

2023-07-06 04:54:33 -07:00
// Functions in this file are only for exporting to C++, DO NOT USE IN RUST
2023-06-09 02:00:37 -07:00
use std::fs::File;
use std::io::BufReader;
use std::mem::ManuallyDrop;
use std::ops::DerefMut;
use std::os::fd::{BorrowedFd, FromRawFd, OwnedFd, RawFd};
use crate::ffi::{FnBoolStr, FnBoolStrStr};
use crate::files::map_file_at;
2023-07-06 04:54:33 -07:00
pub(crate) use crate::xwrap::*;
2023-09-26 20:18:37 -07:00
use crate::{
2025-09-08 11:24:33 -07:00
BufReadExt, Directory, LoggedResult, ResultExt, Utf8CStr, clone_attr, cstr, fclone_attr,
2025-08-24 21:31:53 -07:00
map_fd, map_file, slice_from_ptr,
2023-09-26 20:18:37 -07:00
};
2025-09-08 23:59:29 -07:00
use cfg_if::cfg_if;
use libc::{c_char, mode_t};
use nix::fcntl::OFlag;
2025-03-06 23:04:02 -08:00
#[unsafe(no_mangle)]
2023-06-09 02:00:37 -07:00
unsafe extern "C" fn canonical_path(path: *const c_char, buf: *mut u8, bufsz: usize) -> isize {
2025-03-06 23:04:02 -08:00
unsafe {
match Utf8CStr::from_ptr(path) {
2025-04-21 18:00:43 -07:00
Ok(path) => {
2025-04-22 02:55:53 -07:00
let mut buf = cstr::buf::wrap_ptr(buf, bufsz);
2025-04-21 18:00:43 -07:00
path.realpath(&mut buf)
2025-09-08 11:25:18 -07:00
.log()
2025-04-21 18:00:43 -07:00
.map_or(-1_isize, |_| buf.len() as isize)
2025-03-06 23:04:02 -08:00
}
Err(_) => -1,
}
2023-06-12 05:59:50 -07:00
}
2023-06-09 02:00:37 -07:00
}
2025-03-06 23:04:02 -08:00
#[unsafe(export_name = "mkdirs")]
2023-06-09 02:00:37 -07:00
unsafe extern "C" fn mkdirs_for_cxx(path: *const c_char, mode: mode_t) -> i32 {
2025-03-06 23:04:02 -08:00
unsafe {
match Utf8CStr::from_ptr(path) {
2025-04-21 18:00:43 -07:00
Ok(path) => path.mkdirs(mode).map_or(-1, |_| 0),
2025-03-06 23:04:02 -08:00
Err(_) => -1,
}
2023-06-12 05:59:50 -07:00
}
2023-06-09 02:00:37 -07:00
}
2025-03-06 23:04:02 -08:00
#[unsafe(export_name = "rm_rf")]
2023-06-09 02:00:37 -07:00
unsafe extern "C" fn rm_rf_for_cxx(path: *const c_char) -> bool {
2025-03-06 23:04:02 -08:00
unsafe {
match Utf8CStr::from_ptr(path) {
2025-04-21 18:00:43 -07:00
Ok(path) => path.remove_all().is_ok(),
2025-03-06 23:04:02 -08:00
Err(_) => false,
}
2023-06-12 05:59:50 -07:00
}
2023-06-09 02:00:37 -07:00
}
2025-03-06 23:04:02 -08:00
#[unsafe(no_mangle)]
2023-06-09 02:00:37 -07:00
unsafe extern "C" fn frm_rf(fd: OwnedFd) -> bool {
2025-09-08 11:24:33 -07:00
fn inner(fd: OwnedFd) -> LoggedResult<()> {
2023-06-09 02:00:37 -07:00
Directory::try_from(fd)?.remove_all()
}
inner(fd).is_ok()
2023-06-09 02:00:37 -07:00
}
2023-12-26 23:08:06 +08:00
pub(crate) fn map_file_for_cxx(path: &Utf8CStr, rw: bool) -> &'static mut [u8] {
2025-09-08 11:25:18 -07:00
map_file(path, rw).log().unwrap_or(&mut [])
}
pub(crate) fn map_file_at_for_cxx(fd: RawFd, path: &Utf8CStr, rw: bool) -> &'static mut [u8] {
unsafe {
map_file_at(BorrowedFd::borrow_raw(fd), path, rw)
2025-09-08 11:25:18 -07:00
.log()
.unwrap_or(&mut [])
}
}
pub(crate) fn map_fd_for_cxx(fd: RawFd, sz: usize, rw: bool) -> &'static mut [u8] {
unsafe {
map_fd(BorrowedFd::borrow_raw(fd), sz, rw)
2025-09-08 11:25:18 -07:00
.log()
.unwrap_or(&mut [])
}
}
2023-09-26 20:18:37 -07:00
2025-04-14 18:13:26 -07:00
pub(crate) unsafe fn readlinkat(
2023-09-26 20:18:37 -07:00
dirfd: RawFd,
path: *const c_char,
buf: *mut u8,
bufsz: usize,
) -> isize {
2025-03-06 23:04:02 -08:00
unsafe {
// readlinkat() may fail on x86 platform, returning random value
// instead of number of bytes placed in buf (length of link)
cfg_if! {
if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
libc::memset(buf.cast(), 0, bufsz);
let mut r = libc::readlinkat(dirfd, path, buf.cast(), bufsz - 1);
if r > 0 {
r = libc::strlen(buf.cast()) as isize;
}
} else {
let r = libc::readlinkat(dirfd, path, buf.cast(), bufsz - 1);
if r >= 0 {
*buf.offset(r) = b'\0';
}
2023-09-26 20:18:37 -07:00
}
}
2025-03-06 23:04:02 -08:00
r
2023-09-26 20:18:37 -07:00
}
}
2025-03-06 23:04:02 -08:00
#[unsafe(export_name = "cp_afc")]
2023-09-26 20:18:37 -07:00
unsafe extern "C" fn cp_afc_for_cxx(src: *const c_char, dest: *const c_char) -> bool {
2025-03-06 23:04:02 -08:00
unsafe {
2025-08-28 16:15:59 -07:00
if let Ok(src) = Utf8CStr::from_ptr(src)
2025-09-02 11:11:49 -07:00
&& let Ok(dest) = Utf8CStr::from_ptr(dest)
{
2025-09-08 11:24:33 -07:00
return src.copy_to(dest).is_ok();
2025-09-02 11:11:49 -07:00
}
2025-03-06 23:04:02 -08:00
false
2023-09-26 20:18:37 -07:00
}
}
2025-03-06 23:04:02 -08:00
#[unsafe(export_name = "mv_path")]
2023-09-26 20:18:37 -07:00
unsafe extern "C" fn mv_path_for_cxx(src: *const c_char, dest: *const c_char) -> bool {
2025-03-06 23:04:02 -08:00
unsafe {
2025-08-28 16:15:59 -07:00
if let Ok(src) = Utf8CStr::from_ptr(src)
2025-09-02 11:11:49 -07:00
&& let Ok(dest) = Utf8CStr::from_ptr(dest)
{
2025-09-08 11:24:33 -07:00
return src.move_to(dest).is_ok();
2025-09-02 11:11:49 -07:00
}
2025-03-06 23:04:02 -08:00
false
2023-09-26 20:18:37 -07:00
}
}
2025-03-06 23:04:02 -08:00
#[unsafe(export_name = "link_path")]
2023-09-26 20:18:37 -07:00
unsafe extern "C" fn link_path_for_cxx(src: *const c_char, dest: *const c_char) -> bool {
2025-03-06 23:04:02 -08:00
unsafe {
2025-08-28 16:15:59 -07:00
if let Ok(src) = Utf8CStr::from_ptr(src)
2025-09-02 11:11:49 -07:00
&& let Ok(dest) = Utf8CStr::from_ptr(dest)
{
2025-09-08 11:24:33 -07:00
return src.link_to(dest).is_ok();
2025-09-02 11:11:49 -07:00
}
2025-03-06 23:04:02 -08:00
false
2023-09-26 20:18:37 -07:00
}
}
2025-03-06 23:04:02 -08:00
#[unsafe(export_name = "clone_attr")]
2023-09-26 20:18:37 -07:00
unsafe extern "C" fn clone_attr_for_cxx(src: *const c_char, dest: *const c_char) -> bool {
2025-03-06 23:04:02 -08:00
unsafe {
2025-08-28 16:15:59 -07:00
if let Ok(src) = Utf8CStr::from_ptr(src)
2025-09-02 11:11:49 -07:00
&& let Ok(dest) = Utf8CStr::from_ptr(dest)
{
2025-09-08 11:25:18 -07:00
return clone_attr(src, dest).log().is_ok();
2025-09-02 11:11:49 -07:00
}
2025-03-06 23:04:02 -08:00
false
2023-09-26 20:18:37 -07:00
}
}
2025-03-06 23:04:02 -08:00
#[unsafe(export_name = "fclone_attr")]
2023-09-26 20:18:37 -07:00
unsafe extern "C" fn fclone_attr_for_cxx(a: RawFd, b: RawFd) -> bool {
2025-09-08 11:25:18 -07:00
fclone_attr(a, b).log().is_ok()
2023-09-26 20:18:37 -07:00
}
2023-12-26 23:08:06 +08:00
2025-03-06 23:04:02 -08:00
#[unsafe(export_name = "cxx$utf8str$new")]
2023-12-26 23:08:06 +08:00
unsafe extern "C" fn str_new(this: &mut &Utf8CStr, s: *const u8, len: usize) {
2025-03-06 23:04:02 -08:00
unsafe {
*this = Utf8CStr::from_bytes(slice_from_ptr(s, len)).unwrap_or(cstr!(""));
}
2023-12-26 23:08:06 +08:00
}
2025-03-06 23:04:02 -08:00
#[unsafe(export_name = "cxx$utf8str$ptr")]
2023-12-26 23:08:06 +08:00
unsafe extern "C" fn str_ptr(this: &&Utf8CStr) -> *const u8 {
this.as_ptr().cast()
}
2025-03-06 23:04:02 -08:00
#[unsafe(export_name = "cxx$utf8str$len")]
2023-12-26 23:08:06 +08:00
unsafe extern "C" fn str_len(this: &&Utf8CStr) -> usize {
this.len()
}
pub(crate) fn parse_prop_file_rs(name: &Utf8CStr, f: &FnBoolStrStr) {
2025-09-08 23:59:29 -07:00
if let Ok(file) = name.open(OFlag::O_RDONLY) {
BufReader::new(file).for_each_prop(|key, value| f.call(key, value))
}
}
pub(crate) fn file_readline_for_cxx(fd: RawFd, f: &FnBoolStr) {
let mut fd = ManuallyDrop::new(unsafe { File::from_raw_fd(fd) });
BufReader::new(fd.deref_mut()).for_each_line(|line| f.call(Utf8CStr::from_string(line)));
}