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

source

pub fn new<I, P>(paths: I) -> Result<Self, InvalidDatabasePathError>
where I: IntoIterator<Item = P>, P: AsRef<Path>,

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

source§

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
source§

impl<P: AsRef<Path>> TryFrom<&[P]> for DatabasePaths

source§

fn try_from(value: &[P]) -> Result<Self, <Self as TryFrom<&[P]>>::Error>

§

type Error = InvalidDatabasePathError

The type returned in the event of a conversion error.
source§

impl TryFrom<&OsStr> for DatabasePaths

source§

fn try_from(value: &OsStr) -> Result<Self, <Self as TryFrom<&OsStr>>::Error>

§

type Error = InvalidDatabasePathError

The type returned in the event of a conversion error.
source§

impl TryFrom<&Path> for DatabasePaths

source§

fn try_from(value: &Path) -> Result<Self, <Self as TryFrom<&Path>>::Error>

§

type Error = InvalidDatabasePathError

The type returned in the event of a conversion error.
source§

impl TryFrom<&str> for DatabasePaths

source§

fn try_from(value: &str) -> Result<Self, <Self as TryFrom<&str>>::Error>

§

type Error = InvalidDatabasePathError

The type returned in the event of a conversion error.
source§

impl<P: AsRef<Path>, const N: usize> TryFrom<[P; N]> for DatabasePaths

source§

fn try_from(value: [P; N]) -> Result<Self, <Self as TryFrom<[P; N]>>::Error>

§

type Error = InvalidDatabasePathError

The type returned in the event of a conversion error.
source§

impl TryFrom<OsString> for DatabasePaths

source§

fn try_from(value: OsString) -> Result<Self, <Self as TryFrom<OsString>>::Error>

§

type Error = InvalidDatabasePathError

The type returned in the event of a conversion error.
source§

impl TryFrom<PathBuf> for DatabasePaths

source§

fn try_from(value: PathBuf) -> Result<Self, <Self as TryFrom<PathBuf>>::Error>

§

type Error = InvalidDatabasePathError

The type returned in the event of a conversion error.
source§

impl TryFrom<String> for DatabasePaths

source§

fn try_from(value: String) -> Result<Self, <Self as TryFrom<String>>::Error>

§

type Error = InvalidDatabasePathError

The type returned in the event of a conversion error.
source§

impl<P: AsRef<Path>> TryFrom<Vec<P>> for DatabasePaths

source§

fn try_from(value: Vec<P>) -> Result<Self, <Self as TryFrom<Vec<P>>>::Error>

§

type Error = InvalidDatabasePathError

The type returned in the event of a conversion error.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.