mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-12-12 14:55:20 +00:00
Implement magiskinit logging in Rust
This commit is contained in:
@@ -53,20 +53,20 @@ pub fn __xopen_fd_impl(path: &CStr, flags: i32, mode: mode_t) -> Option<OwnedFd>
|
||||
#[macro_export]
|
||||
macro_rules! open_fd {
|
||||
($path:expr, $flags:expr) => {
|
||||
crate::__open_fd_impl($path, $flags, 0)
|
||||
$crate::__open_fd_impl($path, $flags, 0)
|
||||
};
|
||||
($path:expr, $flags:expr, $mode:expr) => {
|
||||
crate::__open_fd_impl($path, $flags, $mode)
|
||||
$crate::__open_fd_impl($path, $flags, $mode)
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! xopen_fd {
|
||||
($path:expr, $flags:expr) => {
|
||||
crate::__xopen_fd_impl($path, $flags, 0)
|
||||
$crate::__xopen_fd_impl($path, $flags, 0)
|
||||
};
|
||||
($path:expr, $flags:expr, $mode:expr) => {
|
||||
crate::__xopen_fd_impl($path, $flags, $mode)
|
||||
$crate::__xopen_fd_impl($path, $flags, $mode)
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,13 @@ use std::ffi::CStr;
|
||||
use std::fmt::Arguments;
|
||||
use std::{fmt, slice};
|
||||
|
||||
pub fn copy_str(dest: &mut [u8], src: &[u8]) -> usize {
|
||||
let len = min(src.len(), dest.len() - 1);
|
||||
dest[..len].copy_from_slice(&src[..len]);
|
||||
dest[len] = b'\0';
|
||||
len
|
||||
}
|
||||
|
||||
struct BufFmtWriter<'a> {
|
||||
buf: &'a mut [u8],
|
||||
used: usize,
|
||||
@@ -21,12 +28,7 @@ impl<'a> fmt::Write for BufFmtWriter<'a> {
|
||||
// Silent truncate
|
||||
return Ok(());
|
||||
}
|
||||
let remain = &mut self.buf[self.used..];
|
||||
let s_bytes = s.as_bytes();
|
||||
let copied = min(s_bytes.len(), remain.len() - 1);
|
||||
remain[..copied].copy_from_slice(&s_bytes[..copied]);
|
||||
self.used += copied;
|
||||
self.buf[self.used] = b'\0';
|
||||
self.used += copy_str(&mut self.buf[self.used..], s.as_bytes());
|
||||
// Silent truncate
|
||||
Ok(())
|
||||
}
|
||||
@@ -107,6 +109,13 @@ macro_rules! cstr {
|
||||
}};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! str_ptr {
|
||||
($s:literal) => {{
|
||||
cstr!($s).as_ptr()
|
||||
}};
|
||||
}
|
||||
|
||||
pub fn ptr_to_str<'a, T>(ptr: *const T) -> &'a str {
|
||||
if ptr.is_null() {
|
||||
"(null)"
|
||||
|
||||
Reference in New Issue
Block a user