pub struct Globs { /* private fields */ }Expand description
The filename rules, loaded from every globs2 on the system.
Implementations§
Source§impl Globs
impl Globs
Sourcepub fn parse(text: &str) -> Globs
pub fn parse(text: &str) -> Globs
Parses one globs2 file’s contents.
Lines that cannot be read are skipped rather than failing the
file: this is generated data, and one unfamiliar line in it must
not cost every rule after it. __NOGLOBS__ — the database’s way
of saying a type has had its inherited patterns removed — carries
no pattern and is skipped with the rest.
Sourcepub fn parse_legacy(text: &str) -> Globs
pub fn parse_legacy(text: &str) -> Globs
The older globs format: type:pattern, with no weight and no
flags. Still shipped beside globs2, and the only one some
hand-made databases have.
Everything in it is weight 50, which is what the format means
and what update-mime-database writes into globs2 for a rule
that did not ask for anything else.
Sourcepub fn load_from(dirs: &[PathBuf]) -> Globs
pub fn load_from(dirs: &[PathBuf]) -> Globs
Every globs2 under dirs (each a XDG_DATA_DIRS entry),
merged. Earlier directories are more specific and are consulted
first when two rules are otherwise equal.
A directory with no globs2 falls back to its globs — the
older file, which is what a hand-made database is most likely to
have. Never both from the same directory: they say the same
thing, and reading both would double every rule in it.
Sourcepub fn type_of(&self, path: &Path) -> Option<&str>
pub fn type_of(&self, path: &Path) -> Option<&str>
The type of a file with this name, or None when no rule
matches.
None rather than application/octet-stream: “no rule knows
this name” and “this is a stream of bytes” are different answers,
and a caller offering to open the file needs to tell them apart —
the first deserves “nothing here knows what this is”, the second
would be a claim the database never made. This is the same
distinction as a missing config file versus an unparseable one.
When several rules match, the database’s own precedence applies:
the highest weight wins, then the longest pattern — so
archive.tar.gz is a compressed tar rather than a gzip file,
because *.tar.gz is longer than *.gz. A case-sensitive rule
only matches the spelling it gives.
Case is tried exactly first, then folded. a.c and a.C
are different files: the database has *.c for C and *.C for
C++, and a lookup that lowercases everything up front makes one
of them unreachable. So the name is matched as written, and only
if nothing matches is it tried again in lower case — which is
what makes script.PL still a perl script.
Beyond that the specification says the result is undefined, and implementations do differ. Two tie-breaks, in this order, each chosen against what the rest of this machine answers:
- A registered type beats an
x-one.*.objis claimed bymodel/obj,application/x-coffandapplication/x-tgifat the same weight;x-means unregistered, and a Wavefront model is the better reading of a.objthan a COFF object file.gioanswersmodel/objhere too. - Then the first rule wins, which is what the reference
implementation does (it skips a second rule for an extension
it has already seen).
*.jsonis claimed byapplication/jsonandapplication/schema+json; the first is the answer everything else on the machine gives.
Sourcepub fn all_matches(&self, path: &Path) -> Vec<&str>
pub fn all_matches(&self, path: &Path) -> Vec<&str>
Every type whose pattern matches this name, best first.
For mimetype --all, and for a caller that would rather see an
ambiguity than have it resolved: photo.jpg matches one rule,
but archive.tar.gz matches both *.tar.gz and *.gz and a
person may want to know that.
Sourcepub fn mimes(&self) -> impl Iterator<Item = &str>
pub fn mimes(&self) -> impl Iterator<Item = &str>
Every type some rule can name a file after, in load order and with repeats — one type usually has several patterns.
This is as close to “the types this machine knows about” as the
database gets. It is deliberately the glob file and not the
union of everything mentioned anywhere: a type no filename can
produce is not one somebody will go looking for by name, and
subclasses alone names hundreds of them.