upload tizen1.0 source
[external/shared-mime-info.git] / shared-mime-info-spec.xml
1 <?xml version="1.0" standalone="no"?>
2 <!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" [
4   <!ENTITY updated "10 June 2008">
5   <!ENTITY version "0.18">
6 ]>
7 <article id="index">
8
9 <articleinfo>
10         <authorgroup>
11                 <corpauthor>
12                         <ulink url="http://www.freedesktop.org">
13                         X Desktop Group
14                         </ulink>
15                 </corpauthor>
16                 <author>
17                         <firstname>Thomas</firstname>
18                         <surname>Leonard</surname>
19                         <affiliation>
20                                 <address><email>tal197 at users.sf.net</email></address>
21                         </affiliation>
22                 </author>
23         </authorgroup>
24
25         <title>Shared MIME-info Database</title>
26         <date>&updated;</date>
27 </articleinfo>
28
29 <sect1>
30         <title>Introduction</title>
31         <sect2>
32                 <title>Version</title>
33                 <para>
34 This is version &version; of the Shared MIME-info Database specification, last updated &updated;.</para>
35         </sect2>
36         <sect2>
37                 <title>What is this spec?</title>
38                 <para>
39 Many programs and desktops use the MIME system<citation>MIME</citation>
40 to represent the types of files. Frequently, it is necessary to work out the
41 correct MIME type for a file. This is generally done by examining the file's
42 name or contents, and looking up the correct MIME type in a database.
43                 </para>
44                 <para>
45 It is also useful to store information about each type, such as a textual
46 description of it, or a list of applications that can be used to view or edit
47 files of that type.
48                 </para>
49                 <para>
50 For interoperability, it is useful for different programs to use the same
51 database so that different programs agree on the type of a file and
52 information is not duplicated. It is also helpful for application authors to
53 only have to install new information in one place.
54                 </para>
55                 <para>
56 This specification attempts to unify the MIME database systems currently in
57 use by GNOME<citation>GNOME</citation>, KDE<citation>KDE</citation> and
58 ROX<citation>ROX</citation>, and provide room for future extensibility.
59                 </para>
60                 <para>
61 The MIME database does NOT store user preferences (such as a user's preferred
62 application for handling files of a particular type). It may be used to store
63 static information, such as that files of a certain type may be viewed with
64 a particular application.
65                 </para>
66         </sect2>
67         <sect2>
68                 <title>Language used in this specification</title>
69                 <para>
70 The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD",
71 "SHOULD NOT", "RECOMMENDED",  "MAY", and "OPTIONAL" in this document are to be
72 interpreted as described in RFC 2119<citation>RFC-2119</citation>.
73                 </para>
74         </sect2>
75 </sect1>
76
77 <sect1>
78         <title>Unified system</title>
79         <para>
80 In discussions about the previous systems used by GNOME, KDE and ROX (see the
81 "History and related systems" document), it was clear that the differences
82 between the databases were simply a result of them being separate, and not due
83 to any fundamental disagreements between developers. Everyone is keen to see
84 them merged.
85         </para>
86         <para>
87 This specification proposes:
88
89                 <itemizedlist>
90                         <listitem><para>
91 A standard way for applications to install new MIME related information.
92                         </para></listitem>
93                         <listitem><para>
94 A standard way of getting the MIME type for a file.
95                         </para></listitem>
96                         <listitem><para>
97 A standard way of getting information about a MIME type.
98                         </para></listitem>
99                         <listitem><para>
100 Standard locations for all the files, and methods of resolving conflicts.
101                         </para></listitem>
102                 </itemizedlist>
103 Further, the existing databases have been merged into a single package
104 <citation>SharedMIME</citation>.
105         </para>
106         <sect2 id="s2_layout">
107                 <title>Directory layout</title>
108                 <para>
109 There are two important requirements for the way the MIME database is stored:
110                         <itemizedlist>
111                                 <listitem><para>
112 Applications must be able to extend the database in any way when they are installed,
113 to add both new rules for determining type, and new information about specific types.
114                                 </para></listitem>
115                                 <listitem><para>
116 It must be possible to install applications in /usr, /usr/local and the user's home directory
117 (in the normal Unix way) and have the MIME information used.
118                                 </para></listitem>
119                         </itemizedlist>
120                 </para>
121                 <para>
122 This specification uses the XDG Base Directory Specification<citation>BaseDir</citation> to
123 define the prefixes below which the database is stored.
124 In the rest of this document, paths shown with the prefix
125 <filename>&lt;MIME&gt;</filename> indicate the files should be loaded from
126 the <filename>mime</filename> subdirectory of every directory in
127 <envar>XDG_DATA_HOME</envar>:<envar>XDG_DATA_DIRS</envar>.
128                 </para>
129                 <para>
130 For example, when using the default paths, <quote>Load all the
131 <filename>&lt;MIME&gt;/text/html.xml</filename> files</quote> means to
132 load <filename>/usr/share/mime/text/html.xml</filename>,
133 <filename>/usr/local/share/mime/text/html.xml</filename>, and
134 <filename>~/.local/share/mime/text/html.xml</filename> (if they exist).
135                 </para>
136                 <para>
137 Each application that wishes to contribute to the MIME database will install a
138 single XML file, named after the application, into one of the three
139 <filename>&lt;MIME&gt;/packages/</filename> directories (depending on where the user requested
140 the application be installed). After installing, uninstalling or modifying this
141 file, the application MUST run the <command>update-mime-database</command> command,
142 which is provided by the freedesktop.org shared database<citation>SharedMIME</citation>.
143                 </para>
144                 <para>
145 <command>update-mime-database</command> is passed the <filename>mime</filename>
146 directory containing the <filename>packages</filename> subdirectory which was
147 modified as its only argument. It scans all the XML files in the <filename>packages</filename>
148 subdirectory, combines the information in them, and creates a number of output files.
149                 </para>
150                 <para>
151 Where the information from these files is conflicting, information from directories
152 lower in the list takes precedence.
153 Any file named <filename>Override.xml</filename> takes precedence over all other files in
154 the same <filename>packages</filename> directory. This can be used by
155 tools which let the user edit the database to ensure that the user's
156 changes take effect.
157                 </para>
158                 <para>
159 The files created by <command>update-mime-database</command> are:
160                         <itemizedlist>
161                                 <listitem><para>
162 <filename>&lt;MIME&gt;/globs</filename> (contains a mapping from names to MIME types) [deprecated for globs2]
163                                 </para></listitem>
164                                 <listitem><para>
165 <filename>&lt;MIME&gt;/globs2</filename> (contains a mapping from names to MIME types and glob weight)
166                                 </para></listitem>
167                                 <listitem><para>
168 <filename>&lt;MIME&gt;/magic</filename> (contains a mapping from file contents to MIME types)
169                                 </para></listitem>
170                                 <listitem><para>
171 <filename>&lt;MIME&gt;/subclasses</filename> (contains a mapping from MIME types to types they inherit from)
172                                 </para></listitem>
173                                 <listitem><para>
174 <filename>&lt;MIME&gt;/aliases</filename> (contains a mapping from aliases to MIME types)
175                                 </para></listitem>
176                                 <listitem><para>
177 <filename>&lt;MIME&gt;/icons</filename> (contains a mapping from MIME types to icons)
178                                 </para></listitem>
179                                 <listitem><para>
180 <filename>&lt;MIME&gt;/generic-icons</filename> (contains a mapping from MIME types to generic icons)
181                                 </para></listitem>
182                                 <listitem><para>
183 <filename>&lt;MIME&gt;/XMLnamespaces</filename> (contains a mapping from XML
184 (namespaceURI, localName) pairs to MIME types)
185                                 </para></listitem>
186                                 <listitem><para>
187 <filename>&lt;MIME&gt;/MEDIA/SUBTYPE.xml</filename> (one file for each MIME
188 type, giving details about the type, including comment, icon and generic-icon)
189                                 </para></listitem>
190                                 <listitem><para>
191 <filename>&lt;MIME&gt;/mime.cache</filename> (contains the same information as the <filename>globs2</filename>,
192 <filename>magic</filename>, <filename>subclasses</filename>, <filename>aliases</filename>,
193 <filename>icons</filename>, <filename>generic-icons</filename> and <filename>XMLnamespaces</filename> files,
194 in a binary, mmappable format)
195                                 </para></listitem>
196                         </itemizedlist>
197 The format of these generated files and the source files in <filename>packages</filename>
198 are explained in the following sections. This step serves several purposes. First, it allows
199 applications to quickly get the data they need without parsing all the source XML files (the
200 base package alone is over 700K). Second, it allows the database to be used for other
201 purposes (such as creating the <filename>/etc/mime.types</filename> file if
202 desired). Third, it allows validation to be performed on the input data,
203 and removes the need for other applications to carefully check the input for
204 errors themselves.
205                 </para>
206         </sect2>
207         <sect2>
208                 <title>The source XML files</title>
209                 <para>
210 Each application provides only a single XML source file, which is installed in the
211 <filename>packages</filename> directory as described above. This file is an XML file
212 whose document element is named <userinput>mime-info</userinput> and whose namespace URI
213 is <ulink url="http://www.freedesktop.org/standards/shared-mime-info"/>. All elements
214 described in this specification MUST have this namespace too.
215                 </para><para>
216 The document element may contain zero or more <userinput>mime-type</userinput> child nodes,
217 in any order, each describing a single MIME type. Each element has a <userinput>type</userinput>
218 attribute giving the MIME type that it describes.
219                 </para><para>
220 Each <userinput>mime-type</userinput> node may contain any combination of the following elements,
221 and in any order:
222                         <itemizedlist>
223                                 <listitem><para>
224 <userinput>glob</userinput> elements have a <userinput>pattern</userinput> attribute. Any file
225 whose name matches this pattern will be given this MIME type (subject to conflicting rules in
226 other files, of course). There is also an optional <userinput>weight</userinput> attribute which
227 is used when resolving conflicts with other glob matches. The default weight value is 50, and
228 the maximum is 100.
229                 </para>
230                 <para>
231 KDE's glob system replaces GNOME's and ROX's ext/regex fields, since it
232 is trivial to detect a pattern in the form '*.ext' and store it in an
233 extension hash table internally. The full power of regular expressions was
234 not being used by either desktop, and glob patterns are more suitable for
235 filename matching anyway.
236                                 </para></listitem>
237                                 <listitem><para>
238 <userinput>magic</userinput> elements contain a list of
239 <userinput>match</userinput> elements, any of which may match, and an optional
240 <userinput>priority</userinput> attribute for all of the contained rules. Low
241 numbers should be used for more generic types (such as 'gzip compressed data')
242 and higher values for specific subtypes (such as a word processor format that
243 happens to use gzip to compress the file). The default priority value is 50, and
244 the maximum is 100.
245                                 </para><para>
246 Each <userinput>match</userinput> element has a number of attributes:
247
248 <informaltable>
249         <tgroup cols="3">
250         <thead><row><entry>Attribute</entry><entry>Required?</entry><entry>Value</entry></row></thead>
251         <tbody>
252
253         <row><entry>type</entry><entry>Yes</entry><entry>
254 <userinput>string</userinput>, <userinput>host16</userinput>,
255 <userinput>host32</userinput>, <userinput>big16</userinput>,
256 <userinput>big32</userinput>, <userinput>little16</userinput>,
257 <userinput>little32</userinput> or <userinput>byte</userinput>.
258         </entry></row>
259
260         <row><entry>offset</entry><entry>Yes</entry><entry>The byte offset(s)
261         in the file to check. This may be a single number or a range in the
262         form `start:end', indicating that all offsets in the range should be
263         checked. The range is inclusive.</entry></row>
264
265         <row><entry>value</entry><entry>Yes</entry><entry>
266         The value to compare the file contents with, in the format indicated by the type
267         attribute.
268         </entry></row>
269
270         <row><entry>mask</entry><entry>No</entry><entry>
271         The number to AND the value in the file with before comparing it to
272         `value'. Masks for numerical types can be any number, while masks for strings
273         must be in base 16, and start with 0x.
274         </entry></row>
275
276         </tbody></tgroup>
277 </informaltable>
278
279 Each element corresponds to one line of
280 <citerefentry><refentrytitle>file</refentrytitle>
281 <manvolnum>1</manvolnum></citerefentry>'s <filename>magic.mime</filename> file.
282 They can be nested in the same way to provide the equivalent of continuation
283 lines. That is, <![CDATA[<a><b/><c/></a>]]> means 'a and (b or c)'.
284                                 </para></listitem>
285                                 <listitem><para>
286 <userinput>alias</userinput> elements indicate that the type is also sometimes
287 known by another name, given by the <userinput>type</userinput> attribute. For
288 example, <userinput>audio/midi</userinput> has an alias of
289 <userinput>audio/x-midi</userinput>. Note that there should not be a
290 <userinput>mime-type</userinput> element defining each alias; a single
291 element defines the canonical name for the type and lists all its aliases.
292                                 </para></listitem>
293                                 <listitem><para>
294 <userinput>sub-class-of</userinput> elements indicate that any data of this
295 type is also some other type, given by the <userinput>type</userinput>
296 attribute. See <xref linkend="subclassing"/>.
297                                 </para></listitem>
298                                 <listitem><para>
299 <userinput>comment</userinput> elements give a human-readable textual description of the MIME
300 type, usually composed of an acronym of the file name extension and a short description, like
301 "ODS spreadsheet".
302 There may be many of these elements with different <userinput>xml:lang</userinput> attributes
303 to provide the text in multiple languages.
304                                 </para></listitem>
305                                 <listitem><para>
306 <userinput>acronym</userinput> elements give experienced users a terse idea of the document contents.
307 for example "ODS", "GEDCOM", "JPEG" and "XML".
308 There may be many of these elements with different <userinput>xml:lang</userinput> attributes
309 to provide the text in multiple languages, although these should only be used if absolutely neccessary.
310                                 </para></listitem>
311                                 <listitem><para>
312 <userinput>expanded-acronym</userinput> elements are the expanded versions of the acronym elements,
313 for example "OpenDocument Spreadsheet", "GEnealogical Data COMmunication", and "eXtensible Markup Language".
314 The purpose of these elements is to provide users a way to look up information on various MIME types or
315 file formats in third-party resources.
316 There may be many of these elements with different <userinput>xml:lang</userinput> attributes
317 to provide the text in multiple languages, although these should only be used if absolutely neccessary.
318                                 </para></listitem>
319                                 <listitem><para>
320 <userinput>icon</userinput> elements specify the icon to be used for this particular mime-type, given
321 by the <userinput>name</userinput> attribute. Generally the icon used for a mimetype is created
322 based on the mime-type by mapping "/" characters to "-", but users can override this by using
323 the <userinput>icon</userinput> element to customize the icon for a particular mimetype.
324 This element is not used in the system database, but only used in the user overridden database.
325 Only one <userinput>icon</userinput> element is allowed.
326                                 </para></listitem>
327                                 <listitem><para>
328 <userinput>generic-icon</userinput> elements specify the icon to use as a generic icon for this
329 particular mime-type, given by the <userinput>name</userinput> attribute. This is used if there
330 is no specific icon (see <userinput>icon</userinput> for how these are found). These are
331 used for categories of similar types (like spreadsheets or archives) that can use a common icon.
332 The Icon Naming Specification lists a set of such icon names. If this element is not specified
333 then the mimetype is used to generate the generic icon by using the top-level media type (e.g.
334 "video" in "video/ogg") and appending "-x-generic" (i.e. "video-x-generic" in the previous example). 
335 Only one <userinput>generic-icon</userinput> element is allowed.
336                                 </para></listitem>
337                                 <listitem><para>
338 <userinput>root-XML</userinput> elements have <userinput>namespaceURI</userinput> 
339 and <userinput>localName</userinput> attributes. If a file is identified as being an XML file,
340 these rules allow a more specific MIME type to be chosen based on the namespace and localname
341 of the document element.
342                                 </para><para>
343 If <userinput>localName</userinput> is present but empty then the document element may have
344 any name, but the namespace must still match.
345                                 </para></listitem>
346                                 <listitem><para>
347 <userinput>treemagic</userinput> elements contain a list of <userinput>treematch</userinput> elements,
348 any of which may match, and an optional <userinput>priority</userinput> attribute for all of the 
349 contained rules. The default priority value is 50, and the maximum is 100.
350                                 </para><para>
351 Each <userinput>treematch</userinput> element has a number of attributes:
352
353 <informaltable>
354         <tgroup cols="3">
355         <thead><row><entry>Attribute</entry><entry>Required?</entry><entry>Value</entry></row></thead>
356         <tbody>
357
358         <row><entry>path</entry><entry>Yes</entry><entry>A path that must be present on the mounted volume/filesystem. The path is interpreted as a relative path starting at the root of the tested volume/filesystem</entry></row>
359         
360         <row><entry>type</entry><entry>No</entry><entry>The type of path. Possible values: <userinput>file</userinput>, <userinput>directory</userinput>, <userinput>link</userinput></entry></row>
361
362         <row><entry>match-case</entry><entry>No</entry><entry>Whether path should be matched case-sensitively. Possible values: <userinput>true</userinput>, <userinput>false</userinput></entry></row>
363
364         <row><entry>executable</entry><entry>No</entry><entry>Whether the file must be executable. Possible values: <userinput>true</userinput>, <userinput>false</userinput></entry></row>
365
366         <row><entry>non-empty</entry><entry>No</entry><entry>Whether the directory must be non-empty. Possible values: <userinput>true</userinput>, <userinput>false</userinput></entry></row>
367
368         <row><entry>mimetype</entry><entry>No</entry><entry>The mimetype for the file at path</entry></row>
369
370         </tbody></tgroup>
371 </informaltable>
372
373 <userinput>treematch</userinput> elements can be nested, meaning that both the outer and the inner <userinput>treematch</userinput>
374 must be satisfied for a "match".
375                                 </para></listitem>
376                         </itemizedlist>
377 Applications may also define their own elements, provided they are namespaced to prevent collisions.
378 Unknown elements are copied directly to the output XML files like <userinput>comment</userinput>
379 elements. A typical use for this would be to indicate the default handler
380 application for a particular desktop
381 ("Galeon is the GNOME default text/html browser"). Note that this doesn't
382 indicate the user's preferred application, only the (fixed) default.
383                 </para>
384                 <para>
385 Here is an example source file, named <filename>diff.xml</filename>:
386                 <programlisting><![CDATA[
387 <?xml version="1.0"?>
388 <mime-info xmlns='http://www.freedesktop.org/standards/shared-mime-info'>
389   <mime-type type="text/x-diff">
390     <comment>Differences between files</comment>
391     <comment xml:lang="af">verskille tussen lêers</comment>
392     ...
393     <magic priority="50">
394       <match type="string" offset="0" value="diff\t"/>
395       <match type="string" offset="0" value="***\t"/>
396       <match type="string" offset="0" value="Common subdirectories: "/>
397     </magic>
398     <glob pattern="*.diff"/>
399     <glob pattern="*.patch"/>
400   </mime-type>
401 </mime-info>
402 ]]></programlisting>
403                 </para><para>
404 In practice, common types such as text/x-diff are provided by the freedesktop.org shared
405 database. Also, only new information needs to be provided, since this information will be merged
406 with other information about the same type.
407                 </para>
408         </sect2>
409         <sect2>
410                 <title>The MEDIA/SUBTYPE.xml files</title>
411                 <para>
412 These files have a <userinput>mime-type</userinput> element as the root node. The format is
413 as described above. They are created by merging all the <userinput>mime-type</userinput>
414 elements from the source files and creating one output file per MIME type. Each file may contain
415 information from multiple source files. The <userinput>magic</userinput>,
416 <userinput>glob</userinput> and <userinput>root-XML</userinput> elements will
417 have been removed.
418                 </para>
419                 <para>
420 The example source file given above would (on its own) create an output file called
421 <filename>&lt;MIME&gt;/text/x-diff.xml</filename> containing the following:
422                         <programlisting><![CDATA[
423 <?xml version="1.0" encoding="utf-8"?>
424 <mime-type xmlns="http://www.freedesktop.org/standards/shared-mime-info" type="text/x-diff">
425 <!--Created automatically by update-mime-database. DO NOT EDIT!-->
426   <comment>Differences between files</comment>
427   <comment xml:lang="af">verskille tussen lêers</comment>
428   ...
429 </mime-type>
430
431 ]]></programlisting>
432                 </para>
433         </sect2>
434         <sect2>
435                 <title>The glob files</title>
436                 <para>
437 The globs2 file is a simple list of lines containing weight, MIME type and pattern, separated by a colon.
438 The lines are ordered by glob weight.
439 For example:
440                         <programlisting><![CDATA[
441 # This file was automatically generated by the
442 # update-mime-database command. DO NOT EDIT!
443 ...
444 55:text/x-diff:*.patch
445 50:text/x-diff:*.diff
446 ...
447 ]]></programlisting>
448                 </para>
449                 <para>
450 The glob file is a simple list of lines containing a MIME type and pattern, separated by a colon. It is
451 deprecated in favour of the globs2 file which also lists the weight of the glob rule.           
452 The lines are ordered by glob weight.
453 For example:
454                         <programlisting><![CDATA[
455 # This file was automatically generated by the
456 # update-mime-database command. DO NOT EDIT!
457 ...
458 text/x-diff:*.patch
459 text/x-diff:*.diff
460 ...
461 ]]></programlisting>
462                 </para>
463                 <para>
464 Applications MUST first try a case-sensitive match, then try again with the
465 filename converted to lower-case if that fails.
466 This is so that <filename>main.C</filename> will be seen as a C++ file,
467 but <filename>IMAGE.GIF</filename> will still use the *.gif pattern.
468                 </para>
469                 <para>
470 If several patterns of the same weight match then the longest pattern SHOULD be used.
471 In particular, files with multiple extensions (such as
472 <filename>Data.tar.gz</filename>) MUST match the longest sequence of extensions
473 (eg '*.tar.gz' in preference to '*.gz'). Literal patterns (eg, 'Makefile') must
474 be matched before all others. It is suggested that patterns beginning with `*.'
475 and containing no other special characters (`*?[') should be placed in a hash
476 table for efficient lookup, since this covers the majority of the patterns. Thus,
477 patterns of this form should be matched before other wildcarded patterns.
478                 </para>
479                 <para>
480 If a matching pattern is provided by two or more MIME types, applications
481 SHOULD not rely on one of them. They are instead supposed to use magic data
482 (see below) to detect the actual MIME type. This is for instance required to
483 deal with container formats like Ogg or AVI, that map various video and/or
484 audio-encoded data to one extension.
485                 </para>
486                 <para>
487 There may be several rules mapping to the same type. They should all be merged.
488 If the same pattern is defined twice, then they MUST be ordered by the
489 directory the rule came from, as described above.
490                 </para>
491                 <para>
492 Lines beginning with `#' are comments and should be ignored. Everything from
493 the `:' character to the newline is part of the pattern; spaces should not be
494 stripped. The file is in the UTF-8 encoding. The format of the glob pattern
495 is as for fnmatch(3). The format does not allow a pattern to contain a literal
496 newline character, but this is not expected to be a problem.
497                 </para>
498                 <para>
499 Common types (such as MS Word Documents) will be provided in the X Desktop
500 Group's package, which MUST be required by all applications using this
501 specification. Since each application will then only be providing information
502 about its own types, conflicts should be rare.
503                 </para>
504         </sect2>
505         <sect2>
506                 <title>The magic files</title>
507                 <para>
508 The magic data is stored in a binary format for ease of parsing. The old magic database
509 had complex escaping rules; these are now handled by <command>update-mime-database</command>.
510                 </para><para>
511 The file starts with the magic string "MIME-Magic\0\n".
512 There is no version number in the file. Incompatible changes will be handled by
513 creating both the current `magic' file and a newer `magic2' in the new format.
514 Where possible, compatible changes only will be made.
515 All numbers are big-endian, so need to be byte-swapped on little-endian machines.
516                 </para><para>
517 The rest of the file is made up of a sequence of small sections.
518 Each section is introduced by giving the priority and type in brackets, followed by
519 a newline character. Higher priority entries come first. Example:
520 <screen>[50:text/x-diff]\n</screen>
521 Each line in the section takes the form:
522 <screen>[ indent ] ">" start-offset "=" value
523 [ "&amp;" mask ] [ "~" word-size ] [ "+" range-length ] "\n"</screen>
524 <informaltable>
525         <tgroup cols="3">
526         <thead><row><entry>Part</entry><entry>Example</entry><entry>Meaning</entry></row></thead>
527         <tbody>
528
529         <row><entry>indent</entry><entry>1</entry><entry>The nesting
530         depth of the rule, corresponding to the number of '>' characters in the traditional file format.</entry></row>
531         <row><entry>">" start-offset</entry><entry>&gt;4</entry><entry>The offset into the
532         file to look for a match.</entry></row>
533         <row><entry>"=" value</entry><entry>=\0x0\0x2\0x55\0x40</entry><entry>
534         Two bytes giving the (big-endian) length of the value, followed by the value itself.
535         </entry></row>
536         <row><entry>"&amp;" mask</entry><entry>&amp;\0xff\0xf0</entry><entry>
537         The mask, which (if present) is exactly the same length as the value.
538         </entry></row>
539         <row><entry>"~" word-size</entry><entry>~2</entry><entry>On little-endian machines, the
540         size of each group to byte-swap.</entry></row>
541         <row><entry>"+" range-length</entry><entry>+8</entry><entry>The length of the region
542         in the file to check.
543         </entry></row>
544         </tbody>
545         </tgroup>
546 </informaltable>
547                 </para><para>
548 Note that the value, value length and mask are all binary, whereas everything
549 else is textual. Each of the elements begins with a single character to
550 identify it, except for the indent level.
551                 </para><para>
552 The word size is used for byte-swapping. Little-endian systems should reverse
553 the order of groups of bytes in the value and mask if this is greater than one.
554 This only affects `host' matches (`big32' entries still have a word size of 1,
555 for example, because no swapping is necessary, whereas `host32' has a word size
556 of 4).
557                 </para><para>
558 The indent, range-length, word-size and mask components are optional. If
559 missing, indent defaults to 0, range-length to 1, the word-size to 1, and the
560 mask to all 'one' bits.
561                 </para><para>
562 Indent corresponds to the nesting depth of the rule. Top-level rules have an
563 indent of zero. The parent of an entry is the preceding entry with an indent
564 one less than the entry.
565                 </para><para>
566 If an unknown character is found where a newline is expected then the whole
567 line should be ignored (there will be no binary data after the new
568 character, so the next line starts after the next "\n" character). This is for
569 future extensions.
570                 </para><para>
571 The text/x-diff above example would (on its own) create this magic file:
572                         <programlisting><![CDATA[
573 00000000  4d 49 4d 45 2d 4d 61 67  69 63 00 0a 5b 35 30 3a  |MIME-Magic..[50:|
574 00000010  74 65 78 74 2f 78 2d 64  69 66 66 5d 0a 3e 30 3d  |text/x-diff].>0=|
575 00000020  00 05 64 69 66 66 09 0a  3e 30 3d 00 04 2a 2a 2a  |..diff..>0=..***|
576 00000030  09 0a 3e 30 3d 00 17 43  6f 6d 6d 6f 6e 20 73 75  |..>0=..Common su|
577 00000040  62 64 69 72 65 63 74 6f  72 69 65 73 3a 20 0a     |bdirectories: .|
578 ]]></programlisting>
579                 </para>
580         </sect2>
581         <sect2>
582                 <title>The XMLnamespaces files</title>
583                 <para>
584 Each <filename>XMLnamespaces</filename> file is a list of lines in the form:
585 <screen>namespaceURI " " localName " " MIME-Type "\n"</screen>
586 For example:
587 <screen>
588 http://www.w3.org/1999/xhtml html application/xhtml+xml
589 </screen>
590 The lines are sorted (using strcmp in the C locale) and there are no lines with the same namespaceURI and
591 localName in one file. If the localName was empty then there will be two spaces following
592 the namespaceURI.
593                 </para>
594         </sect2>
595         <sect2>
596                 <title>The icon files</title>
597                 <para>
598 The <filename>icons</filename> and <filename>generic-icons</filename> files are list of lines in the form:
599 <screen>MIME-Type ":" icon-name "\n"</screen>
600 For example:
601 <screen>
602 application/msword:x-office-document
603 </screen>
604                 </para>
605         </sect2>
606         <sect2>
607                 <title>The treemagic files</title>
608                 <para>
609 The tree magic data is stored in a file with a format that is very similar to the magic file format.
610                 </para>
611                 <para>
612 The file starts with the magic string "MIME-TreeMagic\0\n". There is no version number in the file.
613 Incompatible changes will be handled by creating both the current `treemagic' and a newer `treemagic2' 
614 in the new format. Where possible, changes will be made in a compatible fashion. 
615                 </para>
616                 <para>
617 The rest of the file is made up of a sequence of small sections. Each section is introduced by giving
618 the priority and type in brackeds, followed by a newline character. Higher priority entries come
619 first. Example:
620 <screen>[50:x-content/image-dcf]\n</screen>
621 Each line in the section takes the form:
622 <screen>[ indent ] ">" "\"" path "\"" "=" type [ "," option ]* "\n"</screen>
623 <informaltable>
624         <tgroup cols="2">
625         <thead><row><entry>Part</entry><entry>Meaning</entry></row></thead>
626         <tbody>
627
628         <row><entry>indent</entry><entry>The nesting depth of the rule.</entry></row>
629         <row><entry>path</entry><entry>The path to match.</entry></row>
630         <row><entry>type</entry><entry>The required file type, one of "file", "directory", "link" or "any"</entry></row>
631         <row><entry>option</entry><entry>Optional for the optional attributes of <userinput>treematch</userinput> elements. 
632 Possible values are "executable", "match-case", "non-empty", or a MIME type</entry></row>
633         </tbody>
634         </tgroup>
635 </informaltable>
636                 </para><para>
637                 </para>
638         </sect2>
639         <sect2>
640                 <title>The mime.cache files</title>
641                 <para>
642 The <filename>mime.cache</filename> files contain the same information as the 
643 <filename>globs2</filename>, <filename>magic</filename>, <filename>subclasses</filename>, 
644 <filename>aliases</filename> and <filename>XMLnamespaces</filename> files, in a binary, 
645 mmappable format:
646 </para>
647 <programlisting>
648 Header:
649 2                       CARD16          MAJOR_VERSION   1       
650 2                       CARD16          MINOR_VERSION   1       
651 4                       CARD32          ALIAS_LIST_OFFSET
652 4                       CARD32          PARENT_LIST_OFFSET
653 4                       CARD32          LITERAL_LIST_OFFSET
654 4                       CARD32          REVERSE_SUFFIX_TREE_OFFSET
655 4                       CARD32          GLOB_LIST_OFFSET
656 4                       CARD32          MAGIC_LIST_OFFSET
657 4                       CARD32          NAMESPACE_LIST_OFFSET
658 4                       CARD32          ICONS_LIST_OFFSET
659 4                       CARD32          GENERIC_ICONS_LIST_OFFSET
660
661 AliasList:
662 4                       CARD32          N_ALIASES
663 8*N_ALIASES             AliasListEntry
664
665 AliasListEntry:
666 4                       CARD32          ALIAS_OFFSET
667 4                       CARD32          MIME_TYPE_OFFSET
668
669 ParentList:
670 4                       CARD32          N_ENTRIES 
671 8*N_ENTRIES             ParentListEntry
672
673 ParentListEntry:
674 4                       CARD32          MIME_TYPE_OFFSET
675 4                       CARD32          PARENTS_OFFSET
676
677 Parents:
678 4                       CARD32          N_PARENTS
679 4*N_PARENTS             CARD32          MIME_TYPE_OFFSET
680
681 LiteralList:
682 4                       CARD32          N_LITERALS
683 12*N_LITERALS           LiteralEntry    
684
685 LiteralEntry:
686 4                       CARD32          LITERAL_OFFSET
687 4                       CARD32          MIME_TYPE_OFFSET
688 4                       CARD32          WEIGHT
689
690 GlobList:
691 4                       CARD32          N_GLOBS
692 12*N_GLOBS              GlobEntry       
693
694 GlobEntry:
695 4                       CARD32          GLOB_OFFSET
696 4                       CARD32          MIME_TYPE_OFFSET
697 4                       CARD32          WEIGHT
698
699 ReverseSuffixTree:
700 4                       CARD32          N_ROOTS
701 4                       CARD32          FIRST_ROOT_OFFSET
702
703 ReverseSuffixTreeNode:
704 4                       CARD32          CHARACTER
705 4                       CARD32          N_CHILDREN                      
706 4                       CARD32          FIRST_CHILD_OFFSET
707
708 ReverseSuffixTreeLeafNode:
709 4                       CARD32          0 
710 4                       CARD32          MIME_TYPE_OFFSET
711 4                       CARD32          WEIGHT
712
713 MagicList:
714 4                       CARD32          N_MATCHES
715 4                       CARD32          MAX_EXTENT
716 4                       CARD32          FIRST_MATCH_OFFSET
717
718 Match:
719 4                       CARD32          PRIORITY
720 4                       CARD32          MIME_TYPE_OFFSET
721 4                       CARD32          N_MATCHLETS
722 4                       CARD32          FIRST_MATCHLET_OFFSET
723
724 Matchlet:
725 4                       CARD32          RANGE_START
726 4                       CARD32          RANGE_LENGTH
727 4                       CARD32          WORD_SIZE
728 4                       CARD32          VALUE_LENGTH
729 4                       CARD32          VALUE
730 4                       CARD32          MASK
731 4                       CARD32          N_CHILDREN
732 4                       CARD32          FIRST_CHILD_OFFSET
733
734 NamespaceList:
735 4                       CARD32          N_NAMESPACES
736 12*N_NAMESPACES         NamespaceEntry  
737
738 NamespaceEntry:
739 4                       CARD32          NAMESPACE_URI_OFFSET
740 4                       CARD32          LOCAL_NAME_OFFSET
741 4                       CARD32          MIME_TYPE_OFFSET
742
743 GenericIconsList:
744 IconsList:
745 4                       CARD32          N_ICONS
746 8*N_ICONS               IconListEntry
747
748 IconListEntry:
749 4                       CARD32          MIME_TYPE_OFFSET
750 4                       CARD32          ICON_NAME_OFFSET
751 </programlisting>
752 <para>
753 Lists in the file are sorted, to enable binary searching. The list of 
754 aliases is sorted by alias, the list of literal globs is sorted by the 
755 literal. The SuffixTreeNode siblings are sorted by character. 
756 The list of namespaces is sorted by namespace uri. The list of icons
757 is sorted by mimetype.
758 </para>
759 <para>
760 Mimetypes are stored in the suffix tree by appending suffix
761 tree leaf nodes with '\0' as character. These nodes appear at the
762 beginning of the list of children.
763 </para>
764 <para>
765 All offsets are in bytes from the beginning of the file.
766 </para>
767 <para>
768 Strings are zero-terminated.
769 </para>
770 <para>
771 All numbers are in network (big-endian) order. This is necessary because the data will be stored in 
772 arch-independent directories like <filename>/usr/share/mime</filename> or even in user's home directories.
773 </para>
774 <para>
775 Cache files have to be written atomically - write to a temporary name, then move over the old file - so 
776 that clients that have the old cache file open and mmap'ed won't get corrupt data.
777 </para>
778         </sect2>
779         <sect2>
780                 <title>Storing the MIME type using Extended Attributes</title>
781                 <para>
782 An implementation MAY also get a file's MIME type from the
783 <userinput>user.mime_type</userinput> extended attribute. <!-- The attr(5) man
784 page documents this name --> The type given here should normally be used in
785 preference to any guessed type, since the user is able to set it explicitly.
786 Applications MAY choose to set the type when saving files. Since many
787 applications and filesystems do not support extended attributes,
788 implementations MUST NOT rely on this method being available.
789                 </para>
790         </sect2>
791         <sect2 id="subclassing">
792                 <title>Subclassing</title>
793                 <para>
794 A type is a subclass of another type if any instance of the first type is
795 also an instance of the second. For example, all image/svg files are also
796 text/xml, text/plain and application/octet-stream files. Subclassing is about
797 the format, rather than the catagory of the data (for example, there is no
798 'generic spreadsheet' class that all spreadsheets inherit from).
799                 </para>
800                 <para>
801 Some subclass rules are implicit:
802                         <itemizedlist>
803 <listitem><para>All text/* types are subclasses of text/plain.</para></listitem>
804 <listitem><para>All streamable types (ie, everything except the inode/* types)
805 are subclasses of application/octet-stream.</para></listitem>
806                         </itemizedlist>
807 In addition to these rules, explicit subclass information may be given using
808 the <userinput>sub-class-of</userinput> element.
809                 </para>
810                 <para>
811 Note that some file formats are also compressed files (application/x-jar files
812 are also application/zip files). However, this is different to a case such as a
813 compressed postscript file, which is not a valid postscript file itself (so
814 application/x-gzpostscript does not inherit from application/postscript,
815 because an application that can handle the latter may not cope with the
816 former).
817                 </para>
818                 <para>
819 Some types may or may not be instances of other types. For example, a
820 spreadsheet file may be compressed or not. It is a valid spreadsheet file
821 either way, but only inherits from application/x-gzip in one case. This
822 information cannot be represented statically; instead an application
823 interested in this information should run all of the magic rules, and
824 use the list of types returned as the subclasses.
825                 </para>
826         </sect2>
827         <sect2>
828                 <title>Recommended checking order</title>
829                 <para>
830 Because different applications have different requirements, they may choose to
831 use the various methods provided by this specification in any order. However, the
832 RECOMMENDED order to perform the checks is:
833                         <itemizedlist>
834                                 <listitem><para>
835 If a MIME type is provided explicitly (eg, by a ContentType HTTP header, a MIME
836 email attachment, an extended attribute or some other means) then that should
837 be used instead of guessing.
838                                 </para></listitem>
839
840                                 <listitem><para>
841 Otherwise, start by doing a glob match of the filename. Keep only globs with the biggest weight.
842 If the patterns are different, keep only globs with the longest pattern, as previously discussed.
843 If after this, there is one or more matching glob, and all the matching globs
844 result in the same mimetype, use that mimetype as the result.
845                                 </para></listitem>
846                                 
847                                 <listitem><para>
848 If the glob matching fails or results in multiple conflicting mimetypes, read the
849 contents of the file and do magic sniffing on it. If no magic rule matches the data (or if
850 the content is not available), use the default type of application/octet-stream for
851 binary data, or text/plain for textual data. If there was no glob match, use the magic match
852 as the result. 
853                                 </para><para>
854 Note: Checking the first 32 bytes of the file for ASCII control characters is
855 a good way to guess whether a file is binary or text, but note that files with high-bit-set
856 characters should still be treated as text since these can appear in UTF-8 text,
857 unlike control characters.
858                                 </para></listitem>
859                                 
860                                 <listitem><para>
861 If any of the mimetypes resulting from a glob match is equal to or a subclass of
862 the result from the magic sniffing, use this as the result. This allows us for example to
863 distinguish text files called "foo.doc" from MS-Word files with the same name, as the
864 magic match for the MS-Word file would be application/x-ole-storage which the MS-Word type
865 inherits.
866                                 </para></listitem>
867                                 
868                                 <listitem><para>
869 Otherwise use the result of the glob match that has the highest weight.
870                                 </para></listitem>
871                         </itemizedlist>
872                 </para>
873                 <para>
874 There are several reasons for checking the glob patterns before the magic.
875 First of all doing magic sniffing is very expensive as reading the contents of the files
876 causes a lot of seeks, which is very expensive. Secondly, some applications don't check
877 the magic at all (sometimes the content is not available or too slow to read), and this
878 makes it more likely that both will get the same type.
879                 </para>
880                 <para>
881 Also, users can easily understand why calling their
882 text file <filename>README.mp3</filename> makes the system think it's an MP3,
883 whereas they have trouble understanding why their computer thinks
884 <filename>README.txt</filename> is a PostScript file. If the system guesses wrongly,
885 the user can often rename the file to fix the problem.
886                 </para>
887         </sect2>
888         <sect2>
889                 <title>Non-regular files</title>
890                 <para>
891 Sometimes it is useful to assign MIME types to other objects in the filesystem,
892 such as directories, sockets and device files. This could be useful when looking up
893 an icon for a type, or for providing a textual description of one of these objects.
894 The media type 'inode' is provided for this purpose, with the following types corresponding
895 to the standard types of object found in a Unix filesystem:
896                 </para>
897                 <simplelist>
898                         <member>inode/blockdevice</member>
899                         <member>inode/chardevice</member>
900                         <member>inode/directory</member>
901                         <member>inode/fifo</member>
902                         <member>inode/mount-point</member>
903                         <member>inode/socket</member>
904                         <member>inode/symlink</member>
905                 </simplelist>
906                 <para>
907 An inode/mount-point is a subclass of inode/directory. It can be useful when adding extra
908 actions for these directories, such as 'mount' or 'eject'. Mounted directories can be
909 detected by comparing the 'st_dev' of a directory with that of its parent. If
910 they differ, they are from different devices and the directory is a mount
911 point.
912                 </para>
913         </sect2>
914         <sect2>
915                 <title>Content types for volumes</title>
916                 <para>
917 Traditional MIME types apply to individual files or bytestreams. It is often useful 
918 to apply the same methodologies when classifying the content of mountable volumes or 
919 filesystems. The x-content type has been introduced for this purpose. Typical examples 
920 are x-content/audio-dvd, x-content/blank-cd or x-content/image-dcf. 
921                 </para>
922                 <para>
923 Matching of content types works with <userinput>treemagic</userinput> elements, which 
924 are analogous to the <userinput>magic</userinput> elements used for MIME type matching.
925 Instead of looking for byte sequences in files, <userinput>treemagic</userinput> element
926 allow to look for files with certain names, permissions or mime types in a directory
927 hierarchy.
928                 </para>
929         </sect2>
930         <sect2>
931                 <title>Security implications</title>
932                 <para>
933 The system described in this document is intended to allow different programs
934 to see the same file as having the same type. This is to help interoperability.
935 The type determined in this way is only a guess, and an application MUST NOT
936 trust a file based simply on its MIME type. For example, a downloader should
937 not pass a file directly to a launcher application without confirmation simply
938 because the type looks `harmless' (eg, text/plain).
939                 </para>
940                 <para>
941 Do not rely on two applications getting the same type for the same file, even
942 if they both use this system. The spec allows some leeway in implementation,
943 and in any case the programs may be following different versions of the spec.
944                 </para>
945         </sect2>
946         <sect2>
947                 <title>User modification</title>
948                 <para>
949 The MIME database is NOT intended to store user preferences. Users should never
950 edit the database. If they wish to make corrections or provide MIME entries for
951 software that doesn't provide these itself, they should do so by means of the
952 Override.xml mentioned in <xref linkend="s2_layout"/>. Information such as
953 "text/html files need to be opened with Mozilla" should NOT go in the database.
954                 </para>
955         </sect2>
956 </sect1>
957
958 <sect1>
959         <title>Contributors</title>
960         <simplelist>
961                 <member>
962                         Thomas Leonard <email>tal197 at users.sf.net</email>
963                 </member>
964                 <member>
965                         David Faure <email>david at mandrakesoft.com</email>
966                 </member>
967                 <member>
968                         Alex Larsson <email>alexl at redhat.com</email>
969                 </member>
970                 <member>
971                         Seth Nickell <email>snickell at stanford.edu</email>
972                 </member>
973                 <member>
974                         Keith Packard <email>keithp at keithp.com</email>
975                 </member>
976                 <member>
977                         Filip Van Raemdonck <email>mechanix at debian.org</email>
978                 </member>
979                 <member>
980                         Christos Zoulas <email>christos at zoulas.com</email>
981                 </member>
982                 <member>
983                         Matthias Clasen <email>mclasen at redhat.com</email>
984                 </member>
985         </simplelist>
986 </sect1>
987
988 <bibliography>
989         <title>References</title>
990
991         <bibliomixed>
992                 <abbrev>GNOME</abbrev><citetitle>The GNOME desktop,
993                 <ulink url="http://www.gnome.org"/></citetitle>
994         </bibliomixed>
995         <bibliomixed>
996                 <abbrev>KDE</abbrev><citetitle>The KDE desktop,
997                 <ulink url="http://www.kde.org"/></citetitle>
998         </bibliomixed>
999         <bibliomixed>
1000                 <abbrev>ROX</abbrev><citetitle>The ROX desktop,
1001                 <ulink url="http://rox.sourceforge.net"/></citetitle>
1002         </bibliomixed>
1003         <bibliomixed>
1004                 <abbrev>DesktopEntries</abbrev><citetitle>Desktop Entry Specification,
1005                 <ulink url="http://www.freedesktop.org/standards/desktop-entry-spec.html"/>
1006                 </citetitle>
1007         </bibliomixed>
1008         <bibliomixed>
1009                 <abbrev>SharedMIME</abbrev><citetitle>Shared MIME-info Database,
1010                 <ulink url="http://www.freedesktop.org/standards/shared-mime-info.html"/>
1011                 </citetitle>
1012         </bibliomixed>
1013         <bibliomixed>
1014                 <abbrev>RFC-2119</abbrev>
1015                 <citetitle>Key words for use in RFCs to Indicate Requirement Levels,
1016                 <ulink url="http://www.ietf.org/rfc/rfc2119.txt?number=2119"/>
1017                 </citetitle>
1018         </bibliomixed>
1019         <bibliomixed>
1020                 <abbrev>BaseDir</abbrev>
1021                 <citetitle>XDG Base Directory Specification
1022                 <ulink url="http://www.freedesktop.org/standards/basedir/draft/basedir-spec/basedir-spec.html"/>
1023                 </citetitle>
1024         </bibliomixed>
1025         <bibliomixed>
1026                 <abbrev>ACAP</abbrev>
1027                 <citetitle>ACAP Media Type Dataset Class
1028                 <ulink url="ftp://ftp.ietf.org/internet-drafts/draft-ietf-acap-mediatype-01.txt"/>
1029                 </citetitle>
1030         </bibliomixed>
1031 </bibliography>
1032
1033 </article>