Skip to main content

rightkit_fs/
lib.rs

1//! Safe filesystem state for Right Suite apps.
2//!
3//! - [`atomic`]: temp + `fsync` + rename replacement, streaming writer, crash-orphan sweep.
4//!   [`atomic_write`] is the one-call entry point.
5//! - [`lock`]: OS advisory cross-process locks with owner/stale reporting. Two primitives:
6//!   [`FileLock::acquire`] / [`FileLock::try_acquire`] take the exclusive lock, and
7//!   [`FileLock::acquire_shared`] takes a shared (reader) lock.
8//! - [`digest`] (feature `digest`): SHA-256 of bytes or files, and lowercase hex.
9//! - [`private`]: owner-only files and directories (0600/0700, Windows DACL).
10//! - [`state`]: [`StateDir`], a descriptor-pinned state directory (`O_NOFOLLOW`, fd-relative
11//!   `*at` operations) that a swapped or symlinked path cannot redirect.
12//! - [`identity`] (feature `json`): stable file identity across renames and atomic replacement.
13//! - [`clone`]: macOS `clonefile(2)` with `CLONE_NOFOLLOW`; never falls back to a byte copy.
14
15#[cfg(feature = "fs")]
16pub mod atomic;
17#[cfg(feature = "fs")]
18pub mod clone;
19#[cfg(feature = "digest")]
20pub mod digest;
21#[cfg(feature = "fs")]
22pub mod error;
23#[cfg(feature = "json")]
24pub mod identity;
25#[cfg(feature = "lock")]
26pub mod lock;
27#[cfg(feature = "fs")]
28pub mod netfs;
29#[cfg(feature = "fs")]
30pub mod private;
31#[cfg(feature = "fs")]
32pub mod state;
33
34#[cfg(feature = "fs")]
35pub use atomic::{
36    atomic_write, atomic_write_private, atomic_write_with, durable_sync, sweep_stale_temps,
37    AtomicOptions, AtomicWriter,
38};
39#[cfg(feature = "fs")]
40pub use clone::{clone_file_nofollow, CloneError};
41#[cfg(feature = "fs")]
42pub use error::{FsError, Result};
43#[cfg(feature = "json")]
44pub use identity::{FileKey, IdentityStore, Resolution, StableId, Tracked};
45#[cfg(feature = "lock")]
46pub use lock::{with_lock, FileLock, LockOptions, LockOwner};
47#[cfg(feature = "fs")]
48pub use netfs::is_network_filesystem;
49#[cfg(feature = "fs")]
50pub use private::{
51    create_private_new, ensure_private_dir, restrict_owner_only, sweep_owner_only,
52    write_private_file, write_private_text,
53};
54#[cfg(feature = "fs")]
55pub use state::{EntryKind, EntryStat, StateDir};
56
57/// Whether process `pid` currently exists.
58#[cfg(feature = "fs")]
59pub fn process_alive(pid: u32) -> bool {
60    #[cfg(unix)]
61    {
62        // SAFETY: signal 0 performs only the existence/permission check.
63        let rc = unsafe { libc::kill(pid as libc::pid_t, 0) };
64        rc == 0 || std::io::Error::last_os_error().raw_os_error() == Some(libc::EPERM)
65    }
66    #[cfg(windows)]
67    {
68        #[link(name = "kernel32")]
69        unsafe extern "system" {
70            fn OpenProcess(access: u32, inherit: i32, pid: u32) -> *mut std::ffi::c_void;
71            fn GetExitCodeProcess(handle: *mut std::ffi::c_void, code: *mut u32) -> i32;
72            fn CloseHandle(handle: *mut std::ffi::c_void) -> i32;
73        }
74        const PROCESS_QUERY_LIMITED_INFORMATION: u32 = 0x1000;
75        const STILL_ACTIVE: u32 = 259;
76        unsafe {
77            let handle = OpenProcess(PROCESS_QUERY_LIMITED_INFORMATION, 0, pid);
78            if handle.is_null() {
79                return false;
80            }
81            let mut code = 0u32;
82            let ok = GetExitCodeProcess(handle, &mut code);
83            CloseHandle(handle);
84            ok != 0 && code == STILL_ACTIVE
85        }
86    }
87    #[cfg(not(any(unix, windows)))]
88    {
89        let _ = pid;
90        true
91    }
92}