Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions crates/fspy/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn unpack_tar_gz(content: impl Read, path: &str) -> anyhow::Result<Vec<u8>> {
for entry in archive.entries()? {
let mut entry = entry?;
if entry.path_bytes().as_ref() == path.as_bytes() {
let mut data = Vec::<u8>::with_capacity(entry.size() as usize);
let mut data = Vec::<u8>::with_capacity(entry.size().try_into().unwrap());
entry.read_to_end(&mut data)?;
return Ok(data);
}
Expand All @@ -48,12 +48,12 @@ const MACOS_BINARY_DOWNLOADS: &[(&str, &[(&str, &str, u128)])] = &[
(
"https://github.com/branchseer/oils-for-unix-binaries/releases/download/0.29.0-manual/oils-for-unix-0.29.0-aarch64-apple-darwin.tar.gz",
"oils-for-unix",
149945237112824769531360595981178091193,
149_945_237_112_824_769_531_360_595_981_178_091_193,
),
(
"https://github.com/uutils/coreutils/releases/download/0.1.0/coreutils-0.1.0-aarch64-apple-darwin.tar.gz",
"coreutils-0.1.0-aarch64-apple-darwin/coreutils",
255656813290649147736009964224176006890,
255_656_813_290_649_147_736_009_964_224_176_006_890,
),
],
),
Expand All @@ -63,12 +63,12 @@ const MACOS_BINARY_DOWNLOADS: &[(&str, &[(&str, &str, u128)])] = &[
(
"https://github.com/branchseer/oils-for-unix-binaries/releases/download/0.29.0-manual/oils-for-unix-0.29.0-x86_64-apple-darwin.tar.gz",
"oils-for-unix",
286203014616009968685843701528129413859,
286_203_014_616_009_968_685_843_701_528_129_413_859,
),
(
"https://github.com/uutils/coreutils/releases/download/0.1.0/coreutils-0.1.0-x86_64-apple-darwin.tar.gz",
"coreutils-0.1.0-x86_64-apple-darwin/coreutils",
75344743234387926348628744659874018387,
75_344_743_234_387_926_348_628_744_659_874_018_387,
),
],
),
Expand All @@ -77,7 +77,7 @@ const MACOS_BINARY_DOWNLOADS: &[(&str, &[(&str, &str, u128)])] = &[
fn fetch_macos_binaries() -> anyhow::Result<()> {
if env::var("CARGO_CFG_TARGET_OS").unwrap() != "macos" {
return Ok(());
};
}
let out_dir = current_dir().unwrap().join(Path::new(&std::env::var_os("OUT_DIR").unwrap()));

let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
Expand Down
21 changes: 11 additions & 10 deletions crates/fspy/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub struct Command {

impl Command {
#[cfg(unix)]
#[must_use]
pub fn get_exec(&self) -> Exec {
use std::{
iter::once,
Expand Down Expand Up @@ -74,27 +75,27 @@ impl Command {
.collect();
}

pub fn env_remove<K: AsRef<OsStr>>(&mut self, key: K) -> &mut Command {
pub fn env_remove<K: AsRef<OsStr>>(&mut self, key: K) -> &mut Self {
self.envs.remove(key.as_ref());
self
}

pub fn stderr<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command {
pub fn stderr<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Self {
self.stderr = Some(cfg.into());
self
}

pub fn stdout<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command {
pub fn stdout<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Self {
self.stdout = Some(cfg.into());
self
}

pub fn stdin<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Command {
pub fn stdin<T: Into<Stdio>>(&mut self, cfg: T) -> &mut Self {
self.stdin = Some(cfg.into());
self
}

pub fn env<K, V>(&mut self, key: K, val: V) -> &mut Command
pub fn env<K, V>(&mut self, key: K, val: V) -> &mut Self
where
K: AsRef<OsStr>,
V: AsRef<OsStr>,
Expand All @@ -103,7 +104,7 @@ impl Command {
self
}

pub fn envs<I, K, V>(&mut self, vars: I) -> &mut Command
pub fn envs<I, K, V>(&mut self, vars: I) -> &mut Self
where
I: IntoIterator<Item = (K, V)>,
K: AsRef<OsStr>,
Expand All @@ -116,17 +117,17 @@ impl Command {
self
}

pub fn current_dir<P: AsRef<Path>>(&mut self, dir: P) -> &mut Command {
pub fn current_dir<P: AsRef<Path>>(&mut self, dir: P) -> &mut Self {
self.cwd = Some(dir.as_ref().to_owned());
self
}

pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Command {
pub fn arg<S: AsRef<OsStr>>(&mut self, arg: S) -> &mut Self {
self.args.push(arg.as_ref().to_os_string());
self
}

pub fn args<I, S>(&mut self, args: I) -> &mut Command
pub fn args<I, S>(&mut self, args: I) -> &mut Self
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
Expand All @@ -136,7 +137,7 @@ impl Command {
}

#[cfg(unix)]
pub fn arg0<S>(&mut self, arg: S) -> &mut Command
pub fn arg0<S>(&mut self, arg: S) -> &mut Self
where
S: AsRef<OsStr>,
{
Expand Down
13 changes: 6 additions & 7 deletions crates/fspy/src/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ use std::{fs::File, io::Write, sync::Arc};
use std::{
io::{self},
iter,
ops::Deref,
os::fd::{AsFd, AsRawFd, BorrowedFd, FromRawFd, OwnedFd, RawFd},
sync::atomic::{AtomicU8, Ordering, fence},
};
Expand Down Expand Up @@ -129,15 +128,16 @@ impl PathAccessIterable {
self.arenas.iter().flat_map(|arena| arena.borrow_accesses().iter()).copied();

let accesses_in_shm = self.shm_mmaps.iter().flat_map(|mmap| {
let buf = mmap.deref();
let buf = &**mmap;
let mut position = 0usize;
iter::from_fn(move || {
let (flag_buf, data_buf) = buf[position..].split_first()?;
let atomic_flag = unsafe { AtomicU8::from_ptr((flag_buf as *const u8).cast_mut()) };
let atomic_flag =
unsafe { AtomicU8::from_ptr(std::ptr::from_ref::<u8>(flag_buf).cast_mut()) };
let flag = atomic_flag.load(Ordering::Acquire);
if flag == 0 {
return None;
};
}
fence(Ordering::Acquire);
let (path_access, decoded_size) =
borrow_decode_from_slice::<PathAccess<'_>, _>(data_buf, BINCODE_CONFIG)
Expand Down Expand Up @@ -165,7 +165,7 @@ fn duplicate_until_safe(mut fd: OwnedFd) -> io::Result<OwnedFd> {
Ok(fd)
}

pub(crate) async fn spawn_impl(mut command: Command) -> io::Result<TrackedChild> {
pub async fn spawn_impl(mut command: Command) -> io::Result<TrackedChild> {
let (shm_fd_sender, shm_fd_receiver) = UnixStream::pair()?;

let shm_fd_sender = shm_fd_sender.into_std()?;
Expand Down Expand Up @@ -251,9 +251,8 @@ pub(crate) async fn spawn_impl(mut command: Command) -> io::Result<TrackedChild>
Err(err) => {
if err.kind() == io::ErrorKind::UnexpectedEof {
break;
} else {
return Err(err);
}
return Err(err);
}
};
shm_fds.push(shm_fd);
Expand Down
4 changes: 2 additions & 2 deletions crates/fspy/tests/node_fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ async fn read_dir_sync() -> io::Result<()> {
#[tokio::test]
async fn subprocess() -> io::Result<()> {
let cmd = if cfg!(windows) {
r#"'cmd', ['/c', 'type hello']"#
r"'cmd', ['/c', 'type hello']"
} else {
r#"'/bin/sh', ['-c', 'cat hello']"#
r"'/bin/sh', ['-c', 'cat hello']"
};
let accesses = track_node_script(&format!(
"try {{ child_process.spawnSync({cmd}, {{ stdio: 'ignore' }}) }} catch {{}}"
Expand Down
2 changes: 1 addition & 1 deletion crates/fspy_e2e/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct AccessCollector {
}

impl AccessCollector {
pub fn new(dir: PathBuf) -> Self {
pub const fn new(dir: PathBuf) -> Self {
Self { dir, accesses: BTreeMap::new() }
}

Expand Down
2 changes: 1 addition & 1 deletion crates/fspy_preload_unix/src/client/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fn get_fd_path(fd: RawFd) -> nix::Result<Option<PathBuf>> {
fn get_fd_path(fd: RawFd) -> nix::Result<Option<PathBuf>> {
if fd == libc::AT_FDCWD {
return Ok(Some(getcwd()?));
};
}
let mut path = std::path::PathBuf::new();
match nix::fcntl::fcntl(
unsafe { std::os::fd::BorrowedFd::borrow_raw(fd) },
Expand Down
14 changes: 6 additions & 8 deletions crates/fspy_preload_unix/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl ShmCursor {
let new_position = self.position.checked_add(len)?;
if new_position > self.mmap_mut.len() {
return None;
};
}
let buf = &mut self.mmap_mut[self.position..new_position];
self.position = new_position;
Some(buf)
Expand Down Expand Up @@ -146,7 +146,7 @@ impl Client {
&& (path.starts_with(b"/proc/") || path.starts_with(b"/sys/")))
{
return Ok(());
};
}
let mut size_writer = SizeWriter::default();
encode_into_writer(path_access, &mut size_writer, BINCODE_CONFIG)?;

Expand Down Expand Up @@ -224,7 +224,7 @@ impl Client {
false
} else {
let mut flags = 0;
let ret = unsafe { libc::posix_spawnattr_getflags(attrp, &mut flags) };
let ret = unsafe { libc::posix_spawnattr_getflags(attrp, &raw mut flags) };
if ret != 0 {
return Err(nix::Error::from_raw(ret));
}
Expand All @@ -239,11 +239,11 @@ impl Client {
if (*file_actions).is_null() {
let shared_file_actions = self.posix_spawn_file_actions.get_or_init(|| {
let mut fa: libc::posix_spawn_file_actions_t = unsafe { zeroed() };
let ret = unsafe { libc::posix_spawn_file_actions_init(&mut fa) };
let ret = unsafe { libc::posix_spawn_file_actions_init(&raw mut fa) };
assert_eq!(ret, 0);
let ret = unsafe {
posix_spawn_file_actions_addinherit_np(
&mut fa,
&raw mut fa,
self.encoded_payload.payload.ipc_fd,
)
};
Expand Down Expand Up @@ -302,7 +302,5 @@ fn init_client() {

CLIENT.set(Client::from_env()).unwrap();
let ret = unsafe { pthread_atfork(None, None, Some(reset_shm_atfork)) };
if ret != 0 {
panic!("pthread_atfork failed: {ret}");
}
assert!((ret == 0), "pthread_atfork failed: {ret}");
}
6 changes: 3 additions & 3 deletions crates/fspy_preload_unix/src/client/raw_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ impl RawExec {
Exec { program, args, envs }
}

pub fn from_exec<R>(cmd: Exec, f: impl FnOnce(RawExec) -> R) -> R {
pub fn from_exec<R>(cmd: Exec, f: impl FnOnce(Self) -> R) -> R {
let envs: Vec<BString> = cmd
.envs
.into_iter()
.map(|(name, value)| {
let mut env = name.to_owned();
let mut env = name;
if let Some(value) = value {
env.push(b'=');
env.extend_from_slice(&value);
Expand All @@ -82,7 +82,7 @@ impl RawExec {

Self::to_c_str(cmd.program, |prog| {
Self::to_c_str_array(cmd.args, |argv| {
Self::to_c_str_array(envs, |envp| f(RawExec { prog, argv, envp }))
Self::to_c_str_array(envs, |envp| f(Self { prog, argv, envp }))
})
})
}
Expand Down
2 changes: 1 addition & 1 deletion crates/fspy_preload_unix/src/interceptions/dirent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ unsafe extern "C" fn scandir(

#[cfg(target_os = "macos")]
mod macos_only {
use super::*;
use super::{AccessMode, c_char, c_int, c_void, handle_open, intercept};
intercept!(scandir_b: unsafe extern "C" fn (
dirname: *const c_char,
namelist: *mut c_void,
Expand Down
4 changes: 2 additions & 2 deletions crates/fspy_preload_unix/src/interceptions/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use crate::{
macros::intercept,
};

fn has_mode_arg(o_flags: c_int) -> bool {
const fn has_mode_arg(o_flags: c_int) -> bool {
if o_flags & libc::O_CREAT != 0 {
return true;
};
}
#[cfg(target_os = "linux")]
if o_flags & libc::O_TMPFILE != 0 {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn handle_exec(
client.handle_exec(config, RawExec { prog, argv, envp }, |raw_command, pre_exec| {
if let Some(pre_exec) = pre_exec {
pre_exec.run()?;
};
}
Ok(execve::original()(raw_command.prog, raw_command.argv, raw_command.envp))
})
};
Expand Down
11 changes: 8 additions & 3 deletions crates/fspy_shared/src/ipc/native_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use bincode::Decode;
use bincode::{BorrowDecode, Encode};
use bstr::BStr;

/// Similar to OsStr, but requires no copy for encode/borrow_decode
/// Similar to `OsStr`, but requires no copy for `encode/borrow_decode`
#[derive(Encode, BorrowDecode, Clone, Copy, PartialEq, Eq)]
pub struct NativeStr<'a> {
#[cfg(windows)]
Expand Down Expand Up @@ -55,7 +55,8 @@ impl<'a> NativeStr<'a> {
}
}

pub fn from_bytes(bytes: &'a [u8]) -> Self {
#[must_use]
pub const fn from_bytes(bytes: &'a [u8]) -> Self {
Self {
#[cfg(windows)]
is_wide: false,
Expand All @@ -70,11 +71,13 @@ impl<'a> NativeStr<'a> {
}

#[cfg(unix)]
#[must_use]
pub fn as_os_str(&self) -> &'a OsStr {
std::os::unix::ffi::OsStrExt::from_bytes(self.data)
}

#[cfg(unix)]
#[must_use]
pub fn as_bstr(&self) -> &'a BStr {
use bstr::ByteSlice;

Expand Down Expand Up @@ -104,6 +107,7 @@ impl<'a> NativeStr<'a> {
}
}

#[must_use]
pub fn to_cow_os_str(&self) -> Cow<'a, OsStr> {
#[cfg(windows)]
return Cow::Owned(self.to_os_string());
Expand Down Expand Up @@ -153,7 +157,7 @@ impl<'a> From<&'a BStr> for NativeStr<'a> {
}
}

impl<'a> Debug for NativeStr<'a> {
impl Debug for NativeStr<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
<OsStr as Debug>::fmt(self.to_cow_os_str().as_ref(), f)
}
Expand Down Expand Up @@ -203,6 +207,7 @@ impl std::ops::Deref for NativeString {

#[cfg(unix)]
impl NativeString {
#[must_use]
pub fn as_os_str(&self) -> &OsStr {
use std::os::unix::ffi::OsStrExt as _;
OsStr::from_bytes(&self.data)
Expand Down
Loading
Loading