Use const_format for const strings

This commit is contained in:
topjohnwu 2024-03-26 18:03:40 -07:00
parent 21ed095601
commit 1eddbfd72c
8 changed files with 79 additions and 68 deletions

83
native/src/Cargo.lock generated
View File

@ -4,9 +4,9 @@ version = 3
[[package]] [[package]]
name = "aho-corasick" name = "aho-corasick"
version = "1.1.2" version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0" checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
dependencies = [ dependencies = [
"memchr", "memchr",
] ]
@ -61,9 +61,9 @@ dependencies = [
[[package]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.1.0" version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" checksum = "f1fdabc7756949593fe60f30ec81974b613357de856987752631dea1e3394c80"
[[package]] [[package]]
name = "base" name = "base"
@ -109,18 +109,18 @@ dependencies = [
[[package]] [[package]]
name = "bytemuck" name = "bytemuck"
version = "1.14.3" version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2ef034f05691a48569bd920a96c81b9d91bbad1ab5ac7c4616c1f6ef36cb79f" checksum = "5d6d68c57235a3a081186990eca2867354726650f42f7516ca50c28d6281fd15"
dependencies = [ dependencies = [
"bytemuck_derive", "bytemuck_derive",
] ]
[[package]] [[package]]
name = "bytemuck_derive" name = "bytemuck_derive"
version = "1.5.0" version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "965ab7eb5f8f97d2a083c799f3a1b994fc397b2fe2da5d1da1626ce15a39f2b1" checksum = "4da9a32f3fed317401fa3c862968128267c3106685286e15d5aaa3d7389c2f60"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -135,9 +135,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.0.88" version = "1.0.90"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "02f341c093d19155a6e41631ce5971aac4e9a868262212153124c15fa22d1cdc" checksum = "8cd6604a82acf3039f1144f54b8eb34e91ffba622051189e71b781822d5ee1f5"
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
@ -176,6 +176,26 @@ version = "0.9.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8" checksum = "c2459377285ad874054d797f3ccebf984978aa39129f6eafde5cdc8315b612f8"
[[package]]
name = "const_format"
version = "0.2.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3a214c7af3d04997541b18d432afaff4c455e79e2029079647e72fc2bd27673"
dependencies = [
"const_format_proc_macros",
]
[[package]]
name = "const_format_proc_macros"
version = "0.2.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c7f6ff08fd20f4f299298a28e2dfa8a8ba1036e6cd2460ac1de7b425d76f2500"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]] [[package]]
name = "cpufeatures" name = "cpufeatures"
version = "0.2.12" version = "0.2.12"
@ -341,9 +361,9 @@ dependencies = [
[[package]] [[package]]
name = "flagset" name = "flagset"
version = "0.4.4" version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d52a7e408202050813e6f1d9addadcaafef3dca7530c7ddfb005d4081cce6779" checksum = "cdeb3aa5e95cf9aabc17f060cfa0ced7b83f042390760ca53bf09df9968acaa1"
[[package]] [[package]]
name = "generic-array" name = "generic-array"
@ -437,9 +457,9 @@ checksum = "4ec2a862134d2a7d32d7983ddcdd1c4923530833c9f2ea1a44fc5fa473989058"
[[package]] [[package]]
name = "log" name = "log"
version = "0.4.20" version = "0.4.21"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c"
[[package]] [[package]]
name = "magisk" name = "magisk"
@ -447,6 +467,7 @@ version = "0.0.0"
dependencies = [ dependencies = [
"base", "base",
"bytemuck", "bytemuck",
"const_format",
"cxx", "cxx",
"cxx-gen", "cxx-gen",
"num-derive", "num-derive",
@ -661,9 +682,9 @@ dependencies = [
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.78" version = "1.0.79"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae" checksum = "e835ff2298f5721608eb1a980ecaee1aef2c132bf95ecc026a11b7bf3c01c02e"
dependencies = [ dependencies = [
"unicode-ident", "unicode-ident",
] ]
@ -722,9 +743,9 @@ dependencies = [
[[package]] [[package]]
name = "regex" name = "regex"
version = "1.10.3" version = "1.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15" checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c"
dependencies = [ dependencies = [
"aho-corasick", "aho-corasick",
"memchr", "memchr",
@ -734,9 +755,9 @@ dependencies = [
[[package]] [[package]]
name = "regex-automata" name = "regex-automata"
version = "0.4.5" version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5bb987efffd3c6d0d8f5f89510bb458559eab11e4f869acb20bf845e016259cd" checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea"
dependencies = [ dependencies = [
"aho-corasick", "aho-corasick",
"memchr", "memchr",
@ -854,9 +875,9 @@ checksum = "9fed904c7fb2856d868b92464fc8fa597fce366edea1a9cbfaa8cb5fe080bd6d"
[[package]] [[package]]
name = "smallvec" name = "smallvec"
version = "1.13.1" version = "1.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7" checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
[[package]] [[package]]
name = "spin" name = "spin"
@ -888,9 +909,9 @@ checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc"
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.51" version = "2.0.55"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ab617d94515e94ae53b8406c628598680aa0c9587474ecbe58188f7b345d66c" checksum = "002a1b3dbf967edfafc32655d0f377ab0bb7b994aa1d32c8cc7e9b8bf3ebb8f0"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -917,18 +938,18 @@ dependencies = [
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.57" version = "1.0.58"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e45bcbe8ed29775f228095caf2cd67af7a4ccf756ebff23a306bf3e8b47b24b" checksum = "03468839009160513471e86a034bb2c5c0e4baae3b43f79ffc55c4a5427b3297"
dependencies = [ dependencies = [
"thiserror-impl", "thiserror-impl",
] ]
[[package]] [[package]]
name = "thiserror-impl" name = "thiserror-impl"
version = "1.0.57" version = "1.0.58"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a953cb265bef375dae3de6663da4d3804eee9682ea80d8e2542529b73c531c81" checksum = "c61f3ba182994efc43764a46c018c347bc492c79f024e705f46567b418f6d4f7"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -974,6 +995,12 @@ version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85" checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85"
[[package]]
name = "unicode-xid"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c"
[[package]] [[package]]
name = "vec_map" name = "vec_map"
version = "0.8.2" version = "0.8.2"

View File

@ -23,6 +23,7 @@ x509-cert = "0.2"
der = "0.7" der = "0.7"
bytemuck = "1.14" bytemuck = "1.14"
fdt = "0.1" fdt = "0.1"
const_format = "0.2"
[workspace.dependencies.argh] [workspace.dependencies.argh]
git = "https://github.com/google/argh.git" git = "https://github.com/google/argh.git"

View File

@ -2,10 +2,10 @@ use std::cmp::min;
use std::ffi::{CStr, FromBytesWithNulError, OsStr}; use std::ffi::{CStr, FromBytesWithNulError, OsStr};
use std::fmt::{Arguments, Debug, Display, Formatter, Write}; use std::fmt::{Arguments, Debug, Display, Formatter, Write};
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
use std::os::unix::ffi::OsStrExt;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::str::{Utf8Chunks, Utf8Error}; use std::str::{Utf8Chunks, Utf8Error};
use std::{fmt, mem, slice, str}; use std::{fmt, mem, slice, str};
use std::os::unix::ffi::OsStrExt;
use cxx::{type_id, ExternType}; use cxx::{type_id, ExternType};
use libc::c_char; use libc::c_char;
@ -664,6 +664,6 @@ macro_rules! cstr {
#[macro_export] #[macro_export]
macro_rules! raw_cstr { macro_rules! raw_cstr {
($($str:tt)*) => {{ ($($str:tt)*) => {{
cstr!($($str)*).as_ptr() $crate::cstr!($($str)*).as_ptr()
}}; }};
} }

View File

@ -10,7 +10,7 @@ use libc::{
}; };
use crate::cxx_extern::readlinkat_for_cxx; use crate::cxx_extern::readlinkat_for_cxx;
use crate::{cstr, errno, raw_cstr, CxxResultExt, FsPath, Utf8CStr, Utf8CStrBufRef}; use crate::{errno, raw_cstr, CxxResultExt, FsPath, Utf8CStr, Utf8CStrBufRef};
fn ptr_to_str<'a, T>(ptr: *const T) -> &'a str { fn ptr_to_str<'a, T>(ptr: *const T) -> &'a str {
if ptr.is_null() { if ptr.is_null() {

View File

@ -17,4 +17,5 @@ cxx = { workspace = true }
num-traits = { workspace = true } num-traits = { workspace = true }
num-derive = { workspace = true } num-derive = { workspace = true }
quick-protobuf = { workspace = true } quick-protobuf = { workspace = true }
const_format = { workspace = true }
bytemuck = { workspace = true, features = ["derive"] } bytemuck = { workspace = true, features = ["derive"] }

View File

@ -9,9 +9,10 @@ use base::{
Utf8CStrBufArr, Utf8CStrBufRef, WalkResult, Utf8CStrBufArr, Utf8CStrBufRef, WalkResult,
}; };
use crate::consts::MAIN_CONFIG;
use crate::ffi::{get_magisk_tmp, CxxMagiskD, RequestCode}; use crate::ffi::{get_magisk_tmp, CxxMagiskD, RequestCode};
use crate::get_prop;
use crate::logging::magisk_logging; use crate::logging::magisk_logging;
use crate::{get_prop, MAIN_CONFIG};
// Global magiskd singleton // Global magiskd singleton
pub static MAGISKD: OnceLock<MagiskD> = OnceLock::new(); pub static MAGISKD: OnceLock<MagiskD> = OnceLock::new();
@ -108,7 +109,7 @@ pub fn daemon_entry() {
let mut buf = Utf8CStrBufArr::<64>::new(); let mut buf = Utf8CStrBufArr::<64>::new();
let path = FsPathBuf::new(&mut buf) let path = FsPathBuf::new(&mut buf)
.join(get_magisk_tmp()) .join(get_magisk_tmp())
.join(MAIN_CONFIG!()); .join(MAIN_CONFIG);
let mut is_recovery = false; let mut is_recovery = false;
if let Ok(file) = path.open(O_RDONLY | O_CLOEXEC) { if let Ok(file) = path.open(O_RDONLY | O_CLOEXEC) {
let mut file = BufReader::new(file); let mut file = BufReader::new(file);

View File

@ -9,6 +9,7 @@ use std::sync::atomic::{AtomicI32, Ordering};
use std::{fs, io}; use std::{fs, io};
use bytemuck::{bytes_of, bytes_of_mut, write_zeroes, Pod, Zeroable}; use bytemuck::{bytes_of, bytes_of_mut, write_zeroes, Pod, Zeroable};
use const_format::concatcp;
use num_derive::{FromPrimitive, ToPrimitive}; use num_derive::{FromPrimitive, ToPrimitive};
use num_traits::FromPrimitive; use num_traits::FromPrimitive;
@ -17,12 +18,15 @@ use base::libc::{
timespec, tm, CLOCK_REALTIME, O_CLOEXEC, O_RDWR, O_WRONLY, PIPE_BUF, SIGPIPE, SIG_BLOCK, timespec, tm, CLOCK_REALTIME, O_CLOEXEC, O_RDWR, O_WRONLY, PIPE_BUF, SIGPIPE, SIG_BLOCK,
SIG_SETMASK, SIG_SETMASK,
}; };
use base::*; use base::{
exit_on_error, libc, raw_cstr, FsPathBuf, LogLevel, Logger, Utf8CStr, Utf8CStrBuf,
Utf8CStrBufArr, Utf8CStrWrite, LOGGER,
};
use crate::consts::{LOGFILE, LOG_PIPE};
use crate::daemon::{MagiskD, MAGISKD}; use crate::daemon::{MagiskD, MAGISKD};
use crate::ffi::get_magisk_tmp; use crate::ffi::get_magisk_tmp;
use crate::logging::LogFile::{Actual, Buffer}; use crate::logging::LogFile::{Actual, Buffer};
use crate::{LOGFILE, LOG_PIPE};
#[allow(dead_code, non_camel_case_types)] #[allow(dead_code, non_camel_case_types)]
#[derive(FromPrimitive, ToPrimitive)] #[derive(FromPrimitive, ToPrimitive)]
@ -192,7 +196,7 @@ pub fn zygisk_get_logd() -> i32 {
let mut buf = Utf8CStrBufArr::default(); let mut buf = Utf8CStrBufArr::default();
let path = FsPathBuf::new(&mut buf) let path = FsPathBuf::new(&mut buf)
.join(get_magisk_tmp()) .join(get_magisk_tmp())
.join(LOG_PIPE!()); .join(LOG_PIPE);
// Open as RW as sometimes it may block // Open as RW as sometimes it may block
fd = unsafe { libc::open(path.as_ptr(), O_RDWR | O_CLOEXEC) }; fd = unsafe { libc::open(path.as_ptr(), O_RDWR | O_CLOEXEC) };
if fd >= 0 { if fd >= 0 {
@ -292,9 +296,9 @@ extern "C" fn logfile_writer(arg: *mut c_void) -> *mut c_void {
pipe.read_exact(bytes_of_mut(&mut meta))?; pipe.read_exact(bytes_of_mut(&mut meta))?;
if meta.prio < 0 { if meta.prio < 0 {
if matches!(logfile, LogFile::Buffer(_)) { if matches!(logfile, Buffer(_)) {
fs::rename(LOGFILE!(), concat!(LOGFILE!(), ".bak")).ok(); fs::rename(LOGFILE, concatcp!(LOGFILE, ".bak")).ok();
let mut out = File::create(LOGFILE!())?; let mut out = File::create(LOGFILE)?;
out.write_all(tmp.as_slice())?; out.write_all(tmp.as_slice())?;
tmp = Vec::new(); tmp = Vec::new();
logfile = Actual(out); logfile = Actual(out);
@ -371,7 +375,7 @@ impl MagiskD {
let mut buf = Utf8CStrBufArr::default(); let mut buf = Utf8CStrBufArr::default();
let path = FsPathBuf::new(&mut buf) let path = FsPathBuf::new(&mut buf)
.join(get_magisk_tmp()) .join(get_magisk_tmp())
.join(LOG_PIPE!()); .join(LOG_PIPE);
unsafe { unsafe {
libc::mkfifo(path.as_ptr(), 0o666); libc::mkfifo(path.as_ptr(), 0o666);

View File

@ -1,29 +1,6 @@
// Expose constant strings as macros so that we can use concat!() on these values use const_format::concatcp;
#[macro_export] pub const LOGFILE: &str = "/cache/magisk.log";
macro_rules! LOGFILE { pub const INTLROOT: &str = ".magisk";
() => { pub const LOG_PIPE: &str = concatcp!(INTLROOT, "/device/log");
"/cache/magisk.log" pub const MAIN_CONFIG: &str = concatcp!(INTLROOT, "/config");
};
}
#[macro_export]
macro_rules! INTLROOT {
() => {
".magisk"
};
}
#[macro_export]
macro_rules! LOG_PIPE {
() => {
concat!($crate::INTLROOT!(), "/device/log")
};
}
#[macro_export]
macro_rules! MAIN_CONFIG {
() => {
concat!($crate::INTLROOT!(), "/config")
};
}