Skip to content

Commit

Permalink
Replace all parse_mount_info usage with Rust
Browse files Browse the repository at this point in the history
  • Loading branch information
topjohnwu committed Feb 27, 2024
1 parent af6965e commit 1a70796
Show file tree
Hide file tree
Showing 8 changed files with 278 additions and 61 deletions.
181 changes: 154 additions & 27 deletions native/src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions native/src/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ x509-cert = "0.2"
der = "0.7"
bytemuck = "1.14"
fdt = "0.1"
procfs = { version = "0.16", default-features = false }

[workspace.dependencies.argh]
git = "https://github.com/google/argh.git"
Expand Down
20 changes: 18 additions & 2 deletions native/src/base/cstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use std::cmp::min;
use std::ffi::{CStr, FromBytesWithNulError, OsStr};
use std::fmt::{Arguments, Debug, Display, Formatter, Write};
use std::ops::{Deref, DerefMut};
use std::path::Path;
use std::path::{Path, PathBuf};
use std::str::{Utf8Chunks, Utf8Error};
use std::{fmt, mem, slice, str};
use std::os::unix::ffi::OsStrExt;

use cxx::{type_id, ExternType};
use libc::c_char;
Expand Down Expand Up @@ -105,7 +106,7 @@ trait AsUtf8CStr {

// Implementation for Utf8CString

trait StringExt {
pub trait StringExt {
fn nul_terminate(&mut self) -> &mut [u8];
}

Expand All @@ -122,6 +123,21 @@ impl StringExt for String {
}
}

impl StringExt for PathBuf {
#[allow(mutable_transmutes)]
fn nul_terminate(&mut self) -> &mut [u8] {
self.reserve(1);
// SAFETY: the PathBuf is reserved to have enough capacity to fit in the null byte
// SAFETY: the null byte is explicitly added outside of the PathBuf's length
unsafe {
let bytes: &mut [u8] = mem::transmute(self.as_mut_os_str().as_bytes());
let buf = slice::from_raw_parts_mut(bytes.as_mut_ptr(), bytes.len() + 1);
*buf.get_unchecked_mut(bytes.len()) = b'\0';
buf
}
}
}

#[derive(Default)]
pub struct Utf8CString(String);

Expand Down
2 changes: 1 addition & 1 deletion native/src/external/crt0
Submodule crt0 updated 1 files
+1 −0 syscall.c
1 change: 1 addition & 0 deletions native/src/init/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ cxx-gen = { workspace = true }
base = { path = "../base" }
magiskpolicy = { path = "../sepolicy" }
cxx = { workspace = true }
procfs = { workspace = true }

0 comments on commit 1a70796

Please sign in to comment.