Design - MIME System

The MIME System is a vital part of the file manager. But it is important that the MIME System can be used outside Thunar at a later time (e.g. in the Xfce panel), so care should be taken during the design.

MIME-type detection

The most important and most critical task of the MIME System is to determine a MIME-type for a given file system entity. This task is time-critical, as it must be performed on every single file system entity used in Thunar.

A basic standard-compliant implementation could like this:

This process describes the detection of the primary MIME-type. The other associated MIME-types will be determined by following the subclassing and aliasing rules.

:?: Since every regular file has an implicit MIME-type of application/octet-stream, it may be a good idea to return application/x-extension-<EXTENSION> from the above algorithm as general fallback.

:!: A possible implementation of the Glob Match is available, which features nice optimizations already.

Implementation Hints

A few hints for the implementation:

  • Don’t read the file contents when starting the MIME type detection unless there’s actually a magic rule > 80 present in the database (by default the Shared MIME Database does not contain such a rule). That way, we can completely skip reading the contents for a lot of files, that will simply match one of the Glob rules.

Glob/Magic Rules

Every glob rule has a source file ($XDG_DATA_DIRS/mime/globs) and every magic rule has a source file ($XDG_DATA_DIRS/mime/magic). So, for every rule, the base path to lookup the .xml file that describes the MIME type is defined; for example, the filename text.html matches a glob rule from /usr/share/mime/globs, then the system should use the file /usr/share/mime/text/html.xml.

The implementation should remember the source file or the base path of the source file for every glob and magic rule to allow fast loading of MIME type data. This avoid unnecessary disk seeks, that would else be required to resolve $XDG_DATA_DIRS/mime/<MEDIA>/<SUBTYPE>.

The base path information can be stored per MIME-type, where the first occurence of a specific MIME-type in a globs or magic file wins (checking $XDG_DATA_HOME then $XDG_DATA_DIRS, automatically done by libxfce4util).

Change notification

The on-disk representation of the MIME database should be checked for changes on a certain interval (30sec to 1min). This process should not only check the existing globs/magic files in $XDG_DATA_DIRS, but should also check for new globs/magic files (e.g. somebody added overrides to $XDG_DATA_HOME while Thunar was running, then Thunar should recognize these entries as well).

In case the on-disk representation changed, Thunar should regenerate the in-memory representation and notify all interested components of this change. The best (and easiest) way to do this, seems to be:

  1. Drop the previous in-memory representation, including all MIME-type data
  2. Parse the on-disk representation
  3. Notify interested components

The other components that use MIME information (e.g. the file info management component) should take care to drop all MIME-type information once they are notified of the change and ask the MIME database to determine the MIME-type for a given file system entity again.

 
  design/mime-system.txt · Last modified: 2005/03/06 23:52 by 217.229.249.18 (benny)
 
Recent changes RSS feed Creative Commons License Driven by DokuWiki