Struct magic::cookie::DatabasePaths
source · pub struct DatabasePaths { /* private fields */ }
Expand description
Magic database file paths
libmagic
requires database file paths for certain operations on a Cookie
that must:
- be a valid C string
- not contain “:” (colon), since that is used to separate multiple file paths (on all platforms)
Those operations are Cookie::load()
, Cookie::compile()
, Cookie::check()
, Cookie::list()
.
Cookie::file()
does not take database file paths but the single file to inspect instead.
The default unnamed database can be constructed with Default::default()
.
Explicit paths can be constructed manually with new()
or by fallible conversion from an array, slice or Vec
containing something convertible as std::path::Path
, or a single something.
Note that this only ensures the paths themselves are valid. Operating on those database file paths can still fail, for example if they refer to files that do not exist, can not be opened or do not have the required format.
§Examples
// `: DatabasePaths` type annotation is only needed for these examples
// if you pass it to Cookie::load() etc., Rust will figure it out
// construct default unnamed database paths
let database: DatabasePaths = Default::default();
// construct from single path
let database: DatabasePaths = "first-directory/first-database".try_into()?;
let database: DatabasePaths =
std::path::Path::new("second-directory/second-database").try_into()?;
// construct from multiple paths in array
let array: [&'static str; 2] = [
"first-directory/first-database",
"second-directory/second-database",
];
let database: DatabasePaths = array.try_into()?;
// construct from multiple paths in slice
let database: DatabasePaths = [
"first-directory/first-database".as_ref(),
std::path::Path::new("second-directory/second-database"),
]
.try_into()?;
// construct from multiple paths in Vec
let database: DatabasePaths = vec![
std::ffi::OsStr::new("first-directory/first-database"),
"second-directory/second-database".as_ref(),
]
.try_into()?;
Implementations§
source§impl DatabasePaths
impl DatabasePaths
sourcepub fn new<I, P>(paths: I) -> Result<Self, InvalidDatabasePathError>
pub fn new<I, P>(paths: I) -> Result<Self, InvalidDatabasePathError>
Create a new database paths instance
Using one of the TryFrom
implementations is recommended instead, see DatabasePaths
examples.
Empty paths
returns Default::default()
.
§Errors
If the paths
contain a “:” (colon), a cookie::InvalidDatabasePathError
will be returned.
Trait Implementations§
source§impl Default for DatabasePaths
impl Default for DatabasePaths
source§fn default() -> Self
fn default() -> Self
Returns the path for the default unnamed database/s
Note that the default database/s can be overwritten by setting the “MAGIC” environment variable to a colon-separated text of database file paths:
$ export MAGIC='data/tests/db-python:data/tests/db-images-png-precompiled.mgc'
$ # file-ish uses `DatabasePaths::default()`
$ cargo run --example file-ish -- data/tests/rust-logo-128x128-blk.png
This is a feature of libmagic
itself, not of this Rust crate.
Note that the file
CLI (which uses libmagic
) prints the location of its default database with:
$ file --version
file-5.38
magic file from /etc/magic:/usr/share/misc/magic
$ export MAGIC='data/tests/db-python:data/tests/db-images-png-precompiled.mgc'
$ file --version
file-5.39
magic file from data/tests/db-python:data/tests/db-images-png-precompiled.mgc