Skip to main content

Module lookup

Module lookup 

Source
Expand description

Putting the name and the contents together: what is this file.

§The order, and why it is this one

1. Is it a file at all?      inode/directory, inode/socket, ...
2. Strong magic (>= 80)      \x89PNG is a PNG whatever it is called
3. The name                  part.3mf is a model, though it is a zip
4. Weaker magic (< 80)       a last look at the contents
5. Fall back                 empty / text / bytes

Every step of that is load bearing, and the two in the middle are the ones implementations get wrong in opposite directions:

  • Contents only — what file --mime-type does, and what xdg-open falls back to on a desktop it does not recognise — calls a .stl application/octet-stream, a .3mf application/zip and a .blend application/zstd. All true about the bytes; none has a default application, so the file opens in a web browser.
  • Name only believes a .txt that is really a JPEG, and has nothing at all to say about a file called download.

Strong magic first is what settles the disagreement: a rule the database marks 80 or above is one nobody should doubt (a PNG header, a PDF header), so it beats a filename. Everything below that yields to the name, because a name is a statement of intent and a weak magic rule is a guess.

This is the same order File::MimeInfo::Magic uses, which is the order xdg-open would follow if the perl package happened to be installed — so a machine with this crate on it answers the same question the same way, rather than a third way.

Structs§

Found
What a file turned out to be.
Lookup
The whole database: names, contents, and the type graph.

Enums§

How
How the answer was reached. Kept because the difference matters to a caller: a type from a filename is a statement about intent, one from contents is a statement about bytes, and the fallbacks are neither.

Constants§

BYTES
Nothing recognised it and it does not.
TEXT
Nothing recognised it and it reads as text.

Functions§

fallback
What to call something nothing recognised.