|
1 | 1 | // SPDX-License-Identifier: MIT AND Palimpsest-0.8 |
| 2 | +// SPDX-FileCopyrightText: 2024-2025 The polysafe-gitfixer Contributors |
2 | 3 |
|
3 | | -//! Polysafe-Gitfixer — Rustler NIF Bindings. |
| 4 | +//! # Rustler NIF Bindings for polysafe-gitfixer |
4 | 5 | //! |
5 | | -//! This crate implements the "Foreign Function Interface" (FFI) required |
6 | | -//! to expose Polysafe's high-assurance Rust components to the Elixir |
7 | | -//! runtime. It uses the `Rustler` library to generate safe, |
8 | | -//! zero-copy bindings. |
| 6 | +//! This crate provides Elixir NIF bindings for the Rust components: |
| 7 | +//! - capability (path safety, audit logging) |
| 8 | +//! - git_ops (git repository operations) |
9 | 9 | //! |
10 | | -//! EXPOSED SUBSYSTEMS: |
11 | | -//! 1. **Capability**: Path safety and unforgeable token management. |
12 | | -//! 2. **AuditLog**: Cryptographic event chaining and verification. |
13 | | -//! 3. **FsOps**: Transactional, atomic filesystem mutations. |
14 | | -//! 4. **GitOps**: Automated repository analysis and state tracking. |
| 10 | +//! The NIFs are exposed to Elixir through the Rustler library. |
| 11 | +//! |
| 12 | +//! ## Architecture |
| 13 | +//! |
| 14 | +//! Each NIF function delegates to the corresponding Rust crate: |
| 15 | +//! - `capability::DirCapability` for path-safe filesystem access |
| 16 | +//! - `capability::AuditLog` for tamper-evident logging |
| 17 | +//! - `git_ops` for git repository introspection and staging |
| 18 | +//! |
| 19 | +//! Handle management uses string-serialised canonical paths as opaque |
| 20 | +//! identifiers, since Rustler NIFs are stateless between calls. The |
| 21 | +//! Elixir side is responsible for tracking handle lifetimes via a |
| 22 | +//! GenServer or ETS table. |
| 23 | +
|
| 24 | +use rustler::{Encoder, Env, NifResult, Term}; |
| 25 | + |
| 26 | +mod atoms { |
| 27 | + rustler::atoms! { |
| 28 | + ok, |
| 29 | + error, |
| 30 | + not_found, |
| 31 | + permission_denied, |
| 32 | + path_traversal, |
| 33 | + invalid_repo, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// --------------------------------------------------------------------------- |
| 38 | +// Capability NIFs |
| 39 | +// --------------------------------------------------------------------------- |
| 40 | + |
| 41 | +/// Create a directory capability for the given path with the specified |
| 42 | +/// permission string. |
| 43 | +/// |
| 44 | +/// ## Parameters |
| 45 | +/// - `path` - Absolute path to the directory root |
| 46 | +/// - `permissions` - One of "full", "read_only", "read_write" |
| 47 | +/// |
| 48 | +/// ## Returns |
| 49 | +/// `{:ok, canonical_root_path}` on success, `{:error, reason}` on failure. |
| 50 | +#[rustler::nif] |
| 51 | +fn create_capability<'a>(env: Env<'a>, path: String, permissions: String) -> NifResult<Term<'a>> { |
| 52 | + let perms = match permissions.as_str() { |
| 53 | + "full" => capability::Permissions::full(), |
| 54 | + "read_only" => capability::Permissions::read_only(), |
| 55 | + "read_write" => capability::Permissions::read_write(), |
| 56 | + other => { |
| 57 | + return Ok(( |
| 58 | + atoms::error(), |
| 59 | + format!("unknown permission string: {other}; expected full|read_only|read_write"), |
| 60 | + ) |
| 61 | + .encode(env)); |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + match capability::DirCapability::new(&path, perms) { |
| 66 | + Ok(cap) => { |
| 67 | + // Return the canonical root as the handle — the Elixir side |
| 68 | + // re-opens a DirCapability from this path on each subsequent call. |
| 69 | + let handle = cap.root().display().to_string(); |
| 70 | + Ok((atoms::ok(), handle).encode(env)) |
| 71 | + } |
| 72 | + Err(capability::CapabilityError::PathNotFound(p)) => { |
| 73 | + Ok((atoms::error(), atoms::not_found(), p.display().to_string()).encode(env)) |
| 74 | + } |
| 75 | + Err(capability::CapabilityError::InvalidRoot(p)) => { |
| 76 | + Ok((atoms::error(), format!("invalid root: {}", p.display())).encode(env)) |
| 77 | + } |
| 78 | + Err(e) => Ok((atoms::error(), e.to_string()).encode(env)), |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/// Resolve a relative path through an existing capability. |
| 83 | +/// |
| 84 | +/// ## Parameters |
| 85 | +/// - `cap_handle` - Canonical root path returned by `create_capability/2` |
| 86 | +/// - `relative_path` - Relative path to resolve within the capability root |
| 87 | +/// |
| 88 | +/// ## Returns |
| 89 | +/// `{:ok, absolute_path}` or `{:error, :path_traversal}` / `{:error, reason}`. |
| 90 | +#[rustler::nif] |
| 91 | +fn resolve_path<'a>( |
| 92 | + env: Env<'a>, |
| 93 | + cap_handle: String, |
| 94 | + relative_path: String, |
| 95 | +) -> NifResult<Term<'a>> { |
| 96 | + // Re-open capability from the handle (canonical root path) |
| 97 | + let cap = match capability::DirCapability::new(&cap_handle, capability::Permissions::read_only()) |
| 98 | + { |
| 99 | + Ok(c) => c, |
| 100 | + Err(e) => return Ok((atoms::error(), e.to_string()).encode(env)), |
| 101 | + }; |
| 102 | + |
| 103 | + match cap.resolve(std::path::Path::new(&relative_path)) { |
| 104 | + Ok(resolved) => Ok((atoms::ok(), resolved.display().to_string()).encode(env)), |
| 105 | + Err(capability::CapabilityError::PathTraversal { .. }) => { |
| 106 | + Ok((atoms::error(), atoms::path_traversal()).encode(env)) |
| 107 | + } |
| 108 | + Err(e) => Ok((atoms::error(), e.to_string()).encode(env)), |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// --------------------------------------------------------------------------- |
| 113 | +// Audit Log NIFs |
| 114 | +// --------------------------------------------------------------------------- |
| 115 | + |
| 116 | +/// Open or create an append-only audit log at the given path. |
| 117 | +/// |
| 118 | +/// ## Parameters |
| 119 | +/// - `path` - Filesystem path for the audit log file |
| 120 | +/// |
| 121 | +/// ## Returns |
| 122 | +/// `{:ok, log_path}` on success (the path doubles as the handle), |
| 123 | +/// `{:error, reason}` on failure. |
| 124 | +#[rustler::nif] |
| 125 | +fn open_audit_log<'a>(env: Env<'a>, path: String) -> NifResult<Term<'a>> { |
| 126 | + match capability::AuditLog::new(&path) { |
| 127 | + Ok(_log) => Ok((atoms::ok(), path).encode(env)), |
| 128 | + Err(e) => Ok((atoms::error(), e.to_string()).encode(env)), |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +/// Append an entry to the audit log. |
| 133 | +/// |
| 134 | +/// ## Parameters |
| 135 | +/// - `log_handle` - Path returned by `open_audit_log/1` |
| 136 | +/// - `operation` - An Elixir map with keys `"type"` and `"details"` that is |
| 137 | +/// serialised to a `capability::audit_log::Operation::Custom` |
| 138 | +/// |
| 139 | +/// ## Returns |
| 140 | +/// `:ok` on success, `{:error, reason}` on failure. |
| 141 | +/// |
| 142 | +/// Note: Currently records every entry as `Operation::Custom` with the |
| 143 | +/// `type` and `details` fields extracted from the Elixir term's string |
| 144 | +/// representation. A richer NIF-side decoder can be added later. |
| 145 | +#[rustler::nif] |
| 146 | +fn append_audit_entry<'a>( |
| 147 | + env: Env<'a>, |
| 148 | + log_handle: String, |
| 149 | + operation: Term<'a>, |
| 150 | +) -> NifResult<Term<'a>> { |
| 151 | + let mut log = match capability::AuditLog::new(&log_handle) { |
| 152 | + Ok(l) => l, |
| 153 | + Err(e) => return Ok((atoms::error(), e.to_string()).encode(env)), |
| 154 | + }; |
| 155 | + |
| 156 | + // Encode the Elixir term as a custom operation. A production NIF would |
| 157 | + // decode structured maps here, but for the initial bridge we capture |
| 158 | + // the term's debug representation as details. |
| 159 | + let op = capability::audit_log::Operation::Custom { |
| 160 | + kind: "nif_entry".to_string(), |
| 161 | + details: format!("{:?}", operation), |
| 162 | + }; |
| 163 | + |
| 164 | + match log.append(op) { |
| 165 | + Ok(()) => Ok(atoms::ok().encode(env)), |
| 166 | + Err(e) => Ok((atoms::error(), e.to_string()).encode(env)), |
| 167 | + } |
| 168 | +} |
| 169 | + |
| 170 | +/// Verify the integrity of the hash chain in an audit log. |
| 171 | +/// |
| 172 | +/// ## Parameters |
| 173 | +/// - `log_handle` - Path returned by `open_audit_log/1` |
| 174 | +/// |
| 175 | +/// ## Returns |
| 176 | +/// `{:ok, entry_count}` if the chain is valid, `{:error, reason}` otherwise. |
| 177 | +#[rustler::nif] |
| 178 | +fn verify_audit_log<'a>(env: Env<'a>, log_handle: String) -> NifResult<Term<'a>> { |
| 179 | + match capability::AuditLog::verify(&log_handle) { |
| 180 | + Ok(count) => Ok((atoms::ok(), count as u64).encode(env)), |
| 181 | + Err(e) => Ok((atoms::error(), e.to_string()).encode(env)), |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +// --------------------------------------------------------------------------- |
| 186 | +// Filesystem Transaction NIFs |
| 187 | +// |
| 188 | +// NOTE: The `fs_ops` crate's `transaction` module is not yet implemented. |
| 189 | +// These NIFs are wired to return descriptive errors until the transaction |
| 190 | +// layer is complete. Each todo!() has been replaced with a graceful error |
| 191 | +// return so that the NIF module loads and the Elixir side gets actionable |
| 192 | +// error tuples instead of panics. |
| 193 | +// --------------------------------------------------------------------------- |
| 194 | + |
| 195 | +/// Begin a new filesystem transaction scoped to a capability root. |
| 196 | +/// |
| 197 | +/// ## Parameters |
| 198 | +/// - `cap_handle` - Canonical root path from `create_capability/2` |
| 199 | +/// |
| 200 | +/// ## Returns |
| 201 | +/// `{:ok, tx_id}` on success, `{:error, reason}` on failure. |
| 202 | +/// |
| 203 | +/// Currently returns an error because the `fs_ops::FsTransaction` backend |
| 204 | +/// is not yet implemented. |
| 205 | +#[rustler::nif] |
| 206 | +fn begin_transaction<'a>(env: Env<'a>, cap_handle: String) -> NifResult<Term<'a>> { |
| 207 | + // Validate that the capability root is still valid |
| 208 | + match capability::DirCapability::new(&cap_handle, capability::Permissions::full()) { |
| 209 | + Ok(_cap) => { |
| 210 | + // fs_ops::FsTransaction is declared but the transaction module is |
| 211 | + // missing — return a structured error so the Elixir caller knows |
| 212 | + // this feature is pending. |
| 213 | + Ok(( |
| 214 | + atoms::error(), |
| 215 | + "fs_ops transaction layer not yet implemented — see crates/fs_ops/src/transaction.rs", |
| 216 | + ) |
| 217 | + .encode(env)) |
| 218 | + } |
| 219 | + Err(e) => Ok((atoms::error(), e.to_string()).encode(env)), |
| 220 | + } |
| 221 | +} |
15 | 222 |
|
16 | | -// ... [Rustler attribute macros and NIF declarations] |
| 223 | +/// Copy a file within a transaction. |
| 224 | +/// |
| 225 | +/// Currently returns an error — transaction layer pending. |
| 226 | +#[rustler::nif] |
| 227 | +fn tx_copy_file<'a>( |
| 228 | + env: Env<'a>, |
| 229 | + _tx_handle: String, |
| 230 | + _from: String, |
| 231 | + _to: String, |
| 232 | +) -> NifResult<Term<'a>> { |
| 233 | + Ok(( |
| 234 | + atoms::error(), |
| 235 | + "fs_ops transaction layer not yet implemented — begin_transaction must succeed first", |
| 236 | + ) |
| 237 | + .encode(env)) |
| 238 | +} |
| 239 | + |
| 240 | +/// Commit a transaction, applying all buffered operations atomically. |
| 241 | +/// |
| 242 | +/// Currently returns an error — transaction layer pending. |
| 243 | +#[rustler::nif] |
| 244 | +fn tx_commit<'a>(env: Env<'a>, _tx_handle: String) -> NifResult<Term<'a>> { |
| 245 | + Ok(( |
| 246 | + atoms::error(), |
| 247 | + "fs_ops transaction layer not yet implemented — begin_transaction must succeed first", |
| 248 | + ) |
| 249 | + .encode(env)) |
| 250 | +} |
| 251 | + |
| 252 | +/// Roll back a transaction, undoing all buffered operations. |
| 253 | +/// |
| 254 | +/// Currently returns an error — transaction layer pending. |
| 255 | +#[rustler::nif] |
| 256 | +fn tx_rollback<'a>(env: Env<'a>, _tx_handle: String) -> NifResult<Term<'a>> { |
| 257 | + Ok(( |
| 258 | + atoms::error(), |
| 259 | + "fs_ops transaction layer not yet implemented — begin_transaction must succeed first", |
| 260 | + ) |
| 261 | + .encode(env)) |
| 262 | +} |
| 263 | + |
| 264 | +// --------------------------------------------------------------------------- |
| 265 | +// Git Operations NIFs |
| 266 | +// --------------------------------------------------------------------------- |
| 267 | + |
| 268 | +/// Check whether the given path contains a valid git repository. |
| 269 | +/// |
| 270 | +/// ## Parameters |
| 271 | +/// - `path` - Filesystem path to check |
| 272 | +/// |
| 273 | +/// ## Returns |
| 274 | +/// `true` or `false`. |
| 275 | +#[rustler::nif] |
| 276 | +fn is_git_repo(path: String) -> bool { |
| 277 | + git_ops::is_valid_repo(&path) |
| 278 | +} |
| 279 | + |
| 280 | +/// Return the full status of a git repository as an Elixir term. |
| 281 | +/// |
| 282 | +/// ## Parameters |
| 283 | +/// - `path` - Root of the git repository |
| 284 | +/// |
| 285 | +/// ## Returns |
| 286 | +/// `{:ok, status_map}` where `status_map` is a map with keys: |
| 287 | +/// - `"path"`, `"branch"`, `"head"`, `"is_clean"`, |
| 288 | +/// `"has_staged"`, `"has_unstaged"`, `"has_untracked"`, |
| 289 | +/// `"entries"` (list of `%{"path" => ..., "index" => ..., "worktree" => ...}`) |
| 290 | +/// |
| 291 | +/// Returns `{:error, :invalid_repo}` if the path is not a git repository. |
| 292 | +#[rustler::nif] |
| 293 | +fn git_status<'a>(env: Env<'a>, path: String) -> NifResult<Term<'a>> { |
| 294 | + match git_ops::repo_status(&path) { |
| 295 | + Ok(status) => { |
| 296 | + // Build a map-like term from the RepoStatus struct. |
| 297 | + // Rustler's Encoder for tuples and vecs gives us a clean Elixir |
| 298 | + // representation without needing a full NifStruct derive. |
| 299 | + let entries: Vec<(String, String, String)> = status |
| 300 | + .entries |
| 301 | + .iter() |
| 302 | + .map(|e| { |
| 303 | + ( |
| 304 | + e.path.clone(), |
| 305 | + format!("{:?}", e.index_status), |
| 306 | + format!("{:?}", e.worktree_status), |
| 307 | + ) |
| 308 | + }) |
| 309 | + .collect(); |
| 310 | + |
| 311 | + Ok(( |
| 312 | + atoms::ok(), |
| 313 | + ( |
| 314 | + ("path", status.path), |
| 315 | + ("branch", format!("{:?}", status.branch)), |
| 316 | + ("head", format!("{:?}", status.head)), |
| 317 | + ("is_clean", status.is_clean), |
| 318 | + ("has_staged", status.has_staged), |
| 319 | + ("has_unstaged", status.has_unstaged), |
| 320 | + ("has_untracked", status.has_untracked), |
| 321 | + ("entries", entries), |
| 322 | + ), |
| 323 | + ) |
| 324 | + .encode(env)) |
| 325 | + } |
| 326 | + Err(_) => Ok((atoms::error(), atoms::invalid_repo()).encode(env)), |
| 327 | + } |
| 328 | +} |
| 329 | + |
| 330 | +/// Stage all changes in the repository (equivalent to `git add -A`). |
| 331 | +/// |
| 332 | +/// ## Parameters |
| 333 | +/// - `path` - Root of the git repository |
| 334 | +/// |
| 335 | +/// ## Returns |
| 336 | +/// `:ok` on success, `{:error, :invalid_repo}` on failure. |
| 337 | +#[rustler::nif] |
| 338 | +fn git_stage_all<'a>(env: Env<'a>, path: String) -> NifResult<Term<'a>> { |
| 339 | + match git_ops::stage_all(&path) { |
| 340 | + Ok(()) => Ok(atoms::ok().encode(env)), |
| 341 | + Err(_) => Ok((atoms::error(), atoms::invalid_repo()).encode(env)), |
| 342 | + } |
| 343 | +} |
| 344 | + |
| 345 | +/// Find all git repositories under a root directory up to `max_depth`. |
| 346 | +/// |
| 347 | +/// ## Parameters |
| 348 | +/// - `root` - Directory to search from |
| 349 | +/// - `max_depth` - Maximum recursion depth |
| 350 | +/// |
| 351 | +/// ## Returns |
| 352 | +/// `{:ok, [path, ...]}` — a list of absolute paths to discovered repos. |
| 353 | +#[rustler::nif] |
| 354 | +fn find_git_repos<'a>(env: Env<'a>, root: String, max_depth: u32) -> NifResult<Term<'a>> { |
| 355 | + match git_ops::find_repos(&root, max_depth as usize) { |
| 356 | + Ok(repos) => Ok((atoms::ok(), repos).encode(env)), |
| 357 | + Err(e) => Ok((atoms::error(), e.to_string()).encode(env)), |
| 358 | + } |
| 359 | +} |
17 | 360 |
|
18 | | -// NIF DISPATCH: Routes Elixir calls to the underlying Rust crates. |
19 | | -#![forbid(unsafe_code)] |
20 | | -/* |
21 | 361 | rustler::init!( |
22 | 362 | "Elixir.PolysafeGitfixer.Native", |
23 | 363 | [ |
24 | 364 | create_capability, |
| 365 | + resolve_path, |
25 | 366 | open_audit_log, |
| 367 | + append_audit_entry, |
26 | 368 | verify_audit_log, |
27 | 369 | begin_transaction, |
| 370 | + tx_copy_file, |
28 | 371 | tx_commit, |
| 372 | + tx_rollback, |
29 | 373 | is_git_repo, |
30 | 374 | git_status, |
| 375 | + git_stage_all, |
| 376 | + find_git_repos, |
31 | 377 | ] |
32 | 378 | ); |
33 | | -*/ |
34 | | - |
35 | | -pub fn placeholder() { |
36 | | - println!("NIF bindings bridge the safety of Rust with the concurrency of Elixir."); |
37 | | -} |
|
0 commit comments