Remove FsPath and FsPathMnt trait

Directly use Utf8CStr
This commit is contained in:
topjohnwu
2025-04-23 17:34:59 -07:00
committed by John Wu
parent 610945ac54
commit 3327fc668e
21 changed files with 62 additions and 89 deletions

View 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(),