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("myapp");
resolver.register(&ResolverConfig::new("myapp.local", "127.0.0.1", 5553))?;
// ...
resolver.unregister("myapp.local")?;Implementations§
Source§impl FileResolver
impl FileResolver
Sourcepub fn new(prefix: &str) -> Self
pub fn new(prefix: &str) -> Self
Creates a resolver targeting the default /etc/resolver directory.
prefix is used for two purposes:
- Marker comment — files are tagged with
# managed by <prefix>. - Environment variable namespace —
{PREFIX}_RESOLVER_DIRoverrides the default/etc/resolverdirectory (prefix is uppercased,-→_).
Sourcepub fn with_marker(marker: impl Into<String>) -> Self
pub fn with_marker(marker: impl Into<String>) -> Self
Creates a resolver with an exact marker string (written as-is).
Use this when you need full control over the marker comment.
Sourcepub fn dir(self, resolver_dir: impl Into<PathBuf>) -> Self
pub fn dir(self, resolver_dir: impl Into<PathBuf>) -> Self
Overrides the resolver 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 register_permanent(&self, config: &ResolverConfig) -> Result<()>
pub fn register_permanent(&self, config: &ResolverConfig) -> Result<()>
Writes /etc/resolver/<domain> as a permanent (static) entry.
Unlike register, this does not embed a PID in
the marker comment. The file is therefore immune to
cleanup_orphaned (which skips files without
a PID) and survives daemon restarts.
Intended for one-time installation commands (e.g. sudo myapp dns install).
§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. Permanent files (no PID) are also left untouched.
§Errors
Returns ResolverError::Io if the directory cannot be read.