Thread overview
mimeapps - finding association between MIME types and applications
Apr 16, 2016
FreeSlave
Apr 16, 2016
Eugene Wissner
Apr 16, 2016
FreeSlave
Apr 16, 2016
ag0aep6g
Apr 16, 2016
FreeSlave
April 16, 2016
mimeapps is a library for detection of which applications can be used to open files of given MIME type.

Note: this is essential only for systems that follow Freedesktop specifications, i.e. Linux, BSD and some others. MS Windows and OS X have their own file type associations systems.

github repo: https://github.com/MyLittleRobo/mimeapps (examples included)
dub package: http://code.dlang.org/packages/mimeapps
Specification: https://www.freedesktop.org/wiki/Specifications/mime-apps-spec/

Use cases:

1. Building "Open with" menu.
2. Finding user preferred application for specific file type, e.g. text editor for text/plain or file manager for inode/directory.
3. Finding user preferred application to open uri, e.g. user preferred web browser for http and https links.

Related libraries:

https://github.com/MyLittleRobo/desktopfile - reading and executing .desktop files.
https://github.com/MyLittleRobo/mime - parsing shared MIME type database and detecting MIME types of files.
April 16, 2016
On Saturday, 16 April 2016 at 18:48:36 UTC, FreeSlave wrote:
> mimeapps is a library for detection of which applications can be used to open files of given MIME type.
>
> Note: this is essential only for systems that follow Freedesktop specifications, i.e. Linux, BSD and some others. MS Windows and OS X have their own file type associations systems.
>
> github repo: https://github.com/MyLittleRobo/mimeapps (examples included)
> dub package: http://code.dlang.org/packages/mimeapps
> Specification: https://www.freedesktop.org/wiki/Specifications/mime-apps-spec/
>
> Use cases:
>
> 1. Building "Open with" menu.
> 2. Finding user preferred application for specific file type, e.g. text editor for text/plain or file manager for inode/directory.
> 3. Finding user preferred application to open uri, e.g. user preferred web browser for http and https links.
>
> Related libraries:
>
> https://github.com/MyLittleRobo/desktopfile - reading and executing .desktop files.
> https://github.com/MyLittleRobo/mime - parsing shared MIME type database and detecting MIME types of files.


Wow. I just wanted to port libmagic since need it. Can you write a short introduction how I can work with the magic database (defining mime type of a file based on its content)?
April 16, 2016
On Saturday, 16 April 2016 at 19:34:52 UTC, Eugene Wissner wrote:
>
> Wow. I just wanted to port libmagic since need it. Can you write a short introduction how I can work with the magic database (defining mime type of a file based on its content)?

Usually mime type detection is done by parsing mime.cache files. These are binary files that can be mapped into memory. mime.cache files are generated by update-mime-database using source packages as base (these are in XML format, see https://specifications.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-0.18.html#idm140001680036896 )

Here's format spec: https://specifications.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-0.18.html#idm140001675194688

Code of 'mime' library responsible for parsing such files: https://github.com/MyLittleRobo/mime/blob/master/source/mime/cache.d

mime.cache file has MagicList entry that store magic rules for all types.
MagicList consists of Match entries sorted by priority. Match includes name of mime type it's related to and has Matchlet entries as children which on their own may have other Matchlets as children (so it's a tree). Each Matchlet describes part of magic rule including content to match and position in file where this content should be found to say that the file is of this type. This information is also stored in separate 'magic' file. Options are described in spec: https://specifications.freedesktop.org/shared-mime-info-spec/shared-mime-info-spec-0.18.html#idm140001675229440

Matchlets have OR logic so if any tree path matches file contents, then this file is of type in this Match.

For better demonstrating of recursive nature of rules see definition of application/x-executable or application/x-sharedlib in /usr/share/mime/packages/freedesktop.org.xml. Here <magic> element coincides with Match entry in mime.cache and <match> elements coincide with Matchlet entries.

So the algorithm is:

