[magika-lib] Support no_dereference option in Session::identify_file and FeaturesOrRuled::extract_file
Author: reyammerCreated Sep 18, 2026Updated Sep 18, 2026
Context
While integrating magika-lib (rust/lib) with the Python pyo3 bindings (rust/pyo3) and running the Python test suite (test_magika_module_with_symlink and test_broken_symlink), we noticed that rust/lib hardcodes no_dereference = true (std::fs::symlink_metadata).
Current State in rust/lib
In rust/lib/src/session.rs:41 (Session::identify_file) and rust/lib/src/future.rs:59 (FeaturesOrRuled::extract_file):
let metadata = std::fs::symlink_metadata(&file)?;
if metadata.is_dir() {
return Ok(FileType::Directory);
}
if metadata.is_symlink() {
return Ok(FileType::Symlink);
}std::fs::symlink_metadatanever follows symlinks (lstat), soSession::identify_filealways behaves asno_dereference = trueand returnsFileType::Symlinkfor every symlink (even broken symlinks).- However, both Python
Magika(no_dereference=False)andmagika-clidefault tono_dereference = false(following symlinks viastd::fs::metadata/stat, identifying the target file, or returningErrorKind::NotFoundif the symlink target does not exist). - Because
rust/libhardcodessymlink_metadata, callers (rust/cliinrust/cli/src/main.rs:344andrust/pyo3) cannot useSession::identify_filewhenno_dereference = falseand have to reimplement path metadata inspection themselves.
Proposed Rust API Changes
Support no_dereference: bool (defaulting to false) in magika-lib (e.g., on Session / Builder configuration, or as a parameter to Session::identify_file and FeaturesOrRuled::extract_file):
- When
no_dereference == true: callstd::fs::symlink_metadata(&file). - When
no_dereference == false: callstd::fs::metadata(&file)so symlinks are followed and broken symlinks returnmagika::Error::IOError(ErrorKind::NotFound).
Source: google/magika