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 / bytesEvery 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-typedoes, and whatxdg-openfalls back to on a desktop it does not recognise — calls a.stlapplication/octet-stream, a.3mfapplication/zipand a.blendapplication/zstd. All true about the bytes; none has a default application, so the file opens in a web browser. - Name only believes a
.txtthat is really a JPEG, and has nothing at all to say about a file calleddownload.
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§
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§
Functions§
- fallback
- What to call something nothing recognised.