Ubuntu uses advanced systems for detecting file types and associating them with applications, but it's not that hard to dive in and take some control over the formats. In this article by Delan Azabani, you'll learn how Ubuntu identifies file types, how to use Assogiate to control these processes, using Ubuntu Tweak to associate types with applications and use Bless to inspect binary files.
Let's not go as far deep as "What is a file?", but before we start, let's take a look at file types. File types are determined by the contents of the files themselves, and are used to allow the opening program to be chosen wisely. In Microsoft Windows, file extension globbing is the sole method of identifying file types. Users must provide a common phrase at the end of files, and the files would be searched by their name, in turn providing the correct icon and program.
Things are a little different in Ubuntu. Of course, globbing (the most basic method) is present for file types, but Ubuntu has a few other tricks up its sleeve. One of these is magic numbers. The magic number of a binary file is the first few bytes, which identify the file type. The definition of a "magic" number has somewhat loosened in recent years; it can now mean any piece of data, generally near the beginning of a file, that can be used to uniquely identify the type.
Another, more powerful, but too rarely used feature, is XML namespace matching. Without this feature, all XML files wouldn't be able to be more specifically identified, with the exception of extension globbing, of course. Namespace matching allows for quick detection of a XML-based format based on not only the namespace, but also the root element. For example, XHTML files (application/xhtml+xml) can not only be matched by an xhtml file extension, but also by its namespace URI (http://www.w3.org/1999/xhtml) and its root element (html).
In Ubuntu, programs such as Nautilus use the shared-mime-database as the sole location for file type information. Unfortunately, other Gnome facilities such as file open and save dialogs only use extension globbing, and are independent from the MIME database. These databases are stored in a similar way to how programs can be located in four tiers, /bin, /usr/bin, /usr/local/bin and ~/bin. These databases can be found in the following directories:
Like the program tiers, it is generally agreed that only MIME types installed from Ubuntu packages should be located in the first level. System-wide changes by the user or programs installed via make install are placed in the second tier, while changes local to the user are in the third.
The directories inside these MIME databases represent MIME groups, for example ./video for video/* MIME types, and ./application for application/* types. Not all of these directories may exist; they'll be created on demand for file types. In these directories, there are multiple XML files, each named by their MIME suffix. They contain nodes with information about magic numbers, extension globs, parent types, child and alias types, and the file type description (often in multiple languages).
The update-mime-database command, invoked manually or as a trigger opened when packages are changed, draws upon the information in these files and turns them into fast-seeking formats that aren't as friendly as XML. These real databases are in the following files:
A long time ago, when I was learning about the MIME database, I used Bless to directly edit these files to create changes, but was always confused by my changes immediately disappearing. This is because the information is converted one way from the XML files to the cache files.
Before we use programs to modify the MIME database for convenience, here's a quick breakdown of the format of the XML files in the database. The root element is mime-info, with the shared MIME info namespace:
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info"/>
This root element contains any number of mime-type nodes, providing detection information about a file type. You could even have an empty mime-info node, but that isn't productive at all.
The following are a selection of the most important elements that can be found in mime-type nodes:
<glob pattern="*.mkv" weight="55"/>
<magic priority="60"> <match type="string" offset="0" value="DVDVIDEO"> </magic>
<alias type="video/x-matroska-mkv"/>
Here's an example XML source file that uses a couple of these features (this file type is bogus, I just created it for the example):
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info"> <mime-type> <comment xml:lang="en-AU">DML source document</comment> <acronym>DML</acronym> <expanded-acronym xml:lang="en-AU">Delan's Markup Language</expanded-acronym> <sub-class-of type="application/xml"/> <glob-deleteall/> <glob pattern="*.dml"/> <root-XML namespaceURI="http://azabani.com/dml" localName="dml"/> </mime-type> </mime-info>
Assogiate is a neat little program that allows you to create and modify file types, modifying the database in a very user-friendly and quick way. It can access the user database, ~/.local/share/mime, or the system override database, /usr/local/share/mime. Changes are not, however, placed in XML files with the file name structure of the MIME type, instead they are placed in ./packages/Override.xml allowing for a memory of the user-changed file types.
Assogiate can be found in the Ubuntu universe repository:
sudo apt-get install assogiate
In the case that it is not, you can download and compile it:
curl http://azabani.com/files/apps/assogiate-0.2.1.tar.gz
| tar xvz
cd assogiate-0.2.1; ./configure; make; sudo make install
You aren't allowed to change the system override database without running the program as a privileged user, so always run it as root:
gksu assogiate
In the Assogiate window, you can use the toolbar buttons to add and modify selected file types, remove and revert changes, or search for file types. The left pane allows you to narrow your view to groups of MIME types, or user modified types.
The process for these two actions is very similar. When you are in the Edit Type dialog, you can edit canonical information, alias and parent types, globbing, magic numbers and XML namespace matching each in its own tab.
Set up, maintain, and secure a small office email server |