mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-12-12 08:32:07 +00:00
Remove FsPath and FsPathMnt trait
Directly use Utf8CStr
This commit is contained in:
@@ -11,7 +11,7 @@ use std::str::Utf8Error;
|
||||
use std::{fmt, mem, slice, str};
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::{FsPath, FsPathMnt, slice_from_ptr_mut};
|
||||
use crate::slice_from_ptr_mut;
|
||||
// Utf8CStr types are UTF-8 validated and null terminated strings.
|
||||
//
|
||||
// Several Utf8CStr types:
|
||||
@@ -609,22 +609,6 @@ impl_cstr_buf_write!(
|
||||
(Utf8CString,)
|
||||
);
|
||||
|
||||
// impl<T: Deref<Target = Utf8CStr>> FsPath for T {}
|
||||
// impl<T: Deref<Target = Utf8CStr>> FsPathMnt for T {}
|
||||
macro_rules! impl_fs_path {
|
||||
($( ($t:ty, $($g:tt)*) )*) => {$(
|
||||
impl<$($g)*> FsPath for $t {}
|
||||
impl<$($g)*> FsPathMnt for $t {}
|
||||
)*}
|
||||
}
|
||||
|
||||
impl_fs_path!(
|
||||
(&Utf8CStr,)
|
||||
(Utf8CStrBufRef<'_>,)
|
||||
(Utf8CStrBufArr<N>, const N: usize)
|
||||
(Utf8CString,)
|
||||
);
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! cstr {
|
||||
($str:expr) => {{
|
||||
|
||||
@@ -8,8 +8,8 @@ use libc::{c_char, mode_t};
|
||||
use crate::files::map_file_at;
|
||||
pub(crate) use crate::xwrap::*;
|
||||
use crate::{
|
||||
CxxResultExt, Directory, FsPath, OsResultStatic, Utf8CStr, clone_attr, cstr, fclone_attr,
|
||||
fd_path, map_fd, map_file, slice_from_ptr,
|
||||
CxxResultExt, Directory, OsResultStatic, Utf8CStr, clone_attr, cstr, fclone_attr, fd_path,
|
||||
map_fd, map_file, slice_from_ptr,
|
||||
};
|
||||
|
||||
pub(crate) fn fd_path_for_cxx(fd: RawFd, buf: &mut [u8]) -> isize {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::cxx_extern::readlinkat;
|
||||
use crate::{
|
||||
FileAttr, FsPath, FsPathBuilder, LibcReturn, OsError, OsResult, OsResultStatic, Utf8CStr,
|
||||
Utf8CStrBuf, cstr, errno, fd_path, fd_set_attr,
|
||||
FileAttr, FsPathBuilder, LibcReturn, OsError, OsResult, OsResultStatic, Utf8CStr, Utf8CStrBuf,
|
||||
cstr, errno, fd_path, fd_set_attr,
|
||||
};
|
||||
use libc::{EEXIST, O_CLOEXEC, O_CREAT, O_RDONLY, O_TRUNC, O_WRONLY, dirent, mode_t};
|
||||
use std::fs::File;
|
||||
|
||||
@@ -14,7 +14,6 @@ use std::ffi::CStr;
|
||||
use std::fmt::Display;
|
||||
use std::fs::File;
|
||||
use std::io::{BufRead, BufReader, Read, Seek, SeekFrom, Write};
|
||||
use std::ops::Deref;
|
||||
use std::os::fd::{AsFd, BorrowedFd};
|
||||
use std::os::unix::ffi::OsStrExt;
|
||||
use std::os::unix::io::{AsRawFd, FromRawFd, OwnedFd, RawFd};
|
||||
@@ -187,27 +186,27 @@ impl FileAttr {
|
||||
|
||||
const XATTR_NAME_SELINUX: &CStr = c"security.selinux";
|
||||
|
||||
pub trait FsPath: Deref<Target = Utf8CStr> {
|
||||
fn follow_link(&self) -> &FsPathFollow {
|
||||
unsafe { mem::transmute(self.deref()) }
|
||||
impl Utf8CStr {
|
||||
pub fn follow_link(&self) -> &FsPathFollow {
|
||||
unsafe { mem::transmute(self) }
|
||||
}
|
||||
|
||||
fn open(&self, flags: i32) -> OsResult<File> {
|
||||
pub fn open(&self, flags: i32) -> OsResult<File> {
|
||||
Ok(File::from(open_fd(self, flags, 0)?))
|
||||
}
|
||||
|
||||
fn create(&self, flags: i32, mode: mode_t) -> OsResult<File> {
|
||||
pub fn create(&self, flags: i32, mode: mode_t) -> OsResult<File> {
|
||||
Ok(File::from(open_fd(self, O_CREAT | flags, mode)?))
|
||||
}
|
||||
|
||||
fn exists(&self) -> bool {
|
||||
pub fn exists(&self) -> bool {
|
||||
unsafe {
|
||||
let mut st: stat = mem::zeroed();
|
||||
libc::lstat(self.as_ptr(), &mut st) == 0
|
||||
}
|
||||
}
|
||||
|
||||
fn rename_to<'a>(&'a self, name: &'a Utf8CStr) -> OsResult<'a, ()> {
|
||||
pub fn rename_to<'a>(&'a self, name: &'a Utf8CStr) -> OsResult<'a, ()> {
|
||||
unsafe {
|
||||
libc::rename(self.as_ptr(), name.as_ptr()).check_os_err(
|
||||
"rename",
|
||||
@@ -217,11 +216,11 @@ pub trait FsPath: Deref<Target = Utf8CStr> {
|
||||
}
|
||||
}
|
||||
|
||||
fn remove(&self) -> OsResult<()> {
|
||||
pub fn remove(&self) -> OsResult<()> {
|
||||
unsafe { libc::remove(self.as_ptr()).check_os_err("remove", Some(self), None) }
|
||||
}
|
||||
|
||||
fn remove_all(&self) -> OsResultStatic<()> {
|
||||
pub fn remove_all(&self) -> OsResultStatic<()> {
|
||||
let attr = self.get_attr()?;
|
||||
if attr.is_dir() {
|
||||
let mut dir = Directory::try_from(open_fd(self, O_RDONLY | O_CLOEXEC, 0)?)?;
|
||||
@@ -231,7 +230,7 @@ pub trait FsPath: Deref<Target = Utf8CStr> {
|
||||
}
|
||||
|
||||
#[allow(clippy::unnecessary_cast)]
|
||||
fn read_link(&self, buf: &mut dyn Utf8CStrBuf) -> OsResult<()> {
|
||||
pub fn read_link(&self, buf: &mut dyn Utf8CStrBuf) -> OsResult<()> {
|
||||
buf.clear();
|
||||
unsafe {
|
||||
let r = libc::readlink(self.as_ptr(), buf.as_mut_ptr(), buf.capacity() - 1)
|
||||
@@ -242,7 +241,7 @@ pub trait FsPath: Deref<Target = Utf8CStr> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn mkdir(&self, mode: mode_t) -> OsResult<()> {
|
||||
pub fn mkdir(&self, mode: mode_t) -> OsResult<()> {
|
||||
unsafe {
|
||||
if libc::mkdir(self.as_ptr(), mode) < 0 {
|
||||
if *errno() == EEXIST {
|
||||
@@ -255,7 +254,7 @@ pub trait FsPath: Deref<Target = Utf8CStr> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn mkdirs(&self, mode: mode_t) -> OsResultStatic<()> {
|
||||
pub fn mkdirs(&self, mode: mode_t) -> OsResultStatic<()> {
|
||||
if self.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
@@ -280,7 +279,7 @@ pub trait FsPath: Deref<Target = Utf8CStr> {
|
||||
}
|
||||
|
||||
// Inspired by https://android.googlesource.com/platform/bionic/+/master/libc/bionic/realpath.cpp
|
||||
fn realpath(&self, buf: &mut dyn Utf8CStrBuf) -> OsResult<()> {
|
||||
pub fn realpath(&self, buf: &mut dyn Utf8CStrBuf) -> OsResult<()> {
|
||||
let fd = self.open(O_PATH | O_CLOEXEC)?;
|
||||
let mut st1: libc::stat;
|
||||
let mut st2: libc::stat;
|
||||
@@ -304,7 +303,7 @@ pub trait FsPath: Deref<Target = Utf8CStr> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_attr(&self) -> OsResult<FileAttr> {
|
||||
pub fn get_attr(&self) -> OsResult<FileAttr> {
|
||||
let mut attr = FileAttr::new();
|
||||
unsafe {
|
||||
libc::lstat(self.as_ptr(), &mut attr.st).check_os_err("lstat", Some(self), None)?;
|
||||
@@ -315,7 +314,7 @@ pub trait FsPath: Deref<Target = Utf8CStr> {
|
||||
Ok(attr)
|
||||
}
|
||||
|
||||
fn set_attr<'a>(&'a self, attr: &'a FileAttr) -> OsResult<'a, ()> {
|
||||
pub fn set_attr<'a>(&'a self, attr: &'a FileAttr) -> OsResult<'a, ()> {
|
||||
unsafe {
|
||||
if !attr.is_symlink() {
|
||||
libc::chmod(self.as_ptr(), (attr.st.st_mode & 0o777).as_()).check_os_err(
|
||||
@@ -338,7 +337,7 @@ pub trait FsPath: Deref<Target = Utf8CStr> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_secontext(&self, con: &mut dyn Utf8CStrBuf) -> OsResult<()> {
|
||||
pub fn get_secontext(&self, con: &mut dyn Utf8CStrBuf) -> OsResult<()> {
|
||||
unsafe {
|
||||
let sz = libc::lgetxattr(
|
||||
self.as_ptr(),
|
||||
@@ -358,7 +357,7 @@ pub trait FsPath: Deref<Target = Utf8CStr> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn set_secontext<'a>(&'a self, con: &'a Utf8CStr) -> OsResult<'a, ()> {
|
||||
pub fn set_secontext<'a>(&'a self, con: &'a Utf8CStr) -> OsResult<'a, ()> {
|
||||
unsafe {
|
||||
libc::lsetxattr(
|
||||
self.as_ptr(),
|
||||
@@ -371,7 +370,7 @@ pub trait FsPath: Deref<Target = Utf8CStr> {
|
||||
}
|
||||
}
|
||||
|
||||
fn copy_to(&self, path: &Utf8CStr) -> OsResultStatic<()> {
|
||||
pub fn copy_to(&self, path: &Utf8CStr) -> OsResultStatic<()> {
|
||||
let attr = self.get_attr()?;
|
||||
if attr.is_dir() {
|
||||
path.mkdir(0o777)?;
|
||||
@@ -401,7 +400,7 @@ pub trait FsPath: Deref<Target = Utf8CStr> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn move_to(&self, path: &Utf8CStr) -> OsResultStatic<()> {
|
||||
pub fn move_to(&self, path: &Utf8CStr) -> OsResultStatic<()> {
|
||||
if path.exists() {
|
||||
let attr = path.get_attr()?;
|
||||
if attr.is_dir() {
|
||||
@@ -416,7 +415,7 @@ pub trait FsPath: Deref<Target = Utf8CStr> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn parent(&self, buf: &mut dyn Utf8CStrBuf) -> bool {
|
||||
pub fn parent(&self, buf: &mut dyn Utf8CStrBuf) -> bool {
|
||||
buf.clear();
|
||||
if let Some(parent) = Path::new(self.as_str()).parent() {
|
||||
let bytes = parent.as_os_str().as_bytes();
|
||||
@@ -430,7 +429,7 @@ pub trait FsPath: Deref<Target = Utf8CStr> {
|
||||
}
|
||||
|
||||
// ln self path
|
||||
fn link_to(&self, path: &Utf8CStr) -> OsResultStatic<()> {
|
||||
pub fn link_to(&self, path: &Utf8CStr) -> OsResultStatic<()> {
|
||||
let attr = self.get_attr()?;
|
||||
if attr.is_dir() {
|
||||
path.mkdir(0o777)?;
|
||||
@@ -451,7 +450,7 @@ pub trait FsPath: Deref<Target = Utf8CStr> {
|
||||
}
|
||||
|
||||
// ln -s target self
|
||||
fn create_symlink_to<'a>(&'a self, target: &'a Utf8CStr) -> OsResult<'a, ()> {
|
||||
pub fn create_symlink_to<'a>(&'a self, target: &'a Utf8CStr) -> OsResult<'a, ()> {
|
||||
unsafe {
|
||||
libc::symlink(target.as_ptr(), self.as_ptr()).check_os_err(
|
||||
"symlink",
|
||||
@@ -461,21 +460,17 @@ pub trait FsPath: Deref<Target = Utf8CStr> {
|
||||
}
|
||||
}
|
||||
|
||||
fn mkfifo(&self, mode: mode_t) -> OsResult<()> {
|
||||
pub fn mkfifo(&self, mode: mode_t) -> OsResult<()> {
|
||||
unsafe { libc::mkfifo(self.as_ptr(), mode).check_os_err("mkfifo", Some(self), None) }
|
||||
}
|
||||
}
|
||||
|
||||
impl FsPath for FsPathFollow {
|
||||
fn follow_link(&self) -> &FsPathFollow {
|
||||
self
|
||||
}
|
||||
|
||||
fn exists(&self) -> bool {
|
||||
impl FsPathFollow {
|
||||
pub fn exists(&self) -> bool {
|
||||
unsafe { libc::access(self.as_ptr(), F_OK) == 0 }
|
||||
}
|
||||
|
||||
fn get_attr(&self) -> OsResult<FileAttr> {
|
||||
pub fn get_attr(&self) -> OsResult<FileAttr> {
|
||||
let mut attr = FileAttr::new();
|
||||
unsafe {
|
||||
libc::stat(self.as_ptr(), &mut attr.st).check_os_err("stat", Some(self), None)?;
|
||||
@@ -486,7 +481,7 @@ impl FsPath for FsPathFollow {
|
||||
Ok(attr)
|
||||
}
|
||||
|
||||
fn set_attr<'a>(&'a self, attr: &'a FileAttr) -> OsResult<'a, ()> {
|
||||
pub fn set_attr<'a>(&'a self, attr: &'a FileAttr) -> OsResult<'a, ()> {
|
||||
unsafe {
|
||||
libc::chmod(self.as_ptr(), (attr.st.st_mode & 0o777).as_()).check_os_err(
|
||||
"chmod",
|
||||
@@ -507,7 +502,7 @@ impl FsPath for FsPathFollow {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_secontext(&self, con: &mut dyn Utf8CStrBuf) -> OsResult<()> {
|
||||
pub fn get_secontext(&self, con: &mut dyn Utf8CStrBuf) -> OsResult<()> {
|
||||
unsafe {
|
||||
let sz = libc::getxattr(
|
||||
self.as_ptr(),
|
||||
@@ -527,7 +522,7 @@ impl FsPath for FsPathFollow {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn set_secontext<'a>(&'a self, con: &'a Utf8CStr) -> OsResult<'a, ()> {
|
||||
pub fn set_secontext<'a>(&'a self, con: &'a Utf8CStr) -> OsResult<'a, ()> {
|
||||
unsafe {
|
||||
libc::setxattr(
|
||||
self.as_ptr(),
|
||||
|
||||
@@ -15,7 +15,6 @@ pub use ffi::fork_dont_care;
|
||||
pub use files::*;
|
||||
pub use logging::*;
|
||||
pub use misc::*;
|
||||
pub use mount::*;
|
||||
pub use result::*;
|
||||
|
||||
pub mod cstr;
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
use crate::{LibcReturn, OsResult, Utf8CStr};
|
||||
use libc::c_ulong;
|
||||
use std::ops::Deref;
|
||||
use std::ptr;
|
||||
|
||||
pub trait FsPathMnt: Deref<Target = Utf8CStr> {
|
||||
fn bind_mount_to<'a>(&'a self, path: &'a Utf8CStr) -> OsResult<'a, ()> {
|
||||
impl Utf8CStr {
|
||||
pub fn bind_mount_to<'a>(&'a self, path: &'a Utf8CStr) -> OsResult<'a, ()> {
|
||||
unsafe {
|
||||
libc::mount(
|
||||
self.as_ptr(),
|
||||
@@ -17,7 +16,7 @@ pub trait FsPathMnt: Deref<Target = Utf8CStr> {
|
||||
}
|
||||
}
|
||||
|
||||
fn remount_with_flags(&self, flags: c_ulong) -> OsResult<()> {
|
||||
pub fn remount_with_flags(&self, flags: c_ulong) -> OsResult<()> {
|
||||
unsafe {
|
||||
libc::mount(
|
||||
ptr::null(),
|
||||
@@ -30,7 +29,7 @@ pub trait FsPathMnt: Deref<Target = Utf8CStr> {
|
||||
}
|
||||
}
|
||||
|
||||
fn remount_with_data(&self, data: &Utf8CStr) -> OsResult<()> {
|
||||
pub fn remount_with_data(&self, data: &Utf8CStr) -> OsResult<()> {
|
||||
unsafe {
|
||||
libc::mount(
|
||||
ptr::null(),
|
||||
@@ -43,7 +42,7 @@ pub trait FsPathMnt: Deref<Target = Utf8CStr> {
|
||||
}
|
||||
}
|
||||
|
||||
fn move_mount_to<'a>(&'a self, path: &'a Utf8CStr) -> OsResult<'a, ()> {
|
||||
pub fn move_mount_to<'a>(&'a self, path: &'a Utf8CStr) -> OsResult<'a, ()> {
|
||||
unsafe {
|
||||
libc::mount(
|
||||
self.as_ptr(),
|
||||
@@ -56,13 +55,13 @@ pub trait FsPathMnt: Deref<Target = Utf8CStr> {
|
||||
}
|
||||
}
|
||||
|
||||
fn unmount(&self) -> OsResult<()> {
|
||||
pub fn unmount(&self) -> OsResult<()> {
|
||||
unsafe {
|
||||
libc::umount2(self.as_ptr(), libc::MNT_DETACH).check_os_err("unmount", Some(self), None)
|
||||
}
|
||||
}
|
||||
|
||||
fn set_mount_private(&self, recursive: bool) -> OsResult<()> {
|
||||
pub fn set_mount_private(&self, recursive: bool) -> OsResult<()> {
|
||||
let flag = if recursive { libc::MS_REC } else { 0 };
|
||||
unsafe {
|
||||
libc::mount(
|
||||
|
||||
@@ -2,8 +2,7 @@
|
||||
|
||||
use crate::cxx_extern::readlinkat;
|
||||
use crate::{
|
||||
BorrowedDirectory, CxxResultExt, FsPath, LibcReturn, Utf8CStr, cstr, slice_from_ptr,
|
||||
slice_from_ptr_mut,
|
||||
BorrowedDirectory, CxxResultExt, LibcReturn, Utf8CStr, cstr, slice_from_ptr, slice_from_ptr_mut,
|
||||
};
|
||||
use libc::{
|
||||
c_char, c_uint, c_ulong, c_void, dev_t, mode_t, nfds_t, off_t, pollfd, sockaddr, socklen_t,
|
||||
|
||||
Reference in New Issue
Block a user