pub struct FileResolver { /* private fields */ }Expand description
Manages /etc/resolver/<domain> files.
§Lifecycle
registerwrites a resolver file.- macOS picks it up immediately (no restart needed).
unregisterremoves the file on shutdown.
§Crash recovery
If the process exits without calling unregister,
the file persists. On next startup, call
cleanup_orphaned to remove files whose
creating PID is no longer running.
§Permissions
/etc/resolver/ requires root. The caller must handle elevation.
§Example
use macos_resolver::{FileResolver, ResolverConfig};
let resolver = FileResolver::new();
resolver.register(&ResolverConfig::new("myapp.local", "127.0.0.1", 5553))?;
// ...
resolver.unregister("myapp.local")?;Implementations§
Source§impl FileResolver
impl FileResolver
Sourcepub fn with_dir(resolver_dir: impl Into<PathBuf>) -> Self
pub fn with_dir(resolver_dir: impl Into<PathBuf>) -> Self
Creates a resolver targeting a custom directory (useful for testing).
Sourcepub fn resolver_dir(&self) -> &Path
pub fn resolver_dir(&self) -> &Path
Returns the resolver directory path.
Sourcepub fn register(&self, config: &ResolverConfig) -> Result<()>
pub fn register(&self, config: &ResolverConfig) -> Result<()>
Writes /etc/resolver/<domain> with the given configuration.
The file contains a marker with the current PID for orphan detection. Calling this again for the same domain overwrites the previous file.
§Errors
Returns ResolverError::Io if the directory cannot be created or
the file cannot be written.
Sourcepub fn unregister(&self, domain: &str) -> Result<()>
pub fn unregister(&self, domain: &str) -> Result<()>
Removes /etc/resolver/<domain>.
Only removes files that contain the ownership marker. Files created
by other tools are left untouched and a ResolverError::NotManaged
error is returned.
If the file does not exist, this is a no-op.
§Errors
Returns ResolverError::Io on I/O failure, or
ResolverError::NotManaged if the file belongs to another tool.
Sourcepub fn list(&self) -> Result<Vec<String>>
pub fn list(&self) -> Result<Vec<String>>
Lists all domains with a managed resolver file.
Returns an empty vec if the directory does not exist.
§Errors
Returns ResolverError::Io if the directory cannot be read.
Sourcepub fn is_registered(&self, domain: &str) -> bool
pub fn is_registered(&self, domain: &str) -> bool
Returns true if domain has a managed resolver file on disk.
Sourcepub fn cleanup_orphaned(&self) -> Result<usize>
pub fn cleanup_orphaned(&self) -> Result<usize>
Removes resolver files whose creating PID is no longer running.
Returns the number of files removed. Non-managed files and files belonging to still-alive processes are left untouched.
§Errors
Returns ResolverError::Io if the directory cannot be read.