1. Iterate over Match entries in MagicList
2. For every Match iterate over every Matchlet.
3. Recursively apply Matchlet rule and its children rules to file content.
4. If some tree path matches file contents the mime type for this file is found (you don't need to check following Match entries, since they have less or the same priority). Otherwise go to the next Match in MagicList.

See source code in 'mime' library responsible for this task: https://github.com/MyLittleRobo/mime/blob/master/source/mime/cache.d#L463

Note that I did not describe how to define mime type when there're more than one mime.cache file and how to handle conflicts and explicitly deleted magic rules. Here's source code though: https://github.com/MyLittleRobo/mime/blob/master/source/mime/detectors/cache.d#L210

Please read the spec.
April 16, 2016
On 16.04.2016 20:48, FreeSlave wrote:
> github repo: https://github.com/MyLittleRobo/mimeapps (examples included)

You've got some bad `@trusted`s in your code:


<https://github.com/MyLittleRobo/mimeapps/blob/03b53ce35d2f4f56fdf76e0b6f5c681660b9d23a/source/mimeapps.d#L29>:

The constraint allows user-defined types that implicitly convert to const(char)[] via alias this. Such a type may, for example, have an unsafe opAssign, which you're mistakenly trusting here.


<https://github.com/MyLittleRobo/mimeapps/blob/03b53ce35d2f4f56fdf76e0b6f5c681660b9d23a/source/mimeapps.d#L191>,
<https://github.com/MyLittleRobo/mimeapps/blob/03b53ce35d2f4f56fdf76e0b6f5c681660b9d23a/source/mimeapps.d#L308>:

Similarly, IniLikeReader may do unsafe stuff and can't be trusted.


> Related libraries:
>
> https://github.com/MyLittleRobo/desktopfile - reading and executing
> .desktop files.
> https://github.com/MyLittleRobo/mime - parsing shared MIME type database
> and detecting MIME types of files.

These have bad `@trusted`s, too. I'm not going to go over all of them, but here are some examples:


<https://github.com/MyLittleRobo/desktopfile/blob/1f2e7a4162493ef292b4c23b152875d83f84a89a/source/desktopfile/utils.d#L754>,
<https://github.com/MyLittleRobo/desktopfile/blob/1f2e7a4162493ef292b4c23b152875d83f84a89a/source/desktopfile/file.d#L705>,
<https://github.com/MyLittleRobo/desktopfile/blob/1f2e7a4162493ef292b4c23b152875d83f84a89a/source/desktopfile/file.d#L718>:

Trusting template arguments again.


<https://github.com/MyLittleRobo/mime/blob/c781948be3ba3f43fa4974592e6a7d7534baaeeb/source/mime/cache.d#L44>:

Reversing the bytes of arbitrary data, including pointers, is not memory-safe.
April 16, 2016
On Saturday, 16 April 2016 at 21:58:35 UTC, ag0aep6g wrote:
> On 16.04.2016 20:48, FreeSlave wrote:
>> github repo: https://github.com/MyLittleRobo/mimeapps (examples included)
>
> You've got some bad `@trusted`s in your code:
>
>
> <https://github.com/MyLittleRobo/mimeapps/blob/03b53ce35d2f4f56fdf76e0b6f5c681660b9d23a/source/mimeapps.d#L29>:
>
> The constraint allows user-defined types that implicitly convert to const(char)[] via alias this. Such a type may, for example, have an unsafe opAssign, which you're mistakenly trusting here.

I would better use inout here, but it has problems with Tuples. I don't know how to rewrite this code to inout so it still remained buildable. The other way of course is to rewrite constraints to match real strings only not types with alias this.

><https://github.com/MyLittleRobo/mimeapps/blob/03b53ce35d2f4f56fdf76e0b6f5c681660b9d23a/source/mimeapps.d#L191>,
> <https://github.com/MyLittleRobo/mimeapps/blob/03b53ce35d2f4f56fdf76e0b6f5c681660b9d23a/source/mimeapps.d#L308>:
>
> Similarly, IniLikeReader may do unsafe stuff and can't be trusted.

This is usually constructed from iniLikeStringReader or iniLikeFileReader from inilike library, but yeah, generally it can be non-trusted user-defined type. Thanks for pointing out. Will fix it